• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dynamically Create Class

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check our the Reflection API. I think it will answer all your questions.
http://java.sun.com/docs/books/tutorial/reflect/index.html
 
Mandan Happy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?]
 
author
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic