Java Reflection – Brief Tutorial

Hi guys, I will write simplified about Java reflection and how you can access with constructors, interfaces, super class, etc…

Java reflection it is useful to instantiate new objects, invoke methods and get/set field values without knowing the names of the classes/methods… and when you want to inspect classes, interfaces, fields and methods at runtime. It is used to examine and modify the structure and code in the same system at runtime.

For example, say you have an object of an unknown type in Java, and you would like to call a ‘doSomething’ method on it if one exists. Java’s static typing system isn’t really designed to support this unless the object conforms to a known interface, but using reflection, your code can look at the object and find out if it has a method called ‘doSomething’ and then call it if you want to.

   Method[] methods = MyObject.class.getMethods();

   for(Method method : methods){
      System.out.println("method = " + method.getName());
   }

What is happening here? It is obtaining the Class object from the class called MyObject, it is using the class object to get a list of methods in this class and print out their names.

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

For a full list you should consult the JavaDoc for java.lang.Class.

Class Object

First you need obtain the object. If you know the name:

    Class myObjectClass = MyObject.class

If you don’t know the name at compile time:

    String className = ... //obtain class name as string at runtime Class class = Class.forName(className);

Class Name

To obtain the class name, you can try both:

    Class aClass = ... //obtain Class object, as above
    String className = aClass.getName();

If you want only the name without the package:

    Class  aClass          = ... //obtain Class object. See prev. section
    String simpleClassName = aClass.getSimpleName();

Modifiers

To obtain the class modifiers:

 Class  aClass = ... //obtain Class object, as above
 int modifiers = aClass.getModifiers();

The modifiers are packed into an int, you can use the methods with java.lang.reflect.Modifier:

    Modifier.isAbstract(int modifiers)
    Modifier.isFinal(int modifiers)
    Modifier.isInterface(int modifiers)
    Modifier.isNative(int modifiers)
    Modifier.isPrivate(int modifiers)
    Modifier.isProtected(int modifiers)
    Modifier.isPublic(int modifiers)
    Modifier.isStatic(int modifiers)
    Modifier.isStrict(int modifiers)
    Modifier.isSynchronized(int modifiers)
    Modifier.isTransient(int modifiers)
    Modifier.isVolatile(int modifiers)

Package Info

With the package you can have a look in the Manifest of the JAR and others.

   Class  aClass = ... //obtain Class object, as above
   Package package = aClass.getPackage();

Superclass

You can use:

  Class superclass = aClass.getSuperclass();

Interfaces

To get a list of interfaces you can use:

   Class  aClass = ... //obtain Class object, as above
   Class[] interfaces = aClass.getInterfaces();

If you need the complete list of interfaces, you will need to consult both class and superclasses recursively.

Constructors

To access constructors:

 Constructor[] constructors = aClass.getConstructors();

Methods

To access methods:

 Method[] method = aClass.getMethods();

Fields

 Field[] method = aClass.getFields();

Annotations

 Annotation[] annotations = aClass.getAnnotations();

 

You can find more details about the reflection in each constructor, annotations. fields in this tutorial. This was only a brief post to get start to use.

Examples of java reflection: http://docs.oracle.com/javase/tutorial/reflect/index.html

 

Source: http://tutorials.jenkov.com/java-reflection/classes.html

http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.