Search in sources :

Example 66 with Method

use of java.lang.reflect.Method in project disconf by knightliao.

the class ClassUtils method filter2Map.

/**
     * 将方法中非static的public方法获取到
     *
     * @param ms
     *
     * @return
     */
public static Map<String, Method> filter2Map(Set<Method> ms) {
    Map<String, Method> map = new HashMap<String, Method>();
    for (Method m : ms) {
        boolean flag = Modifier.isPublic(m.getModifiers()) && !Modifier.isStatic(m.getModifiers());
        if (flag) {
            String name = m.getName().toLowerCase();
            if (name.startsWith("get") && m.getParameterTypes().length == 0) {
            } else if (name.startsWith("is") && m.getParameterTypes().length == 0) {
            } else if (name.startsWith("set") && m.getParameterTypes().length == 1) {
            } else {
                continue;
            }
            // 获取同名的方法
            Method old = map.get(name);
            // 如果之前没有同名方法,则添加本方法
            if (old == null) {
                map.put(name, m);
            // 如果有同名方法,且本方法在子类中声明,且,父类本方法包含了annotation,则替换原来的方法
            } else if (old.getDeclaringClass().isAssignableFrom(m.getDeclaringClass()) && m.getAnnotation(Column.class) != null) {
                map.put(name, m);
            }
        }
    }
    return map;
}
Also used : HashMap(java.util.HashMap) Column(com.baidu.unbiz.common.genericdao.annotation.Column) Method(java.lang.reflect.Method)

Example 67 with Method

use of java.lang.reflect.Method in project disconf by knightliao.

the class PrivateMethodUtil method invokeMethod.

/**
     * spring注入对象的私有方法调用
     * 
     * @param owner
     *            注入的对象
     * @param methodName
     *            私有方法名
     * @param parameterTypes
     *            私有方法参数类型
     * @param parameters
     *            私有方法参数
     * @return 私有方法返回值
     */
@SuppressWarnings({ "rawtypes" })
public static Object invokeMethod(final Object owner, final String methodName, final Class[] parameterTypes, final Object[] parameters) throws Exception {
    // get class
    final Class ownerclass = owner.getClass();
    // get property
    try {
        @SuppressWarnings("unchecked") final Method getTargetClass = ownerclass.getMethod("getTargetSource");
        final TargetSource target = (TargetSource) getTargetClass.invoke(owner, new Object[] {});
        final Class targetClass = target.getTargetClass();
        @SuppressWarnings("unchecked") final Method method = targetClass.getDeclaredMethod(methodName, parameterTypes);
        if (!method.isAccessible()) {
            method.setAccessible(true);
        }
        final Object targetInstance = target.getTarget();
        return method.invoke(targetInstance, parameters);
    } catch (NoSuchMethodException e) {
        return invokeMethod(owner, 0, methodName, parameterTypes, parameters);
    // e.printStackTrace();
    }
}
Also used : TargetSource(org.springframework.aop.TargetSource) Method(java.lang.reflect.Method)

Example 68 with Method

use of java.lang.reflect.Method in project disconf by knightliao.

the class PrivateMethodUtil method invokeMethod.

/**
     * 普通对象私有方法调用
     * 
     * @param owner
     *            : target object
     * @param classLevel
     *            : 0 means itself, 1 means it's father, and so on...
     * @param methodName
     *            : name of the target method
     * @param parameterTypes
     *            : types of the target method's parameters
     * @param parameters
     *            : parameters of the target method
     * @return result of invoked method
     */
@SuppressWarnings("rawtypes")
public static Object invokeMethod(final Object owner, final int classLevel, final String methodName, final Class[] parameterTypes, final Object[] parameters) throws Exception {
    // get class
    final Class ownerclass = getOwnerclass(owner, classLevel);
    // get property
    @SuppressWarnings("unchecked") final Method method = ownerclass.getDeclaredMethod(methodName, parameterTypes);
    if (!method.isAccessible()) {
        method.setAccessible(true);
    }
    return method.invoke(owner, parameters);
}
Also used : Method(java.lang.reflect.Method)

Example 69 with Method

use of java.lang.reflect.Method in project Lazy by l123456789jy.

the class ClassUtils method searchProperty.

@SuppressWarnings("unchecked")
public static Object searchProperty(Object leftParameter, String name) throws Exception {
    Class<?> leftClass = leftParameter.getClass();
    Object result;
    if (leftParameter.getClass().isArray() && "length".equals(name)) {
        result = Array.getLength(leftParameter);
    } else if (leftParameter instanceof Map) {
        result = ((Map<Object, Object>) leftParameter).get(name);
    } else {
        try {
            String getter = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
            Method method = leftClass.getMethod(getter, new Class<?>[0]);
            if (!method.isAccessible()) {
                method.setAccessible(true);
            }
            result = method.invoke(leftParameter, new Object[0]);
        } catch (NoSuchMethodException e2) {
            try {
                String getter = "is" + name.substring(0, 1).toUpperCase() + name.substring(1);
                Method method = leftClass.getMethod(getter, new Class<?>[0]);
                if (!method.isAccessible()) {
                    method.setAccessible(true);
                }
                result = method.invoke(leftParameter, new Object[0]);
            } catch (NoSuchMethodException e3) {
                Field field = leftClass.getField(name);
                result = field.get(leftParameter);
            }
        }
    }
    return result;
}
Also used : Field(java.lang.reflect.Field) Method(java.lang.reflect.Method) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 70 with Method

use of java.lang.reflect.Method in project eweb4j-framework by laiweiwei.

the class ParamUtil method injectFile.

public static void injectFile(Context context, ReflectUtil ru, String startName) throws Exception {
    Hashtable<String, Object> hasPojo = new Hashtable<String, Object>();
    paramForeach: for (Entry<String, List<UploadFile>> en : context.getUploadMap().entrySet()) {
        String paramName = en.getKey();
        List<UploadFile> paramValue = en.getValue();
        if (paramValue == null || paramValue.size() == 0)
            continue;
        Method setter = null;
        String[] pojoParamNames = paramName.split("\\.");
        if (pojoParamNames.length > 1) {
            Object lastPojo = ru.getObject();
            if (startName != null && startName.trim().length() > 0) {
                if (!pojoParamNames[0].equals(startName))
                    continue;
            }
            int lastIndex = pojoParamNames.length - 1;
            for (int i = 0; i < lastIndex; i++) {
                if (startName != null && startName.trim().length() > 0) {
                    if ((i + 1) == lastIndex)
                        break;
                    lastPojo = getLastPojo(lastPojo, pojoParamNames[i + 1], hasPojo);
                } else {
                    lastPojo = getLastPojo(lastPojo, pojoParamNames[i], hasPojo);
                }
                if (lastPojo == null)
                    continue paramForeach;
            }
            String _paramName = pojoParamNames[lastIndex];
            if (Map.class.isAssignableFrom(lastPojo.getClass())) {
                Map<String, Object> map = (HashMap<String, Object>) lastPojo;
                if (paramValue.size() <= 1)
                    map.put(_paramName, paramValue.get(0));
                else
                    map.put(_paramName, paramValue);
                lastPojo = map;
            }
            ReflectUtil lpRu = new ReflectUtil(lastPojo);
            setter = lpRu.getSetter(_paramName);
            if (setter == null)
                continue;
            Class<?> clazz = setter.getParameterTypes()[0];
            if (File.class.isAssignableFrom(clazz)) {
                setter.invoke(lastPojo, paramValue.get(0).getTmpFile());
            } else if (File[].class.isAssignableFrom(clazz)) {
                File[] files = new File[paramValue.size()];
                for (int j = 0; j < files.length; j++) files[j] = paramValue.get(j).getTmpFile();
                setter.invoke(lastPojo, new Object[] { files });
            }
            if (UploadFile.class.isAssignableFrom(clazz)) {
                setter.invoke(lastPojo, paramValue.get(0));
            } else if (UploadFile[].class.isAssignableFrom(clazz)) {
                UploadFile[] files = new UploadFile[paramValue.size()];
                for (int j = 0; j < files.length; j++) files[j] = paramValue.get(j);
                setter.invoke(lastPojo, new Object[] { files });
            }
        } else {
            setter = ru.getSetter(paramName);
            if (setter == null)
                continue;
            Class<?> clazz = setter.getParameterTypes()[0];
            if (File.class.isAssignableFrom(clazz)) {
                setter.invoke(ru.getObject(), paramValue.get(0).getTmpFile());
            } else if (File[].class.isAssignableFrom(clazz)) {
                File[] files = new File[paramValue.size()];
                for (int j = 0; j < files.length; j++) files[j] = paramValue.get(j).getTmpFile();
                setter.invoke(ru.getObject(), new Object[] { files });
            }
            if (UploadFile.class.isAssignableFrom(clazz)) {
                setter.invoke(ru.getObject(), paramValue.get(0));
            } else if (UploadFile[].class.isAssignableFrom(clazz)) {
                UploadFile[] files = new UploadFile[paramValue.size()];
                for (int j = 0; j < files.length; j++) files[j] = paramValue.get(j);
                setter.invoke(ru.getObject(), new Object[] { files });
            }
        }
    }
}
Also used : Hashtable(java.util.Hashtable) Method(java.lang.reflect.Method) ReflectUtil(org.eweb4j.util.ReflectUtil) Entry(java.util.Map.Entry) UploadFile(org.eweb4j.mvc.upload.UploadFile) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) UploadFile(org.eweb4j.mvc.upload.UploadFile)

Aggregations

Method (java.lang.reflect.Method)8797 Test (org.junit.Test)1772 InvocationTargetException (java.lang.reflect.InvocationTargetException)1084 ArrayList (java.util.ArrayList)665 Field (java.lang.reflect.Field)611 IOException (java.io.IOException)549 HashMap (java.util.HashMap)352 Map (java.util.Map)290 List (java.util.List)275 PropertyDescriptor (java.beans.PropertyDescriptor)253 Annotation (java.lang.annotation.Annotation)212 Type (java.lang.reflect.Type)202 HashSet (java.util.HashSet)199 File (java.io.File)173 IndexedPropertyDescriptor (java.beans.IndexedPropertyDescriptor)170 BeanInfo (java.beans.BeanInfo)166 ParameterizedType (java.lang.reflect.ParameterizedType)132 Constructor (java.lang.reflect.Constructor)131 SimpleBeanInfo (java.beans.SimpleBeanInfo)128 FakeFox01BeanInfo (org.apache.harmony.beans.tests.support.mock.FakeFox01BeanInfo)128