GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CodeQL documentation
  • CodeQL overview
  • Writing CodeQL queries
  • CodeQL language guides
  • QL language reference
  • CodeQL standard-libraries
  • CodeQL query help
  • CodeQL repository
  • QL publications
  • CodeQL query help for C and C++
  • CodeQL query help for C#
  • CodeQL query help for GitHub Actions
  • CodeQL query help for Go
  • CodeQL query help for Java and Kotlin

This view

  • Original page
  • New address

GeezerVison is showing codeql.github.com — Externalizable but no public no-argument constructor — CodeQL query help documentation

Externalizable but no public no-argument constructor¶

ID: java/missing-no-arg-constructor-on-externalizable
Kind: problem
Security severity: 
Severity: warning
Precision: medium
Tags:
   - quality
   - reliability
   - correctness
Query suites:
   - java-security-and-quality.qls

Click to see the query in the CodeQL repository

A class that implements java.io.Externalizable must have a public no-argument constructor. The constructor is used by the Java serialization framework when it creates the object during deserialization. If the class does not define such a constructor, the Java serialization framework throws an InvalidClassException.

The Java Development Kit API documentation for Externalizable states:

When an Externalizable object is reconstructed, an instance is created using the public no-arg constructor, then the readExternal method called.

Recommendation¶

Make sure that externalizable classes always have a no-argument constructor.

Example¶

In the following example, WrongMemo does not declare a public no-argument constructor. When the Java serialization framework tries to deserialize the object, an InvalidClassException is thrown. However, Memo does declare a public no-argument constructor, so that the object is deserialized successfully.

class WrongMemo implements Externalizable {
    private String memo;

    // BAD: No public no-argument constructor is defined. Deserializing this object
    // causes an 'InvalidClassException'.

    public WrongMemo(String memo) {
        this.memo = memo;
    }

    public void writeExternal(ObjectOutput arg0) throws IOException {
        //...
    }
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        //...
    }
}

class Memo implements Externalizable {
    private String memo;

    // GOOD: Declare a public no-argument constructor, which is used by the
    // serialization framework when the object is deserialized.
    public Memo() {
    }

    public Memo(String memo) {
        this.memo = memo;
    }

    public void writeExternal(ObjectOutput out) throws IOException {
        //...
    }
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        //...
    }
}

References¶

  • Java API Specification: Externalizable.

  • © GitHub, Inc.
  • Terms
  • Privacy
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.