现在我想根据一个 字符串类型的类名,和方法名,想要利用反射执行这个方法该怎么操作??以前写过生疏了,请高手指点!!
解决方案 »
- java.lang.IllegalArgumentException: Cannot convert value of type [com.ssh.dao.im
- 请高手帮忙看下面得代码,这个问题怎么解决!
- 三层架构中的前后台通信问题
- tomcat端口问题求救?
- javabean的.class问题
- ibatis 千万级数据 sql语句
- 该如何载tomcat的源代码呀?
- 如何才能将一篇.doc 或.pdf文档的标题、段首信息提取出来呢?
- 一个DTD的问题
- 批处理命令可以批量提取文本文件的第一行的数据。
- ognl很不懂的问题
- getHibernateTemplate()中的HQL有group by,如果遍历查到的LIST
Object ower = Class.forName("Test.DaoTest").newInstance();
ower.getClass().getMethod("testStr").invoke(ower, args入参数组);
} catch (Exception e) {
e.printStackTrace();
}
Class c = Class.forName("java.util.Arrays");
//Object o = c.newInstance();//如果类需要实例化则执行这布 Method m = c.getMethod("sort",new int[0].getClass()); int[] arr = new int[] { 2, 1, 3, 4 };
m.invoke(c, arr);//如果是实例方法则给变量o for (int i : arr) {
System.out.println(i);
}
String clazz = "java.lang.String";
Class c = Class.forName(clazz);
Object obj = c.newInstance();String function = "toString";
Method method = c.getMethod(function);
method.invoke();
private String name = "a";
public String getName(){
return name;
}
}
//全类名
String className = "Person";
String method = "getName";
Class clazz = Class.forName(className);
String name = (String) clazz.getMethod(method).invoke(clazz.newInstance());
System.out.println(name);
Class<?> c = Class.forName("Your String ");
private int privateField=2;
public int publicField=3;
public Reflect(){
print("Constructor of Reflect");
}
public Reflect(String str){
print("Constructor of Reflect"+str);
}
public void method1(){
print("public start");
}
public static void method2(String s){
print("static start"+s);
}
private void privateMethod(){
print("privateMethod Start");
}
public static void print(String s){
System.out.println(s);
}
}
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
public static void main(String [] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, InstantiationException{
Class c=null;
c = Class.forName("Reflect.Reflect");
Reflect reflect =(Reflect)c.newInstance();
Method[] methods=c.getMethods();
Constructor[] constructors=c.getConstructors();
Field[] fields=c.getFields();
Method me = c.getMethod("method1", new Class [] {});
Method me1 = c.getDeclaredMethod("privateMethod", new Class [] {});
Method me2=c.getDeclaredMethod("method2", new Class[]{String.class});
me1.setAccessible(true);
me.invoke(reflect,null);
me1.invoke(reflect, null);
me2.invoke(null, new Object[]{"hello"});
}
调用的是无参构造方法
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
public static void main(String [] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, InstantiationException{
Class c=null;c = Class.forName("Reflect.Reflect");
Reflect reflect =(Reflect)c.newInstance();Method[] methods=c.getMethods();
Constructor[] constructors=c.getConstructors();
Field[] fields=c.getFields();
Method me = c.getMethod("method1", new Class [] {}); Method me1 = c.getDeclaredMethod("privateMethod", new Class [] {});
Method me2=c.getDeclaredMethod("method2", new Class[]{String.class});
me1.setAccessible(true);
me.invoke(reflect,null);
me1.invoke(reflect, null);
me2.invoke(null, new Object[]{"hello"});
}再看一下, me.invoke(reflect,null);
这个函数调用的是非静态方法, 所以需要一个对象 ,用来表示调用哪一个对象上的方法
me2.invoke(null, new Object[]{"hello"});
第一个参数为空,因为调用的是静态方法 也就是我们所说的类方法,所以第一个参数为空。
如果是你的,你只需要把第一个参数添上你单例对象的类型就是了。
Reflect reflect =(Reflect)c.newInstance();不用上面这个方法 ,直接让reflect等于你单例返回的对象不就行了?
比如
Reflect reflect=Singleton.getInstance();?
试试看不明白再和我说
创建对象吗??
不用写,
在下面写 Reflect reflect = 你的单利对象? 明白了?????
这个例子不是为你写的,是我以前自己写着玩的,上面我觉得我说的很清楚了, invoke 方法调用 非静态方法,需要在第一个参数传入一个 对象。 这样它菜知道在哪哥对象上调用 该方法。 所以你传入一个对象就行了,但是这个对象是怎么得来的无关紧要 。 你可以由你写的单例类得来中 获得方法取得 此单利对象。把它传给invoke方法第一个参数就行了 ,不用=(Reflect)c.newInstance();这个方法~~我在打dota要是不行我一会给你写吧。
method.invoke(this,参数名);
一个类的类名字符串 String className="SAVClientLogTask";
能 className.class.getClass();吗?