欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-04-04 23:30:39
package review;/*12:43 2019/7/21*/ import model.AnotherClass; import model.OneClassMore; import model.SomeClass; import java.lang.reflect.Constructor;... ......
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(){ } }