001: /*
002: * FindBugs - Find Bugs in Java programs
003: * Copyright (C) 2003-2007 University of Maryland
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package edu.umd.cs.findbugs.ba.jsr305;
021:
022: import java.util.LinkedList;
023: import java.util.List;
024:
025: import javax.annotation.CheckForNull;
026:
027: import edu.umd.cs.findbugs.classfile.analysis.AnnotatedObject;
028:
029: /**
030: * The result of looking up a TypeQualifierAnnotation.
031: * Because type qualifiers are inherited, a full result of
032: * looking resolving a TypeQualifierAnnotation
033: * may include annotations on one or more supertypes.
034: * Potentially, the supertype annotations may conflict with
035: * each other, and/or conflict with the annotation on the annotated
036: * entity. This object makes it possible to report
037: * such conflicts, while still providing a convenient
038: * interface for getting the "effective" TypeQualifierAnnotation.
039: *
040: * @author David Hovemeyer
041: */
042: public class TypeQualifierAnnotationLookupResult {
043: /**
044: * Partial result of looking up a TypeQualifierAnnotation.
045: */
046: public static class PartialResult {
047: private AnnotatedObject annotatedObject;
048: private TypeQualifierAnnotation typeQualifierAnnotation;
049:
050: PartialResult(AnnotatedObject annotatedObject,
051: TypeQualifierAnnotation typeQualifierAnnotation) {
052: this .annotatedObject = annotatedObject;
053: this .typeQualifierAnnotation = typeQualifierAnnotation;
054: }
055:
056: /**
057: * @return Returns the annotatedObject.
058: */
059: public AnnotatedObject getAnnotatedObject() {
060: return annotatedObject;
061: }
062:
063: /**
064: * @return Returns the typeQualifierAnnotation.
065: */
066: public TypeQualifierAnnotation getTypeQualifierAnnotation() {
067: return typeQualifierAnnotation;
068: }
069:
070: /* (non-Javadoc)
071: * @see java.lang.Object#toString()
072: */
073: @Override
074: public String toString() {
075: return annotatedObject + ":" + typeQualifierAnnotation;
076: }
077: }
078:
079: private List<PartialResult> partialResultList;
080:
081: TypeQualifierAnnotationLookupResult() {
082: this .partialResultList = new LinkedList<PartialResult>();
083: }
084:
085: void addPartialResult(PartialResult partialResult) {
086: partialResultList.add(partialResult);
087: }
088:
089: /**
090: * Get the effective TypeQualifierAnnotation.
091: *
092: * @return the effective TypeQualifierAnnotation,
093: * or null if no effective TypeQualifierAnnotation
094: * can be found
095: */
096: public @CheckForNull
097: TypeQualifierAnnotation getEffectiveTypeQualifierAnnotation() {
098: boolean firstPartialResult = true;
099: TypeQualifierAnnotation effective = null;
100:
101: for (PartialResult partialResult : partialResultList) {
102: if (firstPartialResult) {
103: effective = partialResult.getTypeQualifierAnnotation();
104: firstPartialResult = false;
105: } else {
106: effective = combine(effective, partialResult
107: .getTypeQualifierAnnotation());
108: }
109: }
110:
111: return effective;
112: }
113:
114: /**
115: * Subclasses must override this method to combine TypeQualifierAnnotations
116: * found in multiple superclasses.
117: *
118: * @param a a TypeQualifierAnnotation
119: * @param b another TypeQualifierAnnotation
120: * @return combined TypeQualifierAnnotation compatible with both input TypeQualifierAnnotations,
121: * or null if no such TypeQualifierAnnotation exists
122: */
123: protected TypeQualifierAnnotation combine(
124: TypeQualifierAnnotation a, TypeQualifierAnnotation b) {
125: return null;
126: }
127:
128: /* (non-Javadoc)
129: * @see java.lang.Object#toString()
130: */
131: @Override
132: public String toString() {
133: return partialResultList.toString();
134: }
135: }
|