Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > please, your comments on why this code won't work

Reply
Thread Tools

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

 
 
learningjava
Guest
Posts: n/a
 
      12-11-2003
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);
}
}


****
 
Reply With Quote
 
 
 
 
Michael Borgwardt
Guest
Posts: n/a
 
      12-11-2003
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.

 
Reply With Quote
 
 
 
 
Adam
Guest
Posts: n/a
 
      12-11-2003
> 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


 
Reply With Quote
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      12-11-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (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.
 
Reply With Quote
 
learningjava
Guest
Posts: n/a
 
      12-12-2003
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

> (E-Mail Removed) (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.

 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      12-12-2003
learningjava <(E-Mail Removed)> 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.

>> (E-Mail Removed) (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 ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"War! Huh! Good God, y'all! What is it good for? We asked Mayor Quimby."
- Kent Brockman
 
Reply With Quote
 
learningjava
Guest
Posts: n/a
 
      12-12-2003
> > 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
 
Reply With Quote
 
learningjava
Guest
Posts: n/a
 
      12-12-2003
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


> (E-Mail Removed) (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.

 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      12-12-2003
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
(E-Mail Removed)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding proper functionality to code.Please read comments in code andfirst test it in your browser to see the effect. Mclaren Fan Javascript 2 11-08-2011 05:50 PM
Are you fed-up with people "viewing" your code, and stealing your HTML code ? Your Encryption Site HTML 11 04-24-2008 11:02 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
boost::try_mutex mutex--code review/your advise-comments g C++ 3 03-21-2006 08:03 PM



Advertisments