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;
}
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;
}
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;
}
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;
}
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);
}
Aggregations