Merge branch 'pr-2190'

[java] Deprecations for statements PR

Prerequisite for #2164 for PMD 7.0.0
This commit is contained in:
Andreas Dangel
2020-01-05 19:30:52 +01:00
13 changed files with 158 additions and 14 deletions

View File

@@ -106,7 +106,12 @@ You can identify them with the `@InternalApi` annotation. You'll also get a depr
instead. This affects {% jdoc !!java::lang.java.ast.ASTAnnotationTypeDeclaration#getImage() %},
{% jdoc !!java::lang.java.ast.ASTClassOrInterfaceDeclaration#getImage() %}, and
{% jdoc !!java::lang.java.ast.ASTEnumDeclaration#getImage() %}.
* Several methods of {% jdoc java::lang.java.ast.ASTTryStatement %}, replacements with other names
have been added. This includes the XPath attribute `@Finally`, replace it with a test for `child::FinallyStatement`.
* Several methods named `getGuardExpressionNode` are replaced with `getCondition`. This affects the
following nodes: WhileStatement, DoStatement, ForStatement, IfStatement, AssertStatement, ConditionalExpression.
* {% jdoc java::lang.java.ast.ASTYieldStatement %} will not implement {% jdoc java::lang.java.ast.TypeNode %}
anymore come 7.0.0. Test the type of the expression nested within it.
### External Contributions

View File

@@ -37,8 +37,18 @@ public class ASTAssertStatement extends AbstractJavaNode {
/**
* Returns the expression tested by this assert statement.
*
* @deprecated Use {@link #getCondition()}
*/
@Deprecated
public ASTExpression getGuardExpressionNode() {
return getCondition();
}
/**
* Returns the expression tested by this assert statement.
*/
public ASTExpression getCondition() {
return (ASTExpression) jjtGetChild(0);
}

View File

@@ -62,11 +62,22 @@ public class ASTConditionalExpression extends AbstractJavaTypeNode {
/**
* Returns the node that represents the guard of this conditional.
* That is the expression before the '?'.
*
* @deprecated Use {@link #getCondition()}
*/
@Deprecated
public Node getGuardExpressionNode() {
return jjtGetChild(0);
}
/**
* Returns the node that represents the guard of this conditional.
* That is the expression before the '?'.
*/
public Node getCondition() {
return jjtGetChild(0);
}
/**
* Returns the node that represents the expression that will be evaluated

View File

@@ -36,8 +36,19 @@ public class ASTDoStatement extends AbstractJavaNode {
/**
* Returns the node that represents the guard of this loop.
* This may be any expression of type boolean.
*
* @deprecated Use {@link #getCondition()}
*/
@Deprecated
public ASTExpression getGuardExpressionNode() {
return getCondition();
}
/**
* Returns the node that represents the guard of this loop.
* This may be any expression of type boolean.
*/
public ASTExpression getCondition() {
return (ASTExpression) jjtGetChild(1);
}

View File

@@ -24,4 +24,12 @@ public class ASTFinallyStatement extends AbstractJavaNode {
public Object jjtAccept(JavaParserVisitor visitor, Object data) {
return visitor.visit(this, data);
}
/**
* Returns the body of this finally clause.
*/
public ASTBlock getBody() {
return (ASTBlock) jjtGetChild(0);
}
}

View File

@@ -46,8 +46,23 @@ public class ASTForStatement extends AbstractJavaNode {
*
* <p>If this node represents a foreach loop, or if there is
* no specified guard, then returns null.
*
* @deprecated Use {@link #getCondition()}
*/
@Deprecated
public ASTExpression getGuardExpressionNode() {
return getCondition();
}
/**
* Returns the node that represents the guard of this loop.
* This may be any expression of type boolean.
*
* <p>If this node represents a foreach loop, or if there is
* no specified guard, then returns null.
*/
public ASTExpression getCondition() {
if (isForeach()) {
return null;
}

View File

@@ -54,11 +54,22 @@ public class ASTIfStatement extends AbstractJavaNode {
/**
* Returns the node that represents the guard of this conditional.
* This may be any expression of type boolean.
*
* @deprecated Use {@link #getCondition()}
*/
@Deprecated
public ASTExpression getGuardExpressionNode() {
return (ASTExpression) jjtGetChild(0);
}
/**
* Returns the node that represents the guard of this conditional.
* This may be any expression of type boolean.
*/
public ASTExpression getCondition() {
return (ASTExpression) jjtGetChild(0);
}
/**
* Returns the statement that will be run if the guard evaluates

View File

@@ -45,19 +45,40 @@ public class ASTTryStatement extends AbstractJavaNode {
}
/**
* Returns the body of this try statement.
*/
public ASTBlock getBody() {
return (ASTBlock) jjtGetChild(1);
}
/**
* Returns the catch statement nodes of this try statement.
* If there are none, returns an empty list.
*
* @deprecated Use {@link #getCatchClauses()}
*/
@Deprecated
public List<ASTCatchStatement> getCatchStatements() {
return findChildrenOfType(ASTCatchStatement.class);
}
/**
* Returns the catch clauses of this try statement.
* If there are none, returns an empty list.
*/
public List<ASTCatchStatement> getCatchClauses() {
return findChildrenOfType(ASTCatchStatement.class);
}
/**
* Returns true if this try statement has a {@code finally} statement,
* in which case {@link #getFinally()} won't return {@code null}.
* in which case {@link #getFinallyClause()} won't return {@code null}.
*
* @deprecated Check for nullity of {@link #getFinallyClause()}
*/
@Deprecated
public boolean hasFinally() {
return getFirstChildOfType(ASTFinallyStatement.class) != null;
}
@@ -67,9 +88,20 @@ public class ASTTryStatement extends AbstractJavaNode {
* Returns the {@code finally} statement of this try statement, if any.
*
* @return The finally statement, or null if there is none
*
* @deprecated Use {@link #getFinallyClause()}
*/
public ASTFinallyStatement getFinally() {
return getFirstChildOfType(ASTFinallyStatement.class);
}
/**
* Returns the {@code finally} clause of this try statement, if any.
*
* @return The finally statement, or null if there is none
*/
public ASTFinallyStatement getFinallyClause() {
return getFirstChildOfType(ASTFinallyStatement.class);
}
}

View File

@@ -35,11 +35,22 @@ public class ASTWhileStatement extends AbstractJavaNode {
/**
* Returns the node that represents the guard of this loop.
* This may be any expression of type boolean.
*
* @deprecated Use {@link #getCondition()}
*/
@Deprecated
public ASTExpression getGuardExpressionNode() {
return (ASTExpression) jjtGetChild(0);
}
/**
* Returns the node that represents the guard of this loop.
* This may be any expression of type boolean.
*/
public ASTExpression getCondition() {
return (ASTExpression) jjtGetChild(0);
}
/**
* Returns the statement that will be run while the guard

View File

@@ -4,6 +4,8 @@
package net.sourceforge.pmd.lang.java.ast;
import net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition;
public class ASTYieldStatement extends AbstractJavaTypeNode {
ASTYieldStatement(int id) {
@@ -27,4 +29,30 @@ public class ASTYieldStatement extends AbstractJavaTypeNode {
}
return result;
}
/** Returns the yielded expression. */
public ASTExpression getExpr() {
return (ASTExpression) jjtGetChild(0);
}
/**
* @deprecated Use the type of the expression yielded by {@link #getExpr()}
*/
@Deprecated
@Override
public Class<?> getType() {
return super.getType();
}
/**
* @deprecated Use the type of the expression yielded by {@link #getExpr()}
*/
@Deprecated
@Override
public JavaTypeDefinition getTypeDefinition() {
return super.getTypeDefinition();
}
}

View File

@@ -80,7 +80,7 @@ public class CycloVisitor extends JavaParserVisitorAdapter {
public Object visit(ASTConditionalExpression node, Object data) {
((MutableInt) data).increment();
if (considerBooleanPaths) {
((MutableInt) data).add(CycloMetric.booleanExpressionComplexity(node.getGuardExpressionNode()));
((MutableInt) data).add(CycloMetric.booleanExpressionComplexity(node.getCondition()));
}
return super.visit(node, data);
}
@@ -90,7 +90,7 @@ public class CycloVisitor extends JavaParserVisitorAdapter {
public Object visit(ASTWhileStatement node, Object data) {
((MutableInt) data).increment();
if (considerBooleanPaths) {
((MutableInt) data).add(CycloMetric.booleanExpressionComplexity(node.getGuardExpressionNode()));
((MutableInt) data).add(CycloMetric.booleanExpressionComplexity(node.getCondition()));
}
return super.visit(node, data);
}
@@ -100,7 +100,7 @@ public class CycloVisitor extends JavaParserVisitorAdapter {
public Object visit(ASTIfStatement node, Object data) {
((MutableInt) data).increment();
if (considerBooleanPaths) {
((MutableInt) data).add(CycloMetric.booleanExpressionComplexity(node.getGuardExpressionNode()));
((MutableInt) data).add(CycloMetric.booleanExpressionComplexity(node.getCondition()));
}
return super.visit(node, data);
@@ -112,7 +112,7 @@ public class CycloVisitor extends JavaParserVisitorAdapter {
((MutableInt) data).increment();
if (considerBooleanPaths && !node.isForeach()) {
((MutableInt) data).add(CycloMetric.booleanExpressionComplexity(node.getGuardExpressionNode()));
((MutableInt) data).add(CycloMetric.booleanExpressionComplexity(node.getCondition()));
}
return super.visit(node, data);
@@ -123,7 +123,7 @@ public class CycloVisitor extends JavaParserVisitorAdapter {
public Object visit(ASTDoStatement node, Object data) {
((MutableInt) data).increment();
if (considerBooleanPaths) {
((MutableInt) data).add(CycloMetric.booleanExpressionComplexity(node.getGuardExpressionNode()));
((MutableInt) data).add(CycloMetric.booleanExpressionComplexity(node.getCondition()));
}
return super.visit(node, data);
@@ -150,7 +150,7 @@ public class CycloVisitor extends JavaParserVisitorAdapter {
((MutableInt) data).add(2); // equivalent to if (condition) { throw .. }
if (considerBooleanPaths) {
((MutableInt) data).add(CycloMetric.booleanExpressionComplexity(node.getGuardExpressionNode()));
((MutableInt) data).add(CycloMetric.booleanExpressionComplexity(node.getCondition()));
}
}

View File

@@ -88,7 +88,7 @@ public class IdenticalCatchBranchesRule extends AbstractJavaRule {
@Override
public Object visit(ASTTryStatement node, Object data) {
List<ASTCatchStatement> catchStatements = node.getCatchStatements();
List<ASTCatchStatement> catchStatements = node.getCatchClauses();
Set<List<ASTCatchStatement>> equivClasses = equivalenceClasses(catchStatements);
for (List<ASTCatchStatement> identicalStmts : equivClasses) {

View File

@@ -26,6 +26,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTBlockStatement;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType;
import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTExpression;
import net.sourceforge.pmd.lang.java.ast.ASTFinallyStatement;
import net.sourceforge.pmd.lang.java.ast.ASTFormalParameters;
import net.sourceforge.pmd.lang.java.ast.ASTIfStatement;
import net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration;
@@ -362,9 +363,10 @@ public class CloseResourceRule extends AbstractJavaRule {
}
}
if (t.getBeginLine() > id.getBeginLine() && t.hasFinally()) {
ASTBlock f = (ASTBlock) t.getFinally().jjtGetChild(0);
List<ASTName> names = f.findDescendantsOfType(ASTName.class);
ASTFinallyStatement finallyClause = t.getFinallyClause();
if (t.getBeginLine() > id.getBeginLine() && finallyClause != null) {
ASTBlock finallyBody = finallyClause.getBody();
List<ASTName> names = finallyBody.findDescendantsOfType(ASTName.class);
for (ASTName oName : names) {
String name = oName.getImage();
if (name != null && name.contains(".")) {
@@ -373,7 +375,7 @@ public class CloseResourceRule extends AbstractJavaRule {
String methodName = parts[1];
String varName = parts[0];
if (varName.equals(variableToClose) && closeTargets.contains(methodName)
&& nullCheckIfCondition(f, oName, varName)) {
&& nullCheckIfCondition(finallyBody, oName, varName)) {
closed = true;
break;
}
@@ -386,7 +388,7 @@ public class CloseResourceRule extends AbstractJavaRule {
}
List<ASTStatementExpression> exprs = new ArrayList<>();
f.findDescendantsOfType(ASTStatementExpression.class, exprs, true);
finallyBody.findDescendantsOfType(ASTStatementExpression.class, exprs, true);
for (ASTStatementExpression stmt : exprs) {
ASTPrimaryExpression expr = stmt.getFirstChildOfType(ASTPrimaryExpression.class);
if (expr != null) {