• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.doclava;
18 
19 import java.util.ArrayList;
20 
21 public class AnnotationValueInfo implements Resolvable {
22   private Object mValue;
23   private MethodInfo mElement;
24   private String mInstanceName; // exists solely for resolving elements
25   private ArrayList<Resolution> mResolutions;
26 
AnnotationValueInfo()27   public AnnotationValueInfo() {
28       mElement = null;
29       mValue = null;
30       mInstanceName = null;
31   }
32 
AnnotationValueInfo(MethodInfo element)33   public AnnotationValueInfo(MethodInfo element) {
34       mElement = element;
35     }
36 
init(Object value)37   public void init(Object value) {
38     mValue = value;
39   }
40 
element()41   public MethodInfo element() {
42     return mElement;
43   }
44 
setElement(MethodInfo element)45   public void setElement(MethodInfo element) {
46       mElement = element;
47   }
48 
value()49   public Object value() {
50     return mValue;
51   }
52 
setAnnotationInstanceName(String instance)53   public void setAnnotationInstanceName(String instance) {
54       mInstanceName = instance;
55   }
56 
valueString()57   public String valueString() {
58     Object v = mValue;
59     if (v instanceof TypeInfo) {
60       return ((TypeInfo) v).fullName() + ".class";
61     } else if (v instanceof FieldInfo) {
62       StringBuilder str = new StringBuilder();
63       FieldInfo f = (FieldInfo) v;
64       str.append(f.containingClass().qualifiedName());
65       str.append('.');
66       str.append(f.name());
67       return str.toString();
68     } else if (v instanceof AnnotationInstanceInfo) {
69       return v.toString();
70     } else if (v instanceof ArrayList<?>) {
71       StringBuilder str = new StringBuilder();
72 
73       @SuppressWarnings("unchecked")
74       ArrayList<AnnotationValueInfo> values = (ArrayList<AnnotationValueInfo>) v;
75 
76       str.append("{");
77       for (AnnotationValueInfo info : values) {
78           str.append(info.valueString());
79           if (info != values.get(values.size()-1)) {
80             str.append(",");
81           }
82       }
83       str.append("}");
84       return str.toString();
85     } else {
86       return FieldInfo.constantLiteralValue(v);
87     }
88   }
89 
addResolution(Resolution resolution)90   public void addResolution(Resolution resolution) {
91       if (mResolutions == null) {
92           mResolutions = new ArrayList<Resolution>();
93       }
94 
95       mResolutions.add(resolution);
96   }
97 
printResolutions()98   public void printResolutions() {
99       System.out.println("Resolutions for Annotation Value:");
100       for (Resolution r : mResolutions) {
101           System.out.println(r);
102       }
103   }
104 
resolveResolutions()105   public boolean resolveResolutions() {
106       ArrayList<Resolution> resolutions = mResolutions;
107       mResolutions = new ArrayList<Resolution>();
108 
109       boolean allResolved = true;
110       for (Resolution resolution : resolutions) {
111           StringBuilder qualifiedClassName = new StringBuilder();
112           InfoBuilder.resolveQualifiedName(mInstanceName, qualifiedClassName,
113                   resolution.getInfoBuilder());
114 
115           // if we still couldn't resolve it, save it for the next pass
116           if ("".equals(qualifiedClassName.toString())) {
117               mResolutions.add(resolution);
118               allResolved = false;
119           } else if ("element".equals(resolution.getVariable())) {
120               ClassInfo annotation = InfoBuilder.Caches.obtainClass(qualifiedClassName.toString());
121               for (MethodInfo m : annotation.annotationElements()) {
122                   if (resolution.getValue().equals(m.name()) ||
123                           annotation.annotationElements().size() == 1) {
124                       mElement = m;
125                       break;
126                   }
127               }
128           }
129       }
130 
131       return allResolved;
132   }
133 }
134