//$Id: Reflections.java 9572 2008-11-15 22:33:10Z shane.bryzak@jboss.com $
package org.jboss.seam.util;
import java.beans.Introspector;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class Reflections
{
public static Object invoke(Method method, Object target, Object... args) throws Exception
{
try
{
return method.invoke( target, args );
}
catch (IllegalArgumentException iae)
{
String message = "Could not invoke method by reflection: " + toString(method);
if (args!=null && args.length>0)
{
message += " with parameters: (" + Strings.toClassNameString(", ", args) + ')';
}
message += " on: " + target.getClass().getName();
throw new IllegalArgumentException(message, iae);
}
catch (InvocationTargetException ite)
{
if ( ite.getCause() instanceof Exception )
{
throw (Exception) ite.getCause();
}
else
{
throw ite;
}
}
}
public static Object get(Field field, Object target) throws Exception
{
boolean accessible = field.isAccessible();
try
{
field.setAccessible(true);
return field.get(target);
}
catch (IllegalArgumentException iae)
{
String message = "Could not get field value by reflection: " + toString(field) +
" on: " + target.getClass().getName();
throw new IllegalArgumentException(message, iae);
}
finally
{
field.setAccessible(accessible);
}
}
public static void set(Field field, Object target, Object value) throws Exception
{
try
{
field.set(target, value);
}
catch (IllegalArgumentException iae)
{
// target may be null if field is static so use field.getDeclaringClass() instead
String message = "Could not set field value by reflection: " + toString(field) +
" on: " + field.getDeclaringClass().getName();
if (value==null)
{
message += " with null value";
}
else
{
message += " with value: " + value.getClass();
}
throw new IllegalArgumentException(message, iae);
}
}
public static Object getAndWrap(Field field, Object target)
{
boolean accessible = field.isAccessible();
try
{
field.setAccessible(true);
return get(field, target);
}
catch (Exception e)
{
if (e instanceof RuntimeException)
{
throw (RuntimeException) e;
}
else
{
throw new IllegalArgumentException("exception setting: " + field.getName(), e);
}
}
finally
{
field.setAccessible(accessible);
}
}
public static void setAndWrap(Field field, Object target, Object value)
{
boolean accessible = field.isAccessible();
try
{
field.setAccessible(true);
set(field, target, value);
}
catch (Exception e)
{
if (e instanceof RuntimeException)
{
throw (RuntimeException) e;
}
else
{
throw new IllegalArgumentException("exception setting: " + field.getName(), e);
}
}
finally
{
field.setAccessible(accessible);
}
}
public static Object invokeAndWrap(Method method, Object target, Object... args)
{
try
{
return invoke(method, target, args);
}
catch (Exception e)
{
if (e instanceof RuntimeException)
{
throw (RuntimeException) e;
}
else
{
throw new RuntimeException("exception invoking: " + method.getName(), e);
}
}
}
public static String toString(Method method)
{
return Strings.unqualify( method.getDeclaringClass().getName() ) +
'.' +
method.getName() +
'(' +
Strings.toString( ", ", method.getParameterTypes() ) +
')';
}
public static String toString(Member member)
{
return Strings.unqualify( member.getDeclaringClass().getName() ) +
'.' +
member.getName();
}
public static Class classForName(String name) throws ClassNotFoundException
{
try
{
return Thread.currentThread().getContextClassLoader().loadClass(name);
}
catch (Exception e)
{
return Class.forName(name);
}
}
/**
* Return's true if the class can be loaded using Reflections.classForName()
*/
public static boolean isClassAvailable(String name)
{
try
{
classForName(name);
}
catch (ClassNotFoundException e) {
return false;
}
return true;
}
public static Class getCollectionElementType(Type collectionType)
{
if ( !(collectionType instanceof ParameterizedType) )
{
throw new IllegalArgumentException("collection type not parameterized");
}
Type[] typeArguments = ( (ParameterizedType) collectionType ).getActualTypeArguments();
if (typeArguments.length==0)
{
throw new IllegalArgumentException("no type arguments for collection type");
}
Type typeArgument = typeArguments.length==1 ? typeArguments[0] : typeArguments[1]; //handle Maps
if (typeArgument instanceof ParameterizedType)
{
typeArgument = ((ParameterizedType) typeArgument).getRawType();
}
if ( !(typeArgument instanceof Class) )
{
throw new IllegalArgumentException("type argument not a class");
}
return (Class) typeArgument;
}
public static Class getMapKeyType(Type collectionType)
{
if ( !(collectionType instanceof ParameterizedType) )
{
throw new IllegalArgumentException("collection type not parameterized");
}
Type[] typeArguments = ( (ParameterizedType) collectionType ).getActualTypeArguments();
if (typeArguments.length==0)
{
throw new IllegalArgumentException("no type arguments for collection type");
}
Type typeArgument = typeArguments[0];
if ( !(typeArgument instanceof Class) )
{
throw new IllegalArgumentException("type argument not a class");
}
return (Class) typeArgument;
}
public static Method getSetterMethod(Class clazz, String name)
{
Method[] methods = clazz.getMethods();
for (Method method: methods)
{
String methodName = method.getName();
if ( methodName.startsWith("set") && method.getParameterTypes().length==1 )
{
if ( Introspector.decapitalize( methodName.substring(3) ).equals(name) )
{
return method;
}
}
}
throw new IllegalArgumentException("no such setter method: " + clazz.getName() + '.' + name);
}
public static Method getGetterMethod(Class clazz, String name)
{
Method[] methods = clazz.getMethods();
for (Method method: methods)
{
String methodName = method.getName();
if ( method.getParameterTypes().length==0 )
{
if ( methodName.startsWith("get") )
{
if ( Introspector.decapitalize( methodName.substring(3) ).equals(name) )
{
return method;
}
}
else if ( methodName.startsWith("is") )
{
if ( Introspector.decapitalize( methodName.substring(2) ).equals(name) )
{
return method;
}
}
}
}
throw new IllegalArgumentException("no such getter method: " + clazz.getName() + '.' + name);
}
/**
* Get all the getter methods annotated with the given annotation. Returns an empty list if
* none are found
*/
public static List<Method> getGetterMethods(Class clazz, Class annotation)
{
List<Method> methods = new ArrayList<Method>();
for (Method method : clazz.getMethods())
{
if (method.isAnnotationPresent(annotation))
{
methods.add(method);
}
}
return methods;
}
public static Field getField(Class clazz, String name)
{
for ( Class superClass = clazz; superClass!=Object.class; superClass=superClass.getSuperclass() )
{
try
{
return superClass.getDeclaredField(name);
}
catch (NoSuchFieldException nsfe) {}
}
throw new IllegalArgumentException("no such field: " + clazz.getName() + '.' + name);
}
/**
* Get all the fields which are annotated with the given annotation. Returns an empty list
* if none are found
*/
public static List<Field> getFields(Class clazz, Class annotation)
{
List<Field> fields = new ArrayList<Field>();
for (Class superClass = clazz; superClass!=Object.class; superClass=superClass.getSuperclass())
{
for (Field field : superClass.getDeclaredFields())
{
if (field.isAnnotationPresent(annotation))
{
fields.add(field);
}
}
}
return fields;
}
public static Method getMethod(Annotation annotation, String name)
{
try
{
return annotation.annotationType().getMethod(name);
}
catch (NoSuchMethodException nsme)
{
return null;
}
}
public static Method getMethod(Class clazz, String name)
{
for ( Class superClass = clazz; superClass!=Object.class; superClass=superClass.getSuperclass() )
{
try
{
return superClass.getDeclaredMethod(name);
}
catch (NoSuchMethodException nsme) {}
}
throw new IllegalArgumentException("no such method: " + clazz.getName() + '.' + name);
}
/**
* Check to see if clazz is an instance of name
*/
public static boolean isInstanceOf(Class clazz, String name)
{
if (name == null)
{
throw new IllegalArgumentException("name cannot be null");
}
for (Class c = clazz; c != Object.class; c = c.getSuperclass())
{
if (instanceOf(c, name))
{
return true;
}
}
return false;
}
private static boolean instanceOf(Class clazz, String name)
{
if (name.equals(clazz.getName()))
{
return true;
}
else
{
boolean found = false;
Class[] interfaces = clazz.getInterfaces();
for (int i = 0; i < interfaces.length && !found; i++)
{
found = instanceOf(interfaces[i], name);
}
return found;
}
}
}
分享到:
相关推荐
preferences-helper SharePreferences is very popular with any project and all most all project has SharePreferences for saving data. This library will help you faster in configuration and use ...
python库。 资源全名:django-dynamic-preferences-1.2.tar.gz
python库。 资源全名:Preferences-0.08.tar.gz
compile 'com.github.seanzor:shared-preferences-helper:1.1.0' } Usage Define the preferences keys in a resource file: pref_number_of_runs And use them: // As in most cases, create a default shared ...
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
AndroidMaterialPreferences.zip,提供各种“材料设计”样式的首选项。提供各种“材料设计”样式的首选项
logback-redis-appender.zip,主要用于将logback日志传递到logstash through
Windows-->preferences-->java-->Insetallel JREs 右侧 单击ADD standard VM-->Next -->Directory-->Finash 点选 新增jre环境-->点击OK 2. 更换背景颜色 个人感觉白色比较刺激眼睛 可选择性更换 Windows-->...
下载NDK组件com.android.ide.eclipse.ndk_23.0.2.1259578.jar。将文件“com.android.ide.eclipse.ndk_23.0.2.1259578.jar”复制到sdk自带的eclipse/plugins目录下
Android-Shared-Preferences-example-application Android 共享首选项示例应用程序
下载repositories { maven { url " https://jitpack.io " }}dependencies { implementation ' com.github.tfcporciuncula.flow-preferences:flow-preferences:1.4.0 '}看起来如何从常规的SharedPreferences开始: ...