列举java语言中反射的常用方法
2019-08-16 10:51:45来源:博客园 阅读 ()
列举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(){ } }
原文链接:https://www.cnblogs.com/zhang-xing-shuo/p/11220850.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 国外程序员整理的Java资源大全(全部是干货) 2020-06-12
- 2020年深圳中国平安各部门Java中级面试真题合集(附答案) 2020-06-11
- 2020年java就业前景 2020-06-11
- 04.Java基础语法 2020-06-11
- Java--反射(框架设计的灵魂)案例 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash