All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.neogroup.warp.utils.Introspection Maven / Gradle / Ivy

There is a newer version: 1.4.3
Show newest version
package org.neogroup.warp.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class Introspection {

    private static final Map> properties = new HashMap<>();

    public static List getProperties(Class implementationClass) {
        List properties = Introspection.properties.get(implementationClass);
        if (properties == null) {
            properties = new ArrayList<>();
            Introspection.properties.put(implementationClass, properties);
            Field[] declaredFields = implementationClass.getDeclaredFields();
            for (Field field : declaredFields) {
                String propertyName = field.getName();
                String accessorPropertyName = propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
                String getterMethodName = (Boolean.class.isAssignableFrom(field.getType())? "is" : "get") + accessorPropertyName;
                Method getterMethod = null;
                try {
                    getterMethod = implementationClass.getDeclaredMethod(getterMethodName);
                } catch (NoSuchMethodException e) {}
                String setterMethodName = "set" + accessorPropertyName;
                Method setterMethod = null;
                try {
                    setterMethod = implementationClass.getDeclaredMethod(setterMethodName);
                } catch (NoSuchMethodException e) {}
                if (getterMethod != null || setterMethod != null) {
                    Property property = new Property(propertyName, getterMethod, setterMethod);
                    properties.add(property);
                }
            }
        }
        return properties;
    }

    public static class Property {

        private final String name;
        private final Method getterMethod;
        private final Method setterMethod;

        public Property(String name, Method getterMethod, Method setterMethod) {
            this.name = name;
            this.getterMethod = getterMethod;
            this.setterMethod = setterMethod;
        }

        public String getName() {
            return name;
        }

        public  O getValue(Object instance) {
            Object result = null;
            try {
                result = getterMethod.invoke(instance);
            } catch (Exception e) {}
            return (O)result;
        }

        public void setValue(Object instance, Object value) {
            try {
                setterMethod.invoke(instance, value);
            } catch (Exception e) {}
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy