Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

RE : Java.lang.NoSuchMethodException

Thread view: 
ramakrishna - 04 May 2006 13:36 GMT
HI,
  can any one tell me, why NoSuchMethodException will come in
Class.getConstructor( Class[] parameterTypes) method.
 In my program Iam using the one constructor and iam calling the
constructor by passing array of class object

i created the same object using "new" operator with constructor
arguments and its works fine.

my code is :
ActionHelper class:

public ActionHelper(BaseAction tbAction) {
       iAction = tbAction;
   }

GetAuthorisedUnauthorisedRecordsAction  is the subclass of the
BaseAction.

with new :
      GetAuthorisedUnauthorisedRecordsAction gauUnau = new
GetAuthorisedUnauthorisedRecordsAction();

     ActionHelper actionHelper = new ActionHelper( gauUnau );

it is creating the ActionHelper object.

using reflection package iam creating the ActionHelper object.

     Class[] tParameterClasses = new Class[1];
     tParameterClasses[0] = gauUnau.getClass();

     Class tClass = Class.forName( ActionHelper );
     Constructor tConstructor =
tClass.getConstructor(tParameterClasses);

     Object[] tParameterObjects = new Object[1];
     tParameterObjects[0] = (Object) gauUnau;
     ActionHelper actionHelper = (ActionHelper)
tConstructor..newInstance(tParameterObjects);

in the above line it is showing exception at
Constructor tConstructor = tClass.getConstructor(tParameterClasses);
exception is java.lang.NoSuchMethodException..

As iam feeling that it does not accept the SubClass object.

it is accepting if i replace the ActionHelper code with below one.

public ActionHelper(GetAuthorisedUnauthorisedRecordsAction tbAction) {
       iAction = tbAction;
   }

1. Is there any difference b/w "new" and reflection package creation
object.
2. why this is happing.
Bjorn Abelli - 04 May 2006 14:23 GMT
"ramakrishna" wrote...

>   can any one tell me, why NoSuchMethodException will come in
> Class.getConstructor( Class[] parameterTypes) method.
>  In my program Iam using the one constructor and iam calling the
> constructor by passing array of class object

I believe I have already answered your question once...

[snipped a lot of code]

> As iam feeling that it does not accept the SubClass object.

> 1. Is there any difference b/w "new" and reflection package
> creation object.

When using "new" the JVM looks for the "best case" constructor, i.e. if
there is no constructor with *exactly* the type of the argument, it looks
for constructors where polymorphism can be used, i.e. if there are any
constructors that can take a supertype of the parameters.

When using reflection to get a constructor, you need to provide the
*declared* parametertypes. There you provide/get instances of "Class", but a
Class-instance of a subtype is not polymorph to a Class-instance of a
supertype.

You need to search through the constructors yourself, to see which of them
that has the "best fit".

Here's a quick fix you could use to get such a Constructor:

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

 public static Constructor getPossibleConstructor
    (Class motherClass, Class[] pars) {

    Constructor[] ctors = motherClass.getConstructors();

    for (Constructor c : ctors) {
       Class[] cpars = c.getParameterTypes();

       if (cpars.length != pars.length) continue;

       boolean found = true;

       for (int i = 0; i < cpars.length; ++i) {

          if (!cpars[i].isAssignableFrom(pars[i])) {
             found = false;
             break;
          }
       }
       if (found) return c;
    }
    return null;
 }

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

Now, using the names you've used in previous posts, you could:

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

...

Class[] tParameterClasses = new Class[1];
tParameterClasses[0] = gauUnau.getClass();

Class tClass = Class.forName( ActionHelper );

// using the method provided above...
// though it also should have a check for null
// in case no constructors can fit with the
// provided parameter classes...

Constructor tConstructor =
  getPossibleConstructor(tClass, tParameterClasses);

Object[] tParameterObjects = new Object[1];
tParameterObjects[0] = (Object) gauUnau;

ActionHelper actionHelper =
 (ActionHelper)
    tConstructor.newInstance(tParameterObjects);

// Bjorn A
ramakrishna - 04 May 2006 15:40 GMT
Thank y Bjorn its woring........................


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.