[Java] – Understanding Reflections !!!

Use:

Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine.

When :

It is necessary to handle different classes of objects that do not share the same interface.

  • Example working with objects:

To take the names of fields witch are with null values, no matter the class of the object.

/**
 * @param obj - N object from N class
 * @return - List with Null Fields Names
 */
@Test
public static List<String> getNullFieldsNames(Object obj){
    try{
        List<String> allNames = new ArrayList<String>(0);
        Class<?> classObj = obj.getClass();
        Arrays.asList(classObj.getFields()).stream().forEach(field -> {
            try {
                if ( field.get(obj) == null ){
                    allNames.add(field.getName());
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        });
        return allNames;
    }catch (Exception e){
        throw e;
    }
}
  • Example working with classes:

You can load classes, get the interfaces and a lot of others informations to make your implementation.

public class ClassMap{
    private Map<String,String> map;

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    // Here we do the loading of the class and return
    public Class getClass(String keyClass) throws Exception{
        String v = map.get(keyClass);
        if(v != null){
            return Class.forName(v);
        }else{
            throw new RuntimeException("Not found with this key");
        }
    }
}

( Running ):

@Test
public void testClassMap(){
    ClassMap classMap = new ClassMap();
    Map<String, String> myMap = new HashMap<String, String>();
    myMap.put("ArrayList","java.util.ArrayList");
    classMap.setMap(myMap);

    try {
        Class<?> myClass = classMap.getClass("ArrayList");
        System.out.println("Name : " + myClass.getName());
        Arrays.asList(myClass.getInterfaces()).stream().forEach(c-> System.out.println("Interface Name: " + c.getName()));
    }catch (Exception r){
        System.out.println(r);
    }
}

Console:

Screen Shot 2016-06-05 at 12.21.32 AM

API : https://docs.oracle.com/javase/tutorial/reflect/

1 Comment

  1. It’s perfect time to make some plans for the future and it’s time to be happy.

    I have read this post and if I could I desire to suggest you
    some interesting things or suggestions. Maybe you can write next articles referring to this article.

    I want to read even more things about it!

    Like

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