Skip to content

Commit 2a8bc27

Browse files
committed
IllegalArgumentException when getting Javadoc for nested class that has no Javadoc #69
A subclass with no Javadoc does not inherit method docs from superclass #70 Loosen the class name check, so it doesn't complain about canonical versus non-canonical class name. Make it an assertion, so we only pay the cost when running tests. When scanning for inherited Javadoc, rework the "isEmpty" check. Instead of bailing out early if the subclass has no Javadoc, Wait until after the scan is complete.
1 parent ea24a3f commit 2a8bc27

File tree

5 files changed

+62
-21
lines changed

5 files changed

+62
-21
lines changed

Diff for: therapi-runtime-javadoc-scribe/src/test/java/com/github/therapi/runtimejavadoc/JavadocAnnotationProcessorTest.java

+26
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,31 @@ public void nestedClassNameIsPreserved() throws Exception {
548548
ClassJavadoc classJavadoc = expectJavadoc(c);
549549
assertEquals(DOCUMENTED_RECORD + ".Nested", classJavadoc.getName());
550550
}
551+
552+
{
553+
Class<?> c = classLoader.loadClass(DOCUMENTED_CLASS + "$NestedWithoutJavadoc");
554+
expectNoJavadoc(c);
555+
}
556+
557+
{
558+
Class<?> c = classLoader.loadClass(DOCUMENTED_RECORD + "$NestedWithoutJavadoc");
559+
expectNoJavadoc(c);
560+
}
561+
}
562+
}
563+
564+
@Test
565+
public void emptyNestedClassInheritsJavadoc() throws Exception {
566+
try (CompilationClassLoader classLoader = compile(null)) {
567+
Class<?> c = classLoader.loadClass(DOCUMENTED_CLASS + "$NestedSubclassWithoutJavadoc");
568+
ClassJavadoc classJavadoc = expectJavadoc(c);
569+
570+
final String methodName = "frobulate";
571+
Method m1 = c.getMethod(methodName, String.class, int.class);
572+
Method m2 = c.getMethod(methodName, String.class, List.class);
573+
574+
assertMethodDescriptionMatches(m1, "Frobulate <code>a</code> by <code>b</code>");
575+
assertMethodDescriptionMatches(m2, "Frobulate <code>a</code> by multiple oopsifizzle constants");
551576
}
552577
}
553578

@@ -589,6 +614,7 @@ public void noCompanionGeneratedForUndocumentedClass() throws Exception {
589614
try (CompilationClassLoader classLoader = compile(null)) {
590615
expectNoJavadoc(classLoader.loadClass(UNDOCUMENTED));
591616
expectNoJavadoc(classLoader.loadClass(BLANK_COMMENTS));
617+
expectNoJavadoc(classLoader.loadClass(BLANK_COMMENTS + "$NestedBlankBlankSubclass"));
592618
}
593619
}
594620

Diff for: therapi-runtime-javadoc-scribe/src/test/resources/javasource/bar/BlankDocumentation.java

+3
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ public class BlankDocumentation {
99
*/
1010
public void foo() {
1111
}
12+
13+
public static class NestedBlankBlankSubclass extends BlankDocumentation {
14+
}
1215
}

Diff for: therapi-runtime-javadoc-scribe/src/test/resources/javasource/foo/DocumentedClass.java

+7
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,11 @@ public boolean equals(Object o) {
128128
*/
129129
public static class Nested {
130130
}
131+
132+
public static class NestedWithoutJavadoc {
133+
}
134+
135+
public static class NestedSubclassWithoutJavadoc extends DocumentedClass<Object> {
136+
}
137+
131138
}

Diff for: therapi-runtime-javadoc-scribe/src/test/resources/javasource/foo/DocumentedRecord.java

+3
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,7 @@ public String someMethod(boolean frobulate) {
3232
*/
3333
public static class Nested {
3434
}
35+
36+
public static class NestedWithoutJavadoc {
37+
}
3538
}

