12 Replies Latest reply: May 26, 2010 5:12 PM by 843798 RSS

    NoSuchMethodException problem

    843798
      Hi guys!

      I´m having a problem with getMethod using Reflections...I tell us:

      When I try to use a method that uses as parameters Strings, Integers , Chars,etc with the method getMethod everything works fine, but if I try to use a method that takes as parameters some instance of a class defined by me I always catch a NoSuchMethodException....

      Is not possible to use methods that take as parameters other objects defined in my personal classes???

      I am getting so crazy with this problem and I don´t know what else to try...

      Any ideas???
      Thanks
        • 1. Re: NoSuchMethodException problem
          843798
          I show you some code:

          this code throws a NoSucheMethodException

          Class claseCargada=Class.forName(org.miClase.Mobile);
          Class cArgs[] = { claseCargada };
          objMobile=claseCargada.newInstance();
          Object oParams[] = {objMobile  };
          Method mtd4=claseCargadaMms.getMethod("sendMessage",cArgs);

          but this code would work fine:


          Class cArgs[] = { String.class };

          Object oParams[] = {"example of string" };
          Method mtd4=claseCargadaMms.getMethod("sendMessage",cArgs);


          What could I do to fix it???
          I am too crazy with this problem...

          Thanks for all
          • 2. Re: NoSuchMethodException problem
            EJP
            (a) What is the declaration of the method?

            (b) Try this:
            Class claseCargada=Class.forName(org.miClase.Mobile);
            Class claseCargada = org.miClase.Mobile.class;
            • 3. Re: NoSuchMethodException problem
              843798
              I put you de whole code that I use. I wish it was useful.

              In one hand I have the class Mms that has a method sendMessage that receives as parameter an instance of Photo.

              package org.mms.implementacion;

              public class Mms implements Imms {

              public void sendMessage(Photo photo) {
              // TODO Auto-generated method stub

              System.out.println("sendMessage invoked");

              }
              }

              In other hand I have the class Photo that only has a constructor by default

              package org.mms.implementacion;
              public class Photo {

              public Photo(){

              }
              }

              And finally, here it is the code where I try to do introspection:

              System.out.println("inicio de la prueba");
              // cargaClaseClassPath is a method that load classes in the classpath to avoid catching a NotClassFoundException

              Class claseCargada= lector.cargaClaseClassPath("org.mms.implementacion .Mms");
              Class clasePhoto=lector.cargaClaseClassPath("org.mms.imp lementacion.Photo");
              Object objetoPhoto=clasePhoto.newInstance();
              Class cArgsSendMessage[]=new Class[]{clasePhoto};


              Object objPhotoArray[]=new Object[]{objetoPhoto};

              Method mtd1 = claseCargada.getMethod("sendMessage",cArgsSendMess age);


              And then throws me a NoSuchMethodException because it tells me that the parameters, that I pass, mismatch with the parameters of the method.
              But , if the method sendMessage used a String as parameter :

              cArgsSendMessage[]=new Class[]{String.class}
              it would work fine

              What could I do so wrong???
              Thanks
              • 4. Re: NoSuchMethodException problem
                EJP
                I put you de whole code that I use.
                I put you two questions. You didn't answer either of them.

                I also made a suggestion. You didn't use it.

                Instead you posted a whole lot of new code much the same as the old code with different class names.

                I don't know why you did that.
                I wish it was useful.
                What would be useful would be (a) answering two questions and (b) trying the suggestion.
                • 5. Re: NoSuchMethodException problem
                  843798
                  Sorry man, you are totally right.
                  But now I can´t try that.
                  As soon as I can I tell you the results of yours suggestions.
                  Thanks
                  • 6. Re: NoSuchMethodException problem
                    EJP
                    You can't try answering the questions I asked?
                    • 7. Re: NoSuchMethodException problem
                      843798
                      Let´s see,

                      The real code is the latest code that I posted you

                      so the declaration of the method would be

                      public void sendMessage(Photo photo)
                      {

                      }
                      and this method is inside the class org.mms.implementacion.Mms

                      I am not sure that when you mean the declaration of the method , it is what I wrote....

                      then when I tried:
                      Class claseCargada= lector.cargaClaseClassPath("org.mms.implementacion .Mms");
                      Class clasePhoto=lector.cargaClaseClassPath("org.mms.imp lementacion.Photo");

                      If I try as yoy advice me:

                      Class claseCargada= lector.cargaClaseClassPath("org.mms.implementacion .Mms.class");
                      Class clasePhoto=lector.cargaClaseClassPath("org.mms.implementacion.Photo.class");

                      I catch a NoClassFoundException

                      And if I try

                      Class claseCargada = org.mms.implementacion.Mms.class;
                      Class clasePhoto = org.mms.implementacion.Photo.class;

                      these sentences doesn´t compile.

                      Sorry for my english
                      • 8. Re: NoSuchMethodException problem
                        EJP
                        If I try as yoy advice me:

                        Class claseCargada= lector.cargaClaseClassPath("org.mms.implementacion .Mms.class");
                        Class clasePhoto=lector.cargaClaseClassPath("org.mms.implementacion.Photo.class");
                        I didn't advise that at all. Have a look at what I actually wrote.
                        • 9. Re: NoSuchMethodException problem
                          843798
                          Well if you continue reading the post to the end, I say thay I also tried

                          Class claseCargada = org.mms.implementacion.Mms.class;
                          Class clasePhoto = org.mms.implementacion.Photo.class;

                          and these sentences doesn´t compile.

                          Another option is that I haven´t understood what you really mean.
                          • 10. Re: NoSuchMethodException problem
                            EJP
                            Didn't compile how? NB I don't know the correct names of your classes, only you know that. What I'm suggesting is that you should use the class as already loaded, not load it again.
                            • 11. Re: NoSuchMethodException problem
                              jtahlborn
                              if may be that the OP is trying to use clases that are only available at runtime. if so, it's quite possible that the problem here is related to classloaders.
                              • 12. Re: NoSuchMethodException problem
                                843798
                                that´s it.

                                As I was afraid , the problem was with the ClassLoaders. I tell you, in case there could me someone with a problem like mine.

                                The problem was that I wasn´t carefully enough using the ClassLoader, and I used several ClassLoader in several points of my code, so despite of the class´es name were the same for the JVM the classes were different because it had been loaded from differents instances of ClassLoader.

                                Thanks for all and I hope this post will be useful