?
Solved

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

Posted on 2001-09-12
9
Medium Priority
?
281 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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 400 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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

801 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