This allows invoking classes, interfaces, fields and methods at the run-time without knowing their names at the compile time. It is also possible to instantiate new objects, invoke methods and get / set values using the reflections.

Classes


Class aClass = AClass.class; // If you know the class name at compile time :
 Class aClass = Class.forName("com.jenkov.myapp.AClass"); // If you don't know the class name at compile time but have the class name as a string at runtime

 String className = aClass.getName(); //Class Name With package name :
 String simpleClassName = aClass.getSimpleName(); //Without package name :

 int modifiers = aClass.getModifiers(); // Obtain the Class modifiers. Modifier is a flag bit that is either set or cleared and all the modifires like class modifiers here are packed in to one int
 Modifier.isProtected(modifiers);
 Modifier.isPublic(modifiers);
 Modifier.isStatic(modifiers);

 Package package = aClass.getPackage();

 Class superclass = aClass.getSuperclass();

 Class[] interfaces = aClass.getInterfaces(); // Here only the interfaces that directly implemented the invoked class are returned

 Constructor[] constructors = aClass.getConstructors();

 Method[] method = aClass.getMethods();

 Field[] field = aClass.getFields();

 Annotation[] annotations = aClass.getAnnotations();

Constructors


// If you don't know the precise parameter types.
 Constructor[] constructors = aClass.getConstructors();
 Class[] parameterTypes = constructor.getParameterTypes(); // Read set of parameters for a given constructor in the order

// If you know the precise parameter types.
 Constructor constructor = aClass.getConstructor(new Class[]{String.class,String.class}); // getConstructor(String.class , String.class) and getConstructor(new Class[]{String.class,String.class}) are same.

AClass aClassObj = (AClass)constructor.newInstance("constructor-arg1"); // Creating an object

Fields


//If you don’t know the field name :
Field[] fileds = aClass.getFields();
String fieldName = field.getName();

//If you know the field name :
Field field = aClass.getField("someField");

Object fieldType = field.getType(); // Field type

//If the field is an instance field you can get and set the values like this :
Object value = field.get(objectInstance);
field.set(objetInstance, value);

//If the field is a static field pass null as the objectInstance parameter passed above.

Methods


//If you don’t know the method name
Method[] methods = aClass.getMethods();
String methodName = method.getName();
Class[] parameterTypes = method.getParameterTypes();

//If you know the method name and the it’s parameter Types (types, numbers and order) :
Method method = aClass.getMethod("doSomething", new Class[]{String.class}); // If it takes no parameters, pass it as null

Class returnType = method.getReturnType();

Object returnValue = method.invoke(objetInstance, "parameter-value1"); //If the method is a static method, pass null as the objectInstance parameter passed above. This must pass the exactly same number of arguments that was mentioned in the method definition.

Private Fields and Private Methods

– Will be available at the Java Reflection 2 –

Advertisements