-
Bug
-
Resolution: Not an Issue
-
P1
-
None
-
1.3.0
-
None
-
x86
-
windows_nt
I'm using the Dynamic Proxy Class API to generate event hookups between Swing components. The Object that is returned from Proxy.newProxyInstance doesn't seem to have a toString or a hashCode method defined. We need these methods so that these proxy classes can be saved and manipulated.
47 [cerebus] bugs %javac ProxyTest.java && java ProxyTest
Exception in thread "main" java.lang.ClassCastException: java.lang.Object
at $Proxy0.toString(Unknown Source)
at ProxyTest.<init>(ProxyTest.java:30)
at ProxyTest.main(ProxyTest.java:36)
48 [cerebus] bugs %java -version
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-D)
Java(TM) HotSpot Client VM (build 1.3beta-D-release, 1.3beta-D-release, interpreted mode)
49 [cerebus] bugs %which java
/usr/local/java/jdk1.3/solaris/bin/java
50 [cerebus] bugs %
------------------ ProxyTest.java -------------------------------------------------------
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.awt.event.ActionListener;
/* When this code is run, it generates the folowing error:
[h:\data\bugs]javac ProxyTest.java && java ProxyTest
Exception in thread "main" java.lang.ClassCastException: java.lang.Object
at $Proxy0.toString(Unknown Source)
at ProxyTest.<init>(ProxyTest.java:16)
at ProxyTest.main(ProxyTest.java:22)
[h:\data\bugs]java -version
java version "1.3beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3beta-O)
Java(TM) HotSpot Client VM (build 1.3beta-O, mixed mode)
*/
public class ProxyTest {
public ProxyTest() {
Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(),
new Class[] { ActionListener.class },
new Handler());
if (proxy != null && proxy instanceof Object) {
System.out.println("proxy.toString() = " + proxy.toString());
System.out.println("proxy.hashCode() = " + proxy.hashCode());
}
}
public static void main(String[] args) {
new ProxyTest();
}
public class Handler implements InvocationHandler {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
return new Object();
}
}
/***********************************************************************************
// This next block of code shows the context in which I'm using the Proxy class.
/***********************************************************************************/
/**
* Adds the interaction on the target object to the source object. This method uses the
* JDK 1.3 Dynamic Proxy Class API.
* Formerly, this type of behaviour was achieved by dynamically generating listener classes
* using a technique that's described in
* http://java.sun.com/products/jfc/tsc/tech_topics/generic-listener/listener.html
* <p>
* @param source The component which is the originator of the event.
* @param sourceEvent The event which triggers the interaction.
* @param sourceMethod The method that the source object will call.
* @param target The component in which the event will manipulate
* @param targetMethod The target method that will be called.
* @param targetArgs An array of arguments for the target method.
*
public void addInteraction(Component source, EventSetDescriptor sourceEvent,
MethodDescriptor sourceMethod, Component target,
MethodDescriptor targetMethod, Object[] targetArgs) {
Object proxy = Proxy.newProxyInstance(source.getClass().getClassLoader(),
new Class[] { sourceEvent.getListenerType() },
new InteractionHandler(sourceMethod.getMethod(),
targetMethod.getMethod(),
target, targetArgs));
Method addMethod = sourceEvent.getAddListenerMethod();
try {
// Add the listener to the source.
addMethod.invoke(source, new Object[] { proxy });
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* A class that acts as a proxy which binds the source method to a target method invocation.
* This is added dynamically at runtime.
*
private class InteractionHandler implements InvocationHandler {
private Method sourceMethod;
private Method targetMethod;
private Object target;
private Object[] targetArgs;
public InteractionHandler(Method sourceMethod, Method targetMethod,
Object target, Object[] targetArgs) {
this.sourceMethod = sourceMethod;
this.targetMethod = targetMethod;
this.target = target;
this.targetArgs = targetArgs;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object result = null;
if (method.getName().equals(sourceMethod.getName())) {
try {
result = targetMethod.invoke(target, targetArgs);
} catch (InvocationTargetException ex) {
ex.printStackTrace();
throw ex.getTargetException();
} catch (Exception ex2) {
ex2.printStackTrace();
throw new RuntimeException("unexpected invocation exception: " + ex2.getMessage());
}
}
return result;
}
} // end class InteractionHandler
*/
}
47 [cerebus] bugs %javac ProxyTest.java && java ProxyTest
Exception in thread "main" java.lang.ClassCastException: java.lang.Object
at $Proxy0.toString(Unknown Source)
at ProxyTest.<init>(ProxyTest.java:30)
at ProxyTest.main(ProxyTest.java:36)
48 [cerebus] bugs %java -version
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-D)
Java(TM) HotSpot Client VM (build 1.3beta-D-release, 1.3beta-D-release, interpreted mode)
49 [cerebus] bugs %which java
/usr/local/java/jdk1.3/solaris/bin/java
50 [cerebus] bugs %
------------------ ProxyTest.java -------------------------------------------------------
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.awt.event.ActionListener;
/* When this code is run, it generates the folowing error:
[h:\data\bugs]javac ProxyTest.java && java ProxyTest
Exception in thread "main" java.lang.ClassCastException: java.lang.Object
at $Proxy0.toString(Unknown Source)
at ProxyTest.<init>(ProxyTest.java:16)
at ProxyTest.main(ProxyTest.java:22)
[h:\data\bugs]java -version
java version "1.3beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3beta-O)
Java(TM) HotSpot Client VM (build 1.3beta-O, mixed mode)
*/
public class ProxyTest {
public ProxyTest() {
Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(),
new Class[] { ActionListener.class },
new Handler());
if (proxy != null && proxy instanceof Object) {
System.out.println("proxy.toString() = " + proxy.toString());
System.out.println("proxy.hashCode() = " + proxy.hashCode());
}
}
public static void main(String[] args) {
new ProxyTest();
}
public class Handler implements InvocationHandler {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
return new Object();
}
}
/***********************************************************************************
// This next block of code shows the context in which I'm using the Proxy class.
/***********************************************************************************/
/**
* Adds the interaction on the target object to the source object. This method uses the
* JDK 1.3 Dynamic Proxy Class API.
* Formerly, this type of behaviour was achieved by dynamically generating listener classes
* using a technique that's described in
* http://java.sun.com/products/jfc/tsc/tech_topics/generic-listener/listener.html
* <p>
* @param source The component which is the originator of the event.
* @param sourceEvent The event which triggers the interaction.
* @param sourceMethod The method that the source object will call.
* @param target The component in which the event will manipulate
* @param targetMethod The target method that will be called.
* @param targetArgs An array of arguments for the target method.
*
public void addInteraction(Component source, EventSetDescriptor sourceEvent,
MethodDescriptor sourceMethod, Component target,
MethodDescriptor targetMethod, Object[] targetArgs) {
Object proxy = Proxy.newProxyInstance(source.getClass().getClassLoader(),
new Class[] { sourceEvent.getListenerType() },
new InteractionHandler(sourceMethod.getMethod(),
targetMethod.getMethod(),
target, targetArgs));
Method addMethod = sourceEvent.getAddListenerMethod();
try {
// Add the listener to the source.
addMethod.invoke(source, new Object[] { proxy });
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* A class that acts as a proxy which binds the source method to a target method invocation.
* This is added dynamically at runtime.
*
private class InteractionHandler implements InvocationHandler {
private Method sourceMethod;
private Method targetMethod;
private Object target;
private Object[] targetArgs;
public InteractionHandler(Method sourceMethod, Method targetMethod,
Object target, Object[] targetArgs) {
this.sourceMethod = sourceMethod;
this.targetMethod = targetMethod;
this.target = target;
this.targetArgs = targetArgs;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object result = null;
if (method.getName().equals(sourceMethod.getName())) {
try {
result = targetMethod.invoke(target, targetArgs);
} catch (InvocationTargetException ex) {
ex.printStackTrace();
throw ex.getTargetException();
} catch (Exception ex2) {
ex2.printStackTrace();
throw new RuntimeException("unexpected invocation exception: " + ex2.getMessage());
}
}
return result;
}
} // end class InteractionHandler
*/
}