001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.core.ext.typeinfo;
017:
018: import java.lang.annotation.Annotation;
019: import java.lang.annotation.Inherited;
020: import java.util.Collection;
021: import java.util.HashMap;
022: import java.util.Map;
023: import java.util.Map.Entry;
024:
025: /**
026: * Default implementation of the {@link HasAnnotations} interface.
027: */
028: class Annotations implements HasAnnotations {
029: /**
030: * All annotations declared on the annotated element.
031: */
032: private final Map<Class<? extends Annotation>, Annotation> declaredAnnotations = new HashMap<Class<? extends Annotation>, Annotation>();
033:
034: /**
035: * Lazily initialized collection of annotations declared on or inherited by
036: * the annotated element.
037: */
038: private Map<Class<? extends Annotation>, Annotation> lazyAnnotations = null;
039:
040: /**
041: * If not <code>null</code> the parent to inherit annotations from.
042: */
043: private Annotations parent;
044:
045: Annotations() {
046: }
047:
048: Annotations(Annotations otherAnnotations) {
049: if (otherAnnotations != null) {
050: Annotation[] otherDeclaredAnnotations = otherAnnotations
051: .getDeclaredAnnotations();
052: for (Annotation otherDeclaredAnnotation : otherDeclaredAnnotations) {
053: addAnnotation(otherDeclaredAnnotation.annotationType(),
054: otherDeclaredAnnotation);
055: }
056: }
057: }
058:
059: Annotations(
060: Map<Class<? extends Annotation>, Annotation> declaredAnnotations) {
061: this .declaredAnnotations.putAll(declaredAnnotations);
062: }
063:
064: public void addAnnotations(
065: Map<Class<? extends Annotation>, Annotation> annotations) {
066: if (annotations != null) {
067: assert (!annotations.containsValue(null));
068: declaredAnnotations.putAll(annotations);
069: }
070: }
071:
072: public <T extends Annotation> T getAnnotation(
073: Class<T> annotationClass) {
074: initializeAnnotations();
075: return (T) lazyAnnotations.get(annotationClass);
076: }
077:
078: public Annotation[] getAnnotations() {
079: initializeAnnotations();
080: Collection<Annotation> values = lazyAnnotations.values();
081: return values.toArray(new Annotation[values.size()]);
082: }
083:
084: public Annotation[] getDeclaredAnnotations() {
085: Collection<Annotation> values = declaredAnnotations.values();
086: return values.toArray(new Annotation[values.size()]);
087: }
088:
089: public boolean isAnnotationPresent(
090: Class<? extends Annotation> annotationClass) {
091: return getAnnotation(annotationClass) != null;
092: }
093:
094: void addAnnotation(Class<? extends Annotation> annotationClass,
095: Annotation annotationInstance) {
096: assert (annotationClass != null);
097: assert (annotationInstance != null);
098: assert (!declaredAnnotations.containsKey(annotationClass));
099:
100: declaredAnnotations.put(annotationClass, annotationInstance);
101: }
102:
103: void setParent(Annotations parent) {
104: this .parent = parent;
105: }
106:
107: private void initializeAnnotations() {
108: if (lazyAnnotations != null) {
109: return;
110: }
111:
112: if (parent != null) {
113: lazyAnnotations = new HashMap<Class<? extends Annotation>, Annotation>();
114: parent.initializeAnnotations();
115: for (Entry<Class<? extends Annotation>, Annotation> entry : parent.lazyAnnotations
116: .entrySet()) {
117: if (entry.getValue().annotationType()
118: .isAnnotationPresent(Inherited.class)) {
119: lazyAnnotations.put(entry.getKey(), entry
120: .getValue());
121: }
122: }
123:
124: lazyAnnotations.putAll(declaredAnnotations);
125: } else {
126: lazyAnnotations = declaredAnnotations;
127: }
128: }
129: }
|