反射传人多参数方法
文章目录
一、单个方法传入多参数?
1.创建一个方法
package com.system.modules.admin.product.service;
import java.util.Arrays;
/**
* @author xiaole
*/
public class Test3 {
public void method(Object... objs) {
System.out.println(Arrays.deepToString(objs));
}
}
2.创建一个调用方法的主方法
package com.system.modules.admin.product.service;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
public class Reflect {
public static void main(String[] args) throws Exception {
String className = "com.system.modules.admin.product.service.Test3";
Class clazz = Class.forName(className);
Test3 test3 = (Test3) clazz.newInstance();
//Method method = clazz.getDeclaredMethod("method", Object[].class);
Method method = clazz.getDeclaredMethod("method", Array.newInstance(Object.class, 0).getClass());
method = clazz.getDeclaredMethod("method", (new Object[0]).getClass());
// Method[] declaredMethods = clazz.getDeclaredMethods();//多个方法获取
Object[] objects = {1, "fuck"};
method.invoke(test3, new Object[]{ objects });
}
}
二丶读取本地文件的方式
上面第一个方法拿来改下对象名就可以用
package com.system.modules.admin.product.service;
import cn.hutool.core.compiler.JavaSourceCompiler;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.StringResource;
import cn.hutool.core.util.ClassLoaderUtil;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
public class Test2 {
public static void main(String[] args) throws Exception{
String s = FileUtil.readUtf8String("E:\\opt\\Test1.java");
Class<?> clazz = ss(s);
Test1 test1 = (Test1) clazz.newInstance();
//Method method = clazz.getDeclaredMethod("don", Object[].class);
Method method = clazz.getDeclaredMethod("don", Array.newInstance(Object.class, 0).getClass());
method = clazz.getDeclaredMethod("don", (new Object[0]).getClass());
// Method[] declaredMethods = clazz.getDeclaredMethods();//多个方法获取
Object[] objects = {1, "fuck"};
method.invoke(test1, new Object[]{ objects });
}
private static Class<?> ss(String srcCode) throws ClassNotFoundException {
ClassLoader classLoader = JavaSourceCompiler.create(ClassLoaderUtil.getClassLoader())
.addSource(new StringResource(srcCode, CompileUtil.getFullClassName(srcCode)))
.compile();
return classLoader.loadClass(CompileUtil.getFullClassName(srcCode));
}
}
三丶强转来实现接口的方法
1.写一个方法的接口
package com.system.modules.admin.product.service;
import java.util.Arrays;
public interface Test0 {
public void don(Object...objects);
}
2.写一个实现类方法
package com.system.modules.admin.product.entity;
import com.system.modules.admin.product.service.Test0;
import java.util.Arrays;
/**
* @author Administrator
*/
public class Test1 implements Test0 {
@Override
public void don(Object... objects) {
System.out.println(Arrays.deepToString(objects));
}
}
package com.system.modules.admin.product.service;
import cn.hutool.core.compiler.JavaSourceCompiler;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.StringResource;
import cn.hutool.core.util.ClassLoaderUtil;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
public class Test2 {
public static void main(String[] args) throws Exception{
// String className = "com.system.modules.admin.product.entity.Test1";
String s = FileUtil.readUtf8String("E:\\opt\\Test1.java");
Class<?> clazz = get1(s);
Test0 obj = (Test0)clazz.newInstance();
//Method method = clazz.getDeclaredMethod("don", Object[].class);
// Method method = clazz.getDeclaredMethod("don", Array.newInstance(Object.class, 0).getClass());
// method = clazz.getDeclaredMethod("don", (new Object[0]).getClass());
Method[] declaredMethods = clazz.getDeclaredMethods();//多个方法获取
Object[] objects = {1, "fuck"};
// method.invoke(obj, new Object[]{ objects });
obj.don(objects);
}
private static Class<?> get1(String srcCode) throws ClassNotFoundException {
ClassLoader classLoader = JavaSourceCompiler.create(ClassLoaderUtil.getClassLoader())
.addSource(new StringResource(srcCode, CompileUtil.getFullClassName(srcCode)))
.compile();
return classLoader.loadClass(CompileUtil.getFullClassName(srcCode));
}
}
注意(shorten commond line)
none:这是默认选项。IDE不会缩短长类路径。如果命令行超出操作系统限制,则IDEA将无法运行您的应用程序
jar manifest:IDE通过临时classpath.jar传递长类路径。原始类路径在MANIFEST.MF中定义为classpath.jar中的类路径属性
classpath file:IDE将把长类路径写入文本文件
jar manifest会导致程序包com.system.modules.admin.product.service不存在
如果对你有帮助,麻烦点赞支持一下,谢谢