Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 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 | |
Andrew Sapperstein | d6eaacb | 2011-05-20 13:14:56 -0700 | [diff] [blame] | 19 | import java.util.ArrayList; |
| 20 | import java.util.Arrays; |
| 21 | |
| 22 | public class AnnotationInstanceInfo implements Resolvable { |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 23 | private ClassInfo mType; |
Andrew Sapperstein | 6ba612e | 2011-06-20 18:41:24 -0700 | [diff] [blame^] | 24 | private String mAnnotationName; // for debugging purposes TODO - remove |
Andrew Sapperstein | d6eaacb | 2011-05-20 13:14:56 -0700 | [diff] [blame] | 25 | private ArrayList<AnnotationValueInfo> mElementValues; |
| 26 | private ArrayList<Resolution> mResolutions; |
| 27 | |
| 28 | public AnnotationInstanceInfo() { |
| 29 | mType = null; |
| 30 | mElementValues = new ArrayList<AnnotationValueInfo>(); |
| 31 | } |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 32 | |
| 33 | public AnnotationInstanceInfo(ClassInfo type, AnnotationValueInfo[] elementValues) { |
| 34 | mType = type; |
Andrew Sapperstein | d6eaacb | 2011-05-20 13:14:56 -0700 | [diff] [blame] | 35 | mElementValues = new ArrayList<AnnotationValueInfo>(Arrays.asList(elementValues)); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | ClassInfo type() { |
| 39 | return mType; |
| 40 | } |
| 41 | |
Andrew Sapperstein | d6eaacb | 2011-05-20 13:14:56 -0700 | [diff] [blame] | 42 | public void setClass(ClassInfo cl) { |
| 43 | mType = cl; |
| 44 | } |
| 45 | |
Andrew Sapperstein | 6ba612e | 2011-06-20 18:41:24 -0700 | [diff] [blame^] | 46 | public void setSimpleAnnotationName(String name) { |
| 47 | mAnnotationName = name; |
Andrew Sapperstein | d6eaacb | 2011-05-20 13:14:56 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | ArrayList<AnnotationValueInfo> elementValues() { |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 51 | return mElementValues; |
| 52 | } |
| 53 | |
Andrew Sapperstein | d6eaacb | 2011-05-20 13:14:56 -0700 | [diff] [blame] | 54 | public void addElementValue(AnnotationValueInfo info) { |
| 55 | mElementValues.add(info); |
| 56 | } |
| 57 | |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 58 | @Override |
| 59 | public String toString() { |
| 60 | StringBuilder str = new StringBuilder(); |
| 61 | str.append("@"); |
Andrew Sapperstein | d6eaacb | 2011-05-20 13:14:56 -0700 | [diff] [blame] | 62 | if (mType == null) { |
Andrew Sapperstein | 6ba612e | 2011-06-20 18:41:24 -0700 | [diff] [blame^] | 63 | str.append(mAnnotationName); |
Andrew Sapperstein | d6eaacb | 2011-05-20 13:14:56 -0700 | [diff] [blame] | 64 | } else { |
| 65 | str.append(mType.qualifiedName()); |
| 66 | } |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 67 | str.append("("); |
Andrew Sapperstein | d6eaacb | 2011-05-20 13:14:56 -0700 | [diff] [blame] | 68 | |
| 69 | for (AnnotationValueInfo value : mElementValues) { |
| 70 | if (value.element() != null) { |
| 71 | str.append(value.element().name()); |
| 72 | str.append("="); |
| 73 | } |
| 74 | |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 75 | str.append(value.valueString()); |
Andrew Sapperstein | d6eaacb | 2011-05-20 13:14:56 -0700 | [diff] [blame] | 76 | if (value != mElementValues.get(mElementValues.size()-1)) { |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 77 | str.append(","); |
| 78 | } |
| 79 | } |
| 80 | str.append(")"); |
| 81 | return str.toString(); |
| 82 | } |
Andrew Sapperstein | d6eaacb | 2011-05-20 13:14:56 -0700 | [diff] [blame] | 83 | |
| 84 | public void addResolution(Resolution resolution) { |
| 85 | if (mResolutions == null) { |
| 86 | mResolutions = new ArrayList<Resolution>(); |
| 87 | } |
| 88 | |
| 89 | mResolutions.add(resolution); |
| 90 | } |
| 91 | |
| 92 | public void printResolutions() { |
| 93 | System.out.println("Resolutions for Annotation:"); |
| 94 | for (Resolution r : mResolutions) { |
| 95 | System.out.println(r); |
| 96 | } |
| 97 | } |
Andrew Sapperstein | 6ba612e | 2011-06-20 18:41:24 -0700 | [diff] [blame^] | 98 | |
| 99 | public boolean resolveResolutions() { |
| 100 | ArrayList<Resolution> resolutions = mResolutions; |
| 101 | mResolutions = new ArrayList<Resolution>(); |
| 102 | |
| 103 | boolean allResolved = true; |
| 104 | for (Resolution resolution : resolutions) { |
| 105 | StringBuilder qualifiedClassName = new StringBuilder(); |
| 106 | InfoBuilder.resolveQualifiedName(resolution.getValue(), qualifiedClassName, |
| 107 | resolution.getInfoBuilder()); |
| 108 | |
| 109 | // if we still couldn't resolve it, save it for the next pass |
| 110 | if ("".equals(qualifiedClassName.toString())) { |
| 111 | mResolutions.add(resolution); |
| 112 | allResolved = false; |
| 113 | } else if ("annotationTypeName".equals(resolution.getVariable())) { |
| 114 | setClass(InfoBuilder.Caches.obtainClass(qualifiedClassName.toString())); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return allResolved; |
| 119 | } |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 120 | } |