类加载器:
ClassLoader.getSystemClassLoader();
cls.getClassLoader();
获取 Class 三种方式:
Class.forName("com.xnktyu.utils.LOG");
String.class;
obj.getClass();
Class 常用方法:
cls.getDeclaredConstructors();
cls.getDeclaredMethods();
cls.getDeclaredFields();
cls.getDeclaredAnnotations();
cls.getDeclaredClasses(); // 返回内部类
cls.getDeclaringClass(); // 返回外部类
cls.getInterfaces(); // 实现的接口
cls.getSuperclass(); // 继承的父类
cls.getModifiers(); // 修饰符,用 Modifier 解码
cls.getPackage();
cls.getName();
cls.getSimpleName();
cls.isAnnotation(); // 是否是注释
cls.isAnonymousClass(); // 是否是匿名类
cls.isArray();
cls.isEnum();
cls.isInterface();
Field:
field.getType();
Type type = field.getGenericType(); // 泛型类型
if (type instanceof ParameterizedType)
{
ParameterizedType pType = (ParameterizedType) type;
pType.getRawType();
pType.getActualTypeArguments();
}
Constructor / Method 常用方法:(Executable)
method.getModifiers(); // 修饰符,用 Modifier 解码
method.isVarArgs(); // 是否包含变参
method.getParameterCount(); // 参数个数
method.getParameters(); // 参数列表
Parameter 常用方法:
parameter.getModifiers();
parameter.isVarArgs();
parameter.getType(); // 形参类型
parameter.getParameterizedType(); // 带泛形的形参类型
parameter.isNamePresent(); // 是否有形参名
parameter.getName(); // 形参名
实例化对象:
cls.newInstance();
constructor.newInstance();
反射调用:
method.setAccessible(true); // 取消访问权限检查
method.invoke(obj, "abc");
field.setAccessible(true);
field.get(obj);
field.getInt(obj);
field.getBoolean(obj);
........
field.set(obj, "ktkt");
field.setInt(obj, 33);
field.setBoolean(obj, true);
........
数组:
Object arr = Array.newInstance(String.class, 10);
Array.getLength(arr);
Array.set(arr, 5, "abc");
Array.get(arr, 5);
二维数组:
Object array = Array.newInstance(String.class, 2, 5);
Array.set(Array.get(array, 1), 2, "abcd");
for (int i = 0; i < Array.getLength(array); i++)
{
Object arr = Array.get(array, i);
for (int j = 0; j < Array.getLength(arr); j++)
{
System.out.print(Array.get(arr, j) + "\t");
}
System.out.println();
}
输出:
null null null null null
null null abcd null null
另一种写法:
Object[][] array = (Object[][]) Array.newInstance(String.class, 2, 5);
array[1][2] = "aaaa";
for (int i = 0; i < array.length; i++)
{
Object[] arr = array[i];
for (int j = 0; j < arr.length; j++)
{
System.out.print(arr[j] + "\t");
}
System.out.println();
}
动态代理:
// 创建动态代理类
Proxy.getProxyClass(ClassLoader.getSystemClassLoader(), new Class[]{MyInterface.class, MyListener.class});
Proxy.getProxyClass(obj.getClass().getClassLoader(), obj.getClass().getInterfaces());
// 创建代理对象
Class<?> cls = Proxy.getProxyClass(ClassLoader.getSystemClassLoader(), new Class[]{MyInterface.class, MyListener.class});
Constructor<?> constructor = cls.getConstructor(InvocationHandler.class);
MyInterface obj = (MyInterface) constructor.newInstance(new InvocationHandler()
{
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
return null;
}
});
// 快速创建代理对象
MyInterface obj = (MyInterface) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[]{MyInterface.class, MyListener.class}, new InvocationHandler()
{
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
return null;
}
});
动态代理典型用法:
class MyInvocationHandler implements InvocationHandler
{
private Object target;
MyInvocationHandler(Object target)
{
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
// TODO
Object result = method.invoke(target, args);
// TODO
return result;
}
}