Skip to content

Commit

Permalink
fix: Inherit formal javadoc type parameters (#5869)
Browse files Browse the repository at this point in the history
  • Loading branch information
I-Al-Istannen committed Jun 30, 2024
1 parent 98d1c16 commit b511afd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ public List<JavadocElement> completeJavadocWithInheritedTags(CtElement element,
.stream()
.map(CtNamedElement::getSimpleName)
.collect(Collectors.toCollection(LinkedHashSet::new)); // keep decl order
method.getFormalCtTypeParameters()
.stream()
.map(it -> "<" + it.getSimpleName() + ">")
.forEach(paramsToFind::add);
view.getBlockTagArguments(StandardJavadocTagType.PARAM, JavadocText.class)
.forEach(it -> paramsToFind.remove(it.getText()));

Expand All @@ -161,7 +165,7 @@ public List<JavadocElement> completeJavadocWithInheritedTags(CtElement element,
boolean needsReturn = needsReturn(view);
boolean needsBody = view.getBody().isEmpty();

InheritedJavadoc inheritedJavadoc = lookupInheritedDocForMethod(method);
InheritedJavadoc inheritedJavadoc = lookupInheritedDocForMethod(method, paramsToFind, throwsToFind);

// Order by body -> param -> return -> throws
List<JavadocElement> newElements = new ArrayList<>();
Expand Down Expand Up @@ -204,12 +208,18 @@ private static boolean needsReturn(JavadocCommentView view) {
return start.getTagType() != StandardJavadocTagType.RETURN;
}

private static InheritedJavadoc lookupInheritedDocForMethod(CtMethod<?> method) {
private static InheritedJavadoc lookupInheritedDocForMethod(
CtMethod<?> method,
Set<String> paramsToFind,
Set<String> throwsToFind
) {
List<CtMethod<?>> targets = new InheritanceResolver()
.findSuperMethodsInCommentInheritanceOrder(method.getDeclaringType(), method);

List<JavadocElement> body = new ArrayList<>();
JavadocInheritanceCollectionVisitor visitor = new JavadocInheritanceCollectionVisitor(method);
JavadocInheritanceCollectionVisitor visitor = new JavadocInheritanceCollectionVisitor(
paramsToFind, throwsToFind
);

for (CtMethod<?> target : targets) {
if (visitor.isFinished() && !body.isEmpty()) {
Expand Down Expand Up @@ -277,14 +287,9 @@ private static class JavadocInheritanceCollectionVisitor implements JavadocVisit
private final Map<String, JavadocBlockTag> throwsClauses;
private JavadocBlockTag returnTag;

public JavadocInheritanceCollectionVisitor(CtMethod<?> method) {
this.missingParameters = method.getParameters().stream()
.map(CtNamedElement::getSimpleName)
.collect(Collectors.toCollection(HashSet::new));
this.missingThrowsClauses = method.getThrownTypes().stream()
.map(CtTypeInformation::getQualifiedName)
.collect(Collectors.toCollection(HashSet::new));

public JavadocInheritanceCollectionVisitor(Set<String> missingParameters, Set<String> missingThrowsClauses) {
this.missingParameters = new HashSet<>(missingParameters);
this.missingThrowsClauses = new HashSet<>(missingThrowsClauses);
this.returnTag = null;
this.params = new HashMap<>();
this.throwsClauses = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private static CompletedDoc inheritTestFromSuper(String javadoc) {
public static class Sub extends Super {
%s
@Override
public int test(int a) throws IOException { return a; }
public <T> int test(int a) throws IOException { return a; }
}
""".formatted(packageName, InheritanceResolverTest.class.getName() + ".Super", javadoc),
subclassName.replace(".", "/")
Expand All @@ -179,10 +179,11 @@ static class Super {
* This is a foo method.
*
* @param a the a param
* @param <T> a type param
* @return foo
* @throws IOException never.
*/
public int test(int a) throws IOException {
public <T> int test(int a) throws IOException {
return a;
}
}
Expand All @@ -191,15 +192,15 @@ private interface SuperInt1 {
/**
* A test.
*/
int test(int a) throws IOException;
<T> int test(int a) throws IOException;
}

private interface SuperInt2 extends SuperInt1 {
/**
* @param a the a param
*/
@Override
int test(int a) throws IOException;
<T> int test(int a) throws IOException;
}

private interface SuperInt3 {
Expand All @@ -208,21 +209,21 @@ private interface SuperInt3 {
*
* @throws IOException never
*/
int test(int a) throws IOException;
<T> int test(int a) throws IOException;
}

private interface SuperInt4 {
/**
* @return foo
*/
int test(int a) throws IOException;
<T> int test(int a) throws IOException;
}

private static class SubMultiSuperParent {
/**
* Never used in <22, shadowed by the interface.
*/
public int test(int a) throws IOException {
public <T> int test(int a) throws IOException {
return 0;
}
}
Expand Down

0 comments on commit b511afd

Please sign in to comment.