Java Reflection
Why and where Reflectioin?
Reflection allows a program to work with code that may not be present and do so in a reliable way.
"Normal code" have snippets like URLConnection c = null
which by its sheer presence cause the class loader to load the URLConnection class as part of loading this class, throwing a ClassNotFound exception and exiting.
Reflection allow you to load classes based on their names in string form and test them for various properties (useful for multiple versions outside your control) before launching actual classes that depend on them. A typical example is the OS X specific code used to make Java programs look native under OS X, which are not present on other platforms.
Classes
Using Java Reflection you can inspect Java classes at runtime. Inspecting classes is often the first thing you do when using Reflection. From the classes you can obtain information about
- Class Name
- Class Modifies (public, private, synchronized etc.)
- Package Info
- Superclass
- Implemented Interfaces
- Constructors
- Methods
- Fields
- Annotations
plus a lot more information related to Java classes. For a full list you should consult theJavaDoc for java.lang.Class. This text will briefly touch upon all accessing of the above mentioned information. Some of the topics will also be examined in greater detail in separate texts. For instance, this text will show you how to obtain all methods or a specific method, but a separate text will show you how to invoke that method, how to find the method matching a given set of arguments if more than one method exists with the same name, what exceptions are thrown from method invocation via reflection, how to spot a getter/setter etc. The purpose of this text is primarily to introduce the Class
object and the information you can obtain from it.
Get Class Object
Class
object like this:
Class myClass=String.class;
System.out.println(myClass.toString());
result: class java.lang.String
String s="Hello world";//an Object at runtime
String className=s.getClass().getName();//obtain class name as string at runtime
Class myClass=Class.forName(className);
//forname static method support only fully qualified name
System.out.println("fully qualified name: "+className);
System.out.println("get class as string: "+myClass.toString());
get result
fully qualified name: java.lang.String
get class as string: class java.lang.String
Get Class Name
String s="Hello world";//an Object at runtime
String className=s.getClass().getName();//fully qualified name
String classSimpleName=s.getClass().getSimpleName();
System.out.println("fully qualified name: "+className);
System.out.println("simple name: : "+classSimpleName);
fully qualified name: java.lang.String
simple name: : String
Modifier
Class
object. The class modifiers are the keywords "public", "private", "static" etc. You obtain the class modifiers like this: String s="Hello world";
Class myClass=s.getClass();
int modifiers=myClass.getModifiers();
System.out.println(Modifier.toString(modifiers));
//output the modifiers of class string