[java] Deprecate ASTThrowStatement#getFirstClassOrInterfaceTypeImage()

Refs #2665
This commit is contained in:
Andreas Dangel
2020-07-30 11:31:29 +02:00
parent 5bff6ac52e
commit a5eb60243d
3 changed files with 17 additions and 2 deletions

View File

@ -37,7 +37,10 @@ public class ASTThrowStatement extends AbstractJavaNode {
*
* @return the image of the first ASTClassOrInterfaceType node found or
* <code>null</code>
* @deprecated This method is too specific and doesn't support all cases.
* It will be removed with PMD 7.
*/
@Deprecated
public final String getFirstClassOrInterfaceTypeImage() {
final ASTClassOrInterfaceType t = getFirstDescendantOfType(ASTClassOrInterfaceType.class);
return t == null ? null : t.getImage();

View File

@ -6,6 +6,8 @@ package net.sourceforge.pmd.lang.java.rule.design;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import net.sourceforge.pmd.lang.java.ast.ASTCatchStatement;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType;
import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter;
@ -35,8 +37,7 @@ public class ExceptionAsFlowControlRule extends AbstractJavaRule {
ASTFormalParameter fp = (ASTFormalParameter) catchStmt.getChild(0);
ASTType type = fp.getFirstDescendantOfType(ASTType.class);
ASTClassOrInterfaceType name = type.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
if (node.getFirstClassOrInterfaceTypeImage() != null
&& node.getFirstClassOrInterfaceTypeImage().equals(name.getImage())) {
if (isExceptionOfTypeThrown(node, name.getImage())) {
addViolation(data, name);
}
}
@ -44,4 +45,9 @@ public class ExceptionAsFlowControlRule extends AbstractJavaRule {
return data;
}
private boolean isExceptionOfTypeThrown(ASTThrowStatement throwStatement, String typeName) {
final ASTClassOrInterfaceType t = throwStatement.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
String thrownTypeName = t == null ? null : t.getImage();
return StringUtils.equals(thrownTypeName, typeName);
}
}