blob: d35342630f24562a4d49fc87954d55e1b5391269 [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
Jeff Arneson051dace2014-08-12 15:22:00 -070019import com.google.clearsilver.jsilver.data.Data;
20
Andrew Sappersteind6eaacb2011-05-20 13:14:56 -070021import java.util.ArrayList;
22import java.util.Arrays;
23
24public class AnnotationInstanceInfo implements Resolvable {
Ben Dodson920dbbb2010-08-04 15:21:06 -070025 private ClassInfo mType;
Andrew Sapperstein6ba612e2011-06-20 18:41:24 -070026 private String mAnnotationName; // for debugging purposes TODO - remove
Andrew Sappersteind6eaacb2011-05-20 13:14:56 -070027 private ArrayList<AnnotationValueInfo> mElementValues;
28 private ArrayList<Resolution> mResolutions;
29
30 public AnnotationInstanceInfo() {
31 mType = null;
32 mElementValues = new ArrayList<AnnotationValueInfo>();
33 }
Ben Dodson920dbbb2010-08-04 15:21:06 -070034
35 public AnnotationInstanceInfo(ClassInfo type, AnnotationValueInfo[] elementValues) {
36 mType = type;
Andrew Sappersteind6eaacb2011-05-20 13:14:56 -070037 mElementValues = new ArrayList<AnnotationValueInfo>(Arrays.asList(elementValues));
Ben Dodson920dbbb2010-08-04 15:21:06 -070038 }
39
40 ClassInfo type() {
41 return mType;
42 }
43
Andrew Sappersteind6eaacb2011-05-20 13:14:56 -070044 public void setClass(ClassInfo cl) {
45 mType = cl;
46 }
47
Andrew Sapperstein6ba612e2011-06-20 18:41:24 -070048 public void setSimpleAnnotationName(String name) {
49 mAnnotationName = name;
Andrew Sappersteind6eaacb2011-05-20 13:14:56 -070050 }
51
52 ArrayList<AnnotationValueInfo> elementValues() {
Ben Dodson920dbbb2010-08-04 15:21:06 -070053 return mElementValues;
54 }
55
Andrew Sappersteind6eaacb2011-05-20 13:14:56 -070056 public void addElementValue(AnnotationValueInfo info) {
57 mElementValues.add(info);
58 }
59
Ben Dodson920dbbb2010-08-04 15:21:06 -070060 @Override
61 public String toString() {
62 StringBuilder str = new StringBuilder();
63 str.append("@");
Andrew Sappersteind6eaacb2011-05-20 13:14:56 -070064 if (mType == null) {
Andrew Sapperstein6ba612e2011-06-20 18:41:24 -070065 str.append(mAnnotationName);
Andrew Sappersteind6eaacb2011-05-20 13:14:56 -070066 } else {
67 str.append(mType.qualifiedName());
68 }
Ben Dodson920dbbb2010-08-04 15:21:06 -070069 str.append("(");
Andrew Sappersteind6eaacb2011-05-20 13:14:56 -070070
71 for (AnnotationValueInfo value : mElementValues) {
72 if (value.element() != null) {
73 str.append(value.element().name());
74 str.append("=");
75 }
76
Ben Dodson920dbbb2010-08-04 15:21:06 -070077 str.append(value.valueString());
Andrew Sappersteind6eaacb2011-05-20 13:14:56 -070078 if (value != mElementValues.get(mElementValues.size()-1)) {
Ben Dodson920dbbb2010-08-04 15:21:06 -070079 str.append(",");
80 }
81 }
82 str.append(")");
83 return str.toString();
84 }
Andrew Sappersteind6eaacb2011-05-20 13:14:56 -070085
86 public void addResolution(Resolution resolution) {
87 if (mResolutions == null) {
88 mResolutions = new ArrayList<Resolution>();
89 }
90
91 mResolutions.add(resolution);
92 }
93
94 public void printResolutions() {
95 System.out.println("Resolutions for Annotation:");
96 for (Resolution r : mResolutions) {
97 System.out.println(r);
98 }
99 }
Andrew Sapperstein6ba612e2011-06-20 18:41:24 -0700100
101 public boolean resolveResolutions() {
102 ArrayList<Resolution> resolutions = mResolutions;
103 mResolutions = new ArrayList<Resolution>();
104
105 boolean allResolved = true;
106 for (Resolution resolution : resolutions) {
107 StringBuilder qualifiedClassName = new StringBuilder();
108 InfoBuilder.resolveQualifiedName(resolution.getValue(), qualifiedClassName,
109 resolution.getInfoBuilder());
110
111 // if we still couldn't resolve it, save it for the next pass
112 if ("".equals(qualifiedClassName.toString())) {
113 mResolutions.add(resolution);
114 allResolved = false;
115 } else if ("annotationTypeName".equals(resolution.getVariable())) {
116 setClass(InfoBuilder.Caches.obtainClass(qualifiedClassName.toString()));
117 }
118 }
119
120 return allResolved;
121 }
Jeff Arneson051dace2014-08-12 15:22:00 -0700122
Omari Stephens274a19e2015-06-04 21:29:09 -0700123 /**
124 * Convert the specified list of {@code AnnotationInstanceInfo} into an HDF-formatted list, and
125 * add the HDF list into the specified {@code Data}.
126 */
Jeff Arneson051dace2014-08-12 15:22:00 -0700127 public static void makeLinkListHDF(Data data, String base, AnnotationInstanceInfo[] annotations) {
128 if (annotations == null) return;
129
130 final int N = annotations.length;
131 for (int i = 0; i < N; i++) {
132 AnnotationInstanceInfo aii = annotations[i];
Omari Stephens274a19e2015-06-04 21:29:09 -0700133 final String aiiBase = base + "." + i;
134
135 // Serialize data about the annotation element values
136 for (int elemIdx = 0; elemIdx < aii.elementValues().size(); ++elemIdx) {
137 final String elemBase = aiiBase + ".elementValues." + elemIdx;
138 final AnnotationValueInfo value = aii.elementValues().get(elemIdx);
139 data.setValue(elemBase + ".name", value.element().name());
140 data.setValue(elemBase + ".value", value.valueString());
141 }
142
143 aii.type().makeShortDescrHDF(data, aiiBase);
Jeff Arneson051dace2014-08-12 15:22:00 -0700144 }
145 }
146
147 /**
148 * Get a new list containing the set of annotations that are shared between
149 * the input annotations collection and the names of annotations passed in
150 * the showAnnotations parameter
151 */
152 public static ArrayList<AnnotationInstanceInfo> getShowAnnotationsIntersection(
153 ArrayList<AnnotationInstanceInfo> annotations) {
154 ArrayList<AnnotationInstanceInfo> list = new ArrayList<AnnotationInstanceInfo>();
155 if (annotations != null) {
156 for (AnnotationInstanceInfo info : annotations) {
157 if (Doclava.showAnnotations.contains(info.type().qualifiedName())) {
158 list.add(info);
159 }
160 }
161 }
162 return list;
163 }
Ben Dodson920dbbb2010-08-04 15:21:06 -0700164}