Search in sources :

Example 1 with Symbol

use of org.jenkinsci.Symbol in project configuration-as-code-plugin by jenkinsci.

the class DescribableAttribute method getSymbolName.

public static String getSymbolName(Descriptor d, Class extensionPoint, Class target) {
    if (d != null) {
        // explicit @Symbol annotation on descriptor
        Symbol s = d.getClass().getAnnotation(Symbol.class);
        if (s != null)
            return s.value()[0];
        final String ext = extensionPoint.getSimpleName();
        final String cn = d.getKlass().toJavaClass().getSimpleName();
        // extension type Foo is implemented as SomeFoo. => "some"
        if (cn.endsWith(ext)) {
            return normalize(cn.substring(0, cn.length() - ext.length()));
        }
        // extension type Foo is implemented as SomeFooImpl. => "some"
        final String in = target.getSimpleName() + "Impl";
        if (cn.endsWith(in)) {
            return normalize(cn.substring(0, cn.length() - in.length()));
        }
        // Fall back to simple class name
        return normalize(cn);
    }
    // Fall back to simple class name
    return normalize(target.getSimpleName());
}
Also used : Symbol(org.jenkinsci.Symbol)

Example 2 with Symbol

use of org.jenkinsci.Symbol in project configuration-as-code-plugin by jenkinsci.

the class GlobalConfigurationCategoryConfigurator method getName.

@Override
public String getName() {
    final Class c = category.getClass();
    final Symbol symbol = (Symbol) c.getAnnotation(Symbol.class);
    if (symbol != null)
        return symbol.value()[0];
    String name = c.getSimpleName();
    name = StringUtils.remove(name, "Global");
    name = StringUtils.remove(name, "Configuration");
    name = StringUtils.remove(name, "Category");
    return name;
}
Also used : Symbol(org.jenkinsci.Symbol)

Example 3 with Symbol

use of org.jenkinsci.Symbol in project configuration-as-code-plugin by jenkinsci.

the class DescribableAttribute method getSymbols.

/**
 * Retrieve all possible symbols for this descriptor, first one being preferred one.
 * If a {@link Symbol} annotation is set, all values are accepted the last one being preferred
 */
public static List<String> getSymbols(Descriptor d, Class extensionPoint, Class target) {
    if (d != null) {
        List<String> symbols = new ArrayList<>();
        // explicit @Symbol annotation on descriptor
        // first is the preferred one as by Symbol contract
        // "The first one is used as the primary identifier for reverse-mapping."
        Symbol s = d.getClass().getAnnotation(Symbol.class);
        if (s != null) {
            symbols.addAll(Arrays.asList(s.value()));
        }
        // extension type Foo is implemented as SomeFoo. => "some"
        final String ext = extensionPoint.getSimpleName();
        final String cn = d.getKlass().toJavaClass().getSimpleName();
        if (cn.endsWith(ext)) {
            symbols.add(normalize(cn.substring(0, cn.length() - ext.length())));
        }
        // extension type Foo is implemented as SomeFooImpl. => "some"
        final String in = extensionPoint.getSimpleName() + "Impl";
        if (cn.endsWith(in)) {
            symbols.add(normalize(cn.substring(0, cn.length() - in.length())));
        }
        // Fall back to simple class name
        symbols.add(normalize(cn));
        return symbols;
    }
    // Fall back to simple class name
    return Collections.singletonList(normalize(target.getSimpleName()));
}
Also used : Symbol(org.jenkinsci.Symbol) ArrayList(java.util.ArrayList)

Example 4 with Symbol

use of org.jenkinsci.Symbol in project configuration-as-code-plugin by jenkinsci.

the class BaseConfigurator method describe.

public Set<Attribute> describe() {
    Set<Attribute> attributes = new HashSet<>();
    final Class<T> target = getTarget();
    final PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(target);
    LOGGER.log(Level.FINE, "Found {0} properties for {1}", new Object[] { properties.length, target });
    for (PropertyDescriptor p : properties) {
        final String name = p.getName();
        LOGGER.log(Level.FINER, "Processing {0} property", name);
        final Method setter = p.getWriteMethod();
        if (setter == null) {
            LOGGER.log(Level.FINE, "Ignored {0} property: read only", name);
            // read only
            continue;
        }
        if (setter.getAnnotation(Deprecated.class) != null) {
            LOGGER.log(Level.FINE, "Ignored {0} property: deprecated", name);
            // not actually public
            continue;
        }
        if (setter.getAnnotation(Restricted.class) != null) {
            LOGGER.log(Level.FINE, "Ignored {0} property: restricted", name);
            // not actually public     - require access-modifier 1.12
            continue;
        }
        // FIXME move this all into cleaner logic to discover property type
        Type type = setter.getGenericParameterTypes()[0];
        Attribute attribute = detectActualType(name, type);
        if (attribute == null)
            continue;
        attributes.add(attribute);
        final Method getter = p.getReadMethod();
        if (getter != null) {
            final Exported annotation = getter.getAnnotation(Exported.class);
            if (annotation != null && isNotBlank(annotation.name())) {
                attribute.preferredName(annotation.name());
            }
        }
        // See https://github.com/jenkinsci/structs-plugin/pull/18
        final Symbol s = setter.getAnnotation(Symbol.class);
        if (s != null) {
            attribute.preferredName(s.value()[0]);
        }
    }
    return attributes;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) Restricted(org.kohsuke.accmod.Restricted) Symbol(org.jenkinsci.Symbol) Method(java.lang.reflect.Method) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Exported(org.kohsuke.stapler.export.Exported) HashSet(java.util.HashSet)

Example 5 with Symbol

use of org.jenkinsci.Symbol in project configuration-as-code-plugin by jenkinsci.

the class GlobalConfigurationCategoryConfigurator method getNames.

@NonNull
@Override
public List<String> getNames() {
    final Class c = category.getClass();
    final Symbol symbol = (Symbol) c.getAnnotation(Symbol.class);
    if (symbol != null)
        return asList(symbol.value());
    String name = c.getSimpleName();
    name = StringUtils.remove(name, "Global");
    name = StringUtils.remove(name, "Configuration");
    name = StringUtils.remove(name, "Category");
    return singletonList(name.toLowerCase());
}
Also used : Symbol(org.jenkinsci.Symbol) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Aggregations

Symbol (org.jenkinsci.Symbol)5 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 GenericArrayType (java.lang.reflect.GenericArrayType)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 WildcardType (java.lang.reflect.WildcardType)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Restricted (org.kohsuke.accmod.Restricted)1 Exported (org.kohsuke.stapler.export.Exported)1