Solved

How to use relection API to generate a new class by a method?

Posted on 2001-09-12
9
275 Views
Last Modified: 2013-11-23
 I hope to generate a new class by invoking a method/function. For example,

  I hope to create a class and put a field/field value into it. My ideal function is like that:
 
  public Object createNewClass(String classname, String fieldname, String fieldvalue);
 
  -- createNewClass("NewClass", "NewField", "ABCD");

  So the class is like that :

  public class NewClass {
    String NewField = "ABCD";
  }

  Please tell how to do it. Thank you very much.. :0)
0
Comment
Question by:akokchai
9 Comments
 
LVL 6

Expert Comment

by:kotan
ID: 6475976
Questions Asked  60
Question Grading Record  22 Answers Graded / 22 Answers Received
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6475987
Hi,

There is no way to do that using reflection. Reflectiion lets you to find existing properties of existing classes only. You need either generate source and compile class (check com.sun.tools.javac.Main class in $JAVA_HOME/lib/tools.jar), or generate bytecode using other way, like gnu.bytecode package (look at http://www.gnu.org/software/kawa/). Than you can load that class and use it.

BTW, what do you need such thing for? It will be hard to use if you don't know field name in advance and it won't be necessary if you do.

Regards,
Igor Bazarny,
BrainBench MVP for Java 1
www.brainbench.com
0
 

Expert Comment

by:lawpan
ID: 6476257
Well otherwise you could create a .java file in your function. And then use the Compiler class to compile your java file and create the class file you wish.

Another way is to use the Process class to run a javac instance , pass paremeters to it let it compile the .java file to you and create the .class output file.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6476484
well, Compiler is a wrong class. It's supposed to represent just-in-time compiler, not java compiler. Runtime.exec("javac etc.") is not much different to use of javac.Main. There is one problem with javac.Main--javac.Main.main() calls System.exit(). There is another method which does actual job. Use javap tool or some decompiler (e.g. jad) to find it.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6477144
I saw once not long ago an API for dynamic class authoring. I'll hunt around and see if I can't find it. It doesn't seem immediately applicable but there is the Dynamic Proxy API in JDK 1.3. If you could provide more detail on your intent then it may be possible for us to provide better guidance or alternatives to your current direction.

Best regards,
Jim Cakalic
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6477177
Jim,

> I saw once not long ago an API for dynamic class authoring.

Wasn't it gnu.bytecode? It would be interesting though to know other alternative.

Igor
0
 
LVL 1

Author Comment

by:akokchai
ID: 6534947
Hi bazarny,
  Do you have any examples of the gnu.bytecode from kawa? Please tell me more about it. Thank you. :)
0
 
LVL 1

Author Comment

by:akokchai
ID: 6534953
Hi bazarny,
  Do you have any examples of the gnu.bytecode from kawa? Please tell me more about it. Thank you. :)
0
 
LVL 7

Accepted Solution

by:
Igor Bazarny earned 100 total points
ID: 6535572
Hi,

I don't have any examples in hand. I tried to find something at google and got:
http://directory.google.com/Top/Computers/Programming/Languages/Java/Class_Libraries/Data_Formats/Java_Class_Files/

There are some alternatives to gnu.bytecode. It looks like Java ByteCode Engineering Library site contains some examples. Check it out: http://bcel.sourceforge.net/

Regards,
Igor Bazarny  
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 36
by zero exception 10 41
Convert from a json string array to a Java object 3 32
Crystal Reports Licensing Questions 4 12
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question