Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how set some precondition for the object pass into generic method?

 
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java generic Method is defined as follow

<T> void printType(T anyType)
{
System.out.println(anyType.getClass().getName());
}


It says that I can pass any object into it.for example ,conside I pass an object A,here want to constraint on the object A ,A should have a "int" parameter in it and its value should be greater than 10 otherwise ,the method won't accept the object.How It can be done ?.


set some precondition for the object pass into method
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is nothing to do with generics. You need to write some code to check your preconditions and throw an exception if they are not met
 
jacob deiter
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like to do declarativelly rather than programmatically.How it can be dome?
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I know it cant be done declaratively. You could use an aspect to advise the entry to the method and inspect the object passed in the aspect instead of the actual method, but in either case you have to write some code to do it....as far as I know anyway.

Actually just off the top of my head (may be way off) but could write some form of custom annotation?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You definitely can't use any kind of compile-time construct to accept or reject argments based on member values, since members only exist and have values while a program is running, long after the compiler has done its job.

The only preconditions you can set are type restrictions like "List<T extends JComponent>", which accepts only JComponent or its subclasses.
 
jacob deiter
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for reply
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic