aspose file tools*
The moose likes Java in General and the fly likes Dynamically Create Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Dynamically Create Class" Watch "Dynamically Create Class" New topic
Author

Dynamically Create Class

Mandan Happy
Ranch Hand

Joined: Dec 17, 2001
Posts: 65
Dear Geeks,
1 Is it possible to dynamically create a class and instantiate an object of it in java ?
My Headache is as follows...
I have a Singleton Logger which would print a log message on the system console
Logger log= Logger.getInstance();
log.log("going to enter rmi and corba modules");
(This would print "going to enter rmi and corba modules");
Thereafter I need to write code like ...
Logger rmilog = Logger.getInstance("RMI");
rmilog.log("rmi registry created");
(Should print "RMI:rmi registry created")
Logger corbalog = Logger.getInstance("CORBA");
corbalog.log("corba naming service initialised");
(Should print "CORBA:corba naming service initialised")
ie I want the singleton Logger to get me an instance depending on the argument that I pass .
ie I want to dynamically create 2 classes namely RMILogger(a singleton) and CORBALogger(a singleton) that would subclass Logger and override log method and would prefix the log messages with "RMI" and "CORBA" respectively.
Thanks in advance
YAMH ( Yet Another Muddle Head)
BJ Grau
Ranch Hand

Joined: Jul 10, 2001
Posts: 234
You could pass as a parameter some value that will act as a key for some class name in a ResourceBundle or properties file. Once you have obtained your class name, you could do something like this:

newInstance should be an instance of the class you want. I dont know if this compiles, but it conveys the general idea.
Good luck.
[ January 10, 2002: Message edited by: BJ Grau ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Check our the Reflection API. I think it will answer all your questions.
http://java.sun.com/docs/books/tutorial/reflect/index.html


GenRocket - Experts at Building Test Data
Mandan Happy
Ranch Hand

Joined: Dec 17, 2001
Posts: 65
Thanks Grau,
But what I was trying to do was to create a class dynamically ie say
Class dosntExistOnDisk=new Class();
dosntExistOnDisk.addConstructor(..);
dosntExistOnDisk.addMethod(...)
or rather I was trying to create a Type Dynamically .which I have understood that its not possible .
Yet Another Doubt has crept into my head..
I have three packages A,B,C and I have 20 classes in each of these packages. So thought of writing a logger.(<JDK 1.4 hence) Dint want 20 instances of logger to be created in each package hence I made it a singleton( Great I used a pattern). But the behaviour of logger would differ in each package.
In package A it will log the Message,time,X,and Y...In package it will log only message and so on.
I end up creating 3 singleton loggers for Package A,B and C. I see that my logger has 20 methods of which only one differs across A,B and C.
Now The Big Question ? How Can I Abstract the behaviour of singleton objects.IS IT possible .
DO I need to go for Aspect Oriented Programming ?
[ Dont know what that means but aspectj.org speaks a lot]
or am I really missing something inside my head
Thanks in Advance Geeks
-Mandan
[ Any One out there from Kerala,India?]
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
No, Java doesn't allow you to do this (only Smalltalk allows internal access to the compiler that I know of).
However, you can create a text file (a .java file) in your program, invoke the Java compiler on it (using Runtime.exec()) and then so long as the .class file is on your classpath, you can then use Class.forName() to load it. Once you've gotten that far, you're home free...
Kyle


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
By the way, there are MUCH easier ways to do the logging you want. Go look at the Log4J code at java.apache.org.
Kyle
Mandan Happy
Ranch Hand

Joined: Dec 17, 2001
Posts: 65
Thanks Geeks .I have started using log4j.
Also learning BCEL (Byte Code Engineering Library) to create a class on the fly.
Has any one one used that b4
Mandan
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209

But what I was trying to do was to create a class dynamically ie say
Class dosntExistOnDisk=new Class();
dosntExistOnDisk.addConstructor(..);
dosntExistOnDisk.addMethod(...)
or rather I was trying to create a Type Dynamically .which I have understood that its not possible .

try Java CC
http://www.webgain.com/products/java_cc/
could be the thing you are looking for
karthik.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamically Create Class