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