use of hudson.model.Describable in project configuration-as-code-plugin by jenkinsci.
the class BaseConfigurator method detectActualType.
protected Attribute detectActualType(String name, Type type) {
Class c = null;
boolean multiple = false;
if (type instanceof GenericArrayType) {
// type is a parameterized array: <Foo>[]
multiple = true;
GenericArrayType at = (GenericArrayType) type;
type = at.getGenericComponentType();
}
while (type instanceof ParameterizedType) {
// type is parameterized `Some<Foo>`
ParameterizedType pt = (ParameterizedType) type;
Class rawType = (Class) pt.getRawType();
if (Collection.class.isAssignableFrom(rawType)) {
// type is `Collection<Foo>`
multiple = true;
}
type = pt.getActualTypeArguments()[0];
if (type instanceof WildcardType) {
// pt is Some<? extends Foo>
Type t = ((WildcardType) type).getUpperBounds()[0];
if (t == Object.class) {
// pt is Some<?>, so we actually want "Some"
type = pt.getRawType();
} else {
type = t;
}
}
}
if (type instanceof ParameterizedType) {
final Type[] arguments = ((ParameterizedType) type).getActualTypeArguments();
type = ((ParameterizedType) type).getRawType();
}
while (c == null) {
if (type instanceof Class) {
c = (Class) type;
} else if (type instanceof TypeVariable) {
// type is declared as parameterized type
// unfortunately, java reflection doesn't allow to get the actual parameter type
// so, if superclass it parameterized, we assume parameter type match
// i.e target is Foo extends AbtractFoo<Bar> with
// public abstract class AbtractFoo<T> { void setBar(T bar) }
final Type superclass = getTarget().getGenericSuperclass();
if (superclass instanceof ParameterizedType) {
final ParameterizedType psc = (ParameterizedType) superclass;
type = psc.getActualTypeArguments()[0];
continue;
} else {
c = (Class) ((TypeVariable) type).getBounds()[0];
}
TypeVariable tv = (TypeVariable) type;
} else {
throw new IllegalStateException("Unable to detect type of attribute " + getTarget() + '#' + name);
}
}
if (c.isArray()) {
multiple = true;
c = c.getComponentType();
}
Attribute attribute;
if (!c.isPrimitive() && !c.isEnum() && Modifier.isAbstract(c.getModifiers())) {
if (!Describable.class.isAssignableFrom(c)) {
// Not a Describable, so probably not an attribute expected to be selected as sub-component
return null;
}
attribute = new DescribableAttribute<T>(name, c);
} else {
attribute = new Attribute<T>(name, c);
}
attribute.multiple(multiple);
return attribute;
}
use of hudson.model.Describable in project configuration-as-code-plugin by jenkinsci.
the class Configurator method lookup.
/**
* Looks for a configurator for exact type.
* @param type Type
* @return Configurator or {@code null} if it is not found
*/
@CheckForNull
public static Configurator lookup(Type type) {
Class clazz = Types.erasure(type);
final Jenkins jenkins = Jenkins.getInstance();
final ExtensionList<Configurator> l = jenkins.getExtensionList(Configurator.class);
for (Configurator c : l) {
if (clazz == c.getTarget()) {
// this type has a dedicated Configurator implementation
return c;
}
}
if (Collection.class.isAssignableFrom(clazz)) {
// TODO: Only try to cast if we can actually get the parameterized type
if (type instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) type;
Type actualType = pt.getActualTypeArguments()[0];
if (actualType instanceof WildcardType) {
actualType = ((WildcardType) actualType).getUpperBounds()[0];
}
if (!(actualType instanceof Class)) {
throw new IllegalStateException("Can't handle " + type);
}
return lookup(actualType);
}
}
if (Descriptor.class.isAssignableFrom(clazz)) {
return new DescriptorConfigurator((Descriptor) jenkins.getExtensionList(clazz).get(0));
}
if (getDataBoundConstructor(clazz) != null) {
return new DataBoundConfigurator(clazz);
}
if (Modifier.isAbstract(clazz.getModifiers()) && Describable.class.isAssignableFrom(clazz)) {
// this is a jenkins Describable component, with various implementations
return new HeteroDescribableConfigurator(clazz);
}
if (TopLevelItem.class.isAssignableFrom(clazz)) {
return new TopLevelItemConfigurator(clazz);
}
if (Extension.class.isAssignableFrom(clazz)) {
return new ExtensionConfigurator(clazz);
}
if (Stapler.lookupConverter(clazz) != null) {
return new PrimitiveConfigurator(clazz);
}
logger.warning("Configuration-as-Code can't handle type " + type);
return null;
}
use of hudson.model.Describable in project hudson-2.x by hudson.
the class CascadingUtil method buildExternalProperties.
/**
* Creates {@link ExternalProjectProperty} based on Descriptors collection, StaplerRequest and JSON resonse.
*
* @param req StaplerRequest
* @param json JSONObject
* @param descriptors list of descriptors
* @param owner job to be updated.
* @param <T> Describable
* @throws Descriptor.FormException if any.
*/
@SuppressWarnings("unchecked")
public static <T extends Describable<T>> void buildExternalProperties(StaplerRequest req, JSONObject json, List<Descriptor<T>> descriptors, Job owner) throws Descriptor.FormException {
for (Descriptor d : descriptors) {
String name = d.getJsonSafeClassName();
ExternalProjectProperty<Describable> baseProperty = getExternalProjectProperty(owner, name);
Describable describable = null;
if (json.has(name)) {
describable = d.newInstance(req, json.getJSONObject(name));
}
baseProperty.setValue(describable);
}
}
Aggregations