下面列出了怎么用com.sun.javadoc.MethodDoc的API类实例代码及写法,或者点击链接到github查看源代码。
private static void parseAndAnnotate(RootDoc rootDoc) throws ClassNotFoundException, NotFoundException, CannotCompileException, IOException {
for (ClassDoc classDoc : rootDoc.classes()) {
if (StringUtils.isEmpty(classDoc.getRawCommentText())) {
DocLogger.warn("No javadoc found in class " + classDoc.qualifiedName() + "." + classDoc.name());
}
Class clazz = Class.forName(classDoc.qualifiedName());
ClassPool pool = ClassPool.getDefault();
pool.insertClassPath(new ClassClassPath(clazz));
CtClass ctClass = pool.get(clazz.getName());
ApiInfo apiInfo = ApiInfo.fromClassDoc(clazz, classDoc);
if (!apiInfo.hidden()) {
annotateClassAnn(ctClass, apiInfo);
for (MethodDoc methodDoc : classDoc.methods()) {
ApiMethodInfo apiMethodInfo = ApiMethodInfo.fromMethodDoc(matchingMethod(clazz, methodDoc), methodDoc);
if (StringUtils.isEmpty(methodDoc.getRawCommentText())) {
DocLogger.warn("No javadoc found in method " + classDoc.qualifiedName() + "." + methodDoc.name() + methodDoc.signature());
}
annotateMethodAnn(ctClass, apiMethodInfo);
}
DocLogger.info("Successfully annotated " + clazz.getTypeName());
}
ctClass.writeFile(classDir);
}
}
/**
* {@inheritDoc}
*/
@Override
public Content getTagletOutput(Doc doc, TagletWriter writer) throws IllegalArgumentException {
Tag[] tags = doc.tags(getName());
if (tags.length==0 && doc instanceof MethodDoc) { // inherit if necessary and possible
final DocFinder.Output inheritedDoc = DocFinder.search(new DocFinder.Input((MethodDoc) doc, this));
tags = inheritedDoc.holderTag == null ? tags : new Tag[] {inheritedDoc.holderTag};
}
if (tags.length==0)
return null;
final StringBuilder out = writeHeader(new StringBuilder());
for(Tag tag : tags) {
writeTag(out, tag, writer);
}
return new RawHtml(out.toString());
}
/**
* Writes code to initialize the static fields for each method
* using the Java Reflection API.
**/
private void writeMethodFieldInitializers(IndentingWriter p)
throws IOException
{
for (int i = 0; i < methodFieldNames.length; i++) {
p.p(methodFieldNames[i] + " = ");
/*
* Look up the Method object in the somewhat arbitrary
* interface that we find in the Method object.
*/
RemoteClass.Method method = remoteMethods[i];
MethodDoc methodDoc = method.methodDoc();
String methodName = methodDoc.name();
Type paramTypes[] = method.parameterTypes();
p.p(methodDoc.containingClass().qualifiedName() + ".class.getMethod(\"" +
methodName + "\", new java.lang.Class[] {");
for (int j = 0; j < paramTypes.length; j++) {
if (j > 0)
p.p(", ");
p.p(paramTypes[j].toString() + ".class");
}
p.pln("});");
}
}
/**
* Writes code to initialize the static fields for each method
* using the Java Reflection API.
**/
private void writeMethodFieldInitializers(IndentingWriter p)
throws IOException
{
for (int i = 0; i < methodFieldNames.length; i++) {
p.p(methodFieldNames[i] + " = ");
/*
* Look up the Method object in the somewhat arbitrary
* interface that we find in the Method object.
*/
RemoteClass.Method method = remoteMethods[i];
MethodDoc methodDoc = method.methodDoc();
String methodName = methodDoc.name();
Type paramTypes[] = method.parameterTypes();
p.p(methodDoc.containingClass().qualifiedName() + ".class.getMethod(\"" +
methodName + "\", new java.lang.Class[] {");
for (int j = 0; j < paramTypes.length; j++) {
if (j > 0)
p.p(", ");
p.p(paramTypes[j].toString() + ".class");
}
p.pln("});");
}
}
private PSItemDoc(ClassDoc classDoc) {
this.classDoc = classDoc;
this.name = classDoc.name();
this.description = classDoc.commentText();
this.itemFieldDocs = new ArrayList<>();
this.providerDocs = new ArrayList<>();
List<FieldDoc> allFields = new ArrayList<>();
this.getAllFieldDocs(classDoc, allFields);
for (FieldDoc fieldDoc : allFields) {
PSItemFieldDoc itemFieldDoc = PSItemFieldDoc.build(this, fieldDoc);
if (itemFieldDoc != null) this.itemFieldDocs.add(itemFieldDoc);
}
for (MethodDoc methodDoc : classDoc.methods()) {
PSOperatorDoc providerDoc = PSOperatorDoc.build(classDoc, methodDoc);
if (providerDoc != null) this.providerDocs.add(providerDoc);
}
}
/**
* Writes code to initialize the static fields for each method
* using the Java Reflection API.
**/
private void writeMethodFieldInitializers(IndentingWriter p)
throws IOException
{
for (int i = 0; i < methodFieldNames.length; i++) {
p.p(methodFieldNames[i] + " = ");
/*
* Look up the Method object in the somewhat arbitrary
* interface that we find in the Method object.
*/
RemoteClass.Method method = remoteMethods[i];
MethodDoc methodDoc = method.methodDoc();
String methodName = methodDoc.name();
Type paramTypes[] = method.parameterTypes();
p.p(methodDoc.containingClass().qualifiedName() + ".class.getMethod(\"" +
methodName + "\", new java.lang.Class[] {");
for (int j = 0; j < paramTypes.length; j++) {
if (j > 0)
p.p(", ");
p.p(paramTypes[j].toString() + ".class");
}
p.pln("});");
}
}
/**
* Writes code to initialize the static fields for each method
* using the Java Reflection API.
**/
private void writeMethodFieldInitializers(IndentingWriter p)
throws IOException
{
for (int i = 0; i < methodFieldNames.length; i++) {
p.p(methodFieldNames[i] + " = ");
/*
* Look up the Method object in the somewhat arbitrary
* interface that we find in the Method object.
*/
RemoteClass.Method method = remoteMethods[i];
MethodDoc methodDoc = method.methodDoc();
String methodName = methodDoc.name();
Type paramTypes[] = method.parameterTypes();
p.p(methodDoc.containingClass().qualifiedName() + ".class.getMethod(\"" +
methodName + "\", new java.lang.Class[] {");
for (int j = 0; j < paramTypes.length; j++) {
if (j > 0)
p.p(", ");
p.p(paramTypes[j].toString() + ".class");
}
p.pln("});");
}
}
/**
* Writes code to initialize the static fields for each method
* using the Java Reflection API.
**/
private void writeMethodFieldInitializers(IndentingWriter p)
throws IOException
{
for (int i = 0; i < methodFieldNames.length; i++) {
p.p(methodFieldNames[i] + " = ");
/*
* Look up the Method object in the somewhat arbitrary
* interface that we find in the Method object.
*/
RemoteClass.Method method = remoteMethods[i];
MethodDoc methodDoc = method.methodDoc();
String methodName = methodDoc.name();
Type paramTypes[] = method.parameterTypes();
p.p(methodDoc.containingClass().qualifiedName() + ".class.getMethod(\"" +
methodName + "\", new java.lang.Class[] {");
for (int j = 0; j < paramTypes.length; j++) {
if (j > 0)
p.p(", ");
p.p(paramTypes[j].toString() + ".class");
}
p.pln("});");
}
}
/**
* Writes code to initialize the static fields for each method
* using the Java Reflection API.
**/
private void writeMethodFieldInitializers(IndentingWriter p)
throws IOException
{
for (int i = 0; i < methodFieldNames.length; i++) {
p.p(methodFieldNames[i] + " = ");
/*
* Look up the Method object in the somewhat arbitrary
* interface that we find in the Method object.
*/
RemoteClass.Method method = remoteMethods[i];
MethodDoc methodDoc = method.methodDoc();
String methodName = methodDoc.name();
Type paramTypes[] = method.parameterTypes();
p.p(methodDoc.containingClass().qualifiedName() + ".class.getMethod(\"" +
methodName + "\", new java.lang.Class[] {");
for (int j = 0; j < paramTypes.length; j++) {
if (j > 0)
p.p(", ");
p.p(paramTypes[j].toString() + ".class");
}
p.pln("});");
}
}
/**
* Writes code to initialize the static fields for each method
* using the Java Reflection API.
**/
private void writeMethodFieldInitializers(IndentingWriter p)
throws IOException
{
for (int i = 0; i < methodFieldNames.length; i++) {
p.p(methodFieldNames[i] + " = ");
/*
* Look up the Method object in the somewhat arbitrary
* interface that we find in the Method object.
*/
RemoteClass.Method method = remoteMethods[i];
MethodDoc methodDoc = method.methodDoc();
String methodName = methodDoc.name();
Type paramTypes[] = method.parameterTypes();
p.p(methodDoc.containingClass().qualifiedName() + ".class.getMethod(\"" +
methodName + "\", new java.lang.Class[] {");
for (int j = 0; j < paramTypes.length; j++) {
if (j > 0)
p.p(", ");
p.p(paramTypes[j].toString() + ".class");
}
p.pln("});");
}
}
/**
* Writes code to initialize the static fields for each method
* using the Java Reflection API.
**/
private void writeMethodFieldInitializers(IndentingWriter p)
throws IOException
{
for (int i = 0; i < methodFieldNames.length; i++) {
p.p(methodFieldNames[i] + " = ");
/*
* Look up the Method object in the somewhat arbitrary
* interface that we find in the Method object.
*/
RemoteClass.Method method = remoteMethods[i];
MethodDoc methodDoc = method.methodDoc();
String methodName = methodDoc.name();
Type paramTypes[] = method.parameterTypes();
p.p(methodDoc.containingClass().qualifiedName() + ".class.getMethod(\"" +
methodName + "\", new java.lang.Class[] {");
for (int j = 0; j < paramTypes.length; j++) {
if (j > 0)
p.p(", ");
p.p(paramTypes[j].toString() + ".class");
}
p.pln("});");
}
}
private ApiMethodInfo(Method method, MethodDoc methodDoc) {
this.method = method;
this.methodDoc = methodDoc;
value = "";
returnDescription = method.getReturnType().getSimpleName();
noteBuilder = new StringBuilder();
}
/**
* Returns a reader-friendly string representation of the
* specified method's signature. Names of reference types are not
* package-qualified.
**/
static String getFriendlyUnqualifiedSignature(MethodDoc method) {
String sig = method.name() + "(";
Parameter[] parameters = method.parameters();
for (int i = 0; i < parameters.length; i++) {
if (i > 0) {
sig += ", ";
}
Type paramType = parameters[i].type();
sig += paramType.typeName() + paramType.dimension();
}
sig += ")";
return sig;
}
/**
* Creates a new Method instance for the specified method.
**/
Method(MethodDoc methodDoc) {
this.methodDoc = methodDoc;
exceptionTypes = methodDoc.thrownExceptions();
/*
* Sort exception types to improve consistency with
* previous implementations.
*/
Arrays.sort(exceptionTypes, new ClassDocComparator());
operationString = computeOperationString();
nameAndDescriptor =
methodDoc.name() + Util.methodDescriptorOf(methodDoc);
methodHash = computeMethodHash();
}
/**
* Returns the MethodDoc for the method of this remote
* implementation class that implements the specified remote
* method of a remote interface. Returns null if no matching
* method was found in this remote implementation class.
**/
private MethodDoc findImplMethod(MethodDoc interfaceMethod) {
String name = interfaceMethod.name();
String desc = Util.methodDescriptorOf(interfaceMethod);
for (MethodDoc implMethod : implClass.methods()) {
if (name.equals(implMethod.name()) &&
desc.equals(Util.methodDescriptorOf(implMethod)))
{
return implMethod;
}
}
return null;
}
/**
* Creates a new Method instance for the specified method.
**/
Method(MethodDoc methodDoc) {
this.methodDoc = methodDoc;
exceptionTypes = methodDoc.thrownExceptions();
/*
* Sort exception types to improve consistency with
* previous implementations.
*/
Arrays.sort(exceptionTypes, new ClassDocComparator());
operationString = computeOperationString();
nameAndDescriptor =
methodDoc.name() + Util.methodDescriptorOf(methodDoc);
methodHash = computeMethodHash();
}
/**
* Returns the method descriptor for the specified method.
*
* See section 4.3.3 of The Java Virtual Machine Specification
* Second Edition for the definition of a "method descriptor".
**/
static String methodDescriptorOf(MethodDoc method) {
String desc = "(";
Parameter[] parameters = method.parameters();
for (int i = 0; i < parameters.length; i++) {
desc += typeDescriptorOf(parameters[i].type());
}
desc += ")" + typeDescriptorOf(method.returnType());
return desc;
}
/**
* Returns a reader-friendly string representation of the
* specified method's signature. Names of reference types are not
* package-qualified.
**/
static String getFriendlyUnqualifiedSignature(MethodDoc method) {
String sig = method.name() + "(";
Parameter[] parameters = method.parameters();
for (int i = 0; i < parameters.length; i++) {
if (i > 0) {
sig += ", ";
}
Type paramType = parameters[i].type();
sig += paramType.typeName() + paramType.dimension();
}
sig += ")";
return sig;
}
/**
* Creates a new Method instance for the specified method.
**/
Method(MethodDoc methodDoc) {
this.methodDoc = methodDoc;
exceptionTypes = methodDoc.thrownExceptions();
/*
* Sort exception types to improve consistency with
* previous implementations.
*/
Arrays.sort(exceptionTypes, new ClassDocComparator());
operationString = computeOperationString();
nameAndDescriptor =
methodDoc.name() + Util.methodDescriptorOf(methodDoc);
methodHash = computeMethodHash();
}
/**
* Returns the method descriptor for the specified method.
*
* See section 4.3.3 of The Java Virtual Machine Specification
* Second Edition for the definition of a "method descriptor".
**/
static String methodDescriptorOf(MethodDoc method) {
String desc = "(";
Parameter[] parameters = method.parameters();
for (int i = 0; i < parameters.length; i++) {
desc += typeDescriptorOf(parameters[i].type());
}
desc += ")" + typeDescriptorOf(method.returnType());
return desc;
}
/**
* Returns a reader-friendly string representation of the
* specified method's signature. Names of reference types are not
* package-qualified.
**/
static String getFriendlyUnqualifiedSignature(MethodDoc method) {
String sig = method.name() + "(";
Parameter[] parameters = method.parameters();
for (int i = 0; i < parameters.length; i++) {
if (i > 0) {
sig += ", ";
}
Type paramType = parameters[i].type();
sig += paramType.typeName() + paramType.dimension();
}
sig += ")";
return sig;
}
/**
* Returns the MethodDoc for the method of this remote
* implementation class that implements the specified remote
* method of a remote interface. Returns null if no matching
* method was found in this remote implementation class.
**/
private MethodDoc findImplMethod(MethodDoc interfaceMethod) {
String name = interfaceMethod.name();
String desc = Util.methodDescriptorOf(interfaceMethod);
for (MethodDoc implMethod : implClass.methods()) {
if (name.equals(implMethod.name()) &&
desc.equals(Util.methodDescriptorOf(implMethod)))
{
return implMethod;
}
}
return null;
}
/**
* Creates a new Method instance for the specified method.
**/
Method(MethodDoc methodDoc) {
this.methodDoc = methodDoc;
exceptionTypes = methodDoc.thrownExceptions();
/*
* Sort exception types to improve consistency with
* previous implementations.
*/
Arrays.sort(exceptionTypes, new ClassDocComparator());
operationString = computeOperationString();
nameAndDescriptor =
methodDoc.name() + Util.methodDescriptorOf(methodDoc);
methodHash = computeMethodHash();
}
/**
* Returns a reader-friendly string representation of the
* specified method's signature. Names of reference types are not
* package-qualified.
**/
static String getFriendlyUnqualifiedSignature(MethodDoc method) {
String sig = method.name() + "(";
Parameter[] parameters = method.parameters();
for (int i = 0; i < parameters.length; i++) {
if (i > 0) {
sig += ", ";
}
Type paramType = parameters[i].type();
sig += paramType.typeName() + paramType.dimension();
}
sig += ")";
return sig;
}
/**
* Returns the method descriptor for the specified method.
*
* See section 4.3.3 of The Java Virtual Machine Specification
* Second Edition for the definition of a "method descriptor".
**/
static String methodDescriptorOf(MethodDoc method) {
String desc = "(";
Parameter[] parameters = method.parameters();
for (int i = 0; i < parameters.length; i++) {
desc += typeDescriptorOf(parameters[i].type());
}
desc += ")" + typeDescriptorOf(method.returnType());
return desc;
}
private PSOperatorDoc(ClassDoc classDoc, MethodDoc methodDoc) {
this.declaringClassDoc = classDoc;
this.methodDoc = methodDoc;
this.description = methodDoc.commentText().replace('\n', ' ');
this.paramDesc = "";
Tag[] paramTags = methodDoc.tags("param");
for (Tag paramTag : paramTags) {
String paraStr = paramTag.text();
String paraName = paraStr.substring(0, paraStr.indexOf(' ')).replace('\n', ' ');;
String paraDesc = paraStr.substring(paraStr.indexOf(' ') + 1).replace('\n', ' ');;
this.paramDesc += "<br> - `" + paraName + "`: " + paraDesc;
}
this.returnType = methodDoc.returnType();
// ParameterizedType returnType = methodDoc.returnType().asParameterizedType();
// this.inputType = returnType.typeArguments()[0];
// this.outputType = returnType.typeArguments()[1];
// this.completeSignature = "Function<" + this.inputType + ", " + this.outputType + "> " + methodDoc.toString();
String shortSignature = classDoc.name() + "." + methodDoc.name() + "(";
boolean firstParameter = true;
for (Parameter parameter : methodDoc.parameters()) {
if (firstParameter) {
shortSignature += Utils.getSimpleTypeName(parameter.type()) + " " + parameter.name();
firstParameter = false;
} else {
shortSignature += ", " + Utils.getSimpleTypeName(parameter.type()) + " " + parameter.name();
}
}
shortSignature += ")";
this.shortSignature = shortSignature;
}
public static PSOperatorDoc build(ClassDoc classDoc, MethodDoc methodDoc) {
if (methodDoc.isStatic() && methodDoc.isPublic()
&& Utils.instanceOf(methodDoc.returnType().asClassDoc(), Consts.TYPE_FUNCTION)) {
return new PSOperatorDoc(classDoc, methodDoc);
}
return null;
}
private PSOperatorWrapperDoc(ClassDoc classDoc) {
this.classDoc = classDoc;
this.name = classDoc.name();
this.description = classDoc.commentText();
this.operatorDocs = new ArrayList<>();
for (MethodDoc methodDoc : classDoc.methods()) {
PSOperatorDoc operatorDoc = PSOperatorDoc.build(classDoc, methodDoc);
if (operatorDoc != null) this.operatorDocs.add(operatorDoc);
}
}
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;
}
/** Wrap a method doc.
*
* @param source the source
* @return the wrapper.
*/
public MethodDoc wrap(MethodDoc source) {
if (source == null || source instanceof Proxy<?> || !(source instanceof MethodDocImpl)) {
return source;
}
return new MethodDocWrapper((MethodDocImpl) source);
}