Skip to content

Commit

Permalink
Suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Sep 7, 2022
1 parent 9ad87ef commit 4953d01
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
18 changes: 14 additions & 4 deletions check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -809,16 +809,26 @@ private static boolean isFinal(Symbol symbol) {
}

/**
* Flag for a record class and its canonical constructor. Can be replaced by {@code
* com.sun.tools.javac.code.Flags.RECORD} once the minimum JDK version is 14.
* Flag for record types, canonical record constructors and type members that are part of a
* record's state vector. Can be replaced by {@code com.sun.tools.javac.code.Flags.RECORD} once
* the minimum JDK version is 14.
*/
private static final long RECORD_FLAG = 1L << 61;

/**
* Returns true if the given {@link Symbol} is a record or the canonical constructor of a record.
* Returns true if the given {@link Tree} is a record, a record's canonical constructor or a
* member that is part of a record's state vector.
*/
public static boolean isRecord(Tree tree) {
return isRecord(getSymbol(tree));
}

/**
* Returns true if the given {@link Symbol} is a record, a record's canonical constructor or a
* member that is part of a record's state vector.
*/
public static boolean isRecord(Symbol symbol) {
return (symbol.flags() & RECORD_FLAG) == RECORD_FLAG;
return symbol != null && (symbol.flags() & RECORD_FLAG) == RECORD_FLAG;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.errorprone.bugpatterns.javadoc;

import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.MoreCollectors.onlyElement;
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
import static com.google.errorprone.bugpatterns.javadoc.Utils.diagnosticPosition;
import static com.google.errorprone.bugpatterns.javadoc.Utils.getBestMatch;
Expand All @@ -26,6 +27,7 @@
import static com.google.errorprone.names.LevenshteinEditDistance.getEditDistance;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.MoreCollectors;
import com.google.common.collect.Sets;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
Expand Down Expand Up @@ -75,11 +77,10 @@ public final class InvalidParam extends BugChecker implements ClassTreeMatcher,
public Description matchClass(ClassTree classTree, VisitorState state) {
DocTreePath path = getDocTreePath(state);
if (path != null) {
// XXX: `isRecord`... should it also be able to handle `Tree`s?
ImmutableSet<String> parameters =
ASTHelpers.isRecord(ASTHelpers.getSymbol(classTree))
? getGeneratedConstructor(classTree).getParameters().stream()
.map(v -> v.getName().toString())
ASTHelpers.isRecord(classTree)
? getCanonicalRecordConstructor(classTree).getParameters().stream()
.map(p -> p.getName().toString())
.collect(toImmutableSet())
: ImmutableSet.of();

Expand Down Expand Up @@ -110,13 +111,12 @@ public Description matchMethod(MethodTree methodTree, VisitorState state) {
return Description.NO_MATCH;
}

private static MethodTree getGeneratedConstructor(ClassTree classTree) {
private static MethodTree getCanonicalRecordConstructor(ClassTree classTree) {
return classTree.getMembers().stream()
.filter(MethodTree.class::isInstance)
.map(MethodTree.class::cast)
.findFirst()
.filter(tree -> tree.getName().contentEquals("<init>"))
.orElseThrow(() -> new IllegalStateException("Records must have a generated ctor"));
.filter(ASTHelpers::isRecord)
.collect(onlyElement());
}

/** Checks that documented parameters match the method's parameter list. */
Expand Down

0 comments on commit 4953d01

Please sign in to comment.