下面列出了怎么用com.sun.javadoc.AnnotationTypeDoc的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Parse an annotation.
*
* @param annotationTypeDoc
* A AnnotationTypeDoc instance
* @return the annotation node
*/
protected Annotation parseAnnotationTypeDoc(AnnotationTypeDoc annotationTypeDoc) {
Annotation annotationNode = objectFactory.createAnnotation();
annotationNode.setName(annotationTypeDoc.name());
annotationNode.setQualified(annotationTypeDoc.qualifiedName());
String comment = annotationTypeDoc.commentText();
if (comment.length() > 0) {
annotationNode.setComment(comment);
}
annotationNode.setIncluded(annotationTypeDoc.isIncluded());
annotationNode.setScope(parseScope(annotationTypeDoc));
for (AnnotationTypeElementDoc annotationTypeElementDoc : annotationTypeDoc.elements()) {
annotationNode.getElement().add(parseAnnotationTypeElementDoc(annotationTypeElementDoc));
}
for (AnnotationDesc annotationDesc : annotationTypeDoc.annotations()) {
annotationNode.getAnnotation().add(parseAnnotationDesc(annotationDesc, annotationTypeDoc.qualifiedName()));
}
for (Tag tag : annotationTypeDoc.tags()) {
annotationNode.getTag().add(parseTag(tag));
}
return annotationNode;
}
public static PSOperatorWrapperDoc build(ClassDoc classDoc) {
AnnotationDesc[] annotations = classDoc.annotations();
for (AnnotationDesc annotation : annotations) {
AnnotationTypeDoc annotationType = annotation.annotationType();
if (Consts.OPERATOR_WRAPPER_ANNOTATION.equals(annotationType.toString())) {
return new PSOperatorWrapperDoc(classDoc);
}
}
return null;
}
public static PSPipelineDoc build(ClassDoc classDoc, MethodDoc methodDoc) {
AnnotationDesc[] annotations = methodDoc.annotations();
for (AnnotationDesc annotation : annotations) {
AnnotationTypeDoc annotationType = annotation.annotationType();
if (Consts.TRANSFORMATION_ANNOTATION.equals(annotationType.toString())) {
return new PSPipelineDoc(classDoc, methodDoc, annotation, TYPE_TRANSFORMATION);
}
else if (Consts.ACTION_ANNOTATION.equals(annotationType.toString())) {
return new PSPipelineDoc(classDoc, methodDoc, annotation, TYPE_ACTION);
}
}
return null;
}
public static PSItemFieldDoc build(PSItemDoc psItemDoc, FieldDoc fieldDoc) {
AnnotationDesc[] annotations = fieldDoc.annotations();
for (AnnotationDesc annotation : annotations) {
AnnotationTypeDoc annotationType = annotation.annotationType();
if (Consts.ITEM_FIELD_ANNOTATION.equals(annotationType.toString())) {
return new PSItemFieldDoc(psItemDoc, fieldDoc, annotation);
}
}
return null;
}
public static boolean isValidPSItem(ClassDoc classDoc) {
AnnotationDesc[] annotations = classDoc.annotations();
for (AnnotationDesc annotation : annotations) {
AnnotationTypeDoc annotationType = annotation.annotationType();
if (Consts.ITEM_ANNOTATION.equals(annotationType.toString())) {
return true;
}
}
return false;
}
@Override
public AnnotationTypeDoc wrap(AnnotationTypeDoc source) {
if (source == null || source instanceof Proxy<?> || !(source instanceof AnnotationTypeDocImpl)) {
return source;
}
return new AnnotationTypeDocWrapper((AnnotationTypeDocImpl) source);
}
/** Wrap a annotation type doc.
*
* @param source the source
* @return the wrapper.
*/
public AnnotationTypeDoc[] wrap(AnnotationTypeDoc[] source) {
if (source == null) {
return null;
}
final List<AnnotationTypeDoc> list = new ArrayList<>();
for (final AnnotationTypeDoc element : source) {
if (isIncluded(element)) {
list.add(wrap(element));
}
}
return Utils.toArray(source, list);
}
/**
* The entry point into parsing the javadoc.
*
* @param rootDoc
* The RootDoc intstance obtained via the doclet API
* @return The root node, containing everything parsed from javadoc doclet
*/
public Root parseRootDoc(RootDoc rootDoc) {
Root rootNode = objectFactory.createRoot();
for (ClassDoc classDoc : rootDoc.classes()) {
PackageDoc packageDoc = classDoc.containingPackage();
Package packageNode = packages.get(packageDoc.name());
if (packageNode == null) {
packageNode = parsePackage(packageDoc);
packages.put(packageDoc.name(), packageNode);
rootNode.getPackage().add(packageNode);
}
if (classDoc instanceof AnnotationTypeDoc) {
packageNode.getAnnotation().add(parseAnnotationTypeDoc((AnnotationTypeDoc) classDoc));
} else if (classDoc.isEnum()) {
packageNode.getEnum().add(parseEnum(classDoc));
} else if (classDoc.isInterface()) {
packageNode.getInterface().add(parseInterface(classDoc));
} else {
packageNode.getClazz().add(parseClass(classDoc));
}
}
return rootNode;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
String methodName = method.getName();
if (target instanceof Doc) {
if (methodName.equals("isIncluded")) {
Doc doc = (Doc) target;
return !exclude(doc) && doc.isIncluded();
}
if (target instanceof RootDoc) {
if (methodName.equals("classes")) {
return filter(((RootDoc) target).classes(), ClassDoc.class);
} else if (methodName.equals("specifiedClasses")) {
return filter(((RootDoc) target).specifiedClasses(), ClassDoc.class);
} else if (methodName.equals("specifiedPackages")) {
return filter(((RootDoc) target).specifiedPackages(), PackageDoc.class);
}
} else if (target instanceof ClassDoc) {
if (isFiltered(args)) {
if (methodName.equals("methods")) {
return filter(((ClassDoc) target).methods(true), MethodDoc.class);
} else if (methodName.equals("fields")) {
return filter(((ClassDoc) target).fields(true), FieldDoc.class);
} else if (methodName.equals("innerClasses")) {
return filter(((ClassDoc) target).innerClasses(true),
ClassDoc.class);
} else if (methodName.equals("constructors")) {
return filter(((ClassDoc) target).constructors(true),
ConstructorDoc.class);
}
}
} else if (target instanceof PackageDoc) {
if (methodName.equals("allClasses")) {
if (isFiltered(args)) {
return filter(((PackageDoc) target).allClasses(true),
ClassDoc.class);
} else {
return filter(((PackageDoc) target).allClasses(), ClassDoc.class);
}
} else if (methodName.equals("annotationTypes")) {
return filter(((PackageDoc) target).annotationTypes(),
AnnotationTypeDoc.class);
} else if (methodName.equals("enums")) {
return filter(((PackageDoc) target).enums(),
ClassDoc.class);
} else if (methodName.equals("errors")) {
return filter(((PackageDoc) target).errors(),
ClassDoc.class);
} else if (methodName.equals("exceptions")) {
return filter(((PackageDoc) target).exceptions(),
ClassDoc.class);
} else if (methodName.equals("interfaces")) {
return filter(((PackageDoc) target).interfaces(),
ClassDoc.class);
} else if (methodName.equals("ordinaryClasses")) {
return filter(((PackageDoc) target).ordinaryClasses(),
ClassDoc.class);
}
}
}
if (args != null) {
if (methodName.equals("compareTo") || methodName.equals("equals")
|| methodName.equals("overrides")
|| methodName.equals("subclassOf")) {
args[0] = unwrap(args[0]);
}
}
try {
return process(method.invoke(target, args), method.getReturnType());
} catch (InvocationTargetException e) {
throw e.getTargetException();
}
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
String methodName = method.getName();
if (target instanceof Doc) {
if (methodName.equals("isIncluded")) {
Doc doc = (Doc) target;
return !exclude(doc) && doc.isIncluded();
}
if (target instanceof RootDoc) {
if (methodName.equals("classes")) {
return filter(((RootDoc) target).classes(), ClassDoc.class);
} else if (methodName.equals("specifiedClasses")) {
return filter(((RootDoc) target).specifiedClasses(), ClassDoc.class);
} else if (methodName.equals("specifiedPackages")) {
return filter(((RootDoc) target).specifiedPackages(), PackageDoc.class);
}
} else if (target instanceof ClassDoc) {
if (isFiltered(args)) {
if (methodName.equals("methods")) {
return filter(((ClassDoc) target).methods(true), MethodDoc.class);
} else if (methodName.equals("fields")) {
return filter(((ClassDoc) target).fields(true), FieldDoc.class);
} else if (methodName.equals("innerClasses")) {
return filter(((ClassDoc) target).innerClasses(true),
ClassDoc.class);
} else if (methodName.equals("constructors")) {
return filter(((ClassDoc) target).constructors(true),
ConstructorDoc.class);
}
}
} else if (target instanceof PackageDoc) {
if (methodName.equals("allClasses")) {
if (isFiltered(args)) {
return filter(((PackageDoc) target).allClasses(true),
ClassDoc.class);
} else {
return filter(((PackageDoc) target).allClasses(), ClassDoc.class);
}
} else if (methodName.equals("annotationTypes")) {
return filter(((PackageDoc) target).annotationTypes(),
AnnotationTypeDoc.class);
} else if (methodName.equals("enums")) {
return filter(((PackageDoc) target).enums(),
ClassDoc.class);
} else if (methodName.equals("errors")) {
return filter(((PackageDoc) target).errors(),
ClassDoc.class);
} else if (methodName.equals("exceptions")) {
return filter(((PackageDoc) target).exceptions(),
ClassDoc.class);
} else if (methodName.equals("interfaces")) {
return filter(((PackageDoc) target).interfaces(),
ClassDoc.class);
} else if (methodName.equals("ordinaryClasses")) {
return filter(((PackageDoc) target).ordinaryClasses(),
ClassDoc.class);
}
}
}
if (args != null) {
if (methodName.equals("compareTo") || methodName.equals("equals")
|| methodName.equals("overrides")
|| methodName.equals("subclassOf")) {
args[0] = unwrap(args[0]);
}
}
try {
return process(method.invoke(target, args), method.getReturnType());
} catch (InvocationTargetException e) {
throw e.getTargetException();
}
}
@Override
public AnnotationTypeDoc wrap(AnnotationTypeDoc obj) {
return obj;
}
@Override
public AnnotationTypeDoc wrap(AnnotationTypeDoc obj) {
return (AnnotationTypeDoc) wrap((Object) obj);
}
@Override
public AnnotationTypeDoc asAnnotationTypeDoc() {
return wrap(this.delegate.asAnnotationTypeDoc());
}
@Override
public AnnotationTypeDoc[] annotationTypes() {
return wrap(this.delegate.annotationTypes());
}
@Override
public AnnotationTypeDoc asAnnotationTypeDoc() {
return wrap(this.delegate.asAnnotationTypeDoc());
}
@Override
public AnnotationTypeWriter getAnnotationTypeWriter(AnnotationTypeDoc annotationType, Type prevType, Type nextType)
throws Exception {
return new SarlAnnotationTypeWriter(this.configuration, annotationType, prevType, nextType);
}
/**
* Parses annotation instances of an annotable program element
*
* @param annotationDesc
* annotationDesc
* @param programElement
* programElement
* @return representation of annotations
*/
protected AnnotationInstance parseAnnotationDesc(AnnotationDesc annotationDesc, String programElement) {
AnnotationInstance annotationInstanceNode = objectFactory.createAnnotationInstance();
try {
AnnotationTypeDoc annotTypeInfo = annotationDesc.annotationType();
annotationInstanceNode.setName(annotTypeInfo.name());
annotationInstanceNode.setQualified(annotTypeInfo.qualifiedTypeName());
} catch (ClassCastException castException) {
log.error("Unable to obtain type data about an annotation found on: " + programElement);
log.error("Add to the classpath the class/jar that defines this annotation.");
}
for (AnnotationDesc.ElementValuePair elementValuesPair : annotationDesc.elementValues()) {
AnnotationArgument annotationArgumentNode = objectFactory.createAnnotationArgument();
annotationArgumentNode.setName(elementValuesPair.element().name());
Type annotationArgumentType = elementValuesPair.element().returnType();
annotationArgumentNode.setType(parseTypeInfo(annotationArgumentType));
annotationArgumentNode.setPrimitive(annotationArgumentType.isPrimitive());
annotationArgumentNode.setArray(annotationArgumentType.dimension().length() > 0);
Object objValue = elementValuesPair.value().value();
if (objValue instanceof AnnotationValue[]) {
for (AnnotationValue annotationValue : (AnnotationValue[]) objValue) {
if (annotationValue.value() instanceof AnnotationDesc) {
AnnotationDesc annoDesc = (AnnotationDesc) annotationValue.value();
annotationArgumentNode.getAnnotation().add(parseAnnotationDesc(annoDesc, programElement));
} else {
annotationArgumentNode.getValue().add(annotationValue.value().toString());
}
}
} else if (objValue instanceof FieldDoc) {
annotationArgumentNode.getValue().add(((FieldDoc) objValue).name());
} else if (objValue instanceof ClassDoc) {
annotationArgumentNode.getValue().add(((ClassDoc) objValue).qualifiedTypeName());
} else {
annotationArgumentNode.getValue().add(objValue.toString());
}
annotationInstanceNode.getArgument().add(annotationArgumentNode);
}
return annotationInstanceNode;
}
/** Wrap the given annotation type documentation.
*
* @param obj the document to filter.
* @return the filtered {@code obj}.
*/
AnnotationTypeDoc wrap(AnnotationTypeDoc obj);
/** Constructor.
* @param configuration the configuration.
* @param annotationType the type.
* @param prevType the previous type.
* @param nextType the next type.
* @throws Exception in case of error.
*/
public SarlAnnotationTypeWriter(ConfigurationImpl configuration, AnnotationTypeDoc annotationType, Type prevType,
Type nextType) throws Exception {
super(configuration, annotationType, prevType, nextType);
}