N
Nikita A. Visnevski
Hi folks,
my guess is that I am simply missing something in the
java.lang.reflect.Proxy API. This is what I am trying to do (just
learning this stuff):
// File MyInvocationHandler.java
import java.lang.reflect.*;
public final class MyInvocationHandler implements InvocationHandler
{
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable
{
String methodName = method.getName();
System.out.println("Executing: " + methodName);
return null;
}
}
// File SampleInterface.java
public interface SampleInterface {
public double getScalarProperty(String name);
public void setScalarProperty(String name, double val);
}
In main I do a very primitive thing straight from Java API for JDK 1.3:
InvocationHandler handler = new MyInvocationHandler();
SampleInterface si =
(SampleInterface)Proxy.newProxyInstance(SampleInterface.class.getClassLoader(),
new Class[] {SampleInterface.class}, handler);
si.getScalarProperty("aaa");
I expect si to be a proxy class that implements SampleInterface and when
I call si.getScalarProperty() I expect to see
Executing: getScalarProperty
at the output.
Instead, the si becoms $Proxy0, and I get the following output:
Executing: getScalarProperty
java.lang.ClassCastException
at $Proxy0.getScalarProperty(Unknown Source)
I am wondering what went wrong with such a primitive code. Any idea??
Thanks in advance.
Nik
--
=======================================
Nikita A. Visnevski
Adaptive Systems Laboratory
CRL, McMaster University
Phone : (905) 525-9140 x 27282
Web : http://soma.crl.mcmaster.ca
=======================================
my guess is that I am simply missing something in the
java.lang.reflect.Proxy API. This is what I am trying to do (just
learning this stuff):
// File MyInvocationHandler.java
import java.lang.reflect.*;
public final class MyInvocationHandler implements InvocationHandler
{
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable
{
String methodName = method.getName();
System.out.println("Executing: " + methodName);
return null;
}
}
// File SampleInterface.java
public interface SampleInterface {
public double getScalarProperty(String name);
public void setScalarProperty(String name, double val);
}
In main I do a very primitive thing straight from Java API for JDK 1.3:
InvocationHandler handler = new MyInvocationHandler();
SampleInterface si =
(SampleInterface)Proxy.newProxyInstance(SampleInterface.class.getClassLoader(),
new Class[] {SampleInterface.class}, handler);
si.getScalarProperty("aaa");
I expect si to be a proxy class that implements SampleInterface and when
I call si.getScalarProperty() I expect to see
Executing: getScalarProperty
at the output.
Instead, the si becoms $Proxy0, and I get the following output:
Executing: getScalarProperty
java.lang.ClassCastException
at $Proxy0.getScalarProperty(Unknown Source)
I am wondering what went wrong with such a primitive code. Any idea??
Thanks in advance.
Nik
--
=======================================
Nikita A. Visnevski
Adaptive Systems Laboratory
CRL, McMaster University
Phone : (905) 525-9140 x 27282
Web : http://soma.crl.mcmaster.ca
=======================================