blob: 09a9dff336edd96835626c78397cbf1f81dbd270 [file] [log] [blame]
Ben Dodson920dbbb2010-08-04 15:21:06 -07001/*
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
17package com.google.doclava;
18
Andrew Sappersteind6eaacb2011-05-20 13:14:56 -070019import java.util.ArrayList;
20
21public class AnnotationValueInfo implements Resolvable {
Ben Dodson920dbbb2010-08-04 15:21:06 -070022 private Object mValue;
Ben Dodson920dbbb2010-08-04 15:21:06 -070023 private MethodInfo mElement;
Andrew Sapperstein6ba612e2011-06-20 18:41:24 -070024 private String mInstanceName; // exists solely for resolving elements
Andrew Sappersteind6eaacb2011-05-20 13:14:56 -070025 private ArrayList<Resolution> mResolutions;
26
27 public AnnotationValueInfo() {
Andrew Sapperstein6ba612e2011-06-20 18:41:24 -070028 mElement = null;
29 mValue = null;
30 mInstanceName = null;
Andrew Sappersteind6eaacb2011-05-20 13:14:56 -070031 }
Ben Dodson920dbbb2010-08-04 15:21:06 -070032
33 public AnnotationValueInfo(MethodInfo element) {
Andrew Sappersteind6eaacb2011-05-20 13:14:56 -070034 mElement = element;
35 }
Ben Dodson920dbbb2010-08-04 15:21:06 -070036
37 public void init(Object value) {
38 mValue = value;
39 }
40
41 public MethodInfo element() {
42 return mElement;
43 }
44
Andrew Sappersteind6eaacb2011-05-20 13:14:56 -070045 public void setElement(MethodInfo element) {
46 mElement = element;
47 }
48
Ben Dodson920dbbb2010-08-04 15:21:06 -070049 public Object value() {
50 return mValue;
51 }
52
Andrew Sapperstein6ba612e2011-06-20 18:41:24 -070053 public void setAnnotationInstanceName(String instance) {
54 mInstanceName = instance;
55 }
56
Ben Dodson920dbbb2010-08-04 15:21:06 -070057 public String valueString() {
58 Object v = mValue;
59 if (v instanceof TypeInfo) {
60 return ((TypeInfo) v).fullName();
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();
Andrew Sappersteind6eaacb2011-05-20 13:14:56 -070070 } else if (v instanceof ArrayList<?>) {
Ben Dodson920dbbb2010-08-04 15:21:06 -070071 StringBuilder str = new StringBuilder();
Andrew Sappersteind6eaacb2011-05-20 13:14:56 -070072
73 @SuppressWarnings("unchecked")
74 ArrayList<AnnotationValueInfo> values = (ArrayList<AnnotationValueInfo>) v;
75
Ben Dodson920dbbb2010-08-04 15:21:06 -070076 str.append("{");
Andrew Sappersteind6eaacb2011-05-20 13:14:56 -070077 for (AnnotationValueInfo info : values) {
78 str.append(info.valueString());
79 if (info != values.get(values.size()-1)) {
80 str.append(",");
81 }
Ben Dodson920dbbb2010-08-04 15:21:06 -070082 }
83 str.append("}");
84 return str.toString();
85 } else {
86 return FieldInfo.constantLiteralValue(v);
87 }
88 }
Andrew Sappersteind6eaacb2011-05-20 13:14:56 -070089
90 public void addResolution(Resolution resolution) {
91 if (mResolutions == null) {
92 mResolutions = new ArrayList<Resolution>();
93 }
94
95 mResolutions.add(resolution);
96 }
97
98 public void printResolutions() {
99 System.out.println("Resolutions for Annotation Value:");
100 for (Resolution r : mResolutions) {
101 System.out.println(r);
102 }
103 }
Andrew Sapperstein6ba612e2011-06-20 18:41:24 -0700104
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 }
Ben Dodson920dbbb2010-08-04 15:21:06 -0700133}