Created
June 26, 2015 09:34
-
-
Save antoinesd/6fd3b4dacc03753df638 to your computer and use it in GitHub Desktop.
Create a decorating AnnotatedType
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.cdisandbox.injectarget; | |
import java.lang.annotation.Annotation; | |
import java.lang.reflect.Type; | |
import java.util.HashSet; | |
import java.util.Set; | |
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.util.AnnotationLiteral; | |
/** | |
* | |
*/ | |
public class ContextualizedAnnotatedType<T> implements AnnotatedType<T> { | |
private AnnotatedType<T> delegate; | |
private Set<Annotation> annotations; | |
private static final Annotation CONTEXTUALIZED_AT = new AnnotationLiteral<Contextualized>() { | |
}); | |
public ContextualizedAnnotatedType(AnnotatedType<T> delegate) { | |
this.delegate = delegate; | |
annotations = new HashSet<>(delegate.getAnnotations()); | |
annotations.add(CONTEXTUALIZED_AT); | |
} | |
@Override | |
public Set<AnnotatedConstructor<T>> getConstructors() { | |
return delegate.getConstructors(); | |
} | |
@Override | |
public Set<AnnotatedField<? super T>> getFields() { | |
return delegate.getFields(); | |
} | |
@Override | |
public Class<T> getJavaClass() { | |
return delegate.getJavaClass(); | |
} | |
@Override | |
public Set<AnnotatedMethod<? super T>> getMethods() { | |
return delegate.getMethods(); | |
} | |
@Override | |
public <T extends Annotation> T getAnnotation(Class<T> annotationType) { | |
if(annotationType.equals(Contextualized.class)) | |
return (T) CONTEXTUALIZED_AT; | |
return delegate.getAnnotation(annotationType); | |
} | |
@Override | |
public Set<Annotation> getAnnotations() { | |
return annotations; | |
} | |
@Override | |
public Type getBaseType() { | |
return delegate.getBaseType(); | |
} | |
@Override | |
public Set<Type> getTypeClosure() { | |
return delegate.getTypeClosure(); | |
} | |
@Override | |
public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) { | |
if(annotationType.equals(Contextualized.class)) | |
return true; | |
return delegate.isAnnotationPresent(annotationType); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment