Common methods include java language reflected

Package Review; 
/ * 12 is: 43 is 2019 /. 7/21 is * /
Import model.AnotherClass; Import model.OneClassMore; Import model.SomeClass; Import java.lang.reflect.Constructor; Import as java.lang.reflect.Field; Import Java .lang.reflect.InvocationTargetException; Import the java.lang.reflect.Method; / ** * this class java language lists some methods commonly used on reflection * @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 By getMethods (), to give a unique class or interface and inherits all of the array from its parent class public interface composition. By getDeclaredMethods (), get all the unique method of the class or interface, (including public and non-public). * / / * Just AS A Template for Convenience empty * / Private static void m () { } }

 

Guess you like

Origin www.cnblogs.com/zhang-xing-shuo/p/11220850.html