• 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

about istantiating a class at runtime

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i' m in trouble istantiating a class at runtime and calling its method.

I have a javabean and i must use Beans.setDesignTime(true) for using it.
Then i have fc that is a String name of the class i want to istantiate at runtime. This string is variable relative to user input; so i cannot know the exact class name and i must use the superclass FuzzySet to declare it & then use use newIstance() for istantiating my custom class.

The problem is that now i have a FuzzySet istance and i cannot use specific metoth of my custom class.. there are many case and every possible istance of the beans has its different properties that i have to set (from use inpu)

Is there some way to create a variable at runtime that not is the superclass?

tnx


[ September 25, 2005: Message edited by: Marco Vanoli ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You seem to have a bit of a contract with the user here. They believe they can enter the name of FuzzySet or any subclass and you can do something with it. To keep that contract, you'll need to keep the code you have. If the user enters an invalid class you'll get a class cast exception, which is only fair because the user messed up on the contract.

The class they enter may or may not be one of the subclasses you'd like to work with. You can check to see if they gave you one you like.

Look up "instanceof" and "casting" in a good language book (or just Google) to see the full story on how they work.
 
Marco Vanoli
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tnk but would like to use something as reflection.
I explain better my situation:

The user choose a javabeanclass ( we trust is right ) from a list ,
then the program interrogate with reflection the class and give to the user the properties of the javabeanclass without istantiating it.
(attention: every class has differents properties)

Then the user give the value for all properties of the the class he choose.
Here comes the problem:
now i have to set really the javabeans properties so i think i need to istantiate it.
It is possible to make a program that can automatically istantiate a given classname(a java bean) and then set its properties(the properties are all the time differents in refer to the class name used)?
I need to do this possibly not hardcoded all classname but leave java to do all the work using only the String with the class name and value of its properties.

Maybe this is not possible in a normal java program but is possible using some specific javabean api functions.

tnk you
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it can be done in java. See the Reflection classes for how to get info about a class.
To get at the 'properties' of a class you would need to get a list of all the methods it has. Then look for those whose names begin with 'get' or 'set' to find the 'properties'. The definition of a property is that it has a get<Property>() and set<Property>() method.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do all this with reflection.

Use Class.forName() to get the Class but no object instance. Look up Class in the doc and find a way to get all the methods. For any method names that start with "set" assume the rest of the name is a property name, eg "setSize" implies a property "size".

Show that list of property names to the user and let them enter values. If you want to be really cool, inspect the types of the parameters and restrict data entry to valid data such as integers or dates.

When you get the user data make a bean instance, go through those setX methods again and invoke them. Look up Method in the doc to see how to call them.

Good clean fun!
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> Class fuzzyClass = Class.forName("full.Fuzzy." + fc);
>> FuzzySet theFuzzyset = (FuzzySet)fuzzyClass.newInstance();

Here's a safer solution. The class "name" must reference a Type or a sub-class of Type. This eliminates the cast but it only works in Java 5.0 which has better type safety facilities all around.

Type type = Class.forName(name).asSubclass(Type.class).newInstance();

SITE NOTE: When you're writing software you have specific classes that give you a context. Take advantage of that context and use more general method and variable names: "jump" versus "catJump" and "list" versus "fleaList".

If you use generic method and variable names where possible, your code starts to take on a familiar symmetry across the board. This makes it easier to maintain and debug.
[ September 26, 2005: Message edited by: Rick O'Shay ]
 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic