diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/OverrideResolutionPass.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/OverrideResolutionPass.java index 80a9563d9f..f2f7dcc4aa 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/OverrideResolutionPass.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/OverrideResolutionPass.java @@ -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() { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryImportRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryImportRule.java index 79cf14ca2d..c34697dc07 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryImportRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryImportRule.java @@ -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)); } }); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/CouplingBetweenObjectsRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/CouplingBetweenObjectsRule.java index dfea612724..3ce63e495c 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/CouplingBetweenObjectsRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/CouplingBetweenObjectsRule.java @@ -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(); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/LawOfDemeterRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/LawOfDemeterRule.java index 574c9c7967..fb9c6777bf 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/LawOfDemeterRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/LawOfDemeterRule.java @@ -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 diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentSizeRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentSizeRule.java index 8c708b017c..6edff2a963 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentSizeRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentSizeRule.java @@ -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) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/CloneMethodMustImplementCloneableRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/CloneMethodMustImplementCloneableRule.java index 8f9328669a..e58eea9a7e 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/CloneMethodMustImplementCloneableRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/CloneMethodMustImplementCloneableRule.java @@ -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) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/AnnotationSuppressionUtil.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/AnnotationSuppressionUtil.java index 87955a509d..1396be92b9 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/AnnotationSuppressionUtil.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/AnnotationSuppressionUtil.java @@ -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 diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/DataflowPass.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/DataflowPass.java index 4bb5c89f50..0321bf67f5 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/DataflowPass.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/DataflowPass.java @@ -296,7 +296,7 @@ public final class DataflowPass { } } - private static class ReachingDefsVisitor extends JavaVisitorBase { + private static final class ReachingDefsVisitor extends JavaVisitorBase { 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 allAssignments; final Set 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 diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/JavaRuleUtil.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/JavaRuleUtil.java index 1c48bea09a..05c5d1de77 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/JavaRuleUtil.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/JavaRuleUtil.java @@ -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 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) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/StablePathMatcher.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/StablePathMatcher.java index c6df092758..65977f2dba 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/StablePathMatcher.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/StablePathMatcher.java @@ -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; } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/TestFrameworksUtil.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/TestFrameworksUtil.java index f6c9da0eb1..91470fef89 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/TestFrameworksUtil.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/TestFrameworksUtil.java @@ -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) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/ConsecutiveLiteralAppendsRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/ConsecutiveLiteralAppendsRule.java index 1238a746d1..948cae7590 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/ConsecutiveLiteralAppendsRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/ConsecutiveLiteralAppendsRule.java @@ -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; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/FlexibleUnresolvedClassImpl.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/FlexibleUnresolvedClassImpl.java index 33c4933582..f3914b9723 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/FlexibleUnresolvedClassImpl.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/FlexibleUnresolvedClassImpl.java @@ -49,7 +49,7 @@ final class FlexibleUnresolvedClassImpl extends UnresolvedClassImpl { void setTypeParameterCount(int newArity) { if (arity == UNKNOWN_ARITY) { this.arity = newArity; - ArrayList newParams = new ArrayList<>(newArity); + List 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; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/SymbolToStrings.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/SymbolToStrings.java index 169a6e4e71..8e24d277fe 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/SymbolToStrings.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/SymbolToStrings.java @@ -32,7 +32,7 @@ public class SymbolToStrings { return symbol.acceptVisitor(visitor, new StringBuilder()).toString(); } - private static class ToStringVisitor implements SymbolVisitor { + private static final class ToStringVisitor implements SymbolVisitor { private final String impl; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/asm/AsmSymbolResolver.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/asm/AsmSymbolResolver.java index 81d537a020..a0b5d4d72e 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/asm/AsmSymbolResolver.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/asm/AsmSymbolResolver.java @@ -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 knownStubs = new ConcurrentHashMap<>(); + private final ConcurrentMap 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) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/asm/TypeSigParser.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/asm/TypeSigParser.java index 91f400ac4b..d19a977b11 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/asm/TypeSigParser.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/asm/TypeSigParser.java @@ -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 typeStack = new ArrayDeque<>(0); - private final ArrayDeque> listStack = new ArrayDeque<>(0); + private final Deque typeStack = new ArrayDeque<>(0); + private final Deque> listStack = new ArrayDeque<>(0); private final TypeSystem ts; private final LexicalScope lexicalScope; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/coreimpl/CoreResolvers.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/coreimpl/CoreResolvers.java index 52fc80d219..8a3deb6478 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/coreimpl/CoreResolvers.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/coreimpl/CoreResolvers.java @@ -55,7 +55,7 @@ public final class CoreResolvers { return new SingularMapResolver<>(singular); } - private static class SingularMapResolver implements SingleNameResolver { + private static final class SingularMapResolver implements SingleNameResolver { private final Map map; @@ -127,7 +127,7 @@ public final class CoreResolvers { return EmptyResolver.INSTANCE; } - private static class EmptyResolver implements SingleNameResolver { + private static final class EmptyResolver implements SingleNameResolver { private static final EmptyResolver INSTANCE = new EmptyResolver<>(); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/coreimpl/MostlySingularMultimap.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/coreimpl/MostlySingularMultimap.java index d347e0f841..8d7f0aa582 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/coreimpl/MostlySingularMultimap.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/coreimpl/MostlySingularMultimap.java @@ -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 { +final class MostlySingularMultimap { @SuppressWarnings("rawtypes") private static final MostlySingularMultimap EMPTY = new MostlySingularMultimap<>(Collections.emptyMap()); @@ -111,7 +111,7 @@ class MostlySingularMultimap { /** * Builder for a multimap. Can only be used once. */ - public static class Builder { + public static final class Builder { private final MapMaker mapMaker; private @Nullable Map map; @@ -225,7 +225,7 @@ class MostlySingularMultimap { if (noDuplicate && vs.equals(v)) { return vs; } - VList vs2 = new VList<>(2); + List vs2 = new VList<>(2); isSingular = false; vs2.add((V) vs); vs2.add(v); @@ -241,7 +241,7 @@ class MostlySingularMultimap { public @Nullable Map buildAsSingular() { consume(); if (!isSingular) { - return null; + return Collections.emptyMap(); } return (Map) map; } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/ReferenceCtx.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/ReferenceCtx.java index 71fad661d2..063f1613c8 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/ReferenceCtx.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/ReferenceCtx.java @@ -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 distinct = new HashSet<>(found); + Set distinct = new HashSet<>(found); if (distinct.size() == 1) { return distinct.iterator().next(); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/SuperTypesEnumerator.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/SuperTypesEnumerator.java index b60fb4650d..3890162836 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/SuperTypesEnumerator.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/SuperTypesEnumerator.java @@ -59,6 +59,7 @@ public enum SuperTypesEnumerator { @Nullable JClassType sup = t.getSuperClass(); List superItfs = t.getSuperInterfaces(); + @SuppressWarnings("PMD.LooseCoupling") // the set should keep insertion order LinkedHashSet set; if (sup != null) { set = new LinkedHashSet<>(superItfs.size() + 1); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/SymbolTableResolver.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/SymbolTableResolver.java index 51a62119f2..ffada9506f 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/SymbolTableResolver.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/SymbolTableResolver.java @@ -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 diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/DeclarationFinderFunction.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/DeclarationFinderFunction.java index 9dd5336959..7239c5b450 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/DeclarationFinderFunction.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/DeclarationFinderFunction.java @@ -37,11 +37,8 @@ public class DeclarationFinderFunction implements Predicate { } 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) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/JavaNameOccurrence.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/JavaNameOccurrence.java index 168da97e0c..e4688e7748 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/JavaNameOccurrence.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/JavaNameOccurrence.java @@ -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) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/TypeSet.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/TypeSet.java index 20ae57d9c5..f128299a41 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/TypeSet.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/TypeSet.java @@ -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> CLASS_CACHE = new ConcurrentHashMap<>(); + private static final ConcurrentMap> CLASS_CACHE = new ConcurrentHashMap<>(); /** * Creates a {@link ImplicitImportResolver} diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/internal/NullableClassLoader.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/internal/NullableClassLoader.java index b7ba7d8b89..51a28bfd3f 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/internal/NullableClassLoader.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/internal/NullableClassLoader.java @@ -21,7 +21,7 @@ public interface NullableClassLoader { Class loadClassOrNull(String binaryName); - class ClassLoaderWrapper implements NullableClassLoader { + final class ClassLoaderWrapper implements NullableClassLoader { private final ClassLoader classLoader; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/InvocationMatcher.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/InvocationMatcher.java index 0c52fe4b15..09224b77d1 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/InvocationMatcher.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/InvocationMatcher.java @@ -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)); } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/JPrimitiveType.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/JPrimitiveType.java index dcd2eaa06f..3b8eaf35e9 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/JPrimitiveType.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/JPrimitiveType.java @@ -137,7 +137,7 @@ public final class JPrimitiveType implements JTypeMirror { FLOAT(float.class), DOUBLE(double.class); - static final EnumSet FLOATING_POINT_TYPES = EnumSet.of(FLOAT, DOUBLE); + static final Set FLOATING_POINT_TYPES = EnumSet.of(FLOAT, DOUBLE); final String name = name().toLowerCase(Locale.ROOT); private final Class jvm; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/JTypeMirror.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/JTypeMirror.java index 5c1a67c6b0..551a170bd8 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/JTypeMirror.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/JTypeMirror.java @@ -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()); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/Lub.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/Lub.java index 154db01020..64c4e3918a 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/Lub.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/Lub.java @@ -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 relevant(JClassType g, Set stunion) { if (!g.isRaw()) { return null; @@ -87,7 +88,7 @@ final class Lub { } private static Set erasedSuperTypes(Set stui) { - LinkedHashSet erased = new LinkedHashSet<>(); + Set erased = new LinkedHashSet<>(); for (JTypeMirror it : stui) { JTypeMirror t = it instanceof JTypeVar ? it : it.getErasure(); erased.add(t); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypeConversion.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypeConversion.java index 5987a02d87..ed12290517 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypeConversion.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypeConversion.java @@ -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); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypeOps.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypeOps.java index bfe65d8abf..f95dd23617 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypeOps.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypeOps.java @@ -117,7 +117,7 @@ public final class TypeOps { return true; } - private static class SameTypeVisitor implements JTypeVisitor { + private static final class SameTypeVisitor implements JTypeVisitor { static final SameTypeVisitor INFERENCE = new SameTypeVisitor(true); static final SameTypeVisitor PURE = new SameTypeVisitor(false); @@ -222,10 +222,8 @@ public final class TypeOps { @Override public Boolean visitArray(JArrayType t, JTypeMirror s) { - if (s instanceof JArrayType) { - return isSameType(t.getComponentType(), ((JArrayType) s).getComponentType(), inInference); - } - return false; + return s instanceof JArrayType + && isSameType(t.getComponentType(), ((JArrayType) s).getComponentType(), inInference); } } @@ -1168,15 +1166,8 @@ public final class TypeOps { } JMethodSig m1Prime = adaptForTypeParameters(m1, m2); - if (m1Prime != null && isConvertible(m1Prime.getReturnType(), r2) != Convertibility.NEVER) { - return true; - } - - if (!haveSameSignature(m1, m2)) { - return isSameType(r1, r2.getErasure()); - } - - return false; + return m1Prime != null && isConvertible(m1Prime.getReturnType(), r2) != Convertibility.NEVER + || !haveSameSignature(m1, m2) && isSameType(r1, r2.getErasure()); } /** @@ -1291,15 +1282,10 @@ public final class TypeOps { * Thrown exceptions are not part of the signature of a method. */ private static boolean haveSameSignature(JMethodSig m1, JMethodSig m2) { - if (!m1.getName().equals(m2.getName()) || m1.getArity() != m2.getArity()) { - return false; - } - - if (!haveSameTypeParams(m1, m2)) { - return false; - } - - return areSameTypes(m1.getFormalParameters(), + return m1.getName().equals(m2.getName()) + && m1.getArity() == m2.getArity() + && haveSameTypeParams(m1, m2) + && areSameTypes(m1.getFormalParameters(), m2.getFormalParameters(), Substitution.mapping(m2.getTypeParameters(), m1.getTypeParameters())); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypePrettyPrint.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypePrettyPrint.java index e3bdbd9358..93bbe497fc 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypePrettyPrint.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypePrettyPrint.java @@ -119,7 +119,7 @@ public final class TypePrettyPrint { } } - private static class PrettyPrintVisitor implements JTypeVisitor { + private static final class PrettyPrintVisitor implements JTypeVisitor { static final PrettyPrintVisitor INSTANCE = new PrettyPrintVisitor(); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypeSystem.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypeSystem.java index 51a8a1f7ad..800b378730 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypeSystem.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypeSystem.java @@ -102,7 +102,7 @@ public final class TypeSystem { * The set of all primitive types. See {@link #getPrimitive(PrimitiveTypeKind)}. */ public final Set allPrimitives; - private final EnumMap primitivesByKind; + private final Map primitivesByKind; /** * A constant to represent the normal absence of a type. The diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypeTestUtil.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypeTestUtil.java index f3c81422cb..d7547c4353 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypeTestUtil.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypeTestUtil.java @@ -60,12 +60,8 @@ public final class TypeTestUtil { */ public static boolean isA(final @NonNull Class clazz, final @Nullable TypeNode node) { AssertionUtil.requireParamNotNull("class", clazz); - if (node == null) { - return false; - } - - return hasNoSubtypes(clazz) ? isExactlyA(clazz, node) - : isA(clazz, node.getTypeMirror()); + return node != null && (hasNoSubtypes(clazz) ? isExactlyA(clazz, node) + : isA(clazz, node.getTypeMirror())); } /** @@ -127,11 +123,7 @@ public final class TypeTestUtil { public static boolean isA(@NonNull String canonicalName, @Nullable JTypeMirror thisType) { AssertionUtil.requireParamNotNull("canonicalName", (Object) canonicalName); - if (thisType == null) { - return false; - } - - return isA(canonicalName, thisType, null); + return thisType != null && isA(canonicalName, thisType, null); } public static boolean isA(@NonNull JTypeMirror t1, @Nullable TypeNode t2) { @@ -208,19 +200,12 @@ public final class TypeTestUtil { */ public static boolean isExactlyA(final @NonNull Class clazz, final @Nullable TypeNode node) { AssertionUtil.requireParamNotNull("class", clazz); - if (node == null) { - return false; - } - - return isExactlyA(clazz, node.getTypeMirror().getSymbol()); + return node != null && isExactlyA(clazz, node.getTypeMirror().getSymbol()); } public static boolean isExactlyA(@NonNull Class klass, @Nullable JTypeMirror type) { AssertionUtil.requireParamNotNull("class", klass); - if (type == null) { - return false; - } - return isExactlyA(klass, type.getSymbol()); + return type != null && isExactlyA(klass, type.getSymbol()); } public static boolean isExactlyA(@NonNull Class klass, @Nullable JTypeDeclSymbol type) { @@ -272,10 +257,7 @@ public final class TypeTestUtil { */ public static boolean isExactlyA(@NonNull String canonicalName, final @Nullable TypeNode node) { AssertionUtil.assertValidJavaBinaryName(canonicalName); - if (node == null) { - return false; - } - return isExactlyAOrAnon(canonicalName, node.getTypeMirror()) == OptionalBool.YES; + return node != null && isExactlyAOrAnon(canonicalName, node.getTypeMirror()) == OptionalBool.YES; } static OptionalBool isExactlyAOrAnon(@NonNull String canonicalName, final @NonNull JTypeMirror node) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/ast/ExprContext.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/ast/ExprContext.java index 4cd08c218b..8f59b577cd 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/ast/ExprContext.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/ast/ExprContext.java @@ -58,14 +58,12 @@ public abstract class ExprContext { AssertionUtil.requireParamNotNull("type", type); JTypeMirror targetType = getTargetType(); - if (targetType == null) { - return true; - } - // todo there's a gritty detail about compound assignment operators - // with a primitive LHS, see https://github.com/pmd/pmd/issues/2023 - return kind == CAST ? TypeConversion.isConvertibleInCastContext(type, targetType) - : TypeConversion.isConvertibleUsingBoxing(type, targetType); + return targetType == null + // todo there's a gritty detail about compound assignment operators + // with a primitive LHS, see https://github.com/pmd/pmd/issues/2023 + || (kind == CAST ? TypeConversion.isConvertibleInCastContext(type, targetType) + : TypeConversion.isConvertibleUsingBoxing(type, targetType)); } /** diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/ExprOps.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/ExprOps.java index f6f0b64d7b..4847007b1c 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/ExprOps.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/ExprOps.java @@ -143,26 +143,21 @@ final class ExprOps { // not provide explicit type arguments, an explicitly typed // lambda expression for which the corresponding target type // (as derived from the signature of m) is a type parameter of m. - if (m.isGeneric() && invoc.getExplicitTypeArguments().isEmpty()) { - return !formalType.isTypeVariable(); - } - - return true; + return !m.isGeneric() + || !invoc.getExplicitTypeArguments().isEmpty() + || !formalType.isTypeVariable(); } if (arg instanceof MethodRefMirror) { // An inexact method reference expression(§ 15.13 .1). - if (getExactMethod((MethodRefMirror) arg) == null) { - return false; - } - // If m is a generic method and the method invocation does - // not provide explicit type arguments, an exact method - // reference expression for which the corresponding target type - // (as derived from the signature of m) is a type parameter of m. - if (m.isGeneric() && invoc.getExplicitTypeArguments().isEmpty()) { - return !formalType.isTypeVariable(); - } - return true; + return getExactMethod((MethodRefMirror) arg) != null + // If m is a generic method and the method invocation does + // not provide explicit type arguments, an exact method + // reference expression for which the corresponding target type + // (as derived from the signature of m) is a type parameter of m. + && (!m.isGeneric() + || !invoc.getExplicitTypeArguments().isEmpty() + || !formalType.isTypeVariable()); } if (arg instanceof BranchingMirror) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/Graph.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/Graph.java index a4ef6e223a..ceddc46062 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/Graph.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/Graph.java @@ -7,15 +7,16 @@ package net.sourceforge.pmd.lang.java.types.internal.infer; import static java.lang.Math.min; import static net.sourceforge.pmd.util.CollectionUtil.union; +import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collections; +import java.util.Deque; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; -import java.util.Stack; import net.sourceforge.pmd.internal.GraphUtils; import net.sourceforge.pmd.internal.GraphUtils.DotColor; @@ -164,14 +165,14 @@ class Graph { ); } - private static class TarjanState { + private static final class TarjanState { int index; - Stack> stack = new Stack<>(); + Deque> stack = new ArrayDeque<>(); } - static class Vertex { + static final class Vertex { private final Graph owner; private final Set data; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/Infer.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/Infer.java index fdf63b9049..d819709b3a 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/Infer.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/Infer.java @@ -333,7 +333,7 @@ public final class Infer { subst(t.getReturnType(), var -> { assert !(var instanceof InferenceVar) : "Expected a ground type " + t; - assert !(var instanceof JTypeVar) || !(t.getTypeParameters().contains(var)) + assert !(var instanceof JTypeVar) || !t.getTypeParameters().contains(var) : "Some type parameters have not been instantiated"; return var; }); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/InferenceContext.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/InferenceContext.java index aa805cacad..4cb1890ab8 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/InferenceContext.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/InferenceContext.java @@ -80,6 +80,7 @@ final class InferenceContext { * into ivars * @param logger Logger for events related to ivar bounds */ + @SuppressWarnings("PMD.AssignmentToNonFinalStatic") // ctxId InferenceContext(TypeSystem ts, SupertypeCheckCache supertypeCheckCache, List tvars, TypeInferenceLogger logger) { this.ts = ts; this.supertypeCheckCache = supertypeCheckCache; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/InferenceVar.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/InferenceVar.java index 3c0bd5bfb3..be1aeab7a6 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/InferenceVar.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/InferenceVar.java @@ -9,6 +9,7 @@ import java.util.Collections; import java.util.EnumMap; import java.util.EnumSet; import java.util.LinkedHashSet; +import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.function.Function; @@ -149,7 +150,7 @@ public final class InferenceVar implements JTypeMirror, SubstVar { // put the new bounds before updating - LinkedHashSet newBounds = new LinkedHashSet<>(); + Set newBounds = new LinkedHashSet<>(); boundSet.bounds.put(kind, newBounds); for (JTypeMirror prev : prevBounds) { @@ -335,7 +336,7 @@ public final class InferenceVar implements JTypeMirror, SubstVar { private static final class BoundSet { JTypeMirror inst; - EnumMap> bounds = new EnumMap<>(BoundKind.class); + Map> bounds = new EnumMap<>(BoundKind.class); } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/PhaseOverloadSet.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/PhaseOverloadSet.java index f57e57e563..d8876cbbb9 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/PhaseOverloadSet.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/PhaseOverloadSet.java @@ -243,11 +243,8 @@ final class PhaseOverloadSet extends OverloadSet { JTypeMirror rs = sfun.getReturnType(); JTypeMirror rt = tfun.getReturnType(); - if (TypeOps.mentionsAny(rs, sparams) && !ctx.isGround(rt)) { - return false; - } - - return addGenericExprConstraintsRecursive(ctx, ei, rs, rt, tToS, site); + return (!TypeOps.mentionsAny(rs, sparams) || ctx.isGround(rt)) + && addGenericExprConstraintsRecursive(ctx, ei, rs, rt, tToS, site); } private boolean addGenericExprConstraintsRecursive(InferenceContext ctx, ExprMirror ei, JTypeMirror rs, JTypeMirror rt, Substitution tToS, MethodCallSite site) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/TypeInferenceLogger.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/TypeInferenceLogger.java index aef5b99fa3..a21945cb0b 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/TypeInferenceLogger.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/TypeInferenceLogger.java @@ -6,9 +6,10 @@ package net.sourceforge.pmd.lang.java.types.internal.infer; import java.io.PrintStream; +import java.util.ArrayDeque; +import java.util.Deque; import java.util.Iterator; import java.util.List; -import java.util.Stack; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -317,7 +318,7 @@ public interface TypeInferenceLogger { class VerboseLogger extends SimpleLogger { - private final Stack marks = new Stack<>(); + private final Deque marks = new ArrayDeque<>(); public VerboseLogger(PrintStream out) { super(out); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/ast/LambdaMirrorImpl.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/ast/LambdaMirrorImpl.java index ddf7e61f2b..51a4663079 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/ast/LambdaMirrorImpl.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/ast/LambdaMirrorImpl.java @@ -112,11 +112,7 @@ class LambdaMirrorImpl extends BaseFunctionalMirror impleme @Override public boolean isValueCompatible() { ASTBlock block = myNode.getBlock(); - if (block == null) { - return true; - } else { - return isLambdaBodyCompatible(block, false); - } + return block == null || isLambdaBodyCompatible(block, false); } @Override