Using class loaded using Class.forName as base class

I need to create a class, which extends another class which i want to be dynamically loaded using Class.forname().. Is that possible?
[140 byte] By [relaxedgalaxya] at [2007-11-27 3:21:57]
# 1

You mean, can you write a class which extends another class, which is unknown until runtime? No, you can't. Apart from not being syntactically possible, classloaders wouldn't be able to load the subclass because they'd first have to load the superclass, which presumably you're proposing to load from within the subclass which isn't yet loaded, because the superclass isn't loaded yet etc and so on

What were you proposing to do with this? There may be alternatives, such as dynamic proxying, providing these classes are all at least related

georgemca at 2007-7-12 8:24:42 > top of Java-index,Java Essentials,Java Programming...
# 2

public class A {}

public class B extends A {}

...

String className = ...read from a config file, perhaps?...

//perhaps className is "B"

A a = Class.forName(className).newInstance();

...use a...

Is that what you meant?

DrLaszloJamfa at 2007-7-12 8:24:42 > top of Java-index,Java Essentials,Java Programming...
# 3

> > public class A {}

> public class B extends A {}

> ...

> String className = ...read from a config file,

> perhaps?...

> //perhaps className is "B"

> A a = Class.forName(className).newInstance();

> ...use a...

>

> Is that what you meant?

I took it to mean something like

public class A extends Class.forName(getClassNameFromSomewhere()) {}

which is, of course, nonsense

georgemca at 2007-7-12 8:24:42 > top of Java-index,Java Essentials,Java Programming...
# 4
OP: we are confused. Can you describe what you are trying to do, what your goal is?
DrLaszloJamfa at 2007-7-12 8:24:42 > top of Java-index,Java Essentials,Java Programming...
# 5
I'm fairly sure he wants to extend a class that's unknown until runtime. I bet his actual problem is a candidate for the strategy pattern
georgemca at 2007-7-12 8:24:42 > top of Java-index,Java Essentials,Java Programming...
# 6
> I bet his actual problem is a candidate for the strategy pattern.You're on. I bet a donut his solution is actually going to be via theDecorator Pattern. Anyone else what to place a bet?
DrLaszloJamfa at 2007-7-12 8:24:42 > top of Java-index,Java Essentials,Java Programming...
# 7

> > I bet his actual problem is a candidate for the

> strategy pattern.

>

> You're on. I bet a donut his solution is actually

> going to be via the

> Decorator Pattern. Anyone else what to place a bet?

That's hardly fair! You can just give him a solution, use the decorator pattern, and tax a donut from me!

edit: actually, I want a hedge bet that we never hear about it again

Message was edited by:

georgemc

georgemca at 2007-7-12 8:24:42 > top of Java-index,Java Essentials,Java Programming...
# 8

> I need to create a class, which extends another class

> which i want to be dynamically loaded using

> Class.forname().. Is that possible?

Class.forName will load a class,and return a instance of Class to you

if you really want to dynamic to creat a class,the CGLIB may help you out

google:CGLIB

Message was edited by:

iamyess

iamyessa at 2007-7-12 8:24:42 > top of Java-index,Java Essentials,Java Programming...
# 9

> > I need to create a class, which extends another

> class

> > which i want to be dynamically loaded using

> > Class.forname().. Is that possible?

>

>

>

> Class.forName will load a class,and return a instance

> of Class to you

>

> if you really want to dynamic to creat a class,the

> CGLIB may help you out

>

> google:CGLIB

>

> Message was edited by:

> iamyess

What he seems to be after is to extend a class unknown until runtime. CGLIB won't do that, I don't think anything will, and it makes no sense to anyway, really. What use would it be? But I suspect his actual problem will be a lot simpler to solve than all of this

georgemca at 2007-7-12 8:24:42 > top of Java-index,Java Essentials,Java Programming...
# 10
I think OP means dynamic to create a class(such as A) and extend other class,and use A
iamyessa at 2007-7-12 8:24:42 > top of Java-index,Java Essentials,Java Programming...
# 11
> I think OP means > > dynamic to create a class(such as A) and extend other> class,and use AI hope not. That doesn't mean anything! What did you mean?
georgemca at 2007-7-12 8:24:42 > top of Java-index,Java Essentials,Java Programming...
# 12
such as spring frameworkin the AOP part,it look like dynamic to create a class extend your bean and instance it
iamyessa at 2007-7-12 8:24:43 > top of Java-index,Java Essentials,Java Programming...
# 13
> such as spring framework> > in the AOP part,it look like dynamic to create a> class extend your bean and instance itCould be what he means, I suppose. He's not here, though :-(
georgemca at 2007-7-12 8:24:43 > top of Java-index,Java Essentials,Java Programming...