下面列出了怎么用com.sun.javadoc.ClassDoc的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Adds to the supplied list all exceptions in the "froms"
* array that are subclasses of an exception in the "withs"
* array.
**/
private void collectCompatibleExceptions(ClassDoc[] froms,
ClassDoc[] withs,
List<ClassDoc> list)
{
for (ClassDoc from : froms) {
if (!list.contains(from)) {
for (ClassDoc with : withs) {
if (from.subclassOf(with)) {
list.add(from);
break;
}
}
}
}
}
/**
* Adds to the supplied list all exceptions in the "froms"
* array that are subclasses of an exception in the "withs"
* array.
**/
private void collectCompatibleExceptions(ClassDoc[] froms,
ClassDoc[] withs,
List<ClassDoc> list)
{
for (ClassDoc from : froms) {
if (!list.contains(from)) {
for (ClassDoc with : withs) {
if (from.subclassOf(with)) {
list.add(from);
break;
}
}
}
}
}
/**
* Returns a new Method object that is a legal combination of
* this Method object and another one.
*
* Doing this requires determining the exceptions declared by
* the combined method, which must be (only) all of the
* exceptions declared in both old Methods that may thrown in
* either of them.
**/
Method mergeWith(Method other) {
if (!nameAndDescriptor().equals(other.nameAndDescriptor())) {
throw new AssertionError(
"attempt to merge method \"" +
other.nameAndDescriptor() + "\" with \"" +
nameAndDescriptor());
}
List<ClassDoc> legalExceptions = new ArrayList<ClassDoc>();
collectCompatibleExceptions(
other.exceptionTypes, exceptionTypes, legalExceptions);
collectCompatibleExceptions(
exceptionTypes, other.exceptionTypes, legalExceptions);
Method merged = clone();
merged.exceptionTypes =
legalExceptions.toArray(new ClassDoc[legalExceptions.size()]);
return merged;
}
/**
* Adds to the supplied list all exceptions in the "froms"
* array that are subclasses of an exception in the "withs"
* array.
**/
private void collectCompatibleExceptions(ClassDoc[] froms,
ClassDoc[] withs,
List<ClassDoc> list)
{
for (ClassDoc from : froms) {
if (!list.contains(from)) {
for (ClassDoc with : withs) {
if (from.subclassOf(with)) {
list.add(from);
break;
}
}
}
}
}
/** Update the label of the given link with the SARL notation for lambdas.
*
* @param linkInfo the link information to update.
*/
protected void updateLinkLabel(LinkInfo linkInfo) {
if (linkInfo.type != null && linkInfo instanceof LinkInfoImpl) {
final LinkInfoImpl impl = (LinkInfoImpl) linkInfo;
final ClassDoc classdoc = linkInfo.type.asClassDoc();
if (classdoc != null) {
final SARLFeatureAccess kw = Utils.getKeywords();
final String name = classdoc.qualifiedName();
if (isPrefix(name, kw.getProceduresName())) {
linkInfo.label = createProcedureLambdaLabel(impl);
} else if (isPrefix(name, kw.getFunctionsName())) {
linkInfo.label = createFunctionLambdaLabel(impl);
}
}
}
}
/**
* Adds to the supplied list all exceptions in the "froms"
* array that are subclasses of an exception in the "withs"
* array.
**/
private void collectCompatibleExceptions(ClassDoc[] froms,
ClassDoc[] withs,
List<ClassDoc> list)
{
for (ClassDoc from : froms) {
if (!list.contains(from)) {
for (ClassDoc with : withs) {
if (from.subclassOf(with)) {
list.add(from);
break;
}
}
}
}
}
/**
* Adds to the supplied list all exceptions in the "froms"
* array that are subclasses of an exception in the "withs"
* array.
**/
private void collectCompatibleExceptions(ClassDoc[] froms,
ClassDoc[] withs,
List<ClassDoc> list)
{
for (ClassDoc from : froms) {
if (!list.contains(from)) {
for (ClassDoc with : withs) {
if (from.subclassOf(with)) {
list.add(from);
break;
}
}
}
}
}
/**
* Returns a new Method object that is a legal combination of
* this Method object and another one.
*
* Doing this requires determining the exceptions declared by
* the combined method, which must be (only) all of the
* exceptions declared in both old Methods that may thrown in
* either of them.
**/
Method mergeWith(Method other) {
if (!nameAndDescriptor().equals(other.nameAndDescriptor())) {
throw new AssertionError(
"attempt to merge method \"" +
other.nameAndDescriptor() + "\" with \"" +
nameAndDescriptor());
}
List<ClassDoc> legalExceptions = new ArrayList<ClassDoc>();
collectCompatibleExceptions(
other.exceptionTypes, exceptionTypes, legalExceptions);
collectCompatibleExceptions(
exceptionTypes, other.exceptionTypes, legalExceptions);
Method merged = clone();
merged.exceptionTypes =
legalExceptions.toArray(new ClassDoc[legalExceptions.size()]);
return merged;
}
void dump(PrintWriter out, String prefix, ClassDoc cd) {
out.println(prefix + "class: " + cd);
for (FieldDoc fd: cd.fields()) {
out.println(prefix + " " + fd);
if (fd.type().asClassDoc() != null) {
dump(out, prefix + " ", fd.type().asClassDoc());
}
}
}
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;
}
/** Reset the list of packages for avoiding duplicates.
*
* <p>The inherited implementation uses a HashSet that allow
* the same package to be stored multiple times. Here,
* we uses a TreeSet for using the "compareTo" mechanism.
*/
private void resetPackageList() {
final Set<PackageDoc> set = new TreeSet<>();
for (final PackageDoc pack : this.root.specifiedPackages()) {
set.add(pack);
}
for (final ClassDoc clazz : this.root.specifiedClasses()) {
set.add(clazz.containingPackage());
}
this.packages = Utils.toArray(this.packages, set);
}
public static boolean instanceOf(ClassDoc classDoc, String superClassName) {
if (classDoc == null || superClassName == null) return false;
String className = classDoc.containingPackage().name() + "." + classDoc.name();
// System.out.println(className + " " + superClassName);
if (className.startsWith(superClassName)) {
return true;
}
return instanceOf(classDoc.superclass(), superClassName);
}
/** Iterates through javadoc tags on the underlying class and calls specific parsing methods. */
private void parseTags(ClassDoc flowDoc) {
for (Tag tag : flowDoc.tags()) {
// Everything else is not a relevant tag.
if ("@error".equals(tag.name())) {
parseErrorTag(tag);
}
}
}
@Override
public String toString(Tag[] tags) {
if (tags.length == 0) {
return null;
}
ClassDoc classDoc = (ClassDoc) tags[0].holder();
return processExternalResources(classDoc) + processConfigurationParameters(classDoc);
}
private void getAllFieldDocs(ClassDoc classDoc, List<FieldDoc> fieldDocs) {
if (classDoc.superclass() != null) {
this.getAllFieldDocs(classDoc.superclass(), fieldDocs);
}
if (isValidPSItem(classDoc)) {
fieldDocs.addAll(Arrays.asList(classDoc.fields()));
}
}
/**
* @return Create and return a DocWorkUnit-derived object to handle documentation
* for the target feature(s) represented by documentedFeature.
*
* @param documentedFeature DocumentedFeature annotation for the target feature
* @param classDoc javadoc classDoc for the target feature
* @param clazz class of the target feature
* @return DocWorkUnit to be used for this feature
*/
@Override
protected DocWorkUnit createWorkUnit(
final DocumentedFeature documentedFeature,
final ClassDoc classDoc,
final Class<?> clazz)
{
return new DocWorkUnit(
new PicardHelpDocWorkUnitHandler(this),
documentedFeature,
classDoc,
clazz);
}
/**
* Returns the descriptor for the specified type, as appropriate
* for either a parameter or return type in a method descriptor.
**/
private static String typeDescriptorOf(Type type) {
String desc;
ClassDoc classDoc = type.asClassDoc();
if (classDoc == null) {
/*
* Handle primitive types.
*/
String name = type.typeName();
if (name.equals("boolean")) {
desc = "Z";
} else if (name.equals("byte")) {
desc = "B";
} else if (name.equals("char")) {
desc = "C";
} else if (name.equals("short")) {
desc = "S";
} else if (name.equals("int")) {
desc = "I";
} else if (name.equals("long")) {
desc = "J";
} else if (name.equals("float")) {
desc = "F";
} else if (name.equals("double")) {
desc = "D";
} else if (name.equals("void")) {
desc = "V";
} else {
throw new AssertionError(
"unrecognized primitive type: " + name);
}
} else {
/*
* Handle non-array reference types.
*/
desc = "L" + binaryNameOf(classDoc).replace('.', '/') + ";";
}
/*
* Handle array types.
*/
int dimensions = type.dimension().length() / 2;
for (int i = 0; i < dimensions; i++) {
desc = "[" + desc;
}
return desc;
}
@Override
public ClassDoc containingClass() {
return wrap(this.delegate.containingClass());
}
@Override
public ClassDoc asClassDoc() {
return wrap(this.delegate.asClassDoc());
}
/**
* Creates a RemoteClass instance for the specified class. The
* resulting object is not yet initialized.
**/
private RemoteClass(BatchEnvironment env, ClassDoc implClass) {
this.env = env;
this.implClass = implClass;
}
/**
* Computes the exceptions that need to be caught and rethrown in
* a stub method before wrapping Exceptions in
* UnexpectedExceptions, given the exceptions declared in the
* throws clause of the method. Returns a list containing the
* exception to catch. Each exception is guaranteed to be unique,
* i.e. not a subclass of any of the other exceptions in the list,
* so the catch blocks for these exceptions may be generated in
* any order relative to each other.
*
* RemoteException and RuntimeException are each automatically
* placed in the returned list (unless any of their superclasses
* are already present), since those exceptions should always be
* directly rethrown by a stub method.
*
* The returned list will be empty if java.lang.Exception or one
* of its superclasses is in the throws clause of the method,
* indicating that no exceptions need to be caught.
**/
private List<ClassDoc> computeUniqueCatchList(ClassDoc[] exceptions) {
List<ClassDoc> uniqueList = new ArrayList<ClassDoc>();
uniqueList.add(env.docRuntimeException());
uniqueList.add(env.docRemoteException()); // always catch/rethrow these
/* For each exception declared by the stub method's throws clause: */
nextException:
for (ClassDoc ex : exceptions) {
if (env.docException().subclassOf(ex)) {
/*
* If java.lang.Exception (or a superclass) was declared
* in the throws clause of this stub method, then we don't
* have to bother catching anything; clear the list and
* return.
*/
uniqueList.clear();
break;
} else if (!ex.subclassOf(env.docException())) {
/*
* Ignore other Throwables that do not extend Exception,
* because they cannot be thrown by the invoke methods.
*/
continue;
}
/*
* Compare this exception against the current list of
* exceptions that need to be caught:
*/
for (Iterator<ClassDoc> i = uniqueList.iterator(); i.hasNext();) {
ClassDoc ex2 = i.next();
if (ex.subclassOf(ex2)) {
/*
* If a superclass of this exception is already on
* the list to catch, then ignore this one and continue;
*/
continue nextException;
} else if (ex2.subclassOf(ex)) {
/*
* If a subclass of this exception is on the list
* to catch, then remove it;
*/
i.remove();
}
}
/* This exception is unique: add it to the list to catch. */
uniqueList.add(ex);
}
return uniqueList;
}
@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();
}
}
public int compare(ClassDoc o1, ClassDoc o2) {
return Util.binaryNameOf(o1).compareTo(Util.binaryNameOf(o2));
}
@Override
public ClassDoc[] interfaces() {
return wrap(this.delegate.interfaces());
}
public int compare(ClassDoc o1, ClassDoc o2) {
return Util.binaryNameOf(o1).compareTo(Util.binaryNameOf(o2));
}
/**
* Returns the ClassDoc for this remote implementation class.
**/
ClassDoc classDoc() {
return implClass;
}
/**
* Returns the ClassDoc for this remote implementation class.
**/
ClassDoc classDoc() {
return implClass;
}
@Override
public ClassDoc[] specifiedClasses() {
return delegate.specifiedClasses();
}
/**
* Returns the descriptor for the specified type, as appropriate
* for either a parameter or return type in a method descriptor.
**/
private static String typeDescriptorOf(Type type) {
String desc;
ClassDoc classDoc = type.asClassDoc();
if (classDoc == null) {
/*
* Handle primitive types.
*/
String name = type.typeName();
if (name.equals("boolean")) {
desc = "Z";
} else if (name.equals("byte")) {
desc = "B";
} else if (name.equals("char")) {
desc = "C";
} else if (name.equals("short")) {
desc = "S";
} else if (name.equals("int")) {
desc = "I";
} else if (name.equals("long")) {
desc = "J";
} else if (name.equals("float")) {
desc = "F";
} else if (name.equals("double")) {
desc = "D";
} else if (name.equals("void")) {
desc = "V";
} else {
throw new AssertionError(
"unrecognized primitive type: " + name);
}
} else {
/*
* Handle non-array reference types.
*/
desc = "L" + binaryNameOf(classDoc).replace('.', '/') + ";";
}
/*
* Handle array types.
*/
int dimensions = type.dimension().length() / 2;
for (int i = 0; i < dimensions; i++) {
desc = "[" + desc;
}
return desc;
}