• 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

Please let me know how to create a class dynamically using reflection. No rush.

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi... i have not used reflections .... i want to create a class dynamically when my application server starts .....please let me knwo how i can do that .... please let me know if any of you have code sinppets for this .... this is very urgent.... please help me
[ July 16, 2007: Message edited by: David O'Meara ]
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hiya,
I think your best call is to read the reflection trail on the sun site (here).

This will give you the basics of using the reflection API, will explain its advantages and dissadvantages.

hope it helps.

Gavin
 
Vinay kumar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes thats true .... but i dont ahve the time for that ... atleast for now ..... so i just want a example which creates a class dyanmically ..... can you help me in this regard please .....
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


but, if you don't know anything about reflection you should read the tutorial.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well within the trail there is an example of doing this, which is much better written and explained then I could do so, it is here.

You know, you could have finished reading there example in the time it has taken me to find it for you and post teh link here *shurgs*

G
 
Vinay kumar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes .... i have read soem of the classes in the java.lang.reflect ..... but please tell me how it is helping me in creating a class from scratch??? i know i am getting the concept wrong here .... i will tell what i have understood ...

in replections, we have a lot of APIs and lot of ways to get the Class object which will refer to the classes or interfaces in the JVM. through this Class object, we can access the members, field, modifiers etc of the class.... in this example, say class A. so the whole stuff which i am doing here is dynamically acceing the class .... where am i creating the class dynamically ??? whether java allows a person to create a class dynamically??? specifically does reflections does this???
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you want to create classes using reflection? The usual way doesn't work?
 
Vinay kumar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is like... first i will explain the requirement ....

say for example there are some rows in the database....
on the startup of the application server, i want to create a class based on the information present in each row of the table. So if i need to create the class dynamically, then i thought i should use reflections, which will provide the options to do so .... so i am using this ......
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I realise sometimes life gets the better of us, but please Ease Up.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well in the first case using reflection dosnt automaticlly create an instance (object) of the given class, it provides you with a representation of that class so you can explore its methods and fields, however you can tell it to create an instance of the class, which is detailed in teh second link I posted, you modify the code give above in this thread to get a list of constructors and then call newInstance on the construct you wish to use to create the class. (thats a very rougth explaination, really, look at the second link, it is an example of what you want).

In the second place it sounds like you are trying to do ORM (object relational mapping), now having done this from scratch myself, I would strongly advise that you look at some created to do this for you, such as Hibernate (it isnt the only option).
Other wise it is just easier to have a getClassA() method and actual construct an object as normal inside that method.
Of course it is dependent on what you are trying to accomplish.

Really, read the trail, Refelection is very powerful.

G
[ July 16, 2007: Message edited by: Gavin Tranter ]
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I may have misunderstood, but it seems like all the answers so far have been about creating an instance of a class dynamically. But the question is about creating a class dynamically.

It may be that the original poster just was not careful and/or well-informed, but it may be that they really do want to create a class dynamically.

The Reflection API helps you to load classes at a time that's under your control, to find out the methods and fields of a class, and to call methods. All this is done without having to hard-code class names into your code or to have the class available at compile time. That's what Reflection is for.

Reflection does not allow you to define entirely new classes. But, if that is what you want to do, you have to explain more about your requirements?

Do you want to compile Java source? Where will the source come from, if so? Or do you want to generate classes without ever having source code? This latter is possible, but for advanced students only (I've never actually done it). Very, very few applications really need this.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, that is what I understood the OP to require, the creation of an instance of an class.

G
 
Ranch Hand
Posts: 96
Scala VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if there is a 'simple' example of creating the class dynamically (not creating instances). You might want to check out http://asm.objectweb.org/
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For creating classes at runtime I recommend the Javassist library. It comes with an extensive tutorial.
 
Enjoy the full beauty of the english language. Embedded in 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