Dynamically extends a class with java.lang.reflect.Proxy or similar

A

Andrea Francia

If I would dynamically implements a interface called Foo, I can use:

Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
new Class[] { Foo.class },
handler);

But this doesn't work if Foo is a class.

There is any way to dynamically extends a class?
 
R

Robert Klemme

If I would dynamically implements a interface called Foo, I can use:

Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
new Class[] { Foo.class },
handler);

But this doesn't work if Foo is a class.

There is any way to dynamically extends a class?

Would it make any sense?

robert
 
Z

ZelluX

If I would dynamically implements a interface called Foo, I can use:
Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
new Class[] { Foo.class },
handler);
But this doesn't work if Foo is a class.
There is any way to dynamically extends a class?

Would it make any sense?

robert

I have similar problems.
For example, I want to trace all the methods decleared in interfaces
of a JButton instance and print them.
With help of a Proxy with InvocationHandler, how can I do it?
Simply using casting like frame.add((JButton) proxy) surely fails.
 
A

Andrea Francia

Robert said:
If I would dynamically implements a interface called Foo, I can use:

Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
new Class[] { Foo.class },
handler);

But this doesn't work if Foo is a class.

There is any way to dynamically extends a class?

Would it make any sense?
May be yes, may be not.

I am investigating the possibilities to dynamically change or add
behavior to the classes.
 
S

Stefan Ram

Andrea Francia said:
I am investigating the possibilities to dynamically change or
add behavior to the classes.

One can not add a method to a class that is already
loaded at run-time. One only can generate a new class
and load this new class. But this rarely is appropriate
to cope with requirements. Often there are better means
that do not require generating a class at run time.
 
A

Arne Vajhøj

ZelluX said:
For example, I want to trace all the methods decleared in interfaces
of a JButton instance and print them.
With help of a Proxy with InvocationHandler, how can I do it?
Simply using casting like frame.add((JButton) proxy) surely fails.

Using an AOP framework like AspectJ would be one way.

Arne
 
A

Andrea Francia

Stefan said:
One can not add a method to a class that is already
loaded at run-time.
Yes, I express myself badly.
I'm interested to generate a new class and load this class as you say.
One only can generate a new class
and load this new class. But this rarely is appropriate
to cope with requirements. Often there are better means
that do not require generating a class at run time.
Surely, I'm just investigating.
Bur for example this is used my the web containers.
The JSP pages are translated in a Java source file that define a class
that extends HTTPServlet and compiled and loaded at runtime.

There is another way instead creating a source file and compiling it?
 
S

Stefan Ram

Andrea Francia said:
The JSP pages are translated in a Java source file that define a class
that extends HTTPServlet and compiled and loaded at runtime.
There is another way instead creating a source file and compiling it?

No.

Here, the point is that there is a system running (the server),
which should not be terminated and recompiled.

Smalltalk and Squeak have even more dynamic of this type.

But in the case of JSP, each JSP (usually) is written by a /human/.

I thought that you had asked about programs that create
new classes at runtime that are generated solely by the
program - not (indirectly) by a human being like a JSP.
 
T

Tom Anderson

If I would dynamically implements a interface called Foo, I can use:

Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
new Class[] { Foo.class },
handler);

But this doesn't work if Foo is a class.

There is any way to dynamically extends a class?

There's no general way to do it, because classes can be final, or have
final methods, in which case there's no way to generate an effective
proxy.

In the absence of final members, though, i don't see why it couldn't be
done. I'm not aware of any code to make such proxies automatically,
though.

tom
 
A

Andrea Francia

Stefan said:
I thought that you had asked about programs that create
new classes at runtime that are generated solely by the
program - not (indirectly) by a human being like a JSP.
Yes, I was thinking about program that create new classes.
Sorry. Probably I don't express my ideas clearly.
The example of JSPs was only an example of classes that are created at
runtime.
 
K

Kevin McMurtrie

Andrea Francia
If I would dynamically implements a interface called Foo, I can use:

Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
new Class[] { Foo.class },
handler);

But this doesn't work if Foo is a class.

There is any way to dynamically extends a class?

No. Classes have a defined behavior that influences both compilation to
bytecode and compilation to native code. Allowing what you describe
would eliminate a lot optimizations that are critical in a mainstream
development language.

The closest you can get is a delegator class. The delegate can swap the
implementation on the fly. It performs better than a Proxy too.

If defining methods is too restrictive for you, consider a messaging
design pattern. Beware that messaging designs usually become a total
nightmare to maintain and debug as they age.
 
T

Tom Anderson

Andrea Francia
If I would dynamically implements a interface called Foo, I can use:

Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
new Class[] { Foo.class },
handler);

But this doesn't work if Foo is a class.

There is any way to dynamically extends a class?

No. Classes have a defined behavior that influences both compilation to
bytecode and compilation to native code. Allowing what you describe
would eliminate a lot optimizations that are critical in a mainstream
development language.

No it wouldn't. Java already has to deal with unexpected subclasses
appearing and invalidating its optimisations.

tom
 
R

Robert Klemme

Bur for example this is used my the web containers.
The JSP pages are translated in a Java source file that define a class
that extends HTTPServlet and compiled and loaded at runtime.

There is another way instead creating a source file and compiling it?

That's what Servlet containers usually do: they compile the JSP to a
Java source file and compile it (even if they do not store the file on
disk).

robert
 
A

Arne Vajhøj

Robert said:
That's what Servlet containers usually do: they compile the JSP to a
Java source file and compile it (even if they do not store the file on
disk).

Some do store it on disk.

Tomcat does.

And I like it because sometimes it can help debugging.

Arne
 
I

Ian Shef

Andrea Francia
There is another way instead creating a source file and compiling it?

Java Reflection in Action by Ira R. Forman and Nate Forman
( ISBN: 1932394184 )
shows applications where this can be useful. Their methods are:

1) java.lang.reflect.Proxy

2) Using reflection, automated soource code generation and automated
compilation.

3) I don't think that there was a third way but I don't have the book handy
right now.

Disclaimer: I am a satisfied purchaser and reader of the book. I am not
affiliated with the authors or the publisher, and I have not actually
attempted any of the techniques taught in the book (but any day now...).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,841
Latest member
WilmerBelg

Latest Threads

Top