So i have those 2 classes running on jdk 7:
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("full_path.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?
getDeclaredMethiods()states that it 'excludes inherited methods'. The output you claim to have got is therefore impossible. I note that it wasn't formatted correctly for the code you posted, which is curious. Presumably you aren't running the code you think you're running. Perhaps you calledgetMethods()instead ofgetDeclaredMethods().complete_path.because it exposes too many informations. Btw have you tried it yourself ?getDeclaredMethods()JavaDoc. Are you sure that this is a full example?