Q: Issue with using an object created via reflection I am trying to write a program that can be used to send messages to a set of subscribers. The subscribers and technology used to deliver the messages is not important. The message types are defined by an XML schema and are implemented as a Java API. There is an abstract super-type that all message types extend. The API defines static methods to allow for conversion between XML and Java and vice versa (fromXML() and toXML()).
I am having difficulties implementing this. I have included a sample of the code I am trying to get to work below. It does not compile as written. The abstract super-type is the MessageType class and my plan is to pass in the name of the "real" message type and have this code create the necessary objects to do what is needed to send a message of the specified sub-type.
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();
  }

}

Does anyone know if it's possible to do what I've described above?

A: Since your Writer is MessageWriter<? extends MessageType>, this mean it could be any MessageWriter that takes a subclass of MessageType. So the actual instance could be MessageWriter<MySuperMessageType>. Since your MessageType instance cannot be confirmed by the compiler to be MySuperMessageType, the compiler fails it. 
If the writer was of type MessageWriter<MessageType> or MessageWritier<? super MessageType> it would work. The other option is to create a generic method...
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);
}

