• 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

Creating Dynamic Java Class at Runtime

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I'm trying to create a dynamic java class at run time using javassist.
I have managed to create a class with get/set methods for String forename, but wen
I attempt to create another dynamic java class at run time to have get/set methods for String surname, I
get the following error:-

Exception in thread "main" java.lang.NoSuchMethodException: GeneratedGenericResponse.setSurname(java.lang.String)
at java.lang.Class.getMethod(Class.java:1581)
at CodeGeneratorDemo.testGnericClass(CodeGeneratorDemo.java:215)



Mat
 
Mat Anthony
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
can anybody solve this problem.

Mat
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mat,

Your class looks rather complex, and it's difficult to see what you're trying to do and where it's failing. Start by writing an SSCCE for us.
 
Mat Anthony
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephen,
thanks for getting back to me. I want to generate a class called GeneratedGenericResponse at runtime.
I want to generate different attributes (i.e. private String forename with associated public getter/setter methods) within this class at runtime.
Once I have populated the class with data and used it in my application. I would like to remove the forename and its getter/setter methods
from the generated class and then recreate new attributes (i.e. private String registration with associated public getter/setter methods) and then be able
to populate and use this class in my application. In the complete code example that I have given I'm able to create GeneratedGenericResponse at runtime
with the attributes that I require and populate the class with data. In the example I'm also able to remove the original attributes and modify the generated class
with different attributes with getter/setter methods.

The problem that I have is that I don't seem to be able to populate the modified class with data into the new attributes. I get a NoSuchMethodException. My gut feeling
is that my latest attribute class changes are not been populated into the class pool, I think. Or that the object that I'm calling does not contain the method.

Please can you help. It's a tough one, but I feel I'm close to solving it.

Mat
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't shake the feeling that you're using an incredibly heavy and cumbersome tool for something that can be done in a much more elegant way. It doesn't make sense to me that you would add properties to a type, and then remove them again. Why are you doing this exactly?

If you want to continue going this route, first try writing an example that does these steps in a hard-coded manner: in a short method, create the type, add a single property with a hard-coded name, use the type, modify the type with another hard-coded name, and see if you can reproduce the problem in this way. It's extremely difficult to debug a long, generic, reflective piece of code.
 
Mat Anthony
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephan,
I have inherited some old xml based code that is a mixture of html tags and sql code, to provide small inividual displays, that are used with the application.
I have extracted the different sqls (i.e. approx 60+). To cut down writing loads of vo holding the results of the sql. I would like to be able to create a single class on the fly
that would generate different properties within the class at runtime dependent on the data back from the sql (i.e. name and data type). I would then be able to create
restful services that will utilise the above.

Mat
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that's going to lead to very brittle and unmaintainable code. I think it's better to just accept that this problem is not going to benefit from a strongly typed approach, and just get/set Objects from/to a ResultSet when you query/update the database. Either that, or write a strongly typed DAO that can handle each piece of SQL (as a prepared statement!).
 
Mat Anthony
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephan,
I will take your advise and move away from dynamic java class at runtime.

Thanks Stephan.

Mat
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keep us updated on your progress!
 
Ranch Hand
Posts: 127
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just for sake because it was already necromanced: The main issue OP originaly had was to try to re-load a class with the same package and name within the same classloader - wich is still not possible today.
TLDR: it would had worked if either the class would had not been modified - or if different classnames were used for each class
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this example that shows how generating classes at runtime with Burningwave Core.
For this purpose is necessary the use of ClassFactory component and of the sources generating components. Once the sources have been set in UnitSourceGenerator objects, they must be passed to ClassFactory with the ClassLoader where you want to define new generated classes. The buildAndLoadOrUpload method performs the following operations: tries to load all the classes present in the UnitSourceGenerator through the class loader, if at least one of these is not found proceeds to compiling all the UnitSourceGenerators and uploading their classes on class loader: in this case, keep in mind that if a class with the same name was previously loaded by the class loader, the compiled class will not be uploaded. Once the classes have been compiled and loaded, it is possible to invoke their methods in severals ways as shown at the end of the example below.

 
Juan Efisio
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Winter wrote:Just for sake because it was already necromanced: The main issue OP originaly had was to try to re-load a class with the same package and name within the same classloader - wich is still not possible today.
TLDR: it would had worked if either the class would had not been modified - or if different classnames were used for each class



But he can extends the class
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic