Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Is possible to write Class object to .class file?

 
Boccob Do
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have quite interesting question (to my mind) - Is possible to write Class object to .class file?
Maybe someone knows some tool which can do it?

Thanks
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

You can only create Class objects for classes that have already been loaded, so there must be a class file for them already. (Unless the class is created dynamically using a library like Javassist or BCEL, but those APIs offer a way of writing a class file to disk.)

What prompted this question?
 
Boccob Do
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Welcome to JavaRanch.

You can only create Class objects for classes that have already been loaded, so there must be a class file for them already. (Unless the class is created dynamically using a library like Javassist or BCEL, but those APIs offer a way of writing a class file to disk.)

What prompted this question?


Exactly, I have dynamically created Java Class, I already tried Javassist, BCEL and several more tools, but they works or with existed java .class files or with their own dynamically created objects.
So in the case that I havent Class file, but I have the only virtual Class object - is possible to create .class file?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using Javassist to create the class, then you can use the CtClass.toBytecode method to retrieve the bytes that make up the class, and then write it to disk in whichever way you please.
 
Boccob Do
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:If you're using Javassist to create the class, then you can use the CtClass.toBytecode method to retrieve the bytes that make up the class, and then write it to disk in whichever way you please.

No, I am not using Javassist or any other tool, I have the virtual Class object as end result and now I need to create a .class from it. As I checked - Javassist cant work with Class objects.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is no, there's no way to do this using Java or the JNI. It might be possible to do it using native code for a particular JVM if you had detailed knowledge of its implementation, but even then, I am not sure. In general, there's no guarantee that a given Class corresponds to any legal .class file -- for example, the Class that represents the type "int[]".

Where do these Class object come from? Can you arrange to have them loaded via a ClassLoader of your own devising, which could intercept and save the bytes that make up the .class file at load time?


 
Boccob Do
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:The answer is no, there's no way to do this using Java or the JNI. It might be possible to do it using native code for a particular JVM if you had detailed knowledge of its implementation, but even then, I am not sure. In general, there's no guarantee that a given Class corresponds to any legal .class file -- for example, the Class that represents the type "int[]".

Where do these Class object come from? Can you arrange to have them loaded via a ClassLoader of your own devising, which could intercept and save the bytes that make up the .class file at load time?


Thanks, for info. The class is defined by "defineClass"
 
Roshan Khandelwal
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
Its been more than a year since the last post .. and it's now that I have faced a similar problem.

I am using javaAssist to generate a class at runtime .

I am then adding fields and corresponding getters and setters for these fields to the class

Now once everything is done , I try to do a writeFile on it

This however is throwing a "NULL POINTER EXCEPTION" .

But when I am reading this dynamically created class using a ClassPool and try and find the declared methods of this class... this executes properly and shows the correct number of methods (Variables * 2)

I have also tried the

and even this is throwing a "NULL POINTER EXCEPTION" .
Further I have also tried doing a "writeFile and toByteCode" after

and even then its throwing a "NullPointer Exception"

I have run the code in debug Mode and before these steps "writeFile and toByteCode" , i am able to see a valid CtClass Object (i.e not null) . So I am actually baffeled as to why would this happen .
Kindly HELP


Editing : The writeFile method in CtClass writes the modified definition of the class to a class file.
Here if the class file does not already exist.. would it create one or What!!!

Editing : Attaching the log
Exception in thread "main" java.lang.NullPointerException
at javassist.compiler.Lex.<init>(Lex.java:47)
at javassist.compiler.Javac.parseExpr(Javac.java:582)
at javassist.compiler.Javac.compileExpr(Javac.java:572)
at javassist.CtField$CodeInitializer.compileExpr(CtField.java:827)
at javassist.CtField$CodeInitializer0.compile(CtField.java:770)
at javassist.CtClassType.makeFieldInitializer(CtClassType.java:1424)
at javassist.CtClassType.modifyConstructors(CtClassType.java:1377)
at javassist.CtClassType.toBytecode(CtClassType.java:1253)
at javassist.CtNewClass.toBytecode(CtNewClass.java:70)
at javassist.CtClass.writeFile(CtClass.java:1227)
at javassist.CtClass.writeFile(CtClass.java:1199)
at com.dynamic.ReadDataDictionary.decideClasses(ReadDataDictionary.java:93)
at com.dynamic.ReadDataDictionary.main(ReadDataDictionary.java:61)
 
Roshan Khandelwal
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Update to the Previous Post :

I have been able to invoke writeFile() on the cc object now .
It was some issue with the "default value" , that i was assigning to the fields while addition was causing the problem , very specifically to char variables .
However I have three more doubts :
1.) There is no "String type" in the CtClass . So how do we define a String type variable.

Like right now i have assigned a charType to String variables . So is it right or do we have some other option.

2.) Further I am dynamically making one class , then defining an object of that class inside another class . That is being done successfully . But now i want to create an array of objects of that class . How can we define an array as the type for a field.

3.) Where do the class files with "cc.writeFile()" get saved in . Like whats the path , Because right now the code is running successfully , but the files are not visible in the workspace.

Thanks to everyone who might make a try at solving any of the three .

 
Roshan Khandelwal
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Update on Point 3
I just needed to refresh my workspace . I was continuously building the project but not refreshing it. So was not able to see the files.
Now I am...

So one Issue is solved .

Kindly help on the other two.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!