Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   please, your comments on why this code won't work (http://www.velocityreviews.com/forums/t129541-please-your-comments-on-why-this-code-wont-work.html)

learningjava 12-11-2003 04:06 PM

please, your comments on why this code won't work
 
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);
}
}


****

Michael Borgwardt 12-11-2003 04:18 PM

Re: please, your comments on why this code won't work
 
learningjava wrote:

> 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.


Adam 12-11-2003 04:19 PM

Re: please, your comments on why this code won't work
 
> public class Reflection {
>
> private class InnerClass1 {
> //default constructor
> public InnerClass1() { }
>
> public String description() {
> return "InnerClass 1";
> }

<cut>
in main():
> 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



Tor Iver Wilhelmsen 12-11-2003 09:47 PM

Re: please, your comments on why this code won't work
 
compsci@rogers.com (learningjava) writes:

> 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.

learningjava 12-12-2003 03:06 PM

Re: please, your comments on why this code won't work
 
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

> compsci@rogers.com (learningjava) writes:
>
> > 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.


Joona I Palaste 12-12-2003 03:14 PM

Re: please, your comments on why this code won't work
 
learningjava <compsci@rogers.com> scribbled the following:
> 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.


Have you tried this?

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

IOW, just use an empty array of Class references.

>> compsci@rogers.com (learningjava) writes:
>>
>> > 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.


--
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"War! Huh! Good God, y'all! What is it good for? We asked Mayor Quimby."
- Kent Brockman

learningjava 12-12-2003 03:15 PM

Re: please, your comments on why this code won't work
 
> > 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.


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

learningjava 12-12-2003 04:48 PM

Re: please, your comments on why this code won't work
 
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


> compsci@rogers.com (learningjava) writes:
>
> > 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.


John C. Bollinger 12-12-2003 05:05 PM

Re: please, your comments on why this code won't work
 
learningjava wrote:

>>>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.

>
>
> 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[i].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
jobollin@indiana.edu



All times are GMT. The time now is 08:33 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.