一、前言
关于instinct源码包com.googlecode.instinct.internal.edge.java.lang.reflect.ClassEdge、com.googlecode.instinct.internal.edge.java.lang.reflect.ClassEdgeImpl定义接口及实现类,实现字符串类加载forName、指定类class实例化newInstance、指定类class获取加载容器getConstructor、获取类指定getMethod方法Method、获取getDeclaredField/getField类属性Field方法实现,参见下面源码说明部分。
二、源码说明
1.ClassEdge接口
package com.googlecode.instinct.internal.edge.java.lang.reflect;@b@@b@import java.lang.reflect.Constructor;@b@import java.lang.reflect.Field;@b@import java.lang.reflect.Method;@b@@b@public abstract interface ClassEdge@b@{@b@ public abstract Class<?> forName(String paramString);@b@@b@ public abstract <T> T newInstance(Class<T> paramClass);@b@@b@ public abstract <T> Constructor<T> getConstructor(Class<T> paramClass, Class<?>[] paramArrayOfClass);@b@@b@ public abstract <T> Constructor<T> getDeclaredConstructor(Class<T> paramClass, Class<?>[] paramArrayOfClass);@b@@b@ public abstract <T> Method getMethod(Class<T> paramClass, String paramString, Class<?>[] paramArrayOfClass);@b@@b@ public abstract <T> Method getDeclaredMethod(Class<T> paramClass, String paramString, Class<?>[] paramArrayOfClass);@b@@b@ public abstract <T> Field getDeclaredField(Class<T> paramClass, String paramString);@b@@b@ public abstract <T> Field getField(Class<T> paramClass, String paramString);@b@}
2.ClassEdgeImpl实现类
package com.googlecode.instinct.internal.edge.java.lang.reflect;@b@@b@import com.googlecode.instinct.internal.edge.EdgeException;@b@import java.lang.reflect.Constructor;@b@import java.lang.reflect.Field;@b@import java.lang.reflect.Method;@b@@b@public final class ClassEdgeImpl@b@ implements ClassEdge@b@{@b@ public Class<?> forName(String className)@b@ {@b@ try@b@ {@b@ return Class.forName(className);@b@ } catch (ClassNotFoundException e) {@b@ throw new EdgeException(e);@b@ }@b@ }@b@@b@ public <T> T newInstance(Class<T> cls) {@b@ try {@b@ return cls.newInstance();@b@ } catch (InstantiationException e) {@b@ throw new EdgeException(e);@b@ } catch (IllegalAccessException e) {@b@ throw new EdgeException(e);@b@ }@b@ }@b@@b@ public <T> Constructor<T> getConstructor(Class<T> cls, Class<?>[] parameterTypes) {@b@ try {@b@ return cls.getConstructor(parameterTypes);@b@ } catch (NoSuchMethodException e) {@b@ throw new EdgeException(e);@b@ }@b@ }@b@@b@ public <T> Constructor<T> getDeclaredConstructor(Class<T> cls, Class<?>[] parameterTypes) {@b@ try {@b@ return cls.getDeclaredConstructor(parameterTypes);@b@ } catch (NoSuchMethodException e) {@b@ throw new EdgeException(e);@b@ }@b@ }@b@@b@ public <T> Method getMethod(Class<T> cls, String methodName, Class<?>[] parameterTypes) {@b@ try {@b@ return cls.getMethod(methodName, parameterTypes);@b@ } catch (NoSuchMethodException e) {@b@ throw new EdgeException(e);@b@ }@b@ }@b@@b@ public <T> Method getDeclaredMethod(Class<T> cls, String methodName, Class<?>[] parameterTypes) {@b@ try {@b@ return cls.getDeclaredMethod(methodName, parameterTypes);@b@ } catch (NoSuchMethodException e) {@b@ throw new EdgeException(e);@b@ }@b@ }@b@@b@ public <T> Field getDeclaredField(Class<T> cls, String fieldName) {@b@ try {@b@ return cls.getDeclaredField(fieldName);@b@ } catch (NoSuchFieldException e) {@b@ throw new EdgeException(e);@b@ }@b@ }@b@@b@ public <T> Field getField(Class<T> cls, String name) {@b@ try {@b@ return cls.getField(name);@b@ } catch (NoSuchFieldException e) {@b@ throw new EdgeException(e);@b@ }@b@ }@b@}