gw.gosudoc.com.sun.tools.javadoc.main.AnnotationDescImpl Maven / Gradle / Ivy
Show all versions of gosu-doc Show documentation
/*
* This file is a shadowed version of the older javadoc codebase on which gosudoc is based; borrowed from jdk 9.
*/
package gw.gosudoc.com.sun.tools.javadoc.main;
import com.sun.tools.javac.code.Attribute;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.Pair;
import gw.gosudoc.com.sun.javadoc.AnnotationTypeDoc;
/**
* Represents an annotation.
* An annotation associates a value with each element of an annotation type.
* Sure it ought to be called "Annotation", but that clashes with
* java.lang.annotation.Annotation.
*
* This is NOT part of any supported API.
* If you write code that depends on this, you do so at your own risk.
* This code and its internal interfaces are subject to change or
* deletion without notice.
*
* @author Scott Seligman
* @since 1.5
*/
@Deprecated
public class AnnotationDescImpl implements gw.gosudoc.com.sun.javadoc.AnnotationDesc
{
private final gw.gosudoc.com.sun.tools.javadoc.main.DocEnv env;
private final Attribute.Compound annotation;
AnnotationDescImpl( gw.gosudoc.com.sun.tools.javadoc.main.DocEnv env, Attribute.Compound annotation) {
this.env = env;
this.annotation = annotation;
}
/**
* Returns the annotation type of this annotation.
*/
public gw.gosudoc.com.sun.javadoc.AnnotationTypeDoc annotationType() {
ClassSymbol atsym = (ClassSymbol)annotation.type.tsym;
if (annotation.type.isErroneous()) {
env.warning(null, "javadoc.class_not_found", annotation.type.toString());
return new AnnotationTypeDocImpl(env, atsym);
} else {
return (AnnotationTypeDoc)env.getClassDoc(atsym);
}
}
/**
* Returns this annotation's elements and their values.
* Only those explicitly present in the annotation are
* included, not those assuming their default values.
* Returns an empty array if there are none.
*/
public ElementValuePair[] elementValues() {
List> vals = annotation.values;
ElementValuePair res[] = new ElementValuePair[vals.length()];
int i = 0;
for (Pair val : vals) {
res[i++] = new ElementValuePairImpl(env, val.fst, val.snd);
}
return res;
}
/**
* Check for the synthesized bit on the annotation.
*
* @return true if the annotation is synthesized.
*/
public boolean isSynthesized() {
return annotation.isSynthesized();
}
/**
* Returns a string representation of this annotation.
* String is of one of the forms:
*
* {@code @com.example.foo(name1=val1, name2=val2)}
* {@code @com.example.foo(val)}
* {@code @com.example.foo}
*
* Omit parens for marker annotations, and omit "value=" when allowed.
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder("@");
sb.append(annotation.type.tsym);
ElementValuePair vals[] = elementValues();
if (vals.length > 0) { // omit parens for marker annotation
sb.append('(');
boolean first = true;
for (ElementValuePair val : vals) {
if (!first) {
sb.append(", ");
}
first = false;
String name = val.element().name();
if (vals.length == 1 && name.equals("value")) {
sb.append(val.value());
} else {
sb.append(val);
}
}
sb.append(')');
}
return sb.toString();
}
/**
* Represents an association between an annotation type element
* and one of its values.
*/
public static class ElementValuePairImpl implements ElementValuePair {
private final DocEnv env;
private final MethodSymbol meth;
private final Attribute value;
ElementValuePairImpl( DocEnv env, MethodSymbol meth, Attribute value) {
this.env = env;
this.meth = meth;
this.value = value;
}
/**
* Returns the annotation type element.
*/
public gw.gosudoc.com.sun.javadoc.AnnotationTypeElementDoc element() {
return env.getAnnotationTypeElementDoc(meth);
}
/**
* Returns the value associated with the annotation type element.
*/
public gw.gosudoc.com.sun.javadoc.AnnotationValue value() {
return new AnnotationValueImpl(env, value);
}
/**
* Returns a string representation of this pair
* of the form "name=value".
*/
@Override
public String toString() {
return meth.name + "=" + value();
}
}
}