[java] More PMD dogfood fixes
* ClassWithOnlyPrivateConstructorsShouldBeFinal * SimplifyBooleanReturns * LiteralsFirstInComparisons * UnnecessaryReturn * LooseCoupling
This commit is contained in:
43 files changed
+139
-244
No files matched your search
+1
-1
@@ -23,7 +23,7 @@ import net.sourceforge.pmd.lang.java.types.TypeOps;
|
||||
* @author Clément Fournier
|
||||
* @since 7.0.0
|
||||
*/
|
||||
class OverrideResolutionPass {
|
||||
final class OverrideResolutionPass {
|
||||
|
||||
private OverrideResolutionPass() {
|
||||
|
||||
|
||||
+2
-6
@@ -315,12 +315,8 @@ public class UnnecessaryImportRule extends AbstractJavaRule {
|
||||
// maybe we're importing a subclass of the container.
|
||||
TypeSystem ts = symbolOwner.getTypeSystem();
|
||||
JClassSymbol importedContainer = ts.getClassSymbol(it.node.getImportedName());
|
||||
if (importedContainer != null) {
|
||||
return TypeTestUtil.isA(ts.rawType(symbolOwner), ts.rawType(importedContainer));
|
||||
} else {
|
||||
// insufficient classpath, err towards FNs
|
||||
return true;
|
||||
}
|
||||
return importedContainer == null // insufficient classpath, err towards FNs
|
||||
|| TypeTestUtil.isA(ts.rawType(symbolOwner), ts.rawType(importedContainer));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
+1
-1
@@ -132,7 +132,7 @@ public class CouplingBetweenObjectsRule extends AbstractJavaRule {
|
||||
}
|
||||
JTypeDeclSymbol symbol = t.getSymbol();
|
||||
return symbol == null
|
||||
|| symbol.getPackageName().equals(JAccessibleElementSymbol.PRIMITIVE_PACKAGE)
|
||||
|| JAccessibleElementSymbol.PRIMITIVE_PACKAGE.equals(symbol.getPackageName())
|
||||
|| t.isPrimitive()
|
||||
|| t.isBoxedPrimitive();
|
||||
}
|
||||
|
||||
+4
-6
@@ -90,7 +90,7 @@ public class LawOfDemeterRule extends AbstractJavaRule {
|
||||
* Collects the information of one identified method call. The method call
|
||||
* might be a violation of the Law of Demeter or not.
|
||||
*/
|
||||
private static class MethodCall {
|
||||
private static final class MethodCall {
|
||||
private static final String METHOD_CALL_CHAIN = "result from previous method call";
|
||||
private static final String SIMPLE_ASSIGNMENT_OPERATOR = "=";
|
||||
private static final String SCOPE_METHOD_CHAINING = "method-chaining";
|
||||
@@ -193,10 +193,8 @@ public class LawOfDemeterRule extends AbstractJavaRule {
|
||||
|
||||
private static boolean isNotLiteral(ASTPrimaryExpression expression) {
|
||||
ASTPrimaryPrefix prefix = expression.getFirstDescendantOfType(ASTPrimaryPrefix.class);
|
||||
if (prefix != null) {
|
||||
return !prefix.hasDescendantOfType(ASTLiteral.class);
|
||||
}
|
||||
return true;
|
||||
return prefix == null
|
||||
|| !prefix.hasDescendantOfType(ASTLiteral.class);
|
||||
}
|
||||
|
||||
private boolean isNotBuilder() {
|
||||
@@ -275,7 +273,7 @@ public class LawOfDemeterRule extends AbstractJavaRule {
|
||||
violationReason = null;
|
||||
|
||||
if (baseNameInWhitelist) {
|
||||
return;
|
||||
violation = false;
|
||||
} else if (SCOPE_LOCAL.equals(baseScope)) {
|
||||
Assignment lastAssignment = determineLastAssignment();
|
||||
if (lastAssignment != null && !lastAssignment.allocation && !lastAssignment.iterator
|
||||
|
||||
+1
-6
@@ -48,12 +48,7 @@ public class CommentSizeRule extends AbstractJavaRulechainRule {
|
||||
}
|
||||
|
||||
private static boolean hasRealText(String line) {
|
||||
|
||||
if (StringUtils.isBlank(line)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !IGNORED_LINES.contains(line.trim());
|
||||
return !StringUtils.isBlank(line) && !IGNORED_LINES.contains(line.trim());
|
||||
}
|
||||
|
||||
private boolean hasTooManyLines(Comment comment) {
|
||||
|
||||
+2
-4
@@ -47,10 +47,8 @@ public class CloneMethodMustImplementCloneableRule extends AbstractJavaRulechain
|
||||
}
|
||||
|
||||
private static boolean justThrowsCloneNotSupported(ASTBlock body) {
|
||||
if (body.size() != 1) {
|
||||
return false;
|
||||
}
|
||||
return body.getChild(0)
|
||||
return body.size() == 1
|
||||
&& body.getChild(0)
|
||||
.asStream()
|
||||
.filterIs(ASTThrowStatement.class)
|
||||
.map(ASTThrowStatement::getExpr)
|
||||
|
||||
+1
-5
@@ -81,11 +81,7 @@ final class AnnotationSuppressionUtil {
|
||||
*/
|
||||
private static boolean suppresses(final Node node, Rule rule) {
|
||||
Annotatable suppressor = getSuppressor(node);
|
||||
if (suppressor == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return hasSuppressWarningsAnnotationFor(suppressor, rule);
|
||||
return suppressor != null && hasSuppressWarningsAnnotationFor(suppressor, rule);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
+6
-8
@@ -296,7 +296,7 @@ public final class DataflowPass {
|
||||
}
|
||||
}
|
||||
|
||||
private static class ReachingDefsVisitor extends JavaVisitorBase<SpanInfo, SpanInfo> {
|
||||
private static final class ReachingDefsVisitor extends JavaVisitorBase<SpanInfo, SpanInfo> {
|
||||
|
||||
|
||||
static final ReachingDefsVisitor ONLY_LOCALS = new ReachingDefsVisitor(null, false);
|
||||
@@ -862,11 +862,9 @@ public final class DataflowPass {
|
||||
}
|
||||
|
||||
private boolean isRelevantField(ASTExpression lhs) {
|
||||
if (!(lhs instanceof ASTNamedReferenceExpr)) {
|
||||
return false;
|
||||
}
|
||||
return trackThisInstance() && JavaRuleUtil.isThisFieldAccess(lhs)
|
||||
|| trackStaticFields() && isStaticFieldOfThisClass(((ASTNamedReferenceExpr) lhs).getReferencedSym());
|
||||
return (lhs instanceof ASTNamedReferenceExpr)
|
||||
&& (trackThisInstance() && JavaRuleUtil.isThisFieldAccess(lhs)
|
||||
|| trackStaticFields() && isStaticFieldOfThisClass(((ASTNamedReferenceExpr) lhs).getReferencedSym()));
|
||||
}
|
||||
|
||||
private boolean isStaticFieldOfThisClass(JVariableSymbol var) {
|
||||
@@ -1022,7 +1020,7 @@ public final class DataflowPass {
|
||||
* The shared state for all {@link SpanInfo} instances in the same
|
||||
* toplevel class.
|
||||
*/
|
||||
private static class GlobalAlgoState {
|
||||
private static final class GlobalAlgoState {
|
||||
|
||||
final Set<AssignmentEntry> allAssignments;
|
||||
final Set<AssignmentEntry> usedAssignments;
|
||||
@@ -1082,7 +1080,7 @@ public final class DataflowPass {
|
||||
/**
|
||||
* Information about a span of code.
|
||||
*/
|
||||
private static class SpanInfo {
|
||||
private static final class SpanInfo {
|
||||
|
||||
// spans are arranged in a tree, to look for enclosing finallies
|
||||
// when abrupt completion occurs. Blocks that have non-local
|
||||
|
||||
+22
-40
@@ -174,10 +174,9 @@ public final class JavaRuleUtil {
|
||||
* This also considers long literals.
|
||||
*/
|
||||
public static boolean isLiteralInt(JavaNode e, int value) {
|
||||
if (e instanceof ASTNumericLiteral) {
|
||||
return ((ASTNumericLiteral) e).isIntegral() && ((ASTNumericLiteral) e).getValueAsInt() == value;
|
||||
}
|
||||
return false;
|
||||
return e instanceof ASTNumericLiteral
|
||||
&& ((ASTNumericLiteral) e).isIntegral()
|
||||
&& ((ASTNumericLiteral) e).getValueAsInt() == value;
|
||||
}
|
||||
|
||||
/** This is type-aware, so will not pick up on numeric addition. */
|
||||
@@ -237,10 +236,8 @@ public final class JavaRuleUtil {
|
||||
* is a main method.
|
||||
*/
|
||||
public static boolean isMainMethod(JavaNode node) {
|
||||
if (node instanceof ASTMethodDeclaration) {
|
||||
return ((ASTMethodDeclaration) node).isMainMethod();
|
||||
}
|
||||
return false;
|
||||
return node instanceof ASTMethodDeclaration
|
||||
&& ((ASTMethodDeclaration) node).isMainMethod();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -508,10 +505,8 @@ public final class JavaRuleUtil {
|
||||
}
|
||||
|
||||
public static boolean isAnonymousClassCreation(@Nullable ASTExpression expression) {
|
||||
if (expression instanceof ASTConstructorCall) {
|
||||
return ((ASTConstructorCall) expression).isAnonymousClass();
|
||||
}
|
||||
return false;
|
||||
return expression instanceof ASTConstructorCall
|
||||
&& ((ASTConstructorCall) expression).isAnonymousClass();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -689,14 +684,12 @@ public final class JavaRuleUtil {
|
||||
return false;
|
||||
}
|
||||
JVariableSymbol sym = ((ASTNamedReferenceExpr) e).getReferencedSym();
|
||||
if (sym instanceof JFieldSymbol) {
|
||||
return !((JFieldSymbol) sym).isStatic()
|
||||
return sym instanceof JFieldSymbol
|
||||
&& !((JFieldSymbol) sym).isStatic()
|
||||
// not inherited
|
||||
&& ((JFieldSymbol) sym).getEnclosingClass().equals(e.getEnclosingType().getSymbol())
|
||||
// correct syntactic form
|
||||
&& (e instanceof ASTVariableAccess || isSyntacticThisFieldAccess(e));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -744,10 +737,8 @@ public final class JavaRuleUtil {
|
||||
* that references the symbol.
|
||||
*/
|
||||
public static boolean isReferenceToVar(@Nullable ASTExpression expression, @NonNull JVariableSymbol symbol) {
|
||||
if (expression instanceof ASTNamedReferenceExpr) {
|
||||
return symbol.equals(((ASTNamedReferenceExpr) expression).getReferencedSym());
|
||||
}
|
||||
return false;
|
||||
return expression instanceof ASTNamedReferenceExpr
|
||||
&& symbol.equals(((ASTNamedReferenceExpr) expression).getReferencedSym());
|
||||
}
|
||||
|
||||
public static boolean isUnqualifiedThis(ASTExpression e) {
|
||||
@@ -763,10 +754,8 @@ public final class JavaRuleUtil {
|
||||
* that references any of the symbol in the set.
|
||||
*/
|
||||
public static boolean isReferenceToVar(@Nullable ASTExpression expression, @NonNull Set<? extends JVariableSymbol> symbols) {
|
||||
if (expression instanceof ASTNamedReferenceExpr) {
|
||||
return symbols.contains(((ASTNamedReferenceExpr) expression).getReferencedSym());
|
||||
}
|
||||
return false;
|
||||
return expression instanceof ASTNamedReferenceExpr
|
||||
&& symbols.contains(((ASTNamedReferenceExpr) expression).getReferencedSym());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -817,10 +806,8 @@ public final class JavaRuleUtil {
|
||||
* Returns true if the expression is a reference to a local variable.
|
||||
*/
|
||||
public static boolean isReferenceToLocal(ASTExpression expr) {
|
||||
if (expr instanceof ASTVariableAccess) {
|
||||
return ((ASTVariableAccess) expr).getReferencedSym() instanceof AstLocalVarSym;
|
||||
}
|
||||
return false;
|
||||
return expr instanceof ASTVariableAccess
|
||||
&& ((ASTVariableAccess) expr).getReferencedSym() instanceof AstLocalVarSym;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -920,14 +907,12 @@ public final class JavaRuleUtil {
|
||||
&& (isNonLocalLhs(lhs) || isReferenceToVar(lhs, localVarsToTrack));
|
||||
}
|
||||
|
||||
if (e.ancestors(ASTThrowStatement.class).nonEmpty()) {
|
||||
// then this side effect can never be observed in containing code,
|
||||
// because control flow jumps out of the method
|
||||
return false;
|
||||
}
|
||||
|
||||
return e instanceof ASTMethodCall && !isPure((ASTMethodCall) e)
|
||||
|| e instanceof ASTConstructorCall;
|
||||
// when there are throw statements,
|
||||
// then this side effect can never be observed in containing code,
|
||||
// because control flow jumps out of the method
|
||||
return e.ancestors(ASTThrowStatement.class).isEmpty()
|
||||
&& (e instanceof ASTMethodCall && !isPure((ASTMethodCall) e)
|
||||
|| e instanceof ASTConstructorCall);
|
||||
}
|
||||
|
||||
private static boolean isNonLocalLhs(ASTExpression lhs) {
|
||||
@@ -996,10 +981,7 @@ public final class JavaRuleUtil {
|
||||
}
|
||||
|
||||
public static boolean isArrayInitializer(ASTExpression expr) {
|
||||
if (expr instanceof ASTArrayAllocation) {
|
||||
return ((ASTArrayAllocation) expr).getArrayInitializer() != null;
|
||||
}
|
||||
return false;
|
||||
return expr instanceof ASTArrayAllocation && ((ASTArrayAllocation) expr).getArrayInitializer() != null;
|
||||
}
|
||||
|
||||
public static boolean isCloneMethod(ASTMethodDeclaration node) {
|
||||
|
||||
+2
-4
@@ -77,10 +77,8 @@ public final class StablePathMatcher {
|
||||
return Objects.equals(((ASTVariableAccess) e).getReferencedSym(), owner);
|
||||
} else if (e instanceof ASTFieldAccess) {
|
||||
ASTFieldAccess fieldAccess = (ASTFieldAccess) e;
|
||||
if (!JavaRuleUtil.isUnqualifiedThis(fieldAccess.getQualifier())) {
|
||||
return false;
|
||||
}
|
||||
return Objects.equals(fieldAccess.getReferencedSym(), owner);
|
||||
return JavaRuleUtil.isUnqualifiedThis(fieldAccess.getQualifier())
|
||||
&& Objects.equals(fieldAccess.getReferencedSym(), owner);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
+3
-5
@@ -136,11 +136,9 @@ public final class TestFrameworksUtil {
|
||||
public static boolean isCallOnAssertionContainer(ASTMethodCall call) {
|
||||
JTypeMirror declaring = call.getMethodType().getDeclaringType();
|
||||
JTypeDeclSymbol sym = declaring.getSymbol();
|
||||
if (sym instanceof JClassSymbol) {
|
||||
return ASSERT_CONTAINERS.contains(((JClassSymbol) sym).getBinaryName())
|
||||
|| TypeTestUtil.isA("junit.framework.Assert", declaring);
|
||||
}
|
||||
return false;
|
||||
return sym instanceof JClassSymbol
|
||||
&& (ASSERT_CONTAINERS.contains(((JClassSymbol) sym).getBinaryName())
|
||||
|| TypeTestUtil.isA("junit.framework.Assert", declaring));
|
||||
}
|
||||
|
||||
public static boolean isProbableAssertCall(ASTMethodCall call) {
|
||||
|
||||
+1
-1
@@ -276,7 +276,7 @@ public class ConsecutiveLiteralAppendsRule extends AbstractJavaRulechainRule {
|
||||
|| TypeTestUtil.isA(StringBuilder.class, node);
|
||||
}
|
||||
|
||||
private static class ConsecutiveCounter {
|
||||
private static final class ConsecutiveCounter {
|
||||
private int threshold;
|
||||
private int counter;
|
||||
private Node reportNode;
|
||||
|
||||
+2
-2
@@ -49,7 +49,7 @@ final class FlexibleUnresolvedClassImpl extends UnresolvedClassImpl {
|
||||
void setTypeParameterCount(int newArity) {
|
||||
if (arity == UNKNOWN_ARITY) {
|
||||
this.arity = newArity;
|
||||
ArrayList<JTypeVar> newParams = new ArrayList<>(newArity);
|
||||
List<JTypeVar> newParams = new ArrayList<>(newArity);
|
||||
for (int i = 0; i < newArity; i++) {
|
||||
newParams.add(new FakeTypeParam("T" + i, getTypeSystem(), this).getTypeMirror());
|
||||
}
|
||||
@@ -83,7 +83,7 @@ final class FlexibleUnresolvedClassImpl extends UnresolvedClassImpl {
|
||||
return tparams;
|
||||
}
|
||||
|
||||
private static class FakeTypeParam implements JTypeParameterSymbol {
|
||||
private static final class FakeTypeParam implements JTypeParameterSymbol {
|
||||
|
||||
private final String name;
|
||||
private final JTypeParameterOwnerSymbol owner;
|
||||
|
||||
+1
-1
@@ -32,7 +32,7 @@ public class SymbolToStrings {
|
||||
return symbol.acceptVisitor(visitor, new StringBuilder()).toString();
|
||||
}
|
||||
|
||||
private static class ToStringVisitor implements SymbolVisitor<StringBuilder, StringBuilder> {
|
||||
private static final class ToStringVisitor implements SymbolVisitor<StringBuilder, StringBuilder> {
|
||||
|
||||
private final String impl;
|
||||
|
||||
|
||||
+3
-1
@@ -7,6 +7,7 @@ package net.sourceforge.pmd.lang.java.symbols.internal.asm;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
@@ -30,7 +31,7 @@ public class AsmSymbolResolver implements SymbolResolver {
|
||||
private final Classpath classLoader;
|
||||
private final SignatureParser typeLoader;
|
||||
|
||||
private final ConcurrentHashMap<String, SoftClassReference> knownStubs = new ConcurrentHashMap<>();
|
||||
private final ConcurrentMap<String, SoftClassReference> knownStubs = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* Sentinel for when we fail finding a URL. This allows using a single map,
|
||||
@@ -124,6 +125,7 @@ public class AsmSymbolResolver implements SymbolResolver {
|
||||
knownStubs.put(internalName, softRef);
|
||||
}
|
||||
|
||||
@SuppressWarnings("PMD.CompareObjectsWithEquals") // SoftClassReference
|
||||
@NonNull JClassSymbol resolveFromInternalNameCannotFail(@NonNull String internalName, int observedArity) {
|
||||
return knownStubs.compute(internalName, (iname, prev) -> {
|
||||
if (prev != failed && prev != null) {
|
||||
|
||||
+3
-2
@@ -8,6 +8,7 @@ import static java.util.Collections.emptyList;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Deque;
|
||||
import java.util.List;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
@@ -276,8 +277,8 @@ final class TypeSigParser {
|
||||
abstract static class TypeScanner extends SignatureScanner {
|
||||
|
||||
// those stacks usually are 0..1
|
||||
private final ArrayDeque<JTypeMirror> typeStack = new ArrayDeque<>(0);
|
||||
private final ArrayDeque<List<JTypeMirror>> listStack = new ArrayDeque<>(0);
|
||||
private final Deque<JTypeMirror> typeStack = new ArrayDeque<>(0);
|
||||
private final Deque<List<JTypeMirror>> listStack = new ArrayDeque<>(0);
|
||||
|
||||
private final TypeSystem ts;
|
||||
private final LexicalScope lexicalScope;
|
||||
|
||||
+2
-2
@@ -55,7 +55,7 @@ public final class CoreResolvers {
|
||||
return new SingularMapResolver<>(singular);
|
||||
}
|
||||
|
||||
private static class SingularMapResolver<S> implements SingleNameResolver<S> {
|
||||
private static final class SingularMapResolver<S> implements SingleNameResolver<S> {
|
||||
|
||||
private final Map<String, S> map;
|
||||
|
||||
@@ -127,7 +127,7 @@ public final class CoreResolvers {
|
||||
return EmptyResolver.INSTANCE;
|
||||
}
|
||||
|
||||
private static class EmptyResolver<S> implements SingleNameResolver<S> {
|
||||
private static final class EmptyResolver<S> implements SingleNameResolver<S> {
|
||||
|
||||
private static final EmptyResolver INSTANCE = new EmptyResolver<>();
|
||||
|
||||
|
||||
+4
-4
@@ -23,7 +23,7 @@ import net.sourceforge.pmd.internal.util.AssertionUtil;
|
||||
* An unmodifiable multimap type, efficient if the single-value case is the
|
||||
* most common.
|
||||
*/
|
||||
class MostlySingularMultimap<K, V> {
|
||||
final class MostlySingularMultimap<K, V> {
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private static final MostlySingularMultimap EMPTY = new MostlySingularMultimap<>(Collections.emptyMap());
|
||||
@@ -111,7 +111,7 @@ class MostlySingularMultimap<K, V> {
|
||||
/**
|
||||
* Builder for a multimap. Can only be used once.
|
||||
*/
|
||||
public static class Builder<K, V> {
|
||||
public static final class Builder<K, V> {
|
||||
|
||||
private final MapMaker<K> mapMaker;
|
||||
private @Nullable Map<K, Object> map;
|
||||
@@ -225,7 +225,7 @@ class MostlySingularMultimap<K, V> {
|
||||
if (noDuplicate && vs.equals(v)) {
|
||||
return vs;
|
||||
}
|
||||
VList<V> vs2 = new VList<>(2);
|
||||
List<V> vs2 = new VList<>(2);
|
||||
isSingular = false;
|
||||
vs2.add((V) vs);
|
||||
vs2.add(v);
|
||||
@@ -241,7 +241,7 @@ class MostlySingularMultimap<K, V> {
|
||||
public @Nullable Map<K, V> buildAsSingular() {
|
||||
consume();
|
||||
if (!isSingular) {
|
||||
return null;
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
return (Map<K, V>) map;
|
||||
}
|
||||
|
||||
+3
-2
@@ -6,10 +6,10 @@ package net.sourceforge.pmd.lang.java.symbols.table.internal;
|
||||
|
||||
import static net.sourceforge.pmd.lang.java.symbols.table.internal.JavaSemanticErrors.AMBIGUOUS_NAME_REFERENCE;
|
||||
import static net.sourceforge.pmd.lang.java.symbols.table.internal.JavaSemanticErrors.CANNOT_RESOLVE_MEMBER;
|
||||
import static net.sourceforge.pmd.lang.java.types.JVariableSig.FieldSig;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
@@ -25,6 +25,7 @@ import net.sourceforge.pmd.lang.java.symbols.JTypeParameterSymbol;
|
||||
import net.sourceforge.pmd.lang.java.symbols.table.JSymbolTable;
|
||||
import net.sourceforge.pmd.lang.java.types.JClassType;
|
||||
import net.sourceforge.pmd.lang.java.types.JTypeMirror;
|
||||
import net.sourceforge.pmd.lang.java.types.JVariableSig.FieldSig;
|
||||
|
||||
/**
|
||||
* Context of a usage reference ("in which class does the name occur?"),
|
||||
@@ -81,7 +82,7 @@ public final class ReferenceCtx {
|
||||
return null;
|
||||
} else if (found.size() > 1) {
|
||||
// FIXME when type is reachable through several paths, there may be duplicates!
|
||||
HashSet<? extends T> distinct = new HashSet<>(found);
|
||||
Set<? extends T> distinct = new HashSet<>(found);
|
||||
if (distinct.size() == 1) {
|
||||
return distinct.iterator().next();
|
||||
}
|
||||
|
||||
+1
@@ -59,6 +59,7 @@ public enum SuperTypesEnumerator {
|
||||
@Nullable JClassType sup = t.getSuperClass();
|
||||
List<JClassType> superItfs = t.getSuperInterfaces();
|
||||
|
||||
@SuppressWarnings("PMD.LooseCoupling") // the set should keep insertion order
|
||||
LinkedHashSet<JClassType> set;
|
||||
if (sup != null) {
|
||||
set = new LinkedHashSet<>(superItfs.size() + 1);
|
||||
|
||||
+1
-1
@@ -84,7 +84,7 @@ public final class SymbolTableResolver {
|
||||
} while (!todo.isEmpty());
|
||||
}
|
||||
|
||||
private static class DeferredNode {
|
||||
private static final class DeferredNode {
|
||||
|
||||
final JavaNode node;
|
||||
// this is data used to resume the traversal
|
||||
|
||||
+2
-5
@@ -37,11 +37,8 @@ public class DeclarationFinderFunction implements Predicate<NameDeclaration> {
|
||||
}
|
||||
|
||||
private boolean isDeclaredBefore(NameDeclaration nameDeclaration) {
|
||||
if (nameDeclaration.getNode() != null && occurrence.getLocation() != null) {
|
||||
return nameDeclaration.getNode().getBeginLine() <= occurrence.getLocation().getBeginLine();
|
||||
}
|
||||
|
||||
return true;
|
||||
return nameDeclaration.getNode() == null || occurrence.getLocation() == null
|
||||
|| nameDeclaration.getNode().getBeginLine() <= occurrence.getLocation().getBeginLine();
|
||||
}
|
||||
|
||||
private boolean isSameName(NameDeclaration nameDeclaration) {
|
||||
|
||||
+6
-21
@@ -96,23 +96,11 @@ public class JavaNameOccurrence implements NameOccurrence {
|
||||
+ " (location line " + location.getBeginLine() + " col " + location.getBeginColumn() + ")");
|
||||
}
|
||||
|
||||
if (isStandAlonePostfix(primaryExpression)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (primaryExpression.getNumChildren() <= 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!(primaryExpression.getChild(1) instanceof ASTAssignmentOperator)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isPartOfQualifiedName() /* or is an array type */) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !isCompoundAssignment(primaryExpression);
|
||||
return isStandAlonePostfix(primaryExpression)
|
||||
|| primaryExpression.getNumChildren() > 1
|
||||
&& primaryExpression.getChild(1) instanceof ASTAssignmentOperator
|
||||
&& !isPartOfQualifiedName() /* and is not an array type */
|
||||
&& !isCompoundAssignment(primaryExpression);
|
||||
}
|
||||
|
||||
private boolean isCompoundAssignment(Node primaryExpression) {
|
||||
@@ -134,11 +122,8 @@ public class JavaNameOccurrence implements NameOccurrence {
|
||||
|
||||
ASTPrimaryPrefix pf = (ASTPrimaryPrefix) ((ASTPrimaryExpression) primaryExpression.getChild(0))
|
||||
.getChild(0);
|
||||
if (pf.usesThisModifier()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return thirdChildHasDottedName(primaryExpression);
|
||||
return pf.usesThisModifier() || thirdChildHasDottedName(primaryExpression);
|
||||
}
|
||||
|
||||
private boolean thirdChildHasDottedName(Node primaryExpression) {
|
||||
|
||||
@@ -12,6 +12,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
import net.sourceforge.pmd.annotation.InternalApi;
|
||||
import net.sourceforge.pmd.lang.java.typeresolution.PMDASMClassLoader;
|
||||
@@ -291,7 +292,7 @@ public class TypeSet {
|
||||
* cache to have ~90% hit ratio unless abusing star imports (import on
|
||||
* demand)
|
||||
*/
|
||||
private static final ConcurrentHashMap<String, Class<?>> CLASS_CACHE = new ConcurrentHashMap<>();
|
||||
private static final ConcurrentMap<String, Class<?>> CLASS_CACHE = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* Creates a {@link ImplicitImportResolver}
|
||||
|
||||
+1
-1
@@ -21,7 +21,7 @@ public interface NullableClassLoader {
|
||||
Class<?> loadClassOrNull(String binaryName);
|
||||
|
||||
|
||||
class ClassLoaderWrapper implements NullableClassLoader {
|
||||
final class ClassLoaderWrapper implements NullableClassLoader {
|
||||
|
||||
private final ClassLoader classLoader;
|
||||
|
||||
|
||||
@@ -80,10 +80,7 @@ public final class InvocationMatcher {
|
||||
* See {@link #matchesCall(InvocationNode)}.
|
||||
*/
|
||||
public boolean matchesCall(@Nullable JavaNode node) {
|
||||
if (node instanceof InvocationNode) {
|
||||
return matchesCall((InvocationNode) node);
|
||||
}
|
||||
return false;
|
||||
return node instanceof InvocationNode && matchesCall((InvocationNode) node);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -101,10 +98,8 @@ public final class InvocationMatcher {
|
||||
return false;
|
||||
}
|
||||
OverloadSelectionResult info = node.getOverloadSelectionInfo();
|
||||
if (info.isFailed() || !matchQualifier(node)) {
|
||||
return false;
|
||||
}
|
||||
return argsMatchOverload(info.getMethodType());
|
||||
return !info.isFailed() && matchQualifier(node)
|
||||
&& argsMatchOverload(info.getMethodType());
|
||||
}
|
||||
|
||||
private boolean matchQualifier(InvocationNode node) {
|
||||
@@ -289,11 +284,9 @@ public final class InvocationMatcher {
|
||||
}
|
||||
|
||||
boolean matches(JTypeMirror type, boolean exact) {
|
||||
if (name == null) {
|
||||
return true;
|
||||
}
|
||||
return exact ? TypeTestUtil.isExactlyAOrAnon(name, type) == OptionalBool.YES
|
||||
: TypeTestUtil.isA(name, type);
|
||||
return name == null
|
||||
|| (exact ? TypeTestUtil.isExactlyAOrAnon(name, type) == OptionalBool.YES
|
||||
: TypeTestUtil.isA(name, type));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -137,7 +137,7 @@ public final class JPrimitiveType implements JTypeMirror {
|
||||
FLOAT(float.class),
|
||||
DOUBLE(double.class);
|
||||
|
||||
static final EnumSet<PrimitiveTypeKind> FLOATING_POINT_TYPES = EnumSet.of(FLOAT, DOUBLE);
|
||||
static final Set<PrimitiveTypeKind> FLOATING_POINT_TYPES = EnumSet.of(FLOAT, DOUBLE);
|
||||
|
||||
final String name = name().toLowerCase(Locale.ROOT);
|
||||
private final Class<?> jvm;
|
||||
|
||||
@@ -216,11 +216,9 @@ public interface JTypeMirror extends JTypeVisitable {
|
||||
return true;
|
||||
} else if (this instanceof JArrayType) {
|
||||
return ((JArrayType) this).getElementType().isReifiable();
|
||||
} else if (this instanceof JClassType) {
|
||||
return TypeOps.allArgsAreUnboundedWildcards(((JClassType) this).getTypeArgs());
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
return this instanceof JClassType && TypeOps.allArgsAreUnboundedWildcards(((JClassType) this).getTypeArgs());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -69,6 +69,7 @@ final class Lub {
|
||||
*
|
||||
* @return null if G is not a generic type, otherwise Relevant(G)
|
||||
*/
|
||||
@SuppressWarnings("PMD.ReturnEmptyCollectionRatherThanNull") // null is explicit mentioned as a possible return value
|
||||
static @Nullable List<JClassType> relevant(JClassType g, Set<JTypeMirror> stunion) {
|
||||
if (!g.isRaw()) {
|
||||
return null;
|
||||
@@ -87,7 +88,7 @@ final class Lub {
|
||||
}
|
||||
|
||||
private static Set<JTypeMirror> erasedSuperTypes(Set<JTypeMirror> stui) {
|
||||
LinkedHashSet<JTypeMirror> erased = new LinkedHashSet<>();
|
||||
Set<JTypeMirror> erased = new LinkedHashSet<>();
|
||||
for (JTypeMirror it : stui) {
|
||||
JTypeMirror t = it instanceof JTypeVar ? it : it.getErasure();
|
||||
erased.add(t);
|
||||
|
||||
@@ -236,10 +236,8 @@ public final class TypeConversion {
|
||||
* wildcards as type arguments. Capture variables don't count.
|
||||
*/
|
||||
public static boolean isWilcardParameterized(JTypeMirror t) {
|
||||
if (!(t instanceof JClassType)) {
|
||||
return false;
|
||||
}
|
||||
return CollectionUtil.any(((JClassType) t).getTypeArgs(), it -> it instanceof JWildcardType);
|
||||
return t instanceof JClassType
|
||||
&& CollectionUtil.any(((JClassType) t).getTypeArgs(), it -> it instanceof JWildcardType);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loaded 30 of 43 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user