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
17package com.google.doclava;
18
19import com.google.clearsilver.jsilver.data.Data;
20
21import java.util.ArrayList;
22import java.util.Arrays;
23
24public class AnnotationInstanceInfo implements Resolvable {
25  private ClassInfo mType;
26  private String mAnnotationName; // for debugging purposes TODO - remove
27  private ArrayList<AnnotationValueInfo> mElementValues;
28  private ArrayList<Resolution> mResolutions;
29
30  public AnnotationInstanceInfo() {
31      mType = null;
32      mElementValues = new ArrayList<AnnotationValueInfo>();
33    }
34
35  public AnnotationInstanceInfo(ClassInfo type, AnnotationValueInfo[] elementValues) {
36    mType = type;
37    mElementValues = new ArrayList<AnnotationValueInfo>(Arrays.asList(elementValues));
38  }
39
40  ClassInfo type() {
41    return mType;
42  }
43
44  public void setClass(ClassInfo cl) {
45      mType = cl;
46  }
47
48  public void setSimpleAnnotationName(String name) {
49      mAnnotationName = name;
50  }
51
52  ArrayList<AnnotationValueInfo> elementValues() {
53    return mElementValues;
54  }
55
56  public void addElementValue(AnnotationValueInfo info) {
57      mElementValues.add(info);
58  }
59
60  @Override
61  public String toString() {
62    StringBuilder str = new StringBuilder();
63    str.append("@");
64    if (mType == null) {
65        str.append(mAnnotationName);
66    } else {
67        str.append(mType.qualifiedName());
68    }
69    str.append("(");
70
71    for (AnnotationValueInfo value : mElementValues) {
72      if (value.element() != null) {
73          str.append(value.element().name());
74          str.append("=");
75      }
76
77      str.append(value.valueString());
78      if (value != mElementValues.get(mElementValues.size()-1)) {
79        str.append(",");
80      }
81    }
82    str.append(")");
83    return str.toString();
84  }
85
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  }
100
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  }
122
123  public static void makeLinkListHDF(Data data, String base, AnnotationInstanceInfo[] annotations) {
124    if (annotations == null) return;
125
126    final int N = annotations.length;
127    for (int i = 0; i < N; i++) {
128      AnnotationInstanceInfo aii = annotations[i];
129      aii.type().makeShortDescrHDF(data, base + "." + i);
130    }
131  }
132
133  /**
134   * Get a new list containing the set of annotations that are shared between
135   * the input annotations collection and the names of annotations passed in
136   * the showAnnotations parameter
137   */
138  public static ArrayList<AnnotationInstanceInfo> getShowAnnotationsIntersection(
139          ArrayList<AnnotationInstanceInfo> annotations) {
140    ArrayList<AnnotationInstanceInfo> list = new ArrayList<AnnotationInstanceInfo>();
141    if (annotations != null) {
142      for (AnnotationInstanceInfo info : annotations) {
143        if (Doclava.showAnnotations.contains(info.type().qualifiedName())) {
144          list.add(info);
145        }
146      }
147    }
148    return list;
149  }
150}
151