1/* 2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package com.sun.tools.javadoc.main; 27 28import com.sun.javadoc.*; 29 30import com.sun.tools.javac.code.Attribute; 31import com.sun.tools.javac.code.Symbol.*; 32import com.sun.tools.javac.util.List; 33import com.sun.tools.javac.util.Pair; 34 35 36/** 37 * Represents an annotation. 38 * An annotation associates a value with each element of an annotation type. 39 * Sure it ought to be called "Annotation", but that clashes with 40 * java.lang.annotation.Annotation. 41 * 42 * <p><b>This is NOT part of any supported API. 43 * If you write code that depends on this, you do so at your own risk. 44 * This code and its internal interfaces are subject to change or 45 * deletion without notice.</b> 46 * 47 * @author Scott Seligman 48 * @since 1.5 49 */ 50 51@Deprecated 52public class AnnotationDescImpl implements AnnotationDesc { 53 54 private final DocEnv env; 55 private final Attribute.Compound annotation; 56 57 58 AnnotationDescImpl(DocEnv env, Attribute.Compound annotation) { 59 this.env = env; 60 this.annotation = annotation; 61 } 62 63 /** 64 * Returns the annotation type of this annotation. 65 */ 66 public AnnotationTypeDoc annotationType() { 67 ClassSymbol atsym = (ClassSymbol)annotation.type.tsym; 68 if (annotation.type.isErroneous()) { 69 env.warning(null, "javadoc.class_not_found", annotation.type.toString()); 70 return new AnnotationTypeDocImpl(env, atsym); 71 } else { 72 return (AnnotationTypeDoc)env.getClassDoc(atsym); 73 } 74 } 75 76 /** 77 * Returns this annotation's elements and their values. 78 * Only those explicitly present in the annotation are 79 * included, not those assuming their default values. 80 * Returns an empty array if there are none. 81 */ 82 public ElementValuePair[] elementValues() { 83 List<Pair<MethodSymbol,Attribute>> vals = annotation.values; 84 ElementValuePair res[] = new ElementValuePair[vals.length()]; 85 int i = 0; 86 for (Pair<MethodSymbol,Attribute> val : vals) { 87 res[i++] = new ElementValuePairImpl(env, val.fst, val.snd); 88 } 89 return res; 90 } 91 92 /** 93 * Check for the synthesized bit on the annotation. 94 * 95 * @return true if the annotation is synthesized. 96 */ 97 public boolean isSynthesized() { 98 return annotation.isSynthesized(); 99 } 100 101 /** 102 * Returns a string representation of this annotation. 103 * String is of one of the forms: 104 * <pre> 105 * {@code @com.example.foo(name1=val1, name2=val2)} 106 * {@code @com.example.foo(val)} 107 * {@code @com.example.foo} 108 * </pre> 109 * Omit parens for marker annotations, and omit "value=" when allowed. 110 */ 111 @Override 112 public String toString() { 113 StringBuilder sb = new StringBuilder("@"); 114 sb.append(annotation.type.tsym); 115 116 ElementValuePair vals[] = elementValues(); 117 if (vals.length > 0) { // omit parens for marker annotation 118 sb.append('('); 119 boolean first = true; 120 for (ElementValuePair val : vals) { 121 if (!first) { 122 sb.append(", "); 123 } 124 first = false; 125 126 String name = val.element().name(); 127 if (vals.length == 1 && name.equals("value")) { 128 sb.append(val.value()); 129 } else { 130 sb.append(val); 131 } 132 } 133 sb.append(')'); 134 } 135 return sb.toString(); 136 } 137 138 139 /** 140 * Represents an association between an annotation type element 141 * and one of its values. 142 */ 143 public static class ElementValuePairImpl implements ElementValuePair { 144 145 private final DocEnv env; 146 private final MethodSymbol meth; 147 private final Attribute value; 148 149 ElementValuePairImpl(DocEnv env, MethodSymbol meth, Attribute value) { 150 this.env = env; 151 this.meth = meth; 152 this.value = value; 153 } 154 155 /** 156 * Returns the annotation type element. 157 */ 158 public AnnotationTypeElementDoc element() { 159 return env.getAnnotationTypeElementDoc(meth); 160 } 161 162 /** 163 * Returns the value associated with the annotation type element. 164 */ 165 public AnnotationValue value() { 166 return new AnnotationValueImpl(env, value); 167 } 168 169 /** 170 * Returns a string representation of this pair 171 * of the form "name=value". 172 */ 173 @Override 174 public String toString() { 175 return meth.name + "=" + value(); 176 } 177 } 178} 179