package com.pinganfu.openapi.service;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
public class ReflectTest {
static class Type {
public int pubIntField;
public String pubStringField;
private int prvIntField;
public Type() {
Log(
"Default Constructor");
}
Type(
int arg1, String arg2) {
pubIntField = arg1;
pubStringField = arg2;
Log(
"Constructor with parameters");
}
public int getPrvIntField() {
return prvIntField;
}
public void setPrvIntField(
int prvIntField) {
this.prvIntField = prvIntField;
}
private void Log(String msg) {
System.out.println(
"Type:" + msg);
}
}
static class ExtendType extends Type {
public int pubIntExtendField;
public String pubStringExtendField;
private int prvIntExtendField;
public ExtendType() {
Log(
"Default Constructor");
}
ExtendType(
int arg1, String arg2) {
pubIntExtendField = arg1;
pubStringExtendField = arg2;
Log(
"Constructor with parameters");
}
public void setIntExtendField(
int field7) {
this.prvIntExtendField = field7;
}
public int getIntExtendField() {
return prvIntExtendField;
}
private void Log(String msg) {
System.out.println(
"ExtendType:" + msg);
}
}
public static void testClassInstance() {
Boolean foo =
true;
Class<?> obj1 = foo.getClass();
Class<?> obj2 = Boolean.class;
Class<?> obj3 = Boolean.TYPE;
try {
Class<?> obj4 = Class.forName(
"java.lang.Boolean");
System.out.println(obj4);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println(obj1);
System.out.println(obj2);
System.out.println(obj3);
}
public static void testGetFields() {
Class<?> classType = ReflectTest.ExtendType.class;
System.out.println(classType);
Field[] fields = classType.getFields();
for (Field f : fields) {
System.out.println(f);
}
System.out.println(
"+++++");
fields = classType.getDeclaredFields();
for (Field f : fields) {
System.out.println(f);
}
}
public static void testGetMethods() {
Class<?> classType = ExtendType.class;
Method[] methods = classType.getMethods();
for (Method m : methods) {
System.out.println(m);
}
System.out.println(
"======");
methods = classType.getDeclaredMethods();
for (Method m : methods) {
System.out.println(m);
}
}
public static void testGetConstructos() {
Class<?> classType = ExtendType.class;
Constructor<?>[] constructors = classType.getConstructors();
for (Constructor<?> m : constructors) {
System.out.println(m);
}
System.out.println();
constructors = classType.getDeclaredConstructors();
for (Constructor<?> m : constructors) {
System.out.println(m);
}
}
public static void testNewInstance() {
Class<?> classType = ExtendType.class;
Object inst1 =
null;
Object inst2 =
null;
Object inst3 =
null;
try {
inst1 = classType.newInstance();
}
catch (InstantiationException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
Constructor<?> constructor1 =
null;
Constructor<?> constructor2 =
null;
try {
constructor1 = classType.getConstructor();
constructor2 = classType.getDeclaredConstructor(
int.class, String.class);
}
catch (SecurityException e) {
e.printStackTrace();
}
catch (NoSuchMethodException e) {
e.printStackTrace();
}
try {
inst2 = constructor1.newInstance();
inst3 = constructor2.newInstance(
1,
"123");
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (InstantiationException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
System.out.println(inst1);
System.out.println(inst2);
System.out.println(inst3);
}
public static void testInvokeMethods() {
Class<?> classType = ExtendType.class;
Object inst =
null;
try {
inst = classType.newInstance();
}
catch (InstantiationException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
Method logMethod =
null;
try {
logMethod = classType.getDeclaredMethod(
"Log", String.class);
}
catch (SecurityException e) {
e.printStackTrace();
}
catch (NoSuchMethodException e) {
e.printStackTrace();
}
try {
logMethod.setAccessible(
true);
logMethod.invoke(inst,
"test");
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public static void testGetterAndSetter() {
Class<?> classType = ExtendType.class;
Object inst =
null;
try {
inst = classType.newInstance();
}
catch (InstantiationException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
Field intField =
null;
try {
intField = classType.getField(
"pubIntExtendField");
}
catch (SecurityException e) {
e.printStackTrace();
}
catch (NoSuchFieldException e) {
e.printStackTrace();
}
try {
intField.setInt(inst,
100);
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
try {
int value = intField.getInt(inst);
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
testSetter(classType, inst,
"prvIntField",
10);
System.out.println(testGetter(classType, inst,
"prvIntField"));
}
public static Object
testGetter(Class<?> classType, Object obj, String fieldName) {
return testAccessField(classType, obj, fieldName,
null,
false);
}
public static void testSetter(Class<?> classType, Object obj, String fieldName, Object fieldVal) {
testAccessField(classType, obj, fieldName, fieldVal,
true);
}
private static Object
testAccessField(Class<?> classType, Object obj, String fieldName, Object fieldVal,
boolean flag) {
Object fieldObj =
null;
Method[] methods = classType.getMethods();
Method getMethod =
null;
Method setMethod =
null;
for (
int i =
0; i < methods.length; i++) {
System.out.println(methods[i].getName());
String methodName = fieldName.substring(
0,
1).toUpperCase() + fieldName.substring(
1);
if (flag) {
if ((
"set" + methodName).equals(methods[i].getName())) {
setMethod = methods[i];
break;
}
}
else {
if ((
"get" + methodName).equals(methods[i].getName())) {
getMethod = methods[i];
break;
}
}
}
try {
if (flag) {
if (setMethod !=
null) {
setMethod.setAccessible(
true);
setMethod.invoke(obj, fieldVal);
}
else {
System.err.println(
"error");
}
}
else {
if (getMethod !=
null) {
getMethod.setAccessible(
true);
fieldObj = getMethod.invoke(obj);
}
else {
System.err.println(
"error");
}
}
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
return fieldObj;
}
public static void testGetterAndSetter(Class<?> classType, Object obj, String fieldName, Object fieldVal) {
Object fieldObj =
null;
Method[] methods = classType.getMethods();
Method getMethod =
null;
Method setMethod =
null;
for (
int i =
0; i < methods.length; i++) {
System.out.println(methods[i].getName());
String methodName = fieldName.substring(
0,
1).toUpperCase() + fieldName.substring(
1);
if ((
"get" + methodName).equals(methods[i].getName())) {
getMethod = methods[i];
continue;
}
if ((
"set" + methodName).equals(methods[i].getName())) {
setMethod = methods[i];
continue;
}
if (getMethod !=
null && setMethod !=
null)
break;
}
try {
if (setMethod !=
null) {
setMethod.setAccessible(
true);
setMethod.invoke(obj, fieldVal);
}
else {
System.err.println(
"error");
}
if (getMethod !=
null) {
setMethod.setAccessible(
true);
fieldObj = getMethod.invoke(obj);
}
else {
System.err.println(
"error");
}
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
System.out.println(fieldObj);
}
static interface Face {
public void say();
public int count();
}
static class RealTeller implements Face {
@Override
public void say() {
System.out.println(
"hello");
}
@Override
public int count() {
return 100;
}
}
static class CommonHandler implements InvocationHandler {
private Object target;
public CommonHandler(Object target) {
this.target = target;
}
@Override
public Object
invoke(Object proxy, Method method, Object[] args)
throws Throwable {
return method.invoke(target, args);
}
}
public static Object
testProxy(Object target) {
Object proxy = (Face) Proxy.newProxyInstance(ReflectTest.class.getClassLoader(), target.getClass().getInterfaces(),
new CommonHandler(target));
return proxy;
}
public static void main(String[] args) {
Face face = (Face) testProxy(
new RealTeller());
System.out.println(face.getClass());
face.say();
System.out.println(face.count());
}
}