• 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

generating classes dynamically....

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I want to generate classes dynamically based on the user inputs......is it possible in java ? Can anyone help me on this....?
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to clarify more.
What do you exactly mean by dynamic classes? Are you sure you want to mean classes and not objects?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I moved this question to the Intermediate forum - this is definitely not a beginner's question :-)

One way of doing so is to use a library that can construct classes, like Javassist. I wrote an article about doing just that in the JavaRanch Journal a while ago.
 
deshki raja
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what i meant was that...depending on the user inputs ....i want to create a class, generate the methods and create objects of the class...........
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well then Ulf's link talks just exactly about that.
 
deshki raja
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
any other way to do this ?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by "other way"? Is the suggested approach somehow not feasible in your situation? If so, tell us why it isn't.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess you would be looking for two separate functionalities
1 - To generate class dynamically . You can generate java class dynamically using com.sun.codemodel pacakage.
This package provides lots of functionality to generate class programmatically - including adding method, extending/implementing other class and interface.

2 - To compile and generate class file or fetch error in runtime.
For this, you can use sun package - com.sun.tools.javac.Main to compile and to parse result or compiler error there are utility classes provided by apache in the package - org.apache.commons.jci.compilers and org.apache.commons.jci.problems

We are generating, compiling lot of classes dynamically based on user input. Later we are invoking the classes and storing the execution result in our database.
 
straws are for suckers. tiny ads are for attractive people.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic