Search in sources :

Example 56 with Method

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

the class BeanXMLReader method readRecursion.

@SuppressWarnings("unchecked")
private <T> T readRecursion(Element bean) throws Exception {
    Class<T> clazz = (Class<T>) this.classes.get(bean.getName());
    T o = clazz.newInstance();
    ReflectUtil ru = new ReflectUtil(o);
    Field[] fields = ru.getFields();
    for (Field f : fields) {
        String n = f.getName();
        Method m = ru.getSetter(n);
        if (m == null)
            continue;
        Skip skip = f.getAnnotation(Skip.class);
        if (skip != null)
            continue;
        AttrTag attrTag = f.getAnnotation(AttrTag.class);
        Writeonly writeonly = f.getAnnotation(Writeonly.class);
        if (writeonly != null)
            continue;
        if (attrTag != null) {
            if ("clazz".equals(n))
                n = "class";
            Attribute a = bean.attribute(n);
            if (a != null)
                m.invoke(o, a.getData());
        } else if (ClassUtil.isPojo(f.getType())) {
            Element el = bean.element(n);
            if (el == null)
                continue;
            String cls = this.classes.get(el.getName()).getName();
            Object object = Thread.currentThread().getContextClassLoader().loadClass(cls).newInstance();
            // 递归
            object = readRecursion(el);
            m.invoke(o, object);
        } else if (ClassUtil.isListClass(f)) {
            List<?> eList = bean.elements(n);
            if (eList == null)
                continue;
            List<Object> list = new ArrayList<Object>();
            for (Iterator<?> it = eList.iterator(); it.hasNext(); ) {
                Element e = (Element) it.next();
                // 递归
                list.add(readRecursion(e));
            }
            m.invoke(o, list);
        } else if (ClassUtil.isListString(f)) {
            List<?> eList = bean.elements(n);
            if (eList == null)
                continue;
            List<String> list = new ArrayList<String>();
            for (Iterator<?> it = eList.iterator(); it.hasNext(); ) {
                Element e = (Element) it.next();
                list.add(e.getText());
            }
            m.invoke(o, list);
        } else {
            if ("clazz".equals(n))
                n = "class";
            Element a = bean.element(n);
            if (a == null)
                continue;
            m.invoke(o, String.valueOf(a.getData()));
        }
    }
    return o;
}
Also used : Attribute(org.dom4j.Attribute) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ReflectUtil(org.eweb4j.util.ReflectUtil) Field(java.lang.reflect.Field) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 57 with Method

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

the class JsonConverter method convertFromKeyValue.

/**
	 * 将键值对key-value转换为json(给定是否为其他对象属性)
	 * 
	 * @param <T>
	 * @param key
	 * @param value
	 * @return
	 */
