CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 1999
    Posts
    3

    Problems with Reflection in Java



    I am having trouble understanding why the following bit of code results in a NoSuchMethodException. Everyone, please

    take a look at this and help me figure out the problem.

    /**********************************/

    import java.util.*;

    import java.lang.reflect.*;


    public class test

    {

    public static void main(String args[])

    {

    runTest(new HashSet());

    }

    static void runTest(Set set)

    {

    try

    {

    Class the_class=set.getClass();

    Class[] args=new Class[] {the_class};

    Constructor constructor=the_class.getConstructor(args);

    }

    catch (Exception e)

    {

    System.out.println(e);

    }

    }

    }


    /**********************************/

  2. #2
    Join Date
    Mar 1999
    Posts
    7

    Re: Problems with Reflection in Java



    Hye Luke,


    What's up?!

    Well, I came across your problem and decided to give it a shot! I just gotta tell you that you were getting that error

    cos' there actually is no method called "getConstructor()" as far as I know. There is a method called

    "getConstructors()" which returns you an array.

    Here is the edited code :


    --------------------------------------

    import java.util.*;

    import java.lang.reflect.*;


    public class test

    {

    public static void main(String args[])

    {

    runTest(new HashSet());

    }

    static void runTest(Set set)

    {

    try

    {

    Class the_class = set.getClass();

    System.out.println("Class the_class : " + the_class);

    Class[] args = new Class[] {the_class};

    System.out.println("Class[] args : " + args);

    Constructor[] constructor = the_class.getConstructors();

    System.out.println("Constructor[] constructor : " + constructor);

    }

    catch (Exception e)

    {

    System.out.println(e);

    }

    }

    }

    ________________________________________


    See ya around...mail me if you hear sumthin' new related to Java....


    Long Live Java....


    Davender aka JaVaFLow

  3. #3
    Join Date
    Mar 1999
    Posts
    3

    Re: Problems with Reflection in Java



    Thanks for the quick reply. I am not sure if I should have

    been more specific, but I used the JDK1.2 to compile this little bugger of

    a test. In Sun's JDK1.2 documentation, it states that there is in fact a

    Constructor getConstructor(Class[] parameterTypes) method. However,

    it doesn't appear to work correctly in my test application. Is this a bug, or

    is it user (my own) error?


    -Luke

  4. #4
    Join Date
    Mar 1999
    Posts
    19

    Re: Problems with Reflection in Java



    I think the method for getting the constructor is


    getConstructors();

    // and not

    getContructor();


    See carefully .


    this method returns an array which contains the various contructors used

    by the given class


    bye

    sunil

  5. #5
    Join Date
    Mar 1999
    Posts
    3

    Re: Problems with Reflection in Java



    Look in the Java API documentation everybody! It plainly says that

    there is indeed a method:


    Constructor getConstructor(Class[] parameterTypes)


    Of course, there is also a:


    Constructor[] getConstructors()



    Now, is it me, or is the: "Constructor getConstructor(Class[] parameterTypes)"

    that I used in the code in my original post, broken?


    Here is the Java2 code to test run, showing the broken method:


    /**********************************/

    import java.util.*;

    import java.lang.reflect.*;


    public class test

    {

    public static void main(String args[])

    {

    runTest(new HashSet());

    }

    static void runTest(Set set)

    {

    try

    {

    Class the_class=set.getClass();

    Class[] args=new Class[] {the_class};

    Constructor constructor=the_class.getConstructor(args);

    }

    catch (Exception e)

    {

    System.out.println(e);

    }

    }

    }


    /**********************************/

  6. #6
    Join Date
    Jun 2001
    Posts
    1

    Re: Problems with Reflection in Java

    Well, the getConstructor(Class []) takes a class array and returns a constructor. Your code pays in args to the getConstructor. I'm not sure how
    args gets passed down at all (how can runTest see it), but even so I don't
    think args is a Class array--it's a String array.


Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured