summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXin Li <delphij@google.com>2021-08-14 06:31:29 +0000
committerXin Li <delphij@google.com>2021-08-14 06:31:29 +0000
commit8e24c2413ba2f8f1abd7ca0fb4a3bbdadd039f3a (patch)
tree766ef423e8f021e0722776b1d031d9dd4529653f
parent01990004ec2192d7c85bcbfb18f236c60d32828a (diff)
parentfc2c1ce1bcc5927c415c00a1fd24f3282bd51b44 (diff)
downloaddoclava-8e24c2413ba2f8f1abd7ca0fb4a3bbdadd039f3a.tar.gz
Merge sc-dev-plus-aosp-without-vendor@7634622temp_sam_202323961
Merged-In: I0d07b4841f610dfa4b12de9523277f8b9ab27e9f Change-Id: I91645ac90b6665bcbd9b2169eda92a067e83add3
-rw-r--r--Android.bp2
-rw-r--r--src/com/google/doclava/Converter.java38
2 files changed, 36 insertions, 4 deletions
diff --git a/Android.bp b/Android.bp
index a3e2317..4c81ca5 100644
--- a/Android.bp
+++ b/Android.bp
@@ -54,6 +54,7 @@ droiddoc_exported_dir {
java_library_host {
name: "doclava",
+ java_version: "1.8",
srcs: [
"src/**/*.java",
],
@@ -69,6 +70,7 @@ java_library_host {
java_library_host {
name: "doclava-no-guava",
+ java_version: "1.8",
srcs: [
"src/**/*.java",
],
diff --git a/src/com/google/doclava/Converter.java b/src/com/google/doclava/Converter.java
index cf14237..2143875 100644
--- a/src/com/google/doclava/Converter.java
+++ b/src/com/google/doclava/Converter.java
@@ -23,6 +23,7 @@ import com.sun.javadoc.AnnotationTypeElementDoc;
import com.sun.javadoc.AnnotationValue;
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.ConstructorDoc;
+import com.sun.javadoc.Doc;
import com.sun.javadoc.ExecutableMemberDoc;
import com.sun.javadoc.FieldDoc;
import com.sun.javadoc.MemberDoc;
@@ -43,7 +44,6 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
-import java.util.Map;
public class Converter {
private static RootDoc root;
@@ -288,7 +288,7 @@ public class Converter {
}
private static ParameterInfo[] convertParameters(Parameter[] p, ExecutableMemberDoc m) {
- SourcePosition pos = m.position();
+ SourcePosition pos = getPositionSafe(m);
int len = p.length;
ParameterInfo[] q = new ParameterInfo[len];
for (int i = 0; i < len; i++) {
@@ -447,6 +447,27 @@ public class Converter {
return (MethodInfo) mMethods.obtain(o);
}
+ /**
+ * Returns the result of a call to {@code f.position()} but avoids a {@link NullPointerException}
+ * if {@code f.position()} throws a {@link NullPointerException} due to the {@link Doc} element
+ * being related to an annotation.
+ *
+ * <p>This is consistent with the Java runtime where references to annotations that do not exist
+ * on the classpath are allowed. This method is used, for example, to allow Doclava to process
+ * JARs in the classpath that were generated by the Kotlin compiler, without requiring the Kotlin
+ * stdlib to exist on the classpath when Doclava is executed.
+ */
+ private static SourcePosition getPositionSafe(Doc f) {
+ try {
+ return f.position();
+ } catch (NullPointerException e) {
+ if (f.isAnnotationTypeElement() || mFieldDocElementsOnAnnotationClasses.contains(f)) {
+ return null;
+ }
+ throw e;
+ }
+ }
+
private static Cache mMethods = new Cache() {
@Override
protected Object make(Object o) {
@@ -466,7 +487,7 @@ public class Converter {
new ArrayList<ParameterInfo>(Arrays.asList(
Converter.convertParameters(m.parameters(), m))),
new ArrayList<ClassInfo>(Arrays.asList(Converter.convertClasses(
- m.thrownExceptions()))), Converter.convertSourcePosition(m.position()),
+ m.thrownExceptions()))), Converter.convertSourcePosition(getPositionSafe(m)),
new ArrayList<AnnotationInstanceInfo>(Arrays.asList(
Converter.convertAnnotationInstances(m.annotations()))));
result.setVarargs(m.isVarArgs());
@@ -547,7 +568,7 @@ public class Converter {
.obtainClass(f.containingClass()), f.isPublic(), f.isProtected(), f.isPackagePrivate(), f
.isPrivate(), f.isFinal(), f.isStatic(), f.isTransient(), f.isVolatile(),
f.isSynthetic(), Converter.obtainType(f.type()), f.getRawCommentText(),
- f.constantValue(), Converter.convertSourcePosition(f.position()),
+ f.constantValue(), Converter.convertSourcePosition(getPositionSafe(f)),
new ArrayList<AnnotationInstanceInfo>(Arrays.asList(Converter
.convertAnnotationInstances(f.annotations()))));
}
@@ -760,6 +781,12 @@ public class Converter {
new HashMap<AnnotationValue, AnnotationValueInfo>();
private static HashSet<AnnotationValue> mAnnotationValuesNeedingInit =
new HashSet<AnnotationValue>();
+ /**
+ * Stores the {@link FieldDoc} instances the are contained in annotation classes. This is used by
+ * {@link #getPositionSafe(Doc)} to allow {@link NullPointerException} for annotation classes to
+ * be ignored.
+ */
+ private static HashSet<FieldDoc> mFieldDocElementsOnAnnotationClasses = new HashSet<FieldDoc>();
private static AnnotationValueInfo obtainAnnotationValue(AnnotationValue o, MethodInfo element) {
if (o == null) {
@@ -769,6 +796,9 @@ public class Converter {
if (v != null) return v;
v = new AnnotationValueInfo(element);
mAnnotationValues.put(o, v);
+ if (o.value() instanceof FieldDoc) {
+ mFieldDocElementsOnAnnotationClasses.add((FieldDoc) o.value());
+ }
if (mAnnotationValuesNeedingInit != null) {
mAnnotationValuesNeedingInit.add(o);
} else {