Feature Overview

Annotated Type Builder

DeltaSpike provides an {{AnnotatedType}} implementation (and corresponding implementations of the suite of Annotated interfaces from CDI) that should be suitable for the needs of most portable extensions. The {{AnnotatedType}} is created from {{AnnotatedTypeBuilder}}, typically in an extension's observer method, as follows:

Modifying an AnnotatedType
public class NamingConventionAwareMetadataFilter implements Extension
{
    public void ensureNamingConvention(@Observes ProcessAnnotatedType processAnnotatedType)
    {
        Class<?> beanClass = processAnnotatedType.getAnnotatedType().getJavaClass();

        Named namedAnnotation = beanClass.getAnnotation(Named.class);
        if(namedAnnotation != null &&
                namedAnnotation.value().length() > 0 &&
                Character.isUpperCase(namedAnnotation.value().charAt(0)))
        {
            AnnotatedTypeBuilder builder = new AnnotatedTypeBuilder();
            builder.readFromType(beanClass);

            String beanName = namedAnnotation.value();
            String newBeanName = beanName.substring(0, 1).toLowerCase() + beanName.substring(1);

            builder.removeFromClass(Named.class)
                   .addToClass(new NamedLiteral(newBeanName));

            processAnnotatedType.setAnnotatedType(builder.create());
        }
    }
}
//don't forget to configure the extension in /META-INF/services/javax.enterprise.inject.spi.Extension

In the example above we create a new builder, and initialize it using an existing {{AnnotatedType}}. We can then add or remove annotations from the class, and its members. When we have finished modifying the type, we call {{#create()}} to spit out a new, immutable, {{AnnotatedType}}. In the example {{@Named}} gets replaced if the bean-name doesn't start with a lower case character. {{NamedLiteral}} takes the new value which should be used for the new instance of {{@Named}}. That means if a class is annotated e.g. with {{@Named("MyBean")}} it will be replaced with {{@Named("myBean")}} during the bootstrapping process.

AnnotatedTypeBuilder provides the following methods: * addToClass * addToConstructor * addToConstructorParameter * addToField * addToMethod * addToMethodParameter * addToParameter * overrideConstructorParameterType * overrideFieldType * overrideMethodParameterType * overrideParameterType * readFromType * removeFromAll * removeFromClass * removeFromConstructor * removeFromConstructorParameter * removeFromField * removeFromMethod * removeFromMethodParameter * removeFromParameter * getJavaClass * setJavaClass * create

AnnotationInstanceProvider

DeltaSpike has the ability, with the AnnotationInstanceProvider class, to dynamically create instances of annotations. These are useful for adding annotations to bean metadata in extensions, adding qualifiers to events, etc. Usage is very simple and straight forward. There are two public methods: {{of(Class)}} and {{of(Class, Map)}}. The first is simply a short cut passing an empty map to the second. The map parameter is a map of values to be used for the members of the annotation. The keys in the map must be the names of members in the annotation (i.e. value, type, etc.). Simple usages are below.

No Member values
RequestScoped requestScopedInstance = AnnotationInstanceProvider.of(RequestScoped.class);

Multiple member values
Map<String, Object> memberValues = new HashMap<String, Object>();
memberValues.put("booleanValue", false);
memberValues.put("booleanValues", new boolean[]{false});
memberValues.put("byteValue", (byte) 0);
memberValues.put("byteValues", new byte[]{(byte) 0});
memberValues.put("type", Object.class);
memberValues.put("types", new Class[]{Object.class});

TestAnnotation testAnnotation = AnnotationInstanceProvider.of(TestAnnotation.class, memberValues);

AnnotationInstanceProvider provides an implementation of equals, hashCode and toString compliant with the javadocs for Annotation. It also caches the instances for the duration of the application. Creating a new instance of the same class and same member values will yield the same instance as before.