下面列出了怎么用hudson.model.Describable的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Asserts that help files exist for the specified properties of the given instance.
*
* @param type
* The describable class type that should have the associated help files.
* @param properties
* ','-separated list of properties whose help files should exist.
*/
public void assertHelpExists(final Class<? extends Describable> type, final String properties) throws Exception {
executeOnServer(new Callable<Object>() {
public Object call() throws Exception {
Descriptor d = jenkins.getDescriptor(type);
WebClient wc = createWebClient();
for (String property : listProperties(properties)) {
String url = d.getHelpFile(property);
assertThat("Help file for the property " + property + " is missing on " + type, url,
Matchers.notNullValue());
wc.goTo(url); // make sure it successfully loads
}
return null;
}
});
}
protected Attribute createAttribute(String name, final TypePair type) {
boolean multiple =
type.rawType.isArray()
|| Collection.class.isAssignableFrom(type.rawType);
// If attribute is a Collection|Array of T, we need to introspect further to determine T
Class c = multiple ? getComponentType(type) : type.rawType;
if (c == null) {
throw new IllegalStateException("Unable to detect type of attribute " + getTarget() + '#' + name);
}
// special collection types with dedicated handlers to manage data replacement / possible values
if (DescribableList.class.isAssignableFrom(type.rawType)) {
return new DescribableListAttribute(name, c);
} else if (PersistedList.class.isAssignableFrom(type.rawType)) {
return new PersistedListAttribute(name, c);
}
Attribute attribute;
if (!c.isPrimitive() && !c.isEnum() && Modifier.isAbstract(c.getModifiers())) {
if (!Describable.class.isAssignableFrom(c)) {
// Not a Describable, so we don't know how to detect concrete implementation type
LOGGER.warning("Can't handle "+getTarget()+"#"+name+": type is abstract but not Describable.");
return null;
}
attribute = new DescribableAttribute(name, c);
} else {
attribute = new Attribute(name, c);
}
attribute.multiple(multiple);
return attribute;
}
private <T extends Describable<T>,D extends Descriptor<T>> void populateMetaSteps(List<Descriptor<?>> r, Class<T> c) {
Jenkins j = Jenkins.getInstance();
for (Descriptor<?> d : j.getDescriptorList(c)) {
if (SimpleBuildStep.class.isAssignableFrom(d.clazz) && symbolForObject(d) != null) {
r.add(d);
} else if (SimpleBuildWrapper.class.isAssignableFrom(d.clazz) && symbolForObject(d) != null) {
r.add(d);
}
}
}
private JSONObject staplerJsonForDescr(Describable d) {
DescribableModel<?> m = DescribableModel.of(d.getClass());
JSONObject o = new JSONObject();
o.accumulate("stapler-class", d.getClass().getName());
o.accumulate("$class", d.getClass().getName());
if (d instanceof OptionalJobProperty) {
o.accumulate("specified", true);
}
for (DescribableParameter param : m.getParameters()) {
Object v = getValue(param, d);
if (v != null) {
if (v instanceof Describable) {
o.accumulate(param.getName(), staplerJsonForDescr((Describable)v));
} else if (v instanceof List && !((List) v).isEmpty()) {
JSONArray a = new JSONArray();
for (Object obj : (List) v) {
if (obj instanceof Describable) {
a.add(staplerJsonForDescr((Describable) obj));
} else if (obj instanceof Number) {
a.add(obj.toString());
} else {
a.add(obj);
}
}
o.accumulate(param.getName(), a);
} else if (v instanceof Number) {
o.accumulate(param.getName(), v.toString());
} else {
o.accumulate(param.getName(), v);
}
}
}
return o;
}
private <T extends Describable<T>, D extends Descriptor<T>> List<Descriptor<?>> getClassList(Class<T> c) {
ArrayList<Descriptor<?>> r = new ArrayList<Descriptor<?>>();
if (jenkins == null) {
return new ArrayList<Descriptor<?>>();
}
for (Descriptor<?> d : jenkins.getDescriptorList(c)) {
if (SimpleBuildStep.class.isAssignableFrom(d.clazz)) {
r.add(d);
}
}
return r;
}
@SuppressWarnings("unchecked")
private CNode convertToNode(ConfigurationContext context, Configurator configurator, Describable instance) {
return unchecked(() -> configurator.describe(instance, context)).apply();
}
public DescribableAttribute(String name, Class<? extends Describable> type) {
super(name, type);
}
private Configurator internalLookup(Type type) {
Class clazz = Types.erasure(type);
final Jenkins jenkins = Jenkins.get();
final ExtensionList<Configurator> l = jenkins.getExtensionList(Configurator.class);
for (Configurator c : l) {
if (c.canConfigure(clazz)) {
// this type has a dedicated Configurator implementation
return c;
}
}
//TODO: Only try to cast if we can actually get the parameterized type
if (Collection.class.isAssignableFrom(clazz) && type instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) type;
Type actualType = pt.getActualTypeArguments()[0];
if (actualType instanceof WildcardType) {
actualType = ((WildcardType) actualType).getUpperBounds()[0];
}
if (actualType instanceof ParameterizedType) {
actualType = ((ParameterizedType) actualType).getRawType();
}
if (!(actualType instanceof Class)) {
throw new IllegalStateException("Can't handle " + type);
}
return lookup(actualType);
}
if (Configurable.class.isAssignableFrom(clazz)) {
return new ConfigurableConfigurator(clazz);
}
if (Descriptor.class.isAssignableFrom(clazz)) {
ExtensionList extensions = jenkins.getExtensionList(clazz);
if (!extensions.isEmpty()) {
return new DescriptorConfigurator((Descriptor) extensions.get(0));
}
}
if (DataBoundConfigurator.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 (Extension.class.isAssignableFrom(clazz)) {
return new ExtensionConfigurator(clazz);
}
if (Stapler.lookupConverter(clazz) != null) {
return new PrimitiveConfigurator(clazz);
}
if (clazz.isEnum()) {
return new EnumConfigurator(clazz);
}
LOGGER.warning("Configuration-as-Code can't handle type "+ type);
return null;
}