3 Replies Latest reply on May 7, 2012 11:43 AM by BIJ001

    Class.getMethods() failing

      Say we have a class "MyClassToCheck" with code below.

      From another application I want to check if the class' method signatures conform to a certain spec without having all dependencies needed. (as indicated by "ClassNotOnClasspath").
      In this App I load the class with "forName" and then call getMethods() on it.

      cl = Class.forName("checkit.MyClassToCheck");
      Method[] meth=cl.getMethods();

      Of course the class must not have a static member of a type being unavailable at checking time. This would cause "Class.forName" to fail.

      But why do I get a NoClassDefFoundError on "getMethods()" when a var of the unknown type is declared in a try catch block??

      Can anybody help?



      package checkit;

      import refl.ReflTest2.AnotherParamOnClassPath;
      import refl.ReflTest2.ParamOnClassPath;
      import refl.ReflTest2.RetValOnClassPath;
      import tocheck.ClassNotOnClassPath;

      class MyClassToCheck
      ClassNotOnClassPath instance_member1=new ClassNotOnClassPath();; // --> WORKS !!
      //static ClassNotOnClassPath static_member1=new ClassNotOnClassPath(); // --> DOES NOT WORK which is comprehensible!!

      public RetValOnClassPath myMethodName( ParamOnClassPath par1, AnotherParamOnClassPath par2)
      ClassNotOnClassPath local=new ClassNotOnClassPath(); // --> WORKS !!

      ClassNotOnClassPath blocklocal=new ClassNotOnClassPath(); // --> WORKS !!

      try {
      *//ClassNotOnClassPath trylocal=new ClassNotOnClassPath(); // DOES NOT WORK !!!*
      RetValOnClassPath rv=new RetValOnClassPath();
      catch(Exception e)

      return new RetValOnClassPath();
        • 1. Re: Class.getMethods() failing
          The class isn't being executed when it is loaded, so where the unresolvable reference is in the code can't possibly make any difference.
          • 2. Re: Class.getMethods() failing
            So I had thought too.
            But it does matter.
            Some more experiments showed that Class.forName fails on code containing a var decl using an unresolvable class in a try block if that code was compiled with Java 1.4 or Java 5. It does not fail when the code is compiled with J6 or 7. Must have something to do with the bytecode definition. Have tried only with Sun/Oracle Java compilers/VMs and Eclipse's built in JDT compiler/generator.
            • 3. Re: Class.getMethods() failing
              if that code was compiled with Java 1.4 or Java 5. It does not fail when the code is compiled with J6 or 7
              It would not wonder me if an older version JVM could not resolve classes compiled with a newer version of JVM.