Click to See Complete Forum and Search --> : Problems with Reflection in Java


Luke Simon
February 24th, 1999, 08:02 PM
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);

}

}

}


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

Davender Pratap Singh
February 25th, 1999, 02:42 AM
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

Luke Simon
February 25th, 1999, 08:07 PM
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

Sunil Sarje
March 5th, 1999, 06:15 AM
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

sunil sarje
March 5th, 1999, 06:15 AM
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

Luke Simon
March 5th, 1999, 02:09 PM
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);

}

}

}


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

reflector
June 29th, 2001, 11:39 AM
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.