/** given a spec, returns the class of the item it targets, for instance returns {@link Entity} given {@link EntitySpec}; * see also {@link #lookupSpecTypeForTarget(Class)}, * If given null, returns {@link BrooklynObject} */ static <T extends AbstractBrooklynObjectSpec<?,?>> Class<? extends BrooklynObject> lookupTargetTypeForSpec(@Nullable Class<T> specSuperType) { if (specSuperType==null) return BrooklynObject.class; BrooklynObjectType best = null; for (BrooklynObjectType t: BrooklynObjectType.values()) { if (t.getSpecType()==null) continue; if (!t.getSpecType().isAssignableFrom(specSuperType)) continue; // on equality, exit immediately if (t.getSpecType().equals(specSuperType)) return t.getInterfaceType(); // else pick which is best if (best==null) { best = t; continue; } // if t is more specific, it is better (handles case when e.g. a Policy is a subclass of Entity) if (best.getSpecType().isAssignableFrom(t.getSpecType())) { best = t; continue; } } if (best==null) { log.warn("Unexpected spec supertype ("+specSuperType+"); treating as any "+BrooklynObject.class, new Throwable("Trace for unexpected spec supertype")); return BrooklynObject.class; } // the spec is more specific, but we're not familiar with it here; return the best return best.getInterfaceType(); }
public static BrooklynObjectType of(Class<?> objectTypeOrSpecType) { if (objectTypeOrSpecType!=null) { for (BrooklynObjectType t: values()) { if (t.getInterfaceType()!=null && t.getInterfaceType().isAssignableFrom(objectTypeOrSpecType)) return t; if (t.getSpecType()!=null && t.getSpecType().isAssignableFrom(objectTypeOrSpecType)) return t; } } return UNKNOWN; }
/** given a {@link BrooklynObject}, returns the spec class which would generate it, for instance returns {@link EntitySpec} given {@link Entity}, * or null if not known */ static <BO extends BrooklynObject> Class<? extends AbstractBrooklynObjectSpec<?,?>> lookupSpecTypeForTarget(Class<BO> targetSuperType) { if (targetSuperType==null) return null; BrooklynObjectType best = null; for (BrooklynObjectType t: BrooklynObjectType.values()) { if (t.getInterfaceType()==null) continue; if (!t.getInterfaceType().isAssignableFrom(targetSuperType)) continue; // on equality, exit immediately if (t.getInterfaceType().equals(targetSuperType)) return t.getSpecType(); // else pick which is best if (best==null) { best = t; continue; } // if t is more specific, it is better (handles case when e.g. a Policy is a subclass of Entity) if (best.getInterfaceType().isAssignableFrom(t.getInterfaceType())) { best = t; continue; } } if (best==null) { log.warn("Unexpected target supertype ("+targetSuperType+"); unable to infer spec type"); return null; } // the spec is more specific, but we're not familiar with it here; return the best return best.getSpecType(); }
resultO = ((BasicBrooklynTypeRegistry)mgmt.getTypeRegistry()).createSpec(resultT, constraint, boType.getSpecType()); } catch (Exception e) { Exceptions.propagateIfFatal(e);