All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.weld.annotated.enhanced.jlr.AbstractEnhancedAnnotated Maven / Gradle / Ivy

There is a newer version: 3.0.0.Alpha1
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2008, Red Hat, Inc., and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jboss.weld.annotated.enhanced.jlr;

import static org.jboss.weld.util.collections.WeldCollections.immutableMapView;
import static org.jboss.weld.util.reflection.Reflections.EMPTY_ANNOTATIONS;

import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.enterprise.inject.spi.Annotated;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.inject.Qualifier;

import org.jboss.weld.annotated.enhanced.EnhancedAnnotated;
import org.jboss.weld.annotated.slim.SlimAnnotatedType;
import org.jboss.weld.literal.DefaultLiteral;
import org.jboss.weld.logging.ReflectionLogger;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.util.collections.Arrays2;
import org.jboss.weld.util.collections.ImmutableSet;
import org.jboss.weld.util.collections.Multimap;
import org.jboss.weld.util.collections.Multimaps;
import org.jboss.weld.util.collections.SetMultimap;
import org.jboss.weld.util.reflection.Reflections;

/**
 * Represents functionality common for all annotated items, mainly different
 * mappings of the annotations and meta-annotations
 * 

* AbstractAnnotatedItem is an immutable class and therefore threadsafe * * @param * @param * @author Pete Muir * @author Nicklas Karlsson * @see org.jboss.weld.annotated.enhanced.EnhancedAnnotated */ public abstract class AbstractEnhancedAnnotated implements EnhancedAnnotated { // The set of default binding types private static final Set DEFAULT_QUALIFIERS = Collections.singleton(DefaultLiteral.INSTANCE); /** * Builds the annotation map (annotation type -> annotation) * * @param annotations The array of annotations to map * @return The annotation map */ protected static Map, Annotation> buildAnnotationMap(Annotation[] annotations) { Map, Annotation> annotationMap = new HashMap, Annotation>(); for (Annotation annotation : annotations) { annotationMap.put(annotation.annotationType(), annotation); } return annotationMap; } /** * Builds the annotation map (annotation type -> annotation) * * @param annotations The array of annotations to map * @return The annotation map */ protected static Map, Annotation> buildAnnotationMap(Iterable annotations) { Map, Annotation> annotationMap = new HashMap, Annotation>(); for (Annotation annotation : annotations) { annotationMap.put(annotation.annotationType(), annotation); } return annotationMap; } protected static void addMetaAnnotations(SetMultimap, Annotation> metaAnnotationMap, Annotation annotation, Annotation[] metaAnnotations, boolean declared) { for (Annotation metaAnnotation : metaAnnotations) { addMetaAnnotation(metaAnnotationMap, annotation, metaAnnotation.annotationType(), declared); } } protected static void addMetaAnnotations(SetMultimap, Annotation> metaAnnotationMap, Annotation annotation, Iterable metaAnnotations, boolean declared) { for (Annotation metaAnnotation : metaAnnotations) { addMetaAnnotation(metaAnnotationMap, annotation, metaAnnotation.annotationType(), declared); } } private static void addMetaAnnotation(SetMultimap, Annotation> metaAnnotationMap, Annotation annotation, Class metaAnnotationType, boolean declared) { // Only map meta-annotations we are interested in if (declared ? MAPPED_DECLARED_METAANNOTATIONS.contains(metaAnnotationType) : MAPPED_METAANNOTATIONS.contains(metaAnnotationType)) { metaAnnotationMap.put(metaAnnotationType, annotation); } } // The annotation map (annotation type -> annotation) of the item private final Map, Annotation> annotationMap; // The meta-annotation map (annotation type -> set of annotations containing // meta-annotation) of the item private final Multimap, Annotation> metaAnnotationMap; private final Class rawType; private final Type[] actualTypeArguments; private final Annotated delegate; /** * Constructor *

* Also builds the meta-annotation map. Throws a NullPointerException if * trying to register a null map * * @param annotationMap A map of annotation to register */ public AbstractEnhancedAnnotated(Annotated annotated, Map, Annotation> annotationMap, Map, Annotation> declaredAnnotationMap, ClassTransformer classTransformer) { this.delegate = annotated; if (annotated instanceof AnnotatedType) { this.rawType = Reflections.>cast(annotated).getJavaClass(); } else { this.rawType = Reflections.getRawType(annotated.getBaseType()); } if (annotationMap == null) { throw ReflectionLogger.LOG.annotationMapNull(); } this.annotationMap = immutableMapView(annotationMap); SetMultimap, Annotation> metaAnnotationMap = SetMultimap.newSetMultimap(); for (Annotation annotation : annotationMap.values()) { // WELD-1310 Include synthetic annotations SlimAnnotatedType syntheticAnnotationAnnotatedType = classTransformer .getSyntheticAnnotationAnnotatedType(annotation.annotationType()); if (syntheticAnnotationAnnotatedType == null) { addMetaAnnotations(metaAnnotationMap, annotation, classTransformer.getReflectionCache().getAnnotations(annotation.annotationType()), false); } else { addMetaAnnotations(metaAnnotationMap, annotation, syntheticAnnotationAnnotatedType.getAnnotations(), false); } addMetaAnnotations(metaAnnotationMap, annotation, classTransformer.getTypeStore().get(annotation.annotationType()), false); } this.metaAnnotationMap = Multimaps.unmodifiableMultimap(metaAnnotationMap); if (declaredAnnotationMap == null) { throw ReflectionLogger.LOG.declaredAnnotationMapNull(); } if (delegate.getBaseType() instanceof ParameterizedType) { this.actualTypeArguments = ((ParameterizedType) delegate.getBaseType()).getActualTypeArguments(); } else { this.actualTypeArguments = Arrays2.EMPTY_TYPE_ARRAY; } } public Class getJavaClass() { return rawType; } public Type[] getActualTypeArguments() { return Arrays.copyOf(actualTypeArguments, actualTypeArguments.length); } public Set getInterfaceClosure() { Set interfaces = new HashSet(); for (Type t : getTypeClosure()) { if (Reflections.getRawType(t).isInterface()) { interfaces.add(t); } } return Collections.unmodifiableSet(interfaces); } public abstract S getDelegate(); public boolean isParameterizedType() { return rawType.getTypeParameters().length > 0; } public boolean isPrimitive() { return getJavaClass().isPrimitive(); } public Type getBaseType() { return delegate.getBaseType(); } public Set getTypeClosure() { return delegate.getTypeClosure(); } public Set getAnnotations() { return ImmutableSet.copyOf(annotationMap.values()); } public Set getMetaAnnotations(Class metaAnnotationType) { return ImmutableSet.copyOf(metaAnnotationMap.get(metaAnnotationType)); } @Deprecated public Set getQualifiers() { if (getMetaAnnotations(Qualifier.class).size() > 0) { return Collections.unmodifiableSet(getMetaAnnotations(Qualifier.class)); } else { return DEFAULT_QUALIFIERS; } } @Deprecated public Annotation[] getBindingsAsArray() { return getQualifiers().toArray(EMPTY_ANNOTATIONS); } public A getAnnotation(Class annotationType) { return annotationType.cast(annotationMap.get(annotationType)); } public boolean isAnnotationPresent(Class annotationType) { return annotationMap.containsKey(annotationType); } Map, Annotation> getAnnotationMap() { return annotationMap; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy