假设我有构造函数,如何从 class 调用方法?
How to call a method from a class given that I have the Constructor for it?
如果我有一个构造函数对象 Constructor<?> productConstructor
,我如何调用存在于 class 中的 run()
方法?
例如,假设我有 class:
public class product1{
public product1(Instances instance){
// constructor
}
public void run(){
// do something
}
}
我可以 运行 我的 class 的构造函数是这样的:
Constructor<?> productConstructor;
productConstructor = getProductConstructor(instance.getProductName());
// pass instance into constructor to run the tests
// example of what is happening here -> new product1(instance)
productConstructor.newInstance(new Object[] { instance });
public Constructor<?> getProductConstructor(String productName) {
try {
logger.info("Looking for Product Class: {}", productName);
String fullQualifiedClassPath = "com.products." + productName;
Class<?> clazz = Class.forName(fullQualifiedClassPath);
return clazz.getConstructor(Instances.class);
} catch (Exception e) {
logger.error("Product: {} does not exist", productName);
}
return null;
}
如果我保持代码原样,那么我必须将 run()
方法放在构造函数内部到 运行 它,但我希望 run()
方法在构造函数。如何使用构造函数 class 为我的 class 调用我的 运行 方法?
例如,我曾想过但行不通:
Constructor<?> productConstructor;
productConstructor = getProductConstructor(instance.getProductName());
productConstructor.newInstance(new Object[] { instance }).run(); // added .run() to the end but this doesn't work.
这不起作用,因为 run
未定义。这是因为我无法在 class 之前以这种方式调用方法,直到 运行 时间才知道。这就是我的问题所在。
问题是 productConstructor.newInstance(new Object[] { instance });
检索了一个 Object
,您必须将其转换为所需的类型 product1
:
((product1)productConstructor.newInstance(new Object[] { instance })).run();
让我向您提供我的项目之一 reflection-utils。
<dependency>
<groupId>ru.oleg-cherednik.utils.reflection</groupId>
<artifactId>reflection-utils</artifactId>
<version>1.0</version>
</dependency>
product1 product1 = ConstructorUtils.invokeConstructor(product1.class, Instances.class, new Instances());
product1.run();
如果您无法包含该文件(例如文件具有包可见性),您可以使用名称执行相同的操作:
String className = "com.products.product1";
Object product1 = ConstructorUtils.invokeConstructor(className, Instance.class, new Instance());
String methodName = "run";
MethodUtils.invokeMethod(product1, methodName);
您可以在此处找到有关如何使用反射的更多示例。
我是这样做的:
Constructor<?> productConstructor;
Class<?> productClass;
productConstructor = getProductConstructor(instance.getProductName());
productClass = getProductClass(instance.getProductName());
productClass.getMethod("run").invoke(productConstructor.newInstance(new Object[] { instance }));
public Constructor<?> getProductConstructor(String productName) {
try {
String fullQualifiedClassPath = "com.products." + productName;
logger.info("Looking for Product Constructor: {}", fullQualifiedClassPath);
Class<?> clazz = Class.forName(fullQualifiedClassPath);
return clazz.getConstructor(Instances.class);
} catch (Exception e) {
logger.error("Product: {} does not exist", productName);
}
return null;
}
public Class<?> getProductClass(String productName) {
try {
String fullQualifiedClassPath = "com.products." + productName;
logger.info("Looking for Product Class: {}", fullQualifiedClassPath);
return Class.forName(fullQualifiedClassPath);
} catch (Exception e) {
logger.error("Product: {} does not exist", productName);
}
return null;
}
如果我有一个构造函数对象 Constructor<?> productConstructor
,我如何调用存在于 class 中的 run()
方法?
例如,假设我有 class:
public class product1{
public product1(Instances instance){
// constructor
}
public void run(){
// do something
}
}
我可以 运行 我的 class 的构造函数是这样的:
Constructor<?> productConstructor;
productConstructor = getProductConstructor(instance.getProductName());
// pass instance into constructor to run the tests
// example of what is happening here -> new product1(instance)
productConstructor.newInstance(new Object[] { instance });
public Constructor<?> getProductConstructor(String productName) {
try {
logger.info("Looking for Product Class: {}", productName);
String fullQualifiedClassPath = "com.products." + productName;
Class<?> clazz = Class.forName(fullQualifiedClassPath);
return clazz.getConstructor(Instances.class);
} catch (Exception e) {
logger.error("Product: {} does not exist", productName);
}
return null;
}
如果我保持代码原样,那么我必须将 run()
方法放在构造函数内部到 运行 它,但我希望 run()
方法在构造函数。如何使用构造函数 class 为我的 class 调用我的 运行 方法?
例如,我曾想过但行不通:
Constructor<?> productConstructor;
productConstructor = getProductConstructor(instance.getProductName());
productConstructor.newInstance(new Object[] { instance }).run(); // added .run() to the end but this doesn't work.
这不起作用,因为 run
未定义。这是因为我无法在 class 之前以这种方式调用方法,直到 运行 时间才知道。这就是我的问题所在。
问题是 productConstructor.newInstance(new Object[] { instance });
检索了一个 Object
,您必须将其转换为所需的类型 product1
:
((product1)productConstructor.newInstance(new Object[] { instance })).run();
让我向您提供我的项目之一 reflection-utils。
<dependency>
<groupId>ru.oleg-cherednik.utils.reflection</groupId>
<artifactId>reflection-utils</artifactId>
<version>1.0</version>
</dependency>
product1 product1 = ConstructorUtils.invokeConstructor(product1.class, Instances.class, new Instances());
product1.run();
如果您无法包含该文件(例如文件具有包可见性),您可以使用名称执行相同的操作:
String className = "com.products.product1";
Object product1 = ConstructorUtils.invokeConstructor(className, Instance.class, new Instance());
String methodName = "run";
MethodUtils.invokeMethod(product1, methodName);
您可以在此处找到有关如何使用反射的更多示例。
我是这样做的:
Constructor<?> productConstructor;
Class<?> productClass;
productConstructor = getProductConstructor(instance.getProductName());
productClass = getProductClass(instance.getProductName());
productClass.getMethod("run").invoke(productConstructor.newInstance(new Object[] { instance }));
public Constructor<?> getProductConstructor(String productName) {
try {
String fullQualifiedClassPath = "com.products." + productName;
logger.info("Looking for Product Constructor: {}", fullQualifiedClassPath);
Class<?> clazz = Class.forName(fullQualifiedClassPath);
return clazz.getConstructor(Instances.class);
} catch (Exception e) {
logger.error("Product: {} does not exist", productName);
}
return null;
}
public Class<?> getProductClass(String productName) {
try {
String fullQualifiedClassPath = "com.products." + productName;
logger.info("Looking for Product Class: {}", fullQualifiedClassPath);
return Class.forName(fullQualifiedClassPath);
} catch (Exception e) {
logger.error("Product: {} does not exist", productName);
}
return null;
}