Problem with java.lang.reflect.Proxy

Discussion in 'Java' started by Nikita A. Visnevski, Apr 22, 2004.

  1. 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
    =======================================
    Nikita A. Visnevski, Apr 22, 2004
    #1
    1. Advertising

  2. 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(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
    =======================================
    Nikita A. Visnevski, Apr 22, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. snatchitup
    Replies:
    3
    Views:
    1,923
    Larry A. Barowski
    Dec 4, 2003
  2. David C. Partridge
    Replies:
    1
    Views:
    3,250
    Chris Smith
    Apr 13, 2004
  3. Ovidiu

    java.lang.reflect.Proxy

    Ovidiu, Aug 10, 2004, in forum: Java
    Replies:
    1
    Views:
    473
    John C. Bollinger
    Aug 10, 2004
  4. ualex
    Replies:
    2
    Views:
    920
    Mandar Amdekar
    Jan 2, 2006
  5. Karsten Wutzke
    Replies:
    1
    Views:
    870
    Steven Simpson
    Aug 20, 2007
Loading...

Share This Page