Java原生态的反射
说明一下 FanSheTime 为我自己随便用的一个类名
用法1:实例化对象,在通过对象直接使用里面的方法即可
public static void main(String[] args) { try { long begin = System.currentTimeMillis(); Class<?> forName = Class.forName("lcl.FanSheTime"); FanSheTime ss = (FanSheTime) forName.newInstance(); ss.function1("hello world"); long end = System.currentTimeMillis(); System.out.println("耗时>>>>>>>>>>>>>>>>>>>>"+(end - begin)); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); }catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } public void function1(String str){ System.out.println(str); } public void function1(){ System.out.println("hello world"); }
方法2:通过反射使用无参数的方法(不实例化对象)
public static void main(String[] args) { try { long begin = System.currentTimeMillis(); Class<?> forName = Class.forName("lcl.FanSheTime"); Method method = forName.getMethod("function1"); method.invoke(forName.newInstance()); long end = System.currentTimeMillis(); System.out.println("耗时>>>>>>>>>>>>>>>>>>>>"+(end - begin)); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); }catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } public void function1(String str){ System.out.println(str); } public void function1(){ System.out.println("hello world"); }方法3:反射使用带参数的方法: public static void main(String[] args) { try { long begin = System.currentTimeMillis(); Class<?> forName = Class.forName("lcl.FanSheTime"); Method method = forName.getMethod("function1",String.class); method.invoke(forName.newInstance(),"焦剑锋"); long end = System.currentTimeMillis(); System.out.println("耗时>>>>>>>>>>>>>>>>>>>>"+(end - begin)); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); }catch (NoSuchMethodException e) { e.printStackTrace(); }catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } public void function1(String str){ System.out.println(str); } public void function1(){ System.out.println("hello world"); }
如果有多个参数只需要在getMethod方法里面传入不同的参数类型,在invoke方法里传入参数即可
Method method = forName.getMethod("function1",String.class,,String.class,int.class); method.invoke(forName.newInstance(),"焦剑锋","hello world",1);
方法4:通过bsh-core-2.0b4.jar jar包完成反射
这种方法的好处是,你只需要传入响应的对象与方法名即可通过eval方法进行执行想使用的方法,非常容易
package lcl; import bsh.EvalError; import bsh.Interpreter; public class InterpreterWrapper extends Interpreter { public static void main(String[] args) { FanSheTime fanSheTime = new FanSheTime(); InterpreterWrapper ip = new InterpreterWrapper(); long begin = System.currentTimeMillis(); try { ip.set("fanSheTime", fanSheTime); ip.eval("fanSheTime.function1()"); } catch (EvalError e1) { e1.printStackTrace(); } long end = System.currentTimeMillis(); System.out.println("耗时>>>>>>>>>>>>>>>>>>>>"+(end - begin)); } }