Solved

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

Posted on 2001-09-12
9
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How  can  i  resolve  HTTP Status 404 -? 8 78
ejb entity bean example 2 56
jmss example java 2 47
Firefox and firebub 5 25
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

737 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