1.加载并获取该Class对象可以通过三种方式:
1.1:Class.forName(类的全路径)
Class cl = Class.forName("Demo1.GetClass");
1.2:实例对象.getClass()方法
Class c = model.getClass();
1.3:类名.class属性
Class modelClass = TestModel.class;
2.进行反射测试的实体类
package Demo1; import javax.annotation.Resource; import java.io.InputStream; import java.io.Serializable; import java.sql.SQLException; import java.util.IllegalFormatCodePointException; /** * Created by 123 on 2017/07/16. */ public class GetClass implements Serializable, Runnable { private String name; @Resource private int age; public GetClass() { } public GetClass(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getStr(String name) throws SQLException, IllegalFormatCodePointException { return name; } public void run() { } }
2.1-------------------->cl就是class对象
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { Class cl = Class.forName("Demo1.GetClass"); //包名 GetPackNameAndClassName(cl); //获取父类名称+所有的接口 getSuperAndInterface(cl); //字段 getManyFields(cl); //获取方法 getAllMethod(cl); //调用方法 GoMethod(cl); //获取构造 GetConstructors(cl); //方法抛出的异常 getExceptions(cl); //拿到注解 getManyAnnotions(cl); }
3.获取包名
public static void GetPackNameAndClassName(Class cl) { System.out.println("包名" + cl.getPackage().getName()); }
4.获取父类名称+所有的接口
public static void getSuperAndInterface(Class cl) { System.out.println("此类父类的类名是:" + cl.getSuperclass().getName()); Class[] interfaces = cl.getInterfaces(); StringBuffer sb = new StringBuffer("此类实现的接口有:"); for (Class cs : interfaces) { sb.append(cs.getSimpleName()); } System.out.println(sb.substring(0, sb.length() - 1)); }
5.获取字段
private static void getManyFields(Class cl) throws NoSuchFieldException, IllegalAccessException, InstantiationException { StringBuffer sb = new StringBuffer("此类中的字段有:"); Field[] declaredFields = cl.getDeclaredFields(); Field name = cl.getDeclaredField("name"); System.out.println("name字段" + name.getName()); name.setAccessible(true); Object o = cl.newInstance(); System.out.println("name字段的值为:" + name.get(o)); for (Field item : declaredFields) { //忽略字段privet item.setAccessible(true); sb.append("若多字段" + item.getName() + ","); } System.out.println(sb.substring(0, sb.length() - 1)); }
6.调用方法
private static void GoMethod(Class cl) throws NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException { Method method = cl.getMethod("getStr", String.class); Object obj = cl.newInstance(); Object invoke = method.invoke(obj, "厉害了"); System.out.println(invoke); }
7.获取或执行构造
private static void GetConstructors(Class cl) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { Constructor[] cs = cl.getConstructors(); StringBuffer sb = new StringBuffer("此类中的构造有:"); for (Constructor item : cs) { sb.append(item.getName() + ","); } System.out.println(sb.substring(0, sb.length() - 1)); //执行带参构造 Constructor cs1 = cl.getConstructor(String.class, Integer.class); //使用newinstance函数执行构造 cs1.newInstance("呵呵", 18); }
8.获取方法抛出的异常
private static void getExceptions(Class cl) throws NoSuchMethodException { Method getStr = cl.getMethod("getStr", String.class); Class[] exces = getStr.getExceptionTypes(); StringBuffer sb = new StringBuffer("此方法中抛出的异常有:"); for (Class item : exces) { sb.append(item.getName() + ","); } System.out.println(sb.substring(0, sb.length() - 1)); }
9.获取注解
private static void getManyAnnotions(Class cl) throws NoSuchFieldException { Field age = cl.getDeclaredField("age"); Resource ans = age.getAnnotation(Resource.class); System.out.println(ans); }
标签:反射,java,name,cl,void,基础,public,sb,Class From: https://www.cnblogs.com/quliang/p/7223946.html