Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Problem with java.lang.reflect.Proxy

Reply
Thread Tools

Problem with java.lang.reflect.Proxy

 
 
Nikita A. Visnevski
Guest
Posts: n/a
 
      04-22-2004
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(SampleInte rface.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
=======================================

 
Reply With Quote
 
 
 
 
Nikita A. Visnevski
Guest
Posts: n/a
 
      04-22-2004
Never mind, I just have figured it out.

Nik

Nikita A. Visnevski wrote:
> 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(SampleInte rface.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
=======================================

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments