反射 java反射
- 获取类
class c = Class.forName("com.example.TestClass");
- 获取类权限
++public++ ++private++ ++static++
System.out.print(Modifier.toString(c.getModifiers()));
- 获取类名
System.out.print(c.getSimpleName());
- 获取所有属性
Field[] fields = c.getDeclaredFields();
- 属性权限
System.out.print(Modifier.toString(f.getModifiers()) + " ");
- 属性返回类型
System.out.print(f.getType().getSimpleName() + " ");
- 属性名
System.out.print(f.getName() + " ");
- 获取所有方法
Method[] methods = c.getMethods();
System.out.print(Modifier.toString(method.getModifiers()));
System.out.print(" " + method.getReturnType().getSimpleName());
System.out.print(" " + method.getName());
Class<?>[] parameters = method.getParameterTypes();
- 指定方法
c = Class.forName("com.example.TestClass");
o = c.newInstance();
Method method = c.getDeclaredMethod("method2", String.class);
method.setAccessible(true);
System.out.print(" " + (String) method.invoke(o, "123"));
- 指定属性
Field f = c.getField("field1");
f.setAccessible(true);
System.out.println("" + f.get(o));
- 构造方法
System.out.println("无参构造方法:");
o = c.newInstance();
System.out.println("有参构造方法:");
Constructor<?> oo = c.getConstructor(String.class);
例子:通过反射使用SystemProperties的api
package com.sprd.ext;
import android.text.TextUtils;
import java.lang.reflect.Method;
import android.os.SystemProperties;
/**
* Created by SPRD on 6/13/17.
*/
public class SystemPropertiesUtils {
private static final String TAG = "SystemPropertiesUtils";
private static Class<?> mClassType = null;
private static Method mGetMethod = null;
private static Method mGetIntMethod = null;
private static Method mGetBooleanMethod = null;
private static Class<?> getSystemPropertiesClass() throws ClassNotFoundException {
if (mClassType == null) {
mClassType = Class.forName("android.os.SystemProperties");
}
return mClassType;
}
private static Method getMethod() throws Exception {
if (mGetMethod == null) {
Class clazz = getSystemPropertiesClass();
mGetMethod = clazz.getDeclaredMethod("get", String.class);
}
return mGetMethod;
}
private static Method getIntMethod() throws Exception {
if (mGetIntMethod == null) {
Class clazz = getSystemPropertiesClass();
mGetIntMethod = clazz.getDeclaredMethod("getInt", String.class, int.class);
}
return mGetIntMethod;
}
private static Method getBooleanMethod() throws Exception {
if (mGetBooleanMethod == null) {
Class clazz = getSystemPropertiesClass();
mGetBooleanMethod = clazz.getDeclaredMethod("getBoolean", String.class, boolean.class);
}
return mGetBooleanMethod;
}
public static String get(String key, String def) {
try {
String value = (String) getMethod().invoke(null, key);
if (!TextUtils.isEmpty(value)) {
return value;
}
} catch (Exception e) {
LogUtils.d(TAG, "Unable to read system properties");
}
return def;
}
public static int getInt(String key, int def) {
int value = def;
try {
value = (int) getIntMethod().invoke(null, key, def);
} catch (Exception e) {
LogUtils.d(TAG, "Unable to read system properties");
}
return value;
}
public static boolean getBoolean(String key, boolean def) {
boolean value = def;
try {
value = (Boolean) getBooleanMethod().invoke(null, key, def);
} catch (Exception e) {
LogUtils.d(TAG, "Unable to read system properties");
}
return value;
}
}