下面列出了怎么用com.sun.javadoc.AnnotationDesc的API类实例代码及写法,或者点击链接到github查看源代码。
private static String processClass(
ClassDoc classDoc,
StringBuilder pathRoot) {
String defaultRequestMethod = null;
for (AnnotationDesc annotationDesc : classDoc.annotations()) {
if (REQUEST_MAPPING.equals(annotationDesc.annotationType().qualifiedTypeName())) {
for (AnnotationDesc.ElementValuePair pair : annotationDesc.elementValues()) {
if (VALUE.equals(pair.element().name()) || PATH.equals(pair.element().name())) {
setRoot(pathRoot, pair);
}
if (METHOD.equals(pair.element().name())) {
defaultRequestMethod = pair.value().toString();
}
}
break;
}
}
return defaultRequestMethod;
}
private static String getRequestMethod(
AnnotationDesc annotationDesc,
String name,
String defaultRequestMethod) {
if (REQUEST_MAPPING.equals(name)) {
for (AnnotationDesc.ElementValuePair pair : annotationDesc.elementValues()) {
if (METHOD.equals(pair.element().name())) {
return resolveRequestMethod(pair, defaultRequestMethod);
}
}
} else if (PUT_MAPPING.equals(name)) {
return "PUT";
} else if (POST_MAPPING.equals(name)) {
return "POST";
} else if (PATCH_MAPPING.equals(name)) {
return "PATCH";
} else if (GET_MAPPING.equals(name)) {
return "GET";
} else if (DELETE_MAPPING.equals(name)) {
return "DELETE";
}
return defaultRequestMethod;
}
private static boolean exclude( Doc doc )
{
AnnotationDesc[] annotations = null;
if ( doc instanceof ProgramElementDoc )
{
annotations = ( (ProgramElementDoc) doc ).annotations();
}
else if ( doc instanceof PackageDoc )
{
annotations = ( (PackageDoc) doc ).annotations();
}
if ( annotations != null )
{
for ( AnnotationDesc annotation : annotations )
{
String qualifiedTypeName = annotation.annotationType().qualifiedTypeName();
if ( qualifiedTypeName.equals( JavadocExclude.class.getCanonicalName() ) )
{
return true;
}
}
}
// nothing above found a reason to exclude
return false;
}
/**
* 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;
}
/**
* Parses an enum type definition
*
* @param fieldDoc
* @return
*/
protected EnumConstant parseEnumConstant(FieldDoc fieldDoc) {
EnumConstant enumConstant = objectFactory.createEnumConstant();
enumConstant.setName(fieldDoc.name());
String comment = fieldDoc.commentText();
if (comment.length() > 0) {
enumConstant.setComment(comment);
}
for (AnnotationDesc annotationDesc : fieldDoc.annotations()) {
enumConstant.getAnnotation().add(parseAnnotationDesc(annotationDesc, fieldDoc.qualifiedName()));
}
for (Tag tag : fieldDoc.tags()) {
enumConstant.getTag().add(parseTag(tag));
}
return enumConstant;
}
protected Field parseField(FieldDoc fieldDoc) {
Field fieldNode = objectFactory.createField();
fieldNode.setType(parseTypeInfo(fieldDoc.type()));
fieldNode.setName(fieldDoc.name());
fieldNode.setQualified(fieldDoc.qualifiedName());
String comment = fieldDoc.commentText();
if (comment.length() > 0) {
fieldNode.setComment(comment);
}
fieldNode.setScope(parseScope(fieldDoc));
fieldNode.setFinal(fieldDoc.isFinal());
fieldNode.setStatic(fieldDoc.isStatic());
fieldNode.setVolatile(fieldDoc.isVolatile());
fieldNode.setTransient(fieldDoc.isTransient());
fieldNode.setConstant(fieldDoc.constantValueExpression());
for (AnnotationDesc annotationDesc : fieldDoc.annotations()) {
fieldNode.getAnnotation().add(parseAnnotationDesc(annotationDesc, fieldDoc.qualifiedName()));
}
for (Tag tag : fieldDoc.tags()) {
fieldNode.getTag().add(parseTag(tag));
}
return fieldNode;
}
public static boolean start(RootDoc root) {
for (ClassDoc d : root.classes()) {
for (AnnotationDesc a : d.annotations()) {
System.out.println(a.annotationType());
}
}
return true;
}
public static boolean start(RootDoc root) {
for (ClassDoc d : root.classes()) {
for (AnnotationDesc a : d.annotations()) {
System.out.println(a.annotationType());
}
}
return true;
}
private static void setRoot(
StringBuilder pathRoot,
AnnotationDesc.ElementValuePair pair) {
String value = pair.value().toString().replaceAll("\"$|^\"", "");
if (!value.startsWith("/")) {
pathRoot.append("/");
}
if (value.endsWith("/")) {
pathRoot.append(value, 0, value.length() - 1);
} else {
pathRoot.append(value);
}
}
private static void appendPath(
StringBuilder path,
AnnotationDesc.ElementValuePair pair) {
String value = pair.value().toString().replaceAll("\"$|^\"", "");
if (value.startsWith("/")) {
path.append(value).append(".");
} else {
path.append("/").append(value).append(".");
}
}
private static String resolveRequestMethod(
AnnotationDesc.ElementValuePair pair,
String defaultRequestMethod) {
String value = pair.value().toString();
for (String[] each : REQUEST_MAPPINGS) {
if (each[0].equals(value)) {
return each[1];
}
}
return defaultRequestMethod;
}
public static boolean start(RootDoc root) {
for (ClassDoc d : root.classes()) {
for (AnnotationDesc a : d.annotations()) {
System.out.println(a.annotationType());
}
}
return true;
}
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;
}
private PSItemFieldDoc(PSItemDoc psItemDoc, FieldDoc fieldDoc, AnnotationDesc annotation) {
this.psItemDoc = psItemDoc;
this.reference = fieldDoc.name();
this.name = fieldDoc.constantValue().toString();
this.description = fieldDoc.commentText().replace('\n', ' ');
for (AnnotationDesc.ElementValuePair elementValuePair : annotation.elementValues()) {
if ("type".equals(elementValuePair.element().name())) {
Object typeValue = elementValuePair.value().value();
this.type = (Type) typeValue;
}
}
}
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;
}
public static boolean start(RootDoc root) {
for (ClassDoc d : root.classes()) {
for (AnnotationDesc a : d.annotations()) {
System.out.println(a.annotationType());
}
}
return true;
}
public static boolean start(RootDoc root) {
for (ClassDoc d : root.classes()) {
for (AnnotationDesc a : d.annotations()) {
System.out.println(a.annotationType());
}
}
return true;
}
public static boolean start(RootDoc root) {
for (ClassDoc d : root.classes()) {
for (AnnotationDesc a : d.annotations()) {
System.out.println(a.annotationType());
}
}
return true;
}
public static boolean start(RootDoc root) {
for (ClassDoc d : root.classes()) {
for (AnnotationDesc a : d.annotations()) {
System.out.println(a.annotationType());
}
}
return true;
}
public static boolean start(RootDoc root) {
for (ClassDoc d : root.classes()) {
for (AnnotationDesc a : d.annotations()) {
System.out.println(a.annotationType());
}
}
return true;
}
/** Find the first element into the given array.
*
* @param <T> the type of the elements to filter.
* @param original the original array.
* @param filter the filtering action.
* @return the first element.
*/
public static <T extends AnnotationDesc> T findFirst(T[] original, Predicate<T> filter) {
for (final T element : original) {
if (filter.test(element)) {
return element;
}
}
return null;
}
/** Replies the SARL element type of the given type.
*
* @param type the type.
* @return the SARL element type, or {@code null} if unknown.
*/
public static Integer getSarlClassification(ProgramElementDoc type) {
final AnnotationDesc annotation = Utils.findFirst(type.annotations(), it ->
qualifiedNameEquals(it.annotationType().qualifiedTypeName(), getKeywords().getSarlElementTypeAnnotationName()));
if (annotation != null) {
final ElementValuePair[] pairs = annotation.elementValues();
if (pairs != null && pairs.length > 0) {
return ((Number) pairs[0].value().value()).intValue();
}
}
return null;
}
protected Enum parseEnum(ClassDoc classDoc) {
Enum enumNode = objectFactory.createEnum();
enumNode.setName(classDoc.name());
enumNode.setQualified(classDoc.qualifiedName());
String comment = classDoc.commentText();
if (comment.length() > 0) {
enumNode.setComment(comment);
}
enumNode.setIncluded(classDoc.isIncluded());
enumNode.setScope(parseScope(classDoc));
Type superClassType = classDoc.superclassType();
if (superClassType != null) {
enumNode.setClazz(parseTypeInfo(superClassType));
}
for (Type interfaceType : classDoc.interfaceTypes()) {
enumNode.getInterface().add(parseTypeInfo(interfaceType));
}
for (FieldDoc field : classDoc.enumConstants()) {
enumNode.getConstant().add(parseEnumConstant(field));
}
for (AnnotationDesc annotationDesc : classDoc.annotations()) {
enumNode.getAnnotation().add(parseAnnotationDesc(annotationDesc, classDoc.qualifiedName()));
}
for (Tag tag : classDoc.tags()) {
enumNode.getTag().add(parseTag(tag));
}
return enumNode;
}
protected Interface parseInterface(ClassDoc classDoc) {
Interface interfaceNode = objectFactory.createInterface();
interfaceNode.setName(classDoc.name());
interfaceNode.setQualified(classDoc.qualifiedName());
String comment = classDoc.commentText();
if (comment.length() > 0) {
interfaceNode.setComment(comment);
}
interfaceNode.setIncluded(classDoc.isIncluded());
interfaceNode.setScope(parseScope(classDoc));
for (TypeVariable typeVariable : classDoc.typeParameters()) {
interfaceNode.getGeneric().add(parseTypeParameter(typeVariable));
}
for (Type interfaceType : classDoc.interfaceTypes()) {
interfaceNode.getInterface().add(parseTypeInfo(interfaceType));
}
for (MethodDoc method : classDoc.methods()) {
interfaceNode.getMethod().add(parseMethod(method));
}
for (AnnotationDesc annotationDesc : classDoc.annotations()) {
interfaceNode.getAnnotation().add(parseAnnotationDesc(annotationDesc, classDoc.qualifiedName()));
}
for (Tag tag : classDoc.tags()) {
interfaceNode.getTag().add(parseTag(tag));
}
for (FieldDoc field : classDoc.fields()) {
interfaceNode.getField().add(parseField(field));
}
return interfaceNode;
}
protected Constructor parseConstructor(ConstructorDoc constructorDoc) {
Constructor constructorNode = objectFactory.createConstructor();
constructorNode.setName(constructorDoc.name());
constructorNode.setQualified(constructorDoc.qualifiedName());
String comment = constructorDoc.commentText();
if (comment.length() > 0) {
constructorNode.setComment(comment);
}
constructorNode.setScope(parseScope(constructorDoc));
constructorNode.setIncluded(constructorDoc.isIncluded());
constructorNode.setFinal(constructorDoc.isFinal());
constructorNode.setNative(constructorDoc.isNative());
constructorNode.setStatic(constructorDoc.isStatic());
constructorNode.setSynchronized(constructorDoc.isSynchronized());
constructorNode.setVarArgs(constructorDoc.isVarArgs());
constructorNode.setSignature(constructorDoc.signature());
for (Parameter parameter : constructorDoc.parameters()) {
constructorNode.getParameter().add(parseMethodParameter(parameter));
}
for (Type exceptionType : constructorDoc.thrownExceptionTypes()) {
constructorNode.getException().add(parseTypeInfo(exceptionType));
}
for (AnnotationDesc annotationDesc : constructorDoc.annotations()) {
constructorNode.getAnnotation().add(parseAnnotationDesc(annotationDesc, constructorDoc.qualifiedName()));
}
for (Tag tag : constructorDoc.tags()) {
constructorNode.getTag().add(parseTag(tag));
}
return constructorNode;
}
protected Method parseMethod(MethodDoc methodDoc) {
Method methodNode = objectFactory.createMethod();
methodNode.setName(methodDoc.name());
methodNode.setQualified(methodDoc.qualifiedName());
String comment = methodDoc.commentText();
if (comment.length() > 0) {
methodNode.setComment(comment);
}
methodNode.setScope(parseScope(methodDoc));
methodNode.setAbstract(methodDoc.isAbstract());
methodNode.setIncluded(methodDoc.isIncluded());
methodNode.setFinal(methodDoc.isFinal());
methodNode.setNative(methodDoc.isNative());
methodNode.setStatic(methodDoc.isStatic());
methodNode.setSynchronized(methodDoc.isSynchronized());
methodNode.setVarArgs(methodDoc.isVarArgs());
methodNode.setSignature(methodDoc.signature());
methodNode.setReturn(parseTypeInfo(methodDoc.returnType()));
for (Parameter parameter : methodDoc.parameters()) {
methodNode.getParameter().add(parseMethodParameter(parameter));
}
for (Type exceptionType : methodDoc.thrownExceptionTypes()) {
methodNode.getException().add(parseTypeInfo(exceptionType));
}
for (AnnotationDesc annotationDesc : methodDoc.annotations()) {
methodNode.getAnnotation().add(parseAnnotationDesc(annotationDesc, methodDoc.qualifiedName()));
}
for (Tag tag : methodDoc.tags()) {
methodNode.getTag().add(parseTag(tag));
}
return methodNode;
}
protected MethodParameter parseMethodParameter(Parameter parameter) {
MethodParameter parameterMethodNode = objectFactory.createMethodParameter();
parameterMethodNode.setName(parameter.name());
parameterMethodNode.setType(parseTypeInfo(parameter.type()));
for (AnnotationDesc annotationDesc : parameter.annotations()) {
parameterMethodNode.getAnnotation().add(parseAnnotationDesc(annotationDesc, parameter.typeName()));
}
return parameterMethodNode;
}
private static void processMethod(
Properties properties,
MethodDoc methodDoc,
String defaultRequestMethod,
String pathRoot,
boolean exceptionRef) {
for (AnnotationDesc annotationDesc : methodDoc.annotations()) {
String annotationType = annotationDesc.annotationType().toString();
if (isMapping(annotationType)) {
StringBuilder path = new StringBuilder(pathRoot);
for (AnnotationDesc.ElementValuePair pair : annotationDesc.elementValues()) {
if (VALUE.equals(pair.element().name()) || PATH.equals(pair.element().name())) {
appendPath(path, pair);
break;
}
}
if (!path.substring(path.length() - 1).equals(".")) {
path.append(".");
}
String requestMethod = getRequestMethod(annotationDesc, annotationType, defaultRequestMethod);
if (requestMethod != null) {
path.append(requestMethod);
saveProperty(properties, path.toString() + ".notes", methodDoc.commentText());
for (ParamTag paramTag : methodDoc.paramTags()) {
saveProperty(properties, path.toString() + ".param." + paramTag.parameterName(),
paramTag.parameterComment());
}
for (Tag tag : methodDoc.tags()) {
if (tag.name().equals(RETURN)) {
saveProperty(properties, path.toString() + ".return", tag.text());
break;
}
}
if (exceptionRef) {
processThrows(properties, methodDoc.throwsTags(), path);
}
}
}
}
}