private static <T> String convertFromKeyValue(String key, T value, boolean isProperty) {
    String json = null;
    if (value != null) {
        Class<?> cls = value.getClass();
        String format = String.class.isAssignableFrom(cls) ? "\"%s\":\"%s\"" : "\"%s\":%s";
        if (String.class.isAssignableFrom(cls) || Integer.class.isAssignableFrom(cls) || Long.class.isAssignableFrom(cls) || Float.class.isAssignableFrom(cls) || Double.class.isAssignableFrom(cls) || Boolean.class.isAssignableFrom(cls) || int.class.isAssignableFrom(cls) || long.class.isAssignableFrom(cls) || float.class.isAssignableFrom(cls) || double.class.isAssignableFrom(cls) || boolean.class.isAssignableFrom(cls)) {
            // 基本数据类型,key-value
            if (key == null || "".equals(key.trim())) {
                format = String.class.isAssignableFrom(cls) ? "\"%s\"" : "%s";
                json = String.format(format, String.valueOf(value));
            } else {
                json = String.format(format, key, String.valueOf(value));
            }
        } else if (Object[].class.isAssignableFrom(cls)) {
            // 对象数组,使用下标来遍历对象
            json = JsonConverter.convertFromArray(key, (Object[]) value, isProperty);
        } else if (List.class.isAssignableFrom(cls)) {
            // 对象数组,使用下标来遍历对象
            json = JsonConverter.convertFromList(key, (List<?>) value, isProperty);
        } else if (Set.class.isAssignableFrom(cls)) {
            // 对象数组,使用下标来遍历对象
            json = JsonConverter.convertFromSet(key, (Set<?>) value, isProperty);
        } else if (Map.class.isAssignableFrom(cls)) {
            json = JsonConverter.convertFromMap(key, (Map<?, ?>) value, isProperty);
        } else {
            // 当不满足上面所有类型时,认为是自定义类型
            // 反射获取属性,将属性递归
            ReflectUtil ru = new ReflectUtil(value);
            StringBuilder sb = new StringBuilder("{");
            format = "\"%s\":%s";
            for (String name : ru.getFieldsName()) {
                Method m = ru.getMethod("get" + CommonUtil.toUpCaseFirst(name));
                if (m != null) {
                    try {
                        json = JsonConverter.convertFromKeyValue(name, m.invoke(value), true);
                        if (json != null) {
                            if (sb.length() > 1) {
                                sb.append(",");
                            }
                            sb.append(json);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            sb.append("}");
            // 如果key没有给定具体的值,则认为是匿名对象(自定义类型)
            if (key == null || "".equals(key)) {
                format = "%s";
                json = String.format(format, sb.toString());
            } else {
                json = String.format(format, key, sb.toString());
            }
        }
    }
    return json;
}
Also used : Set(java.util.Set) List(java.util.List) Method(java.lang.reflect.Method) Map(java.util.Map)

Example 58 with Method

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

the class ClassUtil method injectFieldValue.

/**
	 * 
	 * @param <T>
	 * @param fieldName
	 * @param v
	 * @param vs
	 * @param pojo
	 * @return
	 * @throws Exception
	 */
public static <T> T injectFieldValue(T pojo, String fieldName, String[] vs) throws Exception {
    if (pojo == null)
        return null;
    if (vs == null)
        return pojo;
    ReflectUtil ru = new ReflectUtil(pojo);
    Field f = ru.getField(fieldName);
    if (f == null)
        return pojo;
    Method setter = ru.getSetter(fieldName);
    if (setter == null)
        return pojo;
    Class<?> clazz = f.getType();
    if (Object[].class.isAssignableFrom(clazz)) {
        Object obj = getParamVals(clazz, vs);
        if (obj != null)
            setter.invoke(pojo, new Object[] { obj });
    } else {
        Object obj = getParamVal(clazz, vs[0]);
        if (obj != null)
            setter.invoke(pojo, obj);
    }
    return pojo;
}
Also used : Field(java.lang.reflect.Field) Method(java.lang.reflect.Method)

Example 59 with Method

use of java.lang.reflect.Method in project libgdx by libgdx.

the class SharedLibraryLoader method canExecute.

private boolean canExecute(File file) {
    try {
        Method canExecute = File.class.getMethod("canExecute");
        if ((Boolean) canExecute.invoke(file))
            return true;
        Method setExecutable = File.class.getMethod("setExecutable", boolean.class, boolean.class);
        setExecutable.invoke(file, true, false);
        return (Boolean) canExecute.invoke(file);
    } catch (Exception ignored) {
    }
    return false;
}
Also used : Method(java.lang.reflect.Method) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 60 with Method

use of java.lang.reflect.Method in project killbill by killbill.

the class MultiTenantConfigBase method convertToListString.

//
// The conversion methds are rather limited (but this is all we need).
// Ideally we could reuse the bully/Coercer from skife package, but those are kept private.
//
protected List<String> convertToListString(final String value, final String methodName) {
    final Method method = getConfigStaticMethodWithChecking(methodName);
    final Iterable<String> tokens = getTokens(method, value);
    return ImmutableList.copyOf(tokens);
}
Also used : Method(java.lang.reflect.Method)

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