please, your comments on why this code won't work

L

learningjava

Hello,
I am trying to use reflection to create a new instance of an inner
class
(and hence use it later).
The code compiles fine, but throws a NoSuchMethodException on runtime.
Can you please comment on and provide the right approach to
do this?

Thanks,

StrugglingWithReflection

code:

****

import java.lang.reflect.*;

public class Reflection {

private class InnerClass1 {
//default constructor
public InnerClass1() { }

public String description() {
return "InnerClass 1";
}
}

public static void main(String[] args) throws ClassNotFoundException,
NoSuchMethodException,
InstantiationException, IllegalAccessException,
InvocationTargetException
{

//trying to create a new instance of the inner class
//code compiled with no syntax errors
Class cls = Class.forName("Reflection$" + "InnerClass1");
Class[] partypes;
partypes = new Class[1];
partypes[0] = Long.TYPE;
//this throws a NoSuchMethodException (runtime error). why?
//the exception text also has something like <init>
//what does it mean?
Constructor con = cls.getConstructor(partypes);
Object[] arglist;
arglist = new Object[1];
arglist[0] = new Long(500);
Object methobj = con.newInstance(arglist);
}
}


****
 
M

Michael Borgwardt

learningjava said:
Hello,
I am trying to use reflection to create a new instance of an inner
class
(and hence use it later).
The code compiles fine, but throws a NoSuchMethodException on runtime.
Can you please comment on and provide the right approach to
do this?

You posted nearly exactly the same question on December 2nd and were given
two correct answers, which still apply here.
 
A

Adam

public class Reflection {
private class InnerClass1 {
//default constructor
public InnerClass1() { }

public String description() {
return "InnerClass 1";
}
Class cls = Class.forName("Reflection$" + "InnerClass1");
Class[] partypes;
partypes = new Class[1];
partypes[0] = Long.TYPE;
//this throws a NoSuchMethodException (runtime error). why?
//the exception text also has something like <init>
//what does it mean?
Constructor con = cls.getConstructor(partypes);
I'm not a reflection expert but i guess that this means that you
want to obtaint a constructor which takes Long as a parameter,
which you don't have in InnerClass1

Adam
 
T

Tor Iver Wilhelmsen

public InnerClass1() { }

Do you see a "long xxx" inside those parentheses? Neither do we.
Class[] partypes;
partypes = new Class[1];
partypes[0] = Long.TYPE;
//this throws a NoSuchMethodException (runtime error). why?
//the exception text also has something like <init>
//what does it mean?
Constructor con = cls.getConstructor(partypes);

It means that despite your insistence you haven't written a
constructor thast takes a long parameter.
 
L

learningjava

Tor,

How do I call a getConstructor on a constructor which has no arguments ?
And I am getting the same runtime exception even after I add
the long argumentto the constructor.

Thanks,

gk
public InnerClass1() { }

Do you see a "long xxx" inside those parentheses? Neither do we.
Class[] partypes;
partypes = new Class[1];
partypes[0] = Long.TYPE;
//this throws a NoSuchMethodException (runtime error). why?
//the exception text also has something like <init>
//what does it mean?
Constructor con = cls.getConstructor(partypes);

It means that despite your insistence you haven't written a
constructor thast takes a long parameter.
 
J

Joona I Palaste

learningjava said:
How do I call a getConstructor on a constructor which has no arguments ?
And I am getting the same runtime exception even after I add
the long argumentto the constructor.

Have you tried this?

Constructor con = cls.getConstructor(new Class[0]);

IOW, just use an empty array of Class references.
public InnerClass1() { }

Do you see a "long xxx" inside those parentheses? Neither do we.
Class[] partypes;
partypes = new Class[1];
partypes[0] = Long.TYPE;
//this throws a NoSuchMethodException (runtime error). why?
//the exception text also has something like <init>
//what does it mean?
Constructor con = cls.getConstructor(partypes);

It means that despite your insistence you haven't written a
constructor thast takes a long parameter.
 
L

learningjava

Hello,
You posted nearly exactly the same question on December 2nd and were given
two correct answers, which still apply here.

But I'm still not able to get it working.
I gave a long argument to my constructor, and tried it.
Still I get the same exception.
Can you post the right code?

Thanks,

gk
 
L

learningjava

Ok, I modified my code.
I was wrong about no providing the proper arguments to the
constructor.
That works now, but I am not able to invoke newInstance().
Here is the modified code :
****

import java.lang.reflect.*;

public class Reflection {

private class InnerClass1 {
//default constructor
public InnerClass1(int num) {}

public String description() {
return "InnerClass 1";
}
}

public static void main(String[] args) throws ClassNotFoundException,
NoSuchMethodException,
InstantiationException, IllegalAccessException,
InvocationTargetException
{

//trying to create a new instance of the inner class
//code compiled with no syntax errors
Class cls = Class.forName("Reflection$" + "InnerClass1");
Class[] partypes;
partypes = new Class[1];
partypes[0] = Integer.class;
Object[] arglist;
arglist = new Object[1];
arglist[0] = new Integer(500);
Constructor[] cons = cls.getConstructors();
System.out.println(cons[0]);
Object methobj = cons[0].newInstance(arglist);


}


}

****

Thanks again for you help,

gk

public InnerClass1() { }

Do you see a "long xxx" inside those parentheses? Neither do we.
Class[] partypes;
partypes = new Class[1];
partypes[0] = Long.TYPE;
//this throws a NoSuchMethodException (runtime error). why?
//the exception text also has something like <init>
//what does it mean?
Constructor con = cls.getConstructor(partypes);

It means that despite your insistence you haven't written a
constructor thast takes a long parameter.
 
J

John C. Bollinger

learningjava said:
But I'm still not able to get it working.
I gave a long argument to my constructor, and tried it.
Still I get the same exception.
Can you post the right code?

The constructors of an inner class each have a hidden argument through
which a reference to the containing class instance is passed. You need
to take that into account, but have not done so. To figure out what is
needed, why don't you use Class.getConstructors() on the inner class,
and Constructor.getParameterTypes() on each constructor? That way you
can figure out exactly what is required.

For example:

[...]

Class myClass = Class.forName("mypackage.Foo$InnerFoo");
Constructor[] constructors = myClass.getConstructors();

for (int i = 0; i < constructors.length; i++) {
Class[] types = constructors.getParameterTypes();
for (int j = 0; j < types.length; j++) {
System.out.print(types[j]);
System.out.print(' ');
}
System.out.println();
System.out.println();
}

[...]


John Bollinger
(e-mail address removed)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top