Results 1 to 4 of 4

Thread: reflection

  1. #1
    rfabbri is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default reflection

    I have to execute by reflaction the next statement

    Globals.getStt().getTrascodifica("CG")

    where getStt is a static method of the Globals class that return an initialized instance of the class TatelleTrascodifica.

    I try with next code but don't work fine

    class1 = Class.forName("it.datamanagement.saia_xml.Globals" );
    Method method1 = class1.getMethod("getStt");
    Object o1 = method1.invoke(class1);
    class2 = o1.getClass();
    Method method2 = class2.getMethod(getTrascodifica, String.class);
    Object o2 = method2.invoke(class2, "CG");

    what I wrong?

    thanks in advance
    Raffaele
    Last edited by rfabbri; 12-06-2010 at 02:47 PM. Reason: wrong statements

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    What errors are you getting?

  3. #3
    rfabbri is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default Errors message

    object is not an instance of declaring class
    java.lang.IllegalArgumentException: object is not an instance of declaring class
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at it.datamanagement.saia_xml.test.TestReflection.get 1(TestReflection.java:31)
    at it.datamanagement.saia_xml.test.TestReflection.mai n(TestReflection.java:87)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    You're invoking the method on the class (class2).
    The method presumably is part of the object returned by getStt(), so should be called on the object (o1).

Similar Threads

  1. Ghost reflection in GUI
    By Russell_joh in forum AWT / Swing
    Replies: 8
    Last Post: 02-06-2010, 04:55 PM
  2. how to use reflection
    By sunilpatel28 in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 10:51 PM
  3. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:08 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •