Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用通过反射创建的对象时出现问题_Java_Generics_Reflection - Fatal编程技术网

Java 使用通过反射创建的对象时出现问题

Java 使用通过反射创建的对象时出现问题,java,generics,reflection,Java,Generics,Reflection,我正在尝试编写一个程序,可以用来向一组订阅者发送消息。用于传递消息的订户和技术并不重要。消息类型由XML模式定义,并作为JavaAPI实现。存在一个所有消息类型都可以扩展的抽象超类型。API定义了静态方法以允许XML和Java之间的转换,反之亦然(fromXML()和toXML()) 我在实施这一点上遇到了困难。我已经在下面提供了一个我正在尝试使用的代码示例。它没有按照编写的方式编译。抽象超类型是MessageType类,我的计划是传入“真实”消息类型的名称,并让此代码创建必要的对象来执行发送指

我正在尝试编写一个程序,可以用来向一组订阅者发送消息。用于传递消息的订户和技术并不重要。消息类型由XML模式定义,并作为JavaAPI实现。存在一个所有消息类型都可以扩展的抽象超类型。API定义了静态方法以允许XML和Java之间的转换,反之亦然(
fromXML()
toXML()

我在实施这一点上遇到了困难。我已经在下面提供了一个我正在尝试使用的代码示例。它没有按照编写的方式编译。抽象超类型是
MessageType
类,我的计划是传入“真实”消息类型的名称,并让此代码创建必要的对象来执行发送指定子类型消息所需的操作

public class Writer {

  public static void runExample(String[] args) throws Exception {
    UUID serviceID = UUID.fromString(args[1]);

    ServiceBus bus = ServiceBus.getServiceBus();
    bus.init(serviceID);

    // The intention is to pass the message type name so that this code can be used for any
    // "message" type.
    String msgName = args[0];

    // Get the message type class from the passed name.
    Class<? extends MessageType> msgClass = (Class<? extends MessageType>) Class.forName(msgName);

    // Create a writer for the specified message type.
    MessageWriter<? extends MessageType> writer = bus.createWriter(msgName, msgClass);

    // Read in some XML content from a file.
    String xml = loadMsg(args[2]);

    // Want to do something like this:
    msgClass object = msgClass.fromXML(xml);
    // Of course, this does not compile. Is there a way to do this?

    // Create a java object of the message type from the XML read in.
    MessageType object = MessageType.fromXML(xml);

    // With the line above the statement below fails to compile with the error:
    //   The method write(capture#4-of ? extends MessageType) in the type
    //   MessageWriter<capture#4-of ? extends MessageType> is not applicable for
    //   the arguments (MessageType)

    writer.write(object);
  }

  private static String loadMsg(String fileName) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(new File(fileName)));
    String line = null;
    StringBuilder sb = new StringBuilder();

    do {
    line = reader.readLine();
    if (line != null)
      sb.append(line);
    } while (line != null);

    reader.close();
    return sb.toString();
  }

}
公共类编写器{
公共静态void运行示例(字符串[]args)引发异常{
UUID serviceID=UUID.fromString(args[1]);
ServiceBus总线=ServiceBus.getServiceBus();
总线初始化(serviceID);
//其目的是传递消息类型名称,以便此代码可用于任何
//“消息”类型。
字符串msgName=args[0];
//从传递的名称获取消息类型类。

类因为您的
编写器
消息编写器编译错误是什么?不要让我们将整个内容复制到IDE中去查找。错误消息作为注释包含在代码中。您可能想指出得更好一点。这并不是您代码中唯一的注释。哎呀,我认为注释很有用。发布了很多我们假设注释是从您的代码复制而来的,而不是作为问题的一部分。看起来它一开始会起作用,但是……下面是我的代码:private void write(类类型、字符串主题、字符串内容)抛出UCIException、IOException{MessageWriter writer=bus.createWriter(topic,type);T object=(T)MessageType.fromXML(content);writer.write(object);}不幸的是,我在线程“main”uci.UCIException中遇到一个运行时错误:(待续)异常:javax.xml.bind.unmarshaleException:无法创建uci.types.MessageType的实例-带有链接异常:uci.types.MessageType.fromXML(MessageType.java:535)中的[java.lang.InstanceionException]在boeingfuzz.Sender.write(Sender.java:34)在boeingfuzz.Sender.send(Sender.java:28)在boeingfuzz.Main.Main(Main.java:24)中的[java.lang.InstanceionException]唉……顺便提一下,我如何输入一条不会丢失格式的注释?
private <T extends MessageType> myMethod(Class<T> type){
   MessageWriter<T> writer = bus.createWriter(msgName, msgClass);
   String xml = loadMsg(args[2]);
   // you could use reflection to get the public static methods from the class instance
   // msgClass object = msgClass.fromXML(xml);
   T object = (T) MessageType.fromXML(xml);
   writer.write(object);
}