
hudson.tools.PropertyDescriptor Maven / Gradle / Ivy
Show all versions of hudson-core Show documentation
/*******************************************************************************
*
* Copyright (c) 2004-2010 Oracle Corporation.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*
*
*******************************************************************************/
package hudson.tools;
import hudson.Functions;
import hudson.model.Describable;
import hudson.model.Descriptor;
import java.util.ArrayList;
import java.util.List;
/**
* Base {@link Descriptor} type used for {@code XyzProperty} classes.
*
* @param Type of the {@code XyzProperty}. Called 'property type'
* @param Type of the {@code Xyz}, that the property attaches to. Called
* 'target type'
* @author Kohsuke Kawaguchi
* @since 1.305
*/
public abstract class PropertyDescriptor, T> extends Descriptor
{
protected PropertyDescriptor(Class extends P> clazz) {
super(clazz);
}
protected PropertyDescriptor() {
}
/**
* Infer the type parameterization 'P'
*/
private Class
getP() {
return Functions.getTypeParameter(getClass(), Descriptor.class, 0);
}
/**
* Returns true if this property type is applicable to the given target
* type.
*
*
The default implementation of this method checks if the given node
* type is assignable according to the parameterization, but subtypes can
* extend this to change this behavior.
*
* @return true to indicate applicable, in which case the property will be
* displayed in the configuration screen of the target, for example.
*/
public boolean isApplicable(Class extends T> targetType) {
Class extends T> applicable = Functions.getTypeParameter(clazz, getP(), 0);
return applicable.isAssignableFrom(targetType);
}
public static , T> List for_(List all, Class extends T> target) {
List result = new ArrayList();
for (D d : all) {
if (d.isApplicable(target)) {
result.add(d);
}
}
return result;
}
public static , T> List for_(List all, T target) {
return for_(all, (Class) target.getClass());
}
}