Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/javadoc/AnnotationDescImpl.java
38899 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.javadoc;2627import com.sun.javadoc.*;2829import com.sun.tools.javac.code.Attribute;30import com.sun.tools.javac.code.Symbol.*;31import com.sun.tools.javac.util.List;32import com.sun.tools.javac.util.Pair;333435/**36* Represents an annotation.37* An annotation associates a value with each element of an annotation type.38* Sure it ought to be called "Annotation", but that clashes with39* java.lang.annotation.Annotation.40*41* <p><b>This is NOT part of any supported API.42* If you write code that depends on this, you do so at your own risk.43* This code and its internal interfaces are subject to change or44* deletion without notice.</b>45*46* @author Scott Seligman47* @since 1.548*/4950public class AnnotationDescImpl implements AnnotationDesc {5152private final DocEnv env;53private final Attribute.Compound annotation;545556AnnotationDescImpl(DocEnv env, Attribute.Compound annotation) {57this.env = env;58this.annotation = annotation;59}6061/**62* Returns the annotation type of this annotation.63*/64public AnnotationTypeDoc annotationType() {65ClassSymbol atsym = (ClassSymbol)annotation.type.tsym;66if (annotation.type.isErroneous()) {67env.warning(null, "javadoc.class_not_found", annotation.type.toString());68return new AnnotationTypeDocImpl(env, atsym);69} else {70return (AnnotationTypeDoc)env.getClassDoc(atsym);71}72}7374/**75* Returns this annotation's elements and their values.76* Only those explicitly present in the annotation are77* included, not those assuming their default values.78* Returns an empty array if there are none.79*/80public ElementValuePair[] elementValues() {81List<Pair<MethodSymbol,Attribute>> vals = annotation.values;82ElementValuePair res[] = new ElementValuePair[vals.length()];83int i = 0;84for (Pair<MethodSymbol,Attribute> val : vals) {85res[i++] = new ElementValuePairImpl(env, val.fst, val.snd);86}87return res;88}8990/**91* Check for the synthesized bit on the annotation.92*93* @return true if the annotation is synthesized.94*/95public boolean isSynthesized() {96return annotation.isSynthesized();97}9899/**100* Returns a string representation of this annotation.101* String is of one of the forms:102* @com.example.foo(name1=val1, name2=val2)103* @com.example.foo(val)104* @com.example.foo105* Omit parens for marker annotations, and omit "value=" when allowed.106*/107@Override108public String toString() {109StringBuilder sb = new StringBuilder("@");110sb.append(annotation.type.tsym);111112ElementValuePair vals[] = elementValues();113if (vals.length > 0) { // omit parens for marker annotation114sb.append('(');115boolean first = true;116for (ElementValuePair val : vals) {117if (!first) {118sb.append(", ");119}120first = false;121122String name = val.element().name();123if (vals.length == 1 && name.equals("value")) {124sb.append(val.value());125} else {126sb.append(val);127}128}129sb.append(')');130}131return sb.toString();132}133134135/**136* Represents an association between an annotation type element137* and one of its values.138*/139public static class ElementValuePairImpl implements ElementValuePair {140141private final DocEnv env;142private final MethodSymbol meth;143private final Attribute value;144145ElementValuePairImpl(DocEnv env, MethodSymbol meth, Attribute value) {146this.env = env;147this.meth = meth;148this.value = value;149}150151/**152* Returns the annotation type element.153*/154public AnnotationTypeElementDoc element() {155return env.getAnnotationTypeElementDoc(meth);156}157158/**159* Returns the value associated with the annotation type element.160*/161public AnnotationValue value() {162return new AnnotationValueImpl(env, value);163}164165/**166* Returns a string representation of this pair167* of the form "name=value".168*/169@Override170public String toString() {171return meth.name + "=" + value();172}173}174}175176177