Skip to content

Commit

Permalink
[java] NonSerializableClass - Fix NPE
Browse files Browse the repository at this point in the history
  • Loading branch information
adangel committed Nov 18, 2022
1 parent 7d0f608 commit 78ac4bc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public Object visit(ASTTypeDeclaration node, Object data) {
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()) {
Expand Down Expand Up @@ -189,7 +189,7 @@ private Set<String> determinePersistentFields(ASTAnyTypeDeclaration typeDeclarat
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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>

0 comments on commit 78ac4bc

Please sign in to comment.