mercredi 28 janvier 2015

Loading java class methods, docs not consistent with method's behavior

So i have those 2 classes:



abstract class Aclass
{
public void foo()
{
}
public void bar()
{
}
}


And:



public class Bclass extends Aclass
{
public void foo(Integer one)
{
}
public void bar(String two)
{
}
}


My goal is to load Bclass, and Bclass ONLY, print out its declared methods and parameters of those declared methods. Here is the code i use:



public static void main(String[] args)
{
try
{
Class<?> clazz = Tester.class.getClassLoader().loadClass("com.nedstat.reporting.consistency.parameters.reflection_test.Bclass");
for (Method method : clazz.getDeclaredMethods())
{
System.out.println("Method name: " + method.getName() + " From class: " + method.getDeclaringClass().getCanonicalName() + " with declared methods:");// test
for (Class<?> param : method.getParameterTypes())
{
System.out.println(param.getCanonicalName());
}
}
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}


Running this code it produces the following output:



Method name: foo From class:
complete_path.Bclass with declared methods:
Method name: foo From class:
complete_path.Bclass with declared methods:
java.lang.Integer
Method name: bar From class:
complete_path.Bclass with declared methods:
Method name: bar From class:
complete_path.Bclass with declared methods:
java.lang.String


But in the javadoc's of the method [getDeclaredMethods()] i see but excludes inherited methods , this seems not to be the case according to my tests, the method apparently does load inherited methods when they are overloaded. Or am i doing something wrong?






Aucun commentaire:

Enregistrer un commentaire