Solved

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

Posted on 2001-09-12
9
270 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bigHeights  challenge 13 55
mapBully challenge 6 89
Java and GPO 11 47
thymeleaf natural templating vs JSP 2 25
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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

18 Experts available now in Live!

Get 1:1 Help Now