Diff for: therapi-runtime-javadoc/src/main/java/com/github/therapi/runtimejavadoc/ClassJavadoc.java

+23-21
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ private ClassJavadoc(String name, Comment comment, Map<String, FieldJavadoc> fie
8888
}
8989

9090
public static ClassJavadoc createEmpty(String qualifiedClassName) {
91-
return new ClassJavadoc(qualifiedClassName, null, (List<FieldJavadoc>) null, null, null, null, null, null,
92-
null) {
91+
return new ClassJavadoc(qualifiedClassName, null, (List<FieldJavadoc>) null, null, null, null, null, null, null) {
9392
@Override
9493
public boolean isEmpty() {
9594
return true;
@@ -98,26 +97,29 @@ public boolean isEmpty() {
9897
}
9998

10099
ClassJavadoc createEnhancedClassJavadoc(Class<?> clazz) {
101-
if (!getName().equals(clazz.getCanonicalName())) {
102-
throw new IllegalArgumentException("Class `" + clazz.getCanonicalName() + "` does not match class doc for `" + getName() + "`");
103-
}
104-
105-
if (isEmpty()) {
106-
return this;
107-
}
100+
assert getName().replace("$", ".").equals(clazz.getCanonicalName())
101+
: "Class `" + clazz.getName() + "` does not match class doc for `" + getName() + "`";
108102

109103
Map<String, ClassJavadoc> classJavadocCache = new HashMap<>();
110104

111105
classJavadocCache.put(clazz.getCanonicalName(), this);
112106
getAllTypeAncestors(clazz).forEach(cls -> classJavadocCache.put(cls.getCanonicalName(), RuntimeJavadoc.getSkinnyClassJavadoc(cls)));
113107

114108
Map<MethodSignature, MethodJavadoc> methodJavadocs = new LinkedHashMap<>();
115-
Arrays.stream(clazz.getDeclaredMethods())
116-
.forEach(method -> methodJavadocs.put(MethodSignature.from(method),
117-
RuntimeJavadoc.getJavadoc(method, classJavadocCache)));
109+
Arrays.stream(clazz.getMethods())
110+
.forEach(method -> {
111+
MethodJavadoc methodJavadoc = RuntimeJavadoc.getJavadoc(method, classJavadocCache);
112+
if (!methodJavadoc.isEmpty()) {
113+
methodJavadocs.put(MethodSignature.from(method), methodJavadoc);
114+
}
115+
});
116+
117+
if (methodJavadocs.isEmpty()) {
118+
return this; // didn't inherit anything.
119+
}
118120

119121
return new ClassJavadoc(getName(), getComment(), fields, enumConstants, methodJavadocs, constructors,
120-
getOther(), getSeeAlso(), recordComponents);
122+
getOther(), getSeeAlso(), recordComponents);
121123
}
122124

123125
public List<FieldJavadoc> getFields() {
@@ -172,13 +174,13 @@ ParamJavadoc findRecordComponent(String recordComponent) {
172174
@Override
173175
public String toString() {
174176
return "ClassJavadoc{"
175-
+ "name='" + getName() + '\''
176-
+ ", comment=" + getComment()
177-
+ ", fields=" + fields
178-
+ ", methods=" + methods
179-
+ ", constructors=" + constructors
180-
+ ", recordComponents=" + recordComponents
181-
+ ", seeAlso=" + getSeeAlso()
182-
+ ", other=" + getOther() + '}';
177+
+ "name='" + getName() + '\''
178+
+ ", comment=" + getComment()
179+
+ ", fields=" + fields
180+
+ ", methods=" + methods
181+
+ ", constructors=" + constructors
182+
+ ", recordComponents=" + recordComponents
183+
+ ", seeAlso=" + getSeeAlso()
184+
+ ", other=" + getOther() + '}';
183185
}
184186
}

0 commit comments

Comments
 (0)