JAVA反射小案例

    xiaoxiao2026-08-09  13

    最近看框架的时候发现自己的反射的知识不是特别清楚,随即就抽了点时间在网上把反射的知识补了一下,下面是我自己敲的一个小案例,见笑见笑

    package com.yc.test; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; import com.yc.entity.Employee; public class MyReflection { public static void main(String[] args) { // Employee e=new Employee(); // System.out.println(e); // e=new MyReflection().invokeTest(Employee.class); // System.out.println(e); // new MyReflection().testForName("com.yc.entity.Employee"); List<String> methodStrs=new MyReflection().showMethod(String.class); for (String s : methodStrs) { System.out.println(s); } } public void testForName(String path){ try { Class<?> c=Class.forName(path); System.out.println(c.newInstance() instanceof Employee); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } @SuppressWarnings({ "rawtypes", "unchecked" }) public <T> T invokeTest(Class c){ T t = null; try { t=(T) c.newInstance(); //类或接口声明的所有方法,包括公共、保护、默认(包)访问和私有方法,但不包括继承的方法。当然也包括它所实现接口的方法。 Method[] ms=t.getClass().getDeclaredMethods(); for (Method m : ms) { /*if(m.getName().equals("toString")){ m.invoke(t); }*/ if(m.getName().startsWith("set")){ Class<?>[] types = m.getParameterTypes(); if((types[0].getSimpleName()).equals("String")){ m.invoke(t,"66666"); } else if((types[0].getSimpleName()).equals("int")){ m.invoke(t,2); } } } } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return t; } //显示所有的方法 @SuppressWarnings({ "unchecked", "rawtypes" }) public <T> List<String> showMethod(Class c){ List<String> methodStrs=new ArrayList<String>(); T t; try { //实例化所给的类 t=(T) c.newInstance(); //返回某个类的所有公用(public)方法包括其继承类的公用方法,当然也包括它所实现接口的方法。 //Method[] ms=t.getClass().getMethods(); //对象表示的类或接口声明的所有方法,包括公共、保护、默认(包)访问和私有方法,但不包括继承的方法。当然也包括它所实现接口的方法。 Method[] ms=t.getClass().getDeclaredMethods(); String str; for (Method m : ms) { //获取方法的修饰符 str=Modifier.toString(m.getModifiers())+" "; //获取返回类型 Class<?> returnType=m.getReturnType(); str+=returnType.getSimpleName()+" "+m.getName()+"("; //获取形参类型 Class<?>[] types=m.getParameterTypes(); if(types.length>0){ for (int i=0;i<types.length;i++) { //获取类型名,不是全限定名 str+=types[i].getSimpleName()+" "+"arg"+i+","; } str=str.substring(0, str.lastIndexOf(",")); } str+=")"; //获取抛出的异常 Class<?>[] exceptions=m.getExceptionTypes(); if(exceptions.length>0){ str+=" throws "; for (Class<?> e : exceptions) { str+=e.getSimpleName()+","; } str=str.substring(0, str.lastIndexOf(",")); } str+="{}"; methodStrs.add(str); } } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return methodStrs; } }

    贴上showMethod方法的测试结果

    转载请注明原文地址: https://ju.6miu.com/read-1311022.html
    最新回复(0)