Exemple #1
0
  public WorldObject create(DialogSession session, Map<String, String> properties) {
    String type = properties.get(WorldObject.FIELD_TYPE);
    if (type == null) {
      // throw new IllegalArgumentException("Missing type property!");
      logger.warn("Missing type property in " + properties);
    }

    WorldObject worldObject;
    if (type == null || !typeRegistry.containsKey(type)) {
      if (type != null) {
        logger.warn("Unknown type: " + type);
      }

      worldObject = new DefaultWorldObject(type);

    } else {
      Class<? extends WorldObject> typeClass = typeRegistry.get(type);
      Constructor<? extends WorldObject> constructor;
      try {
        constructor = typeClass.getConstructor();
      } catch (NoSuchMethodException | SecurityException e) {
        throw new RuntimeException(
            "Cannot access constructor " + typeClass.getSimpleName() + "(DialogSession, Map)", e);
      }

      try {
        worldObject = constructor.newInstance();
      } catch (InstantiationException
          | IllegalAccessException
          | IllegalArgumentException
          | InvocationTargetException e) {
        throw new RuntimeException("Could not create an instance of class " + typeClass, e);
      }
    }

    worldObject.init(session, properties);

    return worldObject;
  }