我可以将参数传递给我动态创建的类吗?

4

我成功地在运行时从外部的MyClass.class文件中加载了一个类,并且可以调用它的方法,假设我知道这些方法的名称,我确实知道。

我的问题是我不知道如何向正在加载的类的构造函数传递参数。

我该如何修改代码以向MyClass的构造函数传递参数?另外,我该如何访问MyClass的公共变量:infoToAccess?

这里是我正在使用的文件。(请注意,ClassLoaderExample.pde是为Processing编写的,但除了sketchPath("")和缺少main函数之外,它与其他代码相同。)

ClassLoaderExample.pde:加载类的主文件:

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.MalformedURLException;
import java.lang.ClassLoader;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

/////// FILE STRUCTURE /////
// ClassLoaderExample     //
// --this file            //
// --tmp                  //
// ----MyClass.class      //
////////////////////////////

// TODO
//   Send parameter to MyClass constructor (by reference)
//   Get public int number from MyClass

void setup()
{
    String className = "MyClass";
    Object instance;
    Method updateMethod;

    // sketchPath("") returns the directory this file is in
    File file = new File(sketchPath(""));
    try
    {
        URL url = file.toURL();
        URL[] urls = new URL[]{url};

        ClassLoader classLoader = new URLClassLoader(urls);
        Class<?> loadedClass = classLoader.loadClass("tmp."+className);

        try
        {
            instance = loadedClass.newInstance();
            updateMethod = loadedClass.getDeclaredMethod("update");

            // Calls MyClass's update() method
            try
            {
                updateMethod.invoke(instance);
            }
            catch (InvocationTargetException e) {System.out.println(e);}
            catch (IllegalAccessException    e) {System.out.println(e);}
        }
        catch (InstantiationException e) {System.out.println(e);}
        catch (IllegalAccessException e) {System.out.println(e);}
        catch (NoSuchMethodException  e) {System.out.println(e);}
    }
    catch (MalformedURLException  e) {System.out.println(e);}
    catch (ClassNotFoundException e) {System.out.println(e);}
}

MyClass.java: 我正在加载的类:

package tmp;
public class MyClass
{
    public int infoToAccess = 1337;

    public MyClass(int i)
    {
        System.out.println("MyClass constructor was called with numebr: " + i);
    }

    public void update()
    {
        System.out.println("Update was called.");
    }
}

感谢您对此的任何帮助!

你可能想在这里看一下:http://www.tutorialspoint.com/java/lang/class_getdeclaredmethod.htm - Blaatz0r
4个回答

4

你可以使用加载的类来获取构造函数,并使用它创建新实例。

import java.lang.reflect.Constructor;

...

Class<?> loadedClass = classLoader.loadClass("tmp."+className);
try {
  Constructor<?> ctor=loadedClass.getConstructor(int.class);
  ctor.newInstance(42);
  ...
}
...

这个完美地运作了。谢谢!我已经将你的贡献在这里合并进来。 - uPaymeiFixit

2

您需要使用反射来完成您所询问的两件事情:

Class<?> loadedClass = classLoader.loadClass("tmp." + className);
Object oInstance = null;
try {
    Constructor<?> c = loadedClass.getConstructor(Integer.class);
    oInstance = c.newInstance(10);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
    //Handle the possibility that you cannot access this constructor
}

访问对象的公共字段:
要访问对象的公共字段,请按以下方式操作:
try {
    Field field = loadedClass.getField("infoToAccess");
    int fieldValue = field.getInt(oInstance);
} catch (NoSuchFieldException | IllegalAccessException e) {
    //Handle the possibility that you cannot access this field
}

当我直接实现你的解决方案时,getConstructor 行会抛出以下错误:java.lang.NoSuchMethodException: tmp.MyClass.<init>(java.lang.Integer)。然而,如果我将 Integer.class 替换为 int.class,它就可以工作了。不过,Field 示例完美地运行了。所以谢谢!我已经将你的贡献在这里加入了。 - uPaymeiFixit
int.class 对我来说是新的 - 每天都会学到新东西 :) - StuPointerException

0
如我在评论中提到的链接所述,您可以在此调用中传递参数。 updateMethod = loadedClass.getDeclaredMethod("update", 参数在此处);
然后,您应该能够使用参数调用该方法。

1
我认为你被踩是因为那不是我问的确切问题,但这只是为了让我学习,所以我感激任何新的信息。所以谢谢!我在这里加入了你的贡献:http://pastebin.com/3yfDH55Z - uPaymeiFixit
谢谢提到这个名字 :) - Blaatz0r

0
如果你想将参数传递给构造函数,就不能使用getInstance(),你需要获取构造函数对象,并在那里传递参数。
示例:
Constructor constructor = aClass.getConstructor(new Class[]{String.class});
MyObject myObject = (MyObject) constructor.newInstance("constructor-arg1");

请查看此教程:Java 反射 - 构造函数


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接