@SuppressWarnings({"rawtypes", "unchecked"})
  private <T extends Entity> brooklyn.entity.proxying.EntitySpec<?> toCoreEntitySpec(
      Class<T> clazz, String name, Map<String, String> configO) {
    Map<String, String> config =
        (configO == null)
            ? Maps.<String, String>newLinkedHashMap()
            : Maps.newLinkedHashMap(configO);

    brooklyn.entity.proxying.EntitySpec<? extends Entity> result;
    if (clazz.isInterface()) {
      result = brooklyn.entity.proxying.EntitySpec.create(clazz);
    } else {
      // If this is a concrete class, particularly for an Application class, we want the proxy
      // to expose all interfaces it implements.
      Class interfaceclazz =
          (Application.class.isAssignableFrom(clazz)) ? Application.class : Entity.class;
      Set<Class<?>> additionalInterfaceClazzes =
          Reflections.getInterfacesIncludingClassAncestors(clazz);
      result =
          brooklyn.entity.proxying.EntitySpec.create(interfaceclazz)
              .impl(clazz)
              .additionalInterfaces(additionalInterfaceClazzes);
    }

    if (!Strings.isEmpty(name)) result.displayName(name);
    result.configure(convertFlagsToKeys(result.getImplementation(), config));
    return result;
  }
 private <T> T constructOldStyle(Class<T> clazz, Map<String, ?> flags)
     throws InstantiationException, IllegalAccessException, InvocationTargetException {
   Optional<T> v =
       Reflections.invokeConstructorWithArgs(clazz, new Object[] {MutableMap.copyOf(flags)}, true);
   if (v.isPresent()) {
     return v.get();
   } else {
     throw new IllegalStateException(
         "No valid constructor defined for "
             + clazz
             + " (expected no-arg or single java.util.Map argument)");
   }
 }