Search in sources :

Example 1 with LRUCache

use of org.teiid.core.util.LRUCache in project teiid by teiid.

the class TeiidScriptEngine method getMethodMap.

public Map<String, Method> getMethodMap(Class<?> clazz) throws ScriptException {
    Map<Class<?>, Map<String, Method>> clazzMaps = null;
    Map<String, Method> methodMap = null;
    if (properties != null) {
        clazzMaps = properties.get();
        if (clazzMaps != null) {
            methodMap = clazzMaps.get(clazz);
            if (methodMap != null) {
                return methodMap;
            }
        }
    }
    try {
        BeanInfo info = Introspector.getBeanInfo(clazz);
        PropertyDescriptor[] pds = info.getPropertyDescriptors();
        methodMap = new LinkedHashMap<String, Method>();
        if (pds != null) {
            for (int j = 0; j < pds.length; j++) {
                PropertyDescriptor pd = pds[j];
                if (pd.getReadMethod() == null || pd instanceof IndexedPropertyDescriptor) {
                    continue;
                }
                String name = pd.getName();
                Method m = pd.getReadMethod();
                methodMap.put(name, m);
            }
        }
        MethodDescriptor[] mds = info.getMethodDescriptors();
        if (pds != null) {
            for (int j = 0; j < mds.length; j++) {
                MethodDescriptor md = mds[j];
                if (md.getMethod() == null || md.getMethod().getParameterTypes().length > 0 || md.getMethod().getReturnType() == Void.class || md.getMethod().getReturnType() == void.class) {
                    continue;
                }
                String name = md.getName();
                Method m = md.getMethod();
                methodMap.put(name, m);
            }
        }
        if (clazzMaps == null) {
            clazzMaps = Collections.synchronizedMap(new LRUCache<Class<?>, Map<String, Method>>(100));
            properties = new SoftReference<Map<Class<?>, Map<String, Method>>>(clazzMaps);
        }
        clazzMaps.put(clazz, methodMap);
    } catch (IntrospectionException e) {
        throw new ScriptException(e);
    }
    return methodMap;
}
Also used : IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor) PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method) MethodDescriptor(java.beans.MethodDescriptor) ScriptException(javax.script.ScriptException) LRUCache(org.teiid.core.util.LRUCache) IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

BeanInfo (java.beans.BeanInfo)1 IndexedPropertyDescriptor (java.beans.IndexedPropertyDescriptor)1 IntrospectionException (java.beans.IntrospectionException)1 MethodDescriptor (java.beans.MethodDescriptor)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Method (java.lang.reflect.Method)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ScriptException (javax.script.ScriptException)1 LRUCache (org.teiid.core.util.LRUCache)1