Problem with java.lang.reflect.Proxy

  • Thread starter Nikita A. Visnevski
  • Start date
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
=======================================
 
N

Nikita A. Visnevski

Never mind, I just have figured it out.

Nik
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
=======================================
 

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

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top