Skip to content

Instantly share code, notes, and snippets.

@antoinesd
Created February 6, 2014 16:34
Show Gist options
  • Save antoinesd/8847785 to your computer and use it in GitHub Desktop.
Save antoinesd/8847785 to your computer and use it in GitHub Desktop.
Example of a CDI 1.1 extrnsion which replace all @RequestScoped by @SessionScoped
package org.jboss.weld.test;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AnnotatedConstructor;
import javax.enterprise.inject.spi.AnnotatedField;
import javax.enterprise.inject.spi.AnnotatedMethod;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.inject.spi.WithAnnotations;
import javax.enterprise.util.AnnotationLiteral;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* @author Antoine Sabot-Durand
*/
public class ReplaceRequestScopedExtension implements Extension {
public static class RequestScopedLiteral extends AnnotationLiteral<RequestScoped> implements RequestScoped {
public static RequestScopedLiteral INSTANCE = new RequestScopedLiteral();
}
public static class SessionScopedLiteral extends AnnotationLiteral<SessionScoped> implements SessionScoped {
public static SessionScopedLiteral INSTANCE = new SessionScopedLiteral();
}
public static class AnnotatedTypeWithReplacedAnnotations<T> implements AnnotatedType<T> {
private final AnnotatedType<T> delegate;
private final Set<Annotation> annotations;
public AnnotatedTypeWithReplacedAnnotations(AnnotatedType<T> delegate, Map<Annotation, Annotation> replace) {
this.delegate = delegate;
if (replace.size() > 0) {
annotations = new HashSet<>();
for (Annotation annotation : delegate.getAnnotations()) {
if (replace.containsKey(annotation))
annotations.add(replace.get(annotation));
else
annotations.add(annotation);
}
} else
annotations = delegate.getAnnotations();
}
@Override
public Class<T> getJavaClass() {
return delegate.getJavaClass();
}
@Override
public Set<AnnotatedConstructor<T>> getConstructors() {
return delegate.getConstructors();
}
@Override
public Set<AnnotatedMethod<? super T>> getMethods() {
return delegate.getMethods();
}
@Override
public Set<AnnotatedField<? super T>> getFields() {
return delegate.getFields();
}
@Override
public Type getBaseType() {
return delegate.getBaseType();
}
@Override
public Set<Type> getTypeClosure() {
return delegate.getTypeClosure();
}
@Override
public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
return delegate.getAnnotation(annotationType);
}
@Override
public Set<Annotation> getAnnotations() {
return annotations;
}
@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
for (Annotation annotation : annotations) {
if (annotationType.equals(annotation.annotationType()))
return true;
}
return false;
}
}
public void replaceRequestScoped(@Observes @WithAnnotations(RequestScoped.class) ProcessAnnotatedType<?> pat) {
Map<Annotation, Annotation> rep = new HashMap<>();
rep.put(RequestScopedLiteral.INSTANCE, SessionScopedLiteral.INSTANCE);
pat.setAnnotatedType(new AnnotatedTypeWithReplacedAnnotations(pat.getAnnotatedType(), rep));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment