This commit is contained in:
Clément Fournier
2023-04-24 17:37:27 +02:00
parent 920386d4bd
commit ff026ce827
2 changed files with 27 additions and 2 deletions

View File

@ -36,6 +36,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTConstructorCall;
import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTContinueStatement; import net.sourceforge.pmd.lang.java.ast.ASTContinueStatement;
import net.sourceforge.pmd.lang.java.ast.ASTDoStatement; import net.sourceforge.pmd.lang.java.ast.ASTDoStatement;
import net.sourceforge.pmd.lang.java.ast.ASTEnumConstant;
import net.sourceforge.pmd.lang.java.ast.ASTExpression; import net.sourceforge.pmd.lang.java.ast.ASTExpression;
import net.sourceforge.pmd.lang.java.ast.ASTFieldAccess; import net.sourceforge.pmd.lang.java.ast.ASTFieldAccess;
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
@ -954,6 +955,7 @@ public final class DataflowPass {
@Override @Override
public SpanInfo visitTypeDecl(ASTAnyTypeDeclaration node, SpanInfo data) { public SpanInfo visitTypeDecl(ASTAnyTypeDeclaration node, SpanInfo data) {
// process initializers and ctors first
processInitializers(node.getDeclarations(), data, node.getSymbol()); processInitializers(node.getDeclarations(), data, node.getSymbol());
for (ASTBodyDeclaration decl : node.getDeclarations()) { for (ASTBodyDeclaration decl : node.getDeclarations()) {
@ -989,12 +991,14 @@ public final class DataflowPass {
for (ASTBodyDeclaration declaration : declarations) { for (ASTBodyDeclaration declaration : declarations) {
final boolean isStatic; final boolean isStatic;
if (declaration instanceof ASTFieldDeclaration) { if (declaration instanceof ASTEnumConstant) {
isStatic = true;
} else if (declaration instanceof ASTFieldDeclaration) {
isStatic = ((ASTFieldDeclaration) declaration).isStatic(); isStatic = ((ASTFieldDeclaration) declaration).isStatic();
} else if (declaration instanceof ASTInitializer) { } else if (declaration instanceof ASTInitializer) {
isStatic = ((ASTInitializer) declaration).isStatic(); isStatic = ((ASTInitializer) declaration).isStatic();
} else if (declaration instanceof ASTConstructorDeclaration } else if (declaration instanceof ASTConstructorDeclaration
|| declaration instanceof ASTCompactConstructorDeclaration) { || declaration instanceof ASTCompactConstructorDeclaration) {
ctors.add(declaration); ctors.add(declaration);
continue; continue;
} else { } else {

View File

@ -400,4 +400,25 @@ record MyRecord(boolean b) {
} }
]]></code> ]]></code>
</test-code> </test-code>
<test-code>
<description>NPE with Switch in enum constant body</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>7</expected-linenumbers>
<code><![CDATA[
enum Foo {
A {
void foo() {
switch (A) {
case A:
if (A != null) {}
default:
throw new UnsupportedOperationException();
}
}
}
}
]]></code>
</test-code>
</test-data> </test-data>