简体   繁体   English

从字符串创建对象

[英]Creating objects from strings

So I've read in some data from an XML:所以我从 XML 中读取了一些数据:

String arg1Value;
String arg2Value;
String arg2TypeAsString;
String arg1TypeAsString;

and I want to create an object: ObjectType(arg1Type, arg2Type);我想创建一个对象: ObjectType(arg1Type, arg2Type);

I'm trying this:我正在尝试这个:

(ObjectType) Class.forName(arg1TypeAsString).getConstructor(arg1Type.class, arg2Type.class).newInstance(arg1Value, arg2Value)

But this gives me a noSuchMethodException: ObjectType.<init>(arg1Type, arg2Type)但这给了我一个noSuchMethodException: ObjectType.<init>(arg1Type, arg2Type)

Note that I'm trying to create different objects but they all extend from ObjectType and all their arguments extend from arg1Type and arg2Type respectively.请注意,我正在尝试创建不同的对象,但它们都从 ObjectType 扩展,并且它们的所有参数分别从 arg1Type 和 arg2Type 扩展。

Given the specifications are a bit vague (no actual code), try the followings:鉴于规范有点模糊(没有实际代码),请尝试以下操作:

  1. Make sure your constructor does not have private , protected or default package modifier, if so, use getDeclaredConstructor() instead确保您的构造函数没有privateprotected或 default 包修饰符,如果有,请改用getDeclaredConstructor()
  2. Define a default constructor for ObjectType classObjectType类定义默认构造函数
  3. Split your statement on multiple lines:将您的语句拆分为多行:

     Class<ObjectType> otClass = (ObjectType) class.forName(arg1TypeAsString); Constructor<ObjectType> otConstructor = otClass.getConstructor(arg1Type.class, arg2Type.class); ObjectType ot = otConstructor.newInstance(arg1Value, arg2Value);

My initial assumption was that newInstance should be called with array of objects.我最初的假设是应该使用对象数组调用newInstance This is not correct, see comment below.这是不正确的,请参阅下面的评论。 Then likely reason is that you are passing primitive type to newInstance for example int instead of Integer .那么可能的原因是您将原始类型传递给newInstance例如int而不是Integer Naturally creating array of Objects will eliminate such possibility.自然地创建对象数组将消除这种可能性。 Finally I managed to replicate error message by removing constructor from my target class so it couldn't be found最后,我设法通过从目标类中删除构造函数来复制错误消息,因此无法找到它

java.lang.NoSuchMethodException: Foo.<init>(java.lang.String, java.lang.String)

Please take a look at all those exceptions which implementation throws.请查看实现抛出的所有异常。 Please create your ObjectType compile it and place it in classpath so it could be found.请创建您的ObjectType编译它并将其放置在类路径中,以便可以找到它。 Here is my implementation of method returning Object which you can cast to desired type这是我返回Object的方法的实现,您可以将其转换为所需的类型

static Object createInstance(String clsName, String ctorType1, String ctorVal1, String ctorType2, String ctorVal2)
      throws ClassNotFoundException, NoSuchMethodException, InstantiationException, 
      IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    return Class.forName(clsName).getConstructor(Class.forName(ctorType1), Class.forName(ctorType2))
        .newInstance(new Object[]{ctorVal1, ctorVal2});
  }

It is tested and works.它经过测试并有效。 Quite few exceptions to catch.要捕获的异常很少。 You may want to adjust parameter types so they fit your design requirements.您可能需要调整参数类型,使其符合您的设计要求。 This is my test class这是我的测试课

public class Foo {
  public Foo(String in, String out){}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM