列举java语言中反射的常用方法

package review;
/*12:43 2019/7/21*/
import model.AnotherClass; import model.OneClassMore; import model.SomeClass; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * 这个类列举了java语言中关于反射机制的常用的一些方法 * @author zhangxingshuo */ public class AboutReflection { public static void main(String[] args) throws Exception { } /*获得Class对象的3种方式*/ private static Class<?> getClazz0(String className) throws ClassNotFoundException { Class clazz=Class.forName(className); return clazz; } private static Class<?> getClazz1(Object object) { Class clazz=object.getClass(); return clazz; } private static Class<?> getClazz2() { Class clazz=model.SomeClass.class; return clazz; } /*经常使用的Class对象的3个方法*/ private static String useClazz0(Class clazz) { String fullyQualifiedName=clazz.getName(); return fullyQualifiedName; } private static String useClazz1(Class clazz) { String className=clazz.getSimpleName(); return className; } //ex:private //ex:abstract private static Object useClazz2(Class clazz) throws IllegalAccessException, InstantiationException { Object object=clazz.newInstance(); return object; } /*获得Constructor对象的3个方法*/ private static Constructor<?>[] getConstructorObject0(Class clazz) { Constructor<?>[] constructors=clazz.getConstructors(); return constructors; } private static Constructor<?>[] getConstructorObject1(Class clazz) { Constructor<?>[] constructors=clazz.getDeclaredConstructors(); return constructors; } private static Constructor<?> getConstructorObject2(Class clazz) throws NoSuchMethodException { Constructor<?> constructor=clazz.getConstructor(SomeClass.class, AnotherClass.class, OneClassMore.class); return constructor; } private static Constructor<?> getConstructorObject3(Class clazz) throws NoSuchMethodException { Constructor<?> constructor=clazz.getDeclaredConstructor(SomeClass.class, AnotherClass.class, OneClassMore.class); return constructor; } /*经常使用的Constructor对象的2个方法*/ private static Object useConstructorObject0(Constructor<?> constructor) throws IllegalAccessException, InvocationTargetException, InstantiationException { //under here,if the variable override==true,jvm willl not check the accessible modifier Object object=constructor.newInstance(new SomeClass(),new AnotherClass(),new OneClassMore()); return object; } private static void useConstructorObject1(Constructor<?> constructor) { //under here changing "override" variable's value who is defined in AccessibleObject,the "super and super" Class of Constructor constructor.setAccessible(true); } /*还有一些*/ private static Class<?> useConstructorObject2(Constructor<?> constructor) { Class clazz=constructor.getDeclaringClass(); return clazz; } private static int useConstructorObject3(Constructor<?> constructor) { int modifiers=constructor.getModifiers(); return modifiers; } private static String useConstructorObject4(Constructor<?> constructor) { //constructor name is same as the class name String constructorName = constructor.getName(); //under here getDeclaringClass().getName(); return constructorName; } /*获取Field对象的4个方法*/ private static Field[] getFieldObject0(Class clazz){ Field[] fields = clazz.getFields(); return fields; } private static Field[] getFieldObject1(Class clazz){ Field[] declaredFields = clazz.getDeclaredFields(); return declaredFields; } private static Field getFieldObject2(Class clazz) throws NoSuchFieldException { Field field = clazz.getField("theFieldName"); return field; } private static Field getField3(Class clazz) throws NoSuchFieldException { Field field = clazz.getDeclaredField("theFieldName"); return field; } /*经常使用的Field对象的3个方法*/ private static Object useFieldObject0(Field field,Object object) throws IllegalAccessException { Object fieldValue = field.get(object); return fieldValue; } private static void useFieldObject1(Field field,Object object) throws IllegalAccessException { //an object as the field value field.set(object,new Object()); } private static void useFieldObject2(Field field){ //same process field.setAccessible(true); } /*还有一些*/ private static int useFieldObject3(Field field){ int modifiers = field.getModifiers(); return modifiers; } private static String useFieldObject4(Field field){ String fieldName = field.getName(); return fieldName; } /*获取Method对象的4个方法*/ private static Method[] getMethodObject0(Class clazz){ Method[] methods=clazz.getMethods(); return methods; } private static Method[] getMethodObject1(Class clazz){ Method[] methods=clazz.getDeclaredMethods(); return methods; } private static Method getMethodObject2(Class clazz) throws NoSuchMethodException { Method method=clazz.getMethod("someMethodName",SomeClass.class,AnotherClass.class,OneClassMore.class); return method; } private static Method getMethodObject3(Class clazz) throws NoSuchMethodException { Method method=clazz.getDeclaredMethod("someMethodName",SomeClass.class,AnotherClass.class,OneClassMore.class); return method; } /*经常使用的Field对象的2个方法*/ private static Object useMethodObject0(Method method,Object object) throws InvocationTargetException, IllegalAccessException { Object returnedobject=method.invoke(object,new SomeClass(),new AnotherClass(),new OneClassMore()); return returnedobject; } private static void useMethodObject1(Method method){ method.setAccessible(true); } /*还有一些*/ private static int useMethodObject2(Method method){ int modifiers = method.getModifiers(); return modifiers; } private static String useMethodObject3(Method method){ String methodName = method.getName(); return methodName; } /* tips 通过getMethods(),得到一个数组,里面是该类或接口独有的,和继承自它的所有父类与接口的public方法. 通过getDeclaredMethods(),得到一个数组,里面是该类或接口独有的所有方法,(包括public和非public). */ /*just as a empty template for convenience*/ private static void m(){ } }

 

点赞