Merge branch 'pr-2721' into master

[java] Deprecate old symbol table, add replacement for TypeHelper #2721
This commit is contained in:
Andreas Dangel committed 2020-08-23 10:01:34 +02:00
commit a98f3d6201
47 files changed
+808 -404

No files matched your search

+7
View File
@@ -142,6 +142,13 @@ See also [[all] Ensure PMD/CPD uses tab width of 1 for tabs consistently #2656](
* {% jdoc !!visualforce::lang.vf.VfSimpleCharStream %}
* {% jdoc java::lang.java.typeresolution.TypeHelper %} is deprecated in
favor of {% jdoc java::lang.java.types.TypeTestUtil %}, which has the
same functionality, but a slightly changed API.
* Many of the classes in {% jdoc_package java::lang.java.symboltable %}
are deprecated as internal API.
### External Contributions
* [#2656](https://github.com/pmd/pmd/pull/2656): \[all] Ensure PMD/CPD uses tab width of 1 for tabs consistently - [Maikel Steneker](https://github.com/maikelsteneker)
@@ -7,7 +7,7 @@ package net.sourceforge.pmd.lang.java.ast;
import java.util.Collection;
import java.util.List;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
// package private
abstract class AbstractJavaAnnotatableNode extends AbstractJavaNode implements Annotatable {
@@ -30,7 +30,7 @@ abstract class AbstractJavaAnnotatableNode extends AbstractJavaNode implements A
List<ASTAnnotation> annotations = getDeclaredAnnotations();
for (ASTAnnotation annotation : annotations) {
ASTName name = annotation.getFirstDescendantOfType(ASTName.class);
if (name != null && TypeHelper.isA(name, annotQualifiedName)) {
if (TypeTestUtil.isA(annotQualifiedName, name)) {
return annotation;
}
}
@@ -10,14 +10,12 @@ import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTAnnotation;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
import net.sourceforge.pmd.lang.java.ast.ASTExtendsList;
import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTName;
import net.sourceforge.pmd.lang.java.ast.TypeNode;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
/**
* @deprecated Internal API
@@ -97,26 +95,7 @@ public abstract class AbstractJUnitRule extends AbstractJavaRule {
private boolean isJUnit3Class(ASTCompilationUnit node) {
ASTClassOrInterfaceDeclaration cid = node.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class);
if (cid == null) {
return false;
}
if (node.getType() != null && TypeHelper.isA(node, JUNIT3_CLASS_NAME)) {
return true;
} else if (node.getType() == null) {
ASTExtendsList extendsList = cid.getFirstChildOfType(ASTExtendsList.class);
if (extendsList == null) {
return false;
}
if (((ASTClassOrInterfaceType) extendsList.getChild(0)).getImage().endsWith("TestCase")) {
return true;
}
String className = cid.getSimpleName();
return className.endsWith("Test");
} else if (hasImports(node, JUNIT3_CLASS_NAME)) {
return cid.getSimpleName().endsWith("Test");
}
return false;
return TypeTestUtil.isA(JUNIT3_CLASS_NAME, cid);
}
private boolean isJUnit4Class(ASTCompilationUnit node) {
@@ -137,7 +116,7 @@ public abstract class AbstractJUnitRule extends AbstractJavaRule {
if (name != null && (name.hasImageEqualTo("Test") || name.hasImageEqualTo(annotationTypeClassName))) {
return true;
}
} else if (TypeHelper.isA(annotationType, annotationTypeClassName)) {
} else if (TypeTestUtil.isA(annotationTypeClassName, annotationType)) {
return true;
}
}
@@ -27,7 +27,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
import net.sourceforge.pmd.lang.symboltable.Scope;
import net.sourceforge.pmd.lang.symboltable.ScopedNode;
@@ -63,7 +63,7 @@ public class ForLoopCanBeForeachRule extends AbstractJavaRule {
List<NameOccurrence> occurrences = indexDecl.getValue();
VariableNameDeclaration index = indexDecl.getKey();
if (TypeHelper.isExactlyAny(index, Iterator.class)) {
if (TypeTestUtil.isA(Iterator.class, index.getDeclaratorId())) {
Entry<VariableNameDeclaration, List<NameOccurrence>> iterableInfo = getIterableDeclOfIteratorLoop(index, node.getScope());
if (iterableInfo != null && isReplaceableIteratorLoop(indexDecl, guardCondition, iterableInfo, node)) {
@@ -23,7 +23,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTReferenceType;
import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression;
import net.sourceforge.pmd.lang.java.rule.AbstractJUnitRule;
import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
import net.sourceforge.pmd.lang.symboltable.Scope;
@@ -123,7 +123,7 @@ public class JUnitTestsShouldIncludeAssertRule extends AbstractJUnitRule {
List<ASTNormalAnnotation> annotations = methodParent.findDescendantsOfType(ASTNormalAnnotation.class);
for (ASTNormalAnnotation annotation : annotations) {
ASTName name = annotation.getFirstChildOfType(ASTName.class);
if (name != null && TypeHelper.isA(name, JUNIT4_CLASS_NAME)) {
if (TypeTestUtil.isA(JUNIT4_CLASS_NAME, name)) {
List<ASTMemberValuePair> memberValues = annotation.findDescendantsOfType(ASTMemberValuePair.class);
for (ASTMemberValuePair pair : memberValues) {
if ("expected".equals(pair.getImage())) {
@@ -219,7 +219,7 @@ public class JUnitTestsShouldIncludeAssertRule extends AbstractJUnitRule {
String varName = tokens[0];
boolean variableTypeIsSoftAssertion = variables.containsKey(varName)
&& TypeHelper.isA(variables.get(varName), "org.assertj.core.api.AbstractSoftAssertions");
&& TypeTestUtil.isA("org.assertj.core.api.AbstractSoftAssertions", variables.get(varName).getDeclaratorId());
return methodIsAssertAll && variableTypeIsSoftAssertion;
}
@@ -22,7 +22,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTResultType;
import net.sourceforge.pmd.lang.java.ast.ASTType;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator;
import net.sourceforge.pmd.lang.java.rule.AbstractIgnoredAnnotationRule;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
import net.sourceforge.pmd.properties.PropertyDescriptor;
public class LinguisticNamingRule extends AbstractIgnoredAnnotationRule {
@@ -150,8 +150,8 @@ public class LinguisticNamingRule extends AbstractIgnoredAnnotationRule {
private boolean isBooleanType(ASTType node) {
return "boolean".equalsIgnoreCase(node.getTypeImage())
|| TypeHelper.isA(node, "java.util.concurrent.atomic.AtomicBoolean")
|| TypeHelper.isA(node, "java.util.function.Predicate");
|| TypeTestUtil.isA("java.util.concurrent.atomic.AtomicBoolean", node)
|| TypeTestUtil.isA("java.util.function.Predicate", node);
}
private void checkBooleanMethods(ASTMethodDeclaration node, Object data, String nameOfMethod) {
@@ -12,10 +12,9 @@ import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression;
import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType;
import net.sourceforge.pmd.lang.java.ast.ASTEnumConstant;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
import net.sourceforge.pmd.properties.BooleanProperty;
import net.sourceforge.pmd.properties.PropertyBuilder.RegexPropertyBuilder;
import net.sourceforge.pmd.properties.PropertyDescriptor;
@@ -70,9 +69,7 @@ public class MethodNamingConventionsRule extends AbstractNamingConventionRule<AS
return false;
}
ASTClassOrInterfaceType superClass = ((ASTClassOrInterfaceDeclaration) parent).getSuperClassTypeNode();
return superClass != null && TypeHelper.isA(superClass, "junit.framework.TestCase");
return TypeTestUtil.isA("junit.framework.TestCase", (ASTClassOrInterfaceDeclaration) parent);
}
@@ -17,7 +17,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTTypeArgument;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
import net.sourceforge.pmd.lang.symboltable.ScopedNode;
@@ -105,7 +105,7 @@ public class UnnecessaryCastRule extends AbstractJavaRule {
}
private ASTClassOrInterfaceType getCollectionItemType(ASTClassOrInterfaceType collectionType) {
if (TypeHelper.isA(collectionType, Map.class)) {
if (TypeTestUtil.isA(Map.class, collectionType)) {
List<ASTClassOrInterfaceType> types = collectionType.findDescendantsOfType(ASTClassOrInterfaceType.class);
if (types.size() >= 2) {
return types.get(1); // the value type of the map
@@ -11,19 +11,17 @@ import net.sourceforge.pmd.lang.java.ast.ASTExpression;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression;
import net.sourceforge.pmd.lang.java.ast.ASTType;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
public class CheckSkipResultRule extends AbstractJavaRule {
@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
ASTType typeNode = node.getTypeNode();
if (typeNode == null || !TypeHelper.isA(typeNode, InputStream.class)) {
if (!TypeTestUtil.isA(InputStream.class, node.getTypeNode())) {
return data;
}
for (NameOccurrence occ : node.getUsages()) {
@@ -45,7 +45,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.java.ast.TypeNode;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
import net.sourceforge.pmd.properties.PropertyDescriptor;
/**
@@ -288,7 +288,7 @@ public class CloseResourceRule extends AbstractJavaRule {
for (String type : allowedResourceTypes) {
// the check here must be a exact type match, since subclasses may override close()
// and actually require closing
if (TypeHelper.isExactlyA(refType, type)) {
if (TypeTestUtil.isExactlyA(type, refType)) {
return true;
}
}
@@ -340,7 +340,7 @@ public class CloseResourceRule extends AbstractJavaRule {
private boolean isNodeInstanceOfResourceType(TypeNode refType) {
for (String resType : types) {
if (TypeHelper.isA(refType, resType)) {
if (TypeTestUtil.isA(resType, refType)) {
return true;
}
}
@@ -36,7 +36,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
public class InvalidLogMessageFormatRule extends AbstractJavaRule {
@@ -128,14 +128,12 @@ public class InvalidLogMessageFormatRule extends AbstractJavaRule {
private boolean isNewThrowable(ASTPrimaryExpression last) {
// in case a new exception is created or the exception class is
// mentioned.
ASTClassOrInterfaceType classOrInterface = last.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
return classOrInterface != null && classOrInterface.getType() != null
&& TypeHelper.isA(classOrInterface, Throwable.class);
return TypeTestUtil.isA(Throwable.class, last.getFirstDescendantOfType(ASTClassOrInterfaceType.class));
}
private boolean hasTypeThrowable(ASTPrimaryExpression last) {
// if the type could be determined already
return last.getType() != null && TypeHelper.isA(last, Throwable.class);
return last.getType() != null && TypeTestUtil.isA(Throwable.class, last);
}
private boolean isReferencingThrowable(ASTPrimaryExpression last) {
@@ -16,7 +16,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTType;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
import net.sourceforge.pmd.util.NumericConstants;
public class MoreThanOneLoggerRule extends AbstractJavaRule {
@@ -70,13 +70,12 @@ public class MoreThanOneLoggerRule extends AbstractJavaRule {
if (reftypeNode instanceof ASTReferenceType) {
Node classOrIntType = reftypeNode.getChild(0);
if (classOrIntType instanceof ASTClassOrInterfaceType) {
Class<?> clazzType = ((ASTClassOrInterfaceType) classOrIntType).getType();
if (clazzType != null
&& (TypeHelper.isA((ASTClassOrInterfaceType) classOrIntType, LOG4J_LOGGER_NAME)
|| TypeHelper.isA((ASTClassOrInterfaceType) classOrIntType, LOG4J2_LOGGER_NAME)
|| TypeHelper.isA((ASTClassOrInterfaceType) classOrIntType, JAVA_LOGGER_NAME)
|| TypeHelper.isA((ASTClassOrInterfaceType) classOrIntType, SLF4J_LOGGER_NAME))
|| clazzType == null && "Logger".equals(classOrIntType.getImage())) {
ASTClassOrInterfaceType classType = (ASTClassOrInterfaceType) classOrIntType;
if (TypeTestUtil.isA(LOG4J_LOGGER_NAME, classType)
|| TypeTestUtil.isA(LOG4J2_LOGGER_NAME, classType)
|| TypeTestUtil.isA(JAVA_LOGGER_NAME, classType)
|| TypeTestUtil.isA(SLF4J_LOGGER_NAME, classType)
|| "Logger".equals(classOrIntType.getImage())) {
++count;
}
}
@@ -17,7 +17,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTName;
import net.sourceforge.pmd.lang.java.ast.ASTSynchronizedStatement;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyFactory;
@@ -60,13 +60,13 @@ public class UnsynchronizedStaticFormatterRule extends AbstractJavaRule {
return data;
}
ASTClassOrInterfaceType cit = node.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
if (cit == null || !TypeHelper.isA(cit, formatterClassToCheck)) {
if (cit == null || !TypeTestUtil.isA(formatterClassToCheck, cit)) {
return data;
}
ASTVariableDeclaratorId var = node.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
for (String formatter: THREAD_SAFE_FORMATTER) {
if (TypeHelper.isA(var, formatter)) {
if (TypeTestUtil.isA(formatter, var)) {
return data;
}
}
@@ -83,7 +83,7 @@ public class UnsynchronizedStaticFormatterRule extends AbstractJavaRule {
ASTExpression expression = syncStatement.getFirstChildOfType(ASTExpression.class);
if (expression != null) {
ASTName name = expression.getFirstDescendantOfType(ASTName.class);
if (name != null && name.hasImageEqualTo(var.getVariableName())) {
if (name != null && name.hasImageEqualTo(var.getName())) {
continue;
}
}
@@ -22,7 +22,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression;
import net.sourceforge.pmd.lang.java.ast.ASTThrowStatement;
import net.sourceforge.pmd.lang.java.ast.ASTWhileStatement;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
public class AvoidInstantiatingObjectsInLoopsRule extends AbstractJavaRule {
@@ -65,7 +65,7 @@ public class AvoidInstantiatingObjectsInLoopsRule extends AbstractJavaRule {
private boolean notCollectionAccess(ASTAllocationExpression node) {
if (node.getNthParent(4) instanceof ASTArgumentList && node.getNthParent(8) instanceof ASTStatementExpression) {
ASTStatementExpression statement = (ASTStatementExpression) node.getNthParent(8);
return !TypeHelper.isA(statement, Collection.class);
return !TypeTestUtil.isA(Collection.class, statement);
}
return true;
}
@@ -17,7 +17,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTArrayDimsAndInits;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType;
import net.sourceforge.pmd.lang.java.ast.ASTLiteral;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
/**
* Rule that marks instantiations of new {@link BigInteger} or
@@ -36,8 +36,8 @@ public class BigIntegerInstantiationRule extends AbstractJavaRule {
boolean jdk15 = ((RuleContext) data).getLanguageVersion()
.compareTo(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.5")) >= 0;
if ((TypeHelper.isA((ASTClassOrInterfaceType) type, BigInteger.class)
|| jdk15 && TypeHelper.isA((ASTClassOrInterfaceType) type, BigDecimal.class))
if ((TypeTestUtil.isA(BigInteger.class, (ASTClassOrInterfaceType) type)
|| jdk15 && TypeTestUtil.isA(BigDecimal.class, (ASTClassOrInterfaceType) type))
&& !node.hasDescendantOfType(ASTArrayDimsAndInits.class)) {
ASTArguments args = node.getFirstChildOfType(ASTArguments.class);
if (args.size() == 1) {
@@ -4,7 +4,6 @@
package net.sourceforge.pmd.lang.java.rule.performance;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression;
import net.sourceforge.pmd.lang.java.ast.ASTArrayDimsAndInits;
import net.sourceforge.pmd.lang.java.ast.ASTBooleanLiteral;
@@ -17,7 +16,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
/**
* Avoid instantiating Boolean objects; you can reference Boolean.TRUE,
@@ -67,8 +66,8 @@ public class BooleanInstantiationRule extends AbstractJavaRule {
return super.visit(node, data);
}
Node n1 = node.getFirstChildOfType(ASTClassOrInterfaceType.class);
if (TypeHelper.isA((ASTClassOrInterfaceType) n1, Boolean.class)) {
ASTClassOrInterfaceType n1 = node.getFirstChildOfType(ASTClassOrInterfaceType.class);
if (TypeTestUtil.isA(Boolean.class, n1)) {
super.addViolation(data, node);
return data;
}
@@ -18,10 +18,9 @@ import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix;
import net.sourceforge.pmd.lang.java.ast.ASTStatement;
import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.lang.java.ast.AbstractJavaNode;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
public class ConsecutiveAppendsShouldReuseRule extends AbstractJavaRule {
@@ -125,11 +124,11 @@ public class ConsecutiveAppendsShouldReuseRule extends AbstractJavaRule {
return null;
}
private boolean isAStringBuilderBuffer(AbstractJavaNode node, String name) {
private boolean isAStringBuilderBuffer(JavaNode node, String name) {
Map<VariableNameDeclaration, List<NameOccurrence>> declarations = node.getScope()
.getDeclarations(VariableNameDeclaration.class);
for (VariableNameDeclaration decl : declarations.keySet()) {
if (decl.getName().equals(name) && TypeHelper.isExactlyAny(decl, StringBuilder.class, StringBuffer.class)) {
if (decl.getName().equals(name) && ConsecutiveLiteralAppendsRule.isStringBuilderOrBuffer(decl.getDeclaratorId())) {
return true;
}
}
@@ -36,7 +36,7 @@ import net.sourceforge.pmd.lang.java.ast.TypeNode;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence;
import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyFactory;
@@ -259,7 +259,7 @@ public class ConsecutiveLiteralAppendsRule extends AbstractJavaRule {
private int processAdditive(Object data, int concurrentCount, Node sn, Node rootNode) {
ASTAdditiveExpression additive = sn.getFirstDescendantOfType(ASTAdditiveExpression.class);
// The additive expression must of be type String to count
if (additive == null || additive.getType() != null && !TypeHelper.isA(additive, String.class)) {
if (additive == null || additive.getType() != null && !TypeTestUtil.isA(String.class, additive)) {
return 0;
}
// check for at least one string literal
@@ -396,14 +396,8 @@ public class ConsecutiveLiteralAppendsRule extends AbstractJavaRule {
return n instanceof ASTLiteral;
}
private static boolean isStringBuilderOrBuffer(ASTVariableDeclaratorId node) {
if (node.getType() != null) {
return TypeHelper.isEither(node, StringBuffer.class, StringBuilder.class);
}
Node nn = node.getTypeNameNode();
if (nn == null || nn.getNumChildren() == 0) {
return false;
}
return TypeHelper.isEither((TypeNode) nn.getChild(0), StringBuffer.class, StringBuilder.class);
static boolean isStringBuilderOrBuffer(TypeNode node) {
return TypeTestUtil.isA(StringBuffer.class, node)
|| TypeTestUtil.isA(StringBuilder.class, node);
}
}
@@ -27,9 +27,8 @@ import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression;
import net.sourceforge.pmd.lang.java.ast.ASTType;
import net.sourceforge.pmd.lang.java.ast.AccessNode;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.symboltable.TypedNameDeclaration;
import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
/**
* How this rule works: find additive expressions: + check that the addition is
@@ -128,10 +127,7 @@ public class InefficientStringBufferingRule extends AbstractJavaRule {
if (type != null) {
List<ASTClassOrInterfaceType> types = type.findDescendantsOfType(ASTClassOrInterfaceType.class);
if (!types.isEmpty()) {
ASTClassOrInterfaceType typeDeclaration = types.get(0);
if (TypeHelper.isA(typeDeclaration, String.class)) {
return true;
}
return TypeTestUtil.isA(String.class, types.get(0));
}
}
return false;
@@ -186,7 +182,7 @@ public class InefficientStringBufferingRule extends AbstractJavaRule {
}
ASTName n = s.getFirstDescendantOfType(ASTName.class);
if (n == null || n.getImage().indexOf(methodName) == -1
|| !(n.getNameDeclaration() instanceof TypedNameDeclaration)) {
|| !(n.getNameDeclaration() instanceof VariableNameDeclaration)) {
return false;
}
@@ -198,8 +194,7 @@ public class InefficientStringBufferingRule extends AbstractJavaRule {
if (argList == null || argList.getNumChildren() > 1) {
return false;
}
return TypeHelper.isExactlyAny((TypedNameDeclaration) n.getNameDeclaration(), StringBuffer.class,
StringBuilder.class);
return ConsecutiveLiteralAppendsRule.isStringBuilderOrBuffer(((VariableNameDeclaration) n.getNameDeclaration()).getDeclaratorId());
}
private boolean isAllocatedStringBuffer(ASTAdditiveExpression node) {
@@ -210,7 +205,7 @@ public class InefficientStringBufferingRule extends AbstractJavaRule {
// note that the child can be an ArrayDimsAndInits, for example, from
// java.lang.FloatingDecimal: t = new int[ nWords+wordcount+1 ];
ASTClassOrInterfaceType an = ao.getFirstChildOfType(ASTClassOrInterfaceType.class);
return an != null && TypeHelper.isEither(an, StringBuffer.class, StringBuilder.class);
return ConsecutiveLiteralAppendsRule.isStringBuilderOrBuffer(an);
}
private static class MethodCallChain {
@@ -226,12 +221,12 @@ public class InefficientStringBufferingRule extends AbstractJavaRule {
boolean isExactlyOfAnyType(Class<?> clazz, Class<?> ... clazzes) {
ASTPrimaryPrefix typeNode = getTypeNode();
if (TypeHelper.isExactlyA(typeNode, clazz.getName())) {
if (TypeTestUtil.isExactlyA(clazz, typeNode)) {
return true;
}
if (clazzes != null) {
for (Class<?> c : clazzes) {
if (TypeHelper.isExactlyA(typeNode, c.getName())) {
if (TypeTestUtil.isExactlyA(c, typeNode)) {
return true;
}
}
@@ -31,7 +31,6 @@ import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
/**
@@ -56,7 +55,7 @@ public class InsufficientStringBufferDeclarationRule extends AbstractJavaRule {
@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
if (node.getNameDeclaration() == null
|| !TypeHelper.isExactlyAny(node.getNameDeclaration(), StringBuffer.class, StringBuilder.class)) {
|| !ConsecutiveLiteralAppendsRule.isStringBuilderOrBuffer(node)) {
return data;
}
Node rootNode = node;
@@ -19,7 +19,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.symboltable.TypedNameDeclaration;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
public class StringInstantiationRule extends AbstractJavaRule {
@@ -34,7 +34,7 @@ public class StringInstantiationRule extends AbstractJavaRule {
return data;
}
if (!TypeHelper.isA((ASTClassOrInterfaceType) node.getChild(0), String.class)) {
if (!TypeTestUtil.isA(String.class, (ASTClassOrInterfaceType) node.getChild(0))) {
return data;
}
@@ -60,11 +60,11 @@ public class StringInstantiationRule extends AbstractJavaRule {
}
NameDeclaration nd = name.getNameDeclaration();
if (nd == null) {
if (!(nd instanceof TypedNameDeclaration)) {
return data;
}
if (nd instanceof TypedNameDeclaration && TypeHelper.isExactlyAny((TypedNameDeclaration) nd, String.class)) {
if (TypeTestUtil.isA(String.class, ((TypedNameDeclaration) nd).getTypeNode())) {
addViolation(data, node);
}
return data;
@@ -29,7 +29,7 @@ import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence;
import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
import net.sourceforge.pmd.lang.symboltable.ScopedNode;
@@ -121,7 +121,8 @@ public class StringToStringRule extends AbstractJavaRule {
private boolean isStringVariableDeclarator(ASTVariableDeclaratorId varDeclaratorId) {
VariableNameDeclaration varNameDeclaration = varDeclaratorId.getNameDeclaration();
return varNameDeclaration != null
&& TypeHelper.isExactlyAny(varNameDeclaration, String.class, String[].class);
&& TypeTestUtil.isExactlyA(String.class, varDeclaratorId)
|| TypeTestUtil.isExactlyA(String[].class, varDeclaratorId);
}
private NameOccurrence getVarUsageQualifier(NameOccurrence varUsage) {
@@ -21,7 +21,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.lang.java.ast.ASTWhileStatement;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
public class UseStringBufferForStringAppendsRule extends AbstractJavaRule {
@@ -38,7 +38,7 @@ public class UseStringBufferForStringAppendsRule extends AbstractJavaRule {
*/
@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
if (!TypeHelper.isA(node, String.class) || node.isArray()
if (!TypeTestUtil.isA(String.class, node) || node.isArray()
|| node.getNthParent(3) instanceof ASTForStatement) {
return data;
}
@@ -14,8 +14,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTLiteral;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTName;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.symboltable.TypedNameDeclaration;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
/**
@@ -63,8 +62,8 @@ public class UseStringBufferLengthRule extends AbstractJavaRule {
if (nd == null) {
return data;
}
if (alreadySeen.contains(nd) || !(nd instanceof TypedNameDeclaration) || nd instanceof TypedNameDeclaration
&& TypeHelper.isExactlyNone((TypedNameDeclaration) nd, StringBuffer.class, StringBuilder.class)) {
if (alreadySeen.contains(nd) || !(nd instanceof VariableNameDeclaration)
|| !ConsecutiveLiteralAppendsRule.isStringBuilderOrBuffer(((VariableNameDeclaration) nd).getDeclaratorId())) {
return data;
}
alreadySeen.add(nd);
@@ -16,7 +16,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
import net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
/**
* Finds hard coded encryption keys that are passed to
@@ -35,8 +35,7 @@ public class HardCodedCryptoKeyRule extends AbstractJavaRule {
@Override
public Object visit(ASTAllocationExpression node, Object data) {
ASTClassOrInterfaceType declClassName = node.getFirstChildOfType(ASTClassOrInterfaceType.class);
if (declClassName != null && TypeHelper.isA(declClassName, SECRET_KEY_SPEC)) {
if (TypeTestUtil.isA(SECRET_KEY_SPEC, node.getFirstChildOfType(ASTClassOrInterfaceType.class))) {
Node firstArgument = null;
ASTArguments arguments = node.getFirstChildOfType(ASTArguments.class);
@@ -16,7 +16,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
import net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
/**
* Finds hardcoded static Initialization Vectors vectors used with cryptographic
@@ -42,8 +42,7 @@ public class InsecureCryptoIvRule extends AbstractJavaRule {
@Override
public Object visit(ASTAllocationExpression node, Object data) {
ASTClassOrInterfaceType declClassName = node.getFirstChildOfType(ASTClassOrInterfaceType.class);
if (declClassName != null && TypeHelper.isA(declClassName, javax.crypto.spec.IvParameterSpec.class)) {
if (TypeTestUtil.isA(javax.crypto.spec.IvParameterSpec.class, node.getFirstChildOfType(ASTClassOrInterfaceType.class))) {
Node firstArgument = null;
ASTArguments arguments = node.getFirstChildOfType(ASTArguments.class);
@@ -6,6 +6,7 @@ package net.sourceforge.pmd.lang.java.symboltable;
import java.util.Set;
import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.lang.symboltable.AbstractScope;
import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
@@ -15,6 +16,8 @@ import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
*
* @see <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.3">JLS 6.3</a>
*/
@Deprecated
@InternalApi
public abstract class AbstractJavaScope extends AbstractScope {
@Override
@@ -5,8 +5,10 @@
package net.sourceforge.pmd.lang.java.symboltable;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.java.ast.TypeNode;
import net.sourceforge.pmd.lang.java.ast.internal.PrettyPrintingUtil;
import net.sourceforge.pmd.lang.symboltable.AbstractNameDeclaration;
public class ClassNameDeclaration extends AbstractNameDeclaration implements TypedNameDeclaration {
@@ -17,15 +19,10 @@ public class ClassNameDeclaration extends AbstractNameDeclaration implements Typ
@Override
public String toString() {
if (node instanceof ASTClassOrInterfaceDeclaration) {
if (((ASTClassOrInterfaceDeclaration) node).isInterface()) {
return "Interface " + node.getImage();
} else {
return "Class " + node.getImage();
}
} else {
return "Enum " + node.getImage();
if (node instanceof ASTAnyTypeDeclaration) {
return PrettyPrintingUtil.kindName((ASTAnyTypeDeclaration) node) + node.getImage();
}
return "anonymous";
}
public Node getAccessNodeParent() {
@@ -34,11 +31,22 @@ public class ClassNameDeclaration extends AbstractNameDeclaration implements Typ
@Override
public String getTypeImage() {
return ((ASTClassOrInterfaceDeclaration) node).getImage();
return getTypeNode().getImage();
}
@Override
public Class<?> getType() {
return ((ASTClassOrInterfaceDeclaration) node).getType();
if (node instanceof ASTAnyTypeDeclaration) {
return ((ASTAnyTypeDeclaration) node).getType();
}
return null;
}
/**
* Null for anonymous classes.
*/
@Override
public TypeNode getTypeNode() {
return node instanceof TypeNode ? (TypeNode) node : null;
}
}
@@ -4,11 +4,14 @@
package net.sourceforge.pmd.lang.java.symboltable;
import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.lang.java.ast.ASTMethodReference;
import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
import net.sourceforge.pmd.util.SearchFunction;
@Deprecated
@InternalApi
public class DeclarationFinderFunction implements SearchFunction<NameDeclaration> {
private NameOccurrence occurrence;
@@ -9,6 +9,7 @@ import java.util.List;
import java.util.StringTokenizer;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.lang.java.ast.ASTArguments;
import net.sourceforge.pmd.lang.java.ast.ASTMemberSelector;
import net.sourceforge.pmd.lang.java.ast.ASTMethodReference;
@@ -19,6 +20,8 @@ import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
@Deprecated
@InternalApi
public class NameFinder {
private List<JavaNameOccurrence> names = new ArrayList<>();
Loaded 30 of 47 files, more files were not shown because too many files have changed in this diff. Show more