Sparkles

that were shone when I got tempered!

Java Reflection 1 – Accessing classes, interfaces, fields, methods at the run-time without knowing their names at the compile time

with 4 comments

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

Written by Namal Fernando

August 12, 2013 at 11:09 pm

Posted in Java, Reflection

Tagged with ,

4 Responses

Subscribe to comments with RSS.

  1. […] Java Reflection – how to invoke classes, interfaces, fields and methods at the run-time withou… (ntechgeek.wordpress.com) […]

  2. […] Java Reflection – how to invoke classes, interfaces, fields and methods at the run-time withou… (ntechgeek.wordpress.com) […]

  3. […] Java Reflection – how to invoke classes, interfaces, fields and methods at the run-time withou… (nandirx.wordpress.com) […]

  4. […] Java Reflection – how to invoke classes, interfaces, fields and methods at the run-time withou… (nandirx.wordpress.com) […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Ruth's Reflections

Contemplations from quakey quirky Christchurch

TED Blog

The TED Blog shares interesting news about TED, TED Talks video, the TED Prize and more.

Ziplok

Learn and discover simple things

Meihta Dwiguna Saputra's Knowledge Base

~In learning you will teach and in teaching you will (re)learn~

The Java Blog

Thoughts, tips and tricks about the Java programming language

Sparkles

that were shone when I got tempered!

%d bloggers like this: