Solved

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

Posted on 2001-09-12
9
273 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Starting to learn JAVA, 7 59
JDeveloper 12c for 32 bit 4 71
Java Timer (static) 9 36
arguments to jar 5 15
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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.

895 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now