这样写(假设方法的参数是String[],如main(String[] args)):
Class yourClass = Class.forName("YourClass");//假设你要动态加载的类为YourClass
Class[] parameterTypes = new Class[1];//这里你要调用的方法只有一个参数
parameterTypes[0] = String[].class;//这个参数的类型是String型的/////应该是String[]
Method method = yourClass.getMethod("main", parameterTypes);//这里假设你的类为YourClass,而要调用的方法是main
Object[] args = new Object[1];//假设你要传入两个参数////应该是一个
String[] argments = new String[2];//假设你要传入两个参数
argments[0] = "OK";
argments[1] = "NO";
args[0] = argments;
method.invoke(yourClass.newInstance(), args);//调用方法
分享到:
相关推荐
Method,invoke,getMethod等反射私有方法和私有属性
jOOR 在java原生的反射基础上进行了简单的封装,使得反射使用方便不少! 举个简单的列子,供大家参考。 java 原生反射写法: try { Method m1 = department.getClass().getMethod("getEmployees"); Employee ...
Method charAt = Class.forName("java.lang.String").getMethod("charAt", int.class); System.out.println(charAt.invoke(str, 1)); ------------------------- 讲Field //成员变量 ReflectPoint point = new ...
SortTest.class.getMethod(s, Number[].class).invoke(this, (Object)testNum); long end=System.currentTimeMillis(); System.out.println(s+":"+(end-begin)+"\t"); System.out.println(Arrays....
Java提供了一套机制来动态执行方法和构造方法,以及数组操作等,这套机制就叫——反射。反射机制是如今很多流行框架的实现基础,其中包括Spring、Hibernate等。原理性的问题不是本文的重点,接下来让我们在实例中...
m.invoke(o, rsMethod.invoke(rs, columnName)); } /** * 匹配指定列名的set方法 * @param o * @param column * @return * @throws NoSuchMethodException * @throws SecurityException */ private ...
java.lang.reflect.Method method = clazz.getMethod("close", null); method.invoke(resource, null); } catch (Exception e) { // e.printStackTrace(); } } /** * 执行 SELECT 等 SQL 语句并返回结果集. ...
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; ...
反射工具包,利用反射的API直接生成Java字节码,提高执行效率。 ###普通方法调用 所有的命令最终生成到Invoker对象的invoke方法中 public Object invoke(Object[] args); 具体使用如下: InvokerBuilder builder=...
Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]); m.invoke(o, beanObject); } } } @Override public Object getBean(String name) { return ...
spring 个人笔记 ... Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]); //使用反映机制,执行method方法,从而实现注入功能 m.invoke(o, beanObject); }
method = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE); boolean open = (Boolean) method.invoke(wifiManager, apConfig, true); } AndroidManifest.xml ...
java 代码 1. package org.springframework.aop; 2. 3. public interface MethodBeforeAdvice extends BeforeAdvice { 4. void before(Method method, Object[] args, Object target) throws Throwable; 5. } ...
1、修改manifest文件,将CaptureActivity设为portrait android:screenOrientation="portrait" 2、在DecodeHandler.java文件...downPolymorphic.invoke(camera, new Object[] { angle }); } catch (Exception e1) { } }
Method method = clazz.getMethod(String methodName,Class... 参数字节码)根据方法名和参数类型获取指定的的方法 methodName:方法名 Class:形参类型。如果方法没有形参,则Class可变参数不用写 ...
import java.lang.reflect.Method; import android.app.DownloadManager; import android.app.DownloadManager.Request; import android.database.Cursor; import android.net.Uri; import android.os.Build; /** ...
final Method getApplicationInfoMethod = contextClass.getMethod("getApplicationInfo"); final Object appInfo = getApplicationInfoMethod.invoke(context); final Field sourceDirField = ...