[java] NonSerializableClass - Fix NPE

This commit is contained in:
Andreas Dangel
2022-11-18 11:19:23 +01:00
parent 7d0f60891c
commit 78ac4bc387
2 changed files with 16 additions and 3 deletions

View File

@@ -71,8 +71,8 @@ public class NonSerializableClassRule extends AbstractJavaRule {
ASTAnyTypeDeclaration anyTypeDeclaration = node.getFirstChildOfType(ASTAnyTypeDeclaration.class);
for (ASTFieldDeclaration field : anyTypeDeclaration.findDescendantsOfType(ASTFieldDeclaration.class)) {
for (ASTVariableDeclaratorId varId : field) {
if (SERIAL_PERSISTENT_FIELDS_NAME.equals(varId.getName())) {
if (!TypeTestUtil.isA(SERIAL_PERSISTENT_FIELDS_TYPE, field)
if (SERIAL_PERSISTENT_FIELDS_NAME.equals(varId.getName()) && varId.getType() != null) {
if (!TypeTestUtil.isA(SERIAL_PERSISTENT_FIELDS_TYPE, varId)
|| !field.isPrivate()
|| !field.isStatic()
|| !field.isFinal()) {
@@ -189,7 +189,7 @@ public class NonSerializableClassRule extends AbstractJavaRule {
if (fields.isEmpty()) {
// field initializer might be a reference to a constant
ASTName reference = persistentFieldsDecl.getFirstDescendantOfType(ASTName.class);
if (reference.getNameDeclaration() != null) {
if (reference != null && reference.getNameDeclaration() != null) {
for (ASTLiteral literal : reference.getNameDeclaration().getNode().getParent().findDescendantsOfType(ASTLiteral.class)) {
if (literal.isStringLiteral()) {
fields.add(StringUtil.removeDoubleQuotes(literal.getImage()));

View File

@@ -349,6 +349,19 @@ class Buzz implements Serializable {
private FileInputStream stream; // not serializable
private String name;
}
]]></code>
</test-code>
<test-code>
<description>NPE with empty array</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.io.ObjectStreamField;
import java.io.Serializable;
class Buzz implements Serializable {
private static final ObjectStreamField[] serialPersistentFields =
new ObjectStreamField[0];
}
]]></code>
</test-code>
</test-data>