Skip to content

Consider use x.getClass as a null check #107

Closed
@retronym

Description

@retronym

These are semantically equivalent:

if (x eq null) throw new NullPointerException() //1
if (x eq null) throw null //2
x.getClass //3

We use option 2 in our null checks for the outer pointer in inner class constructors. I just noticed that javac used option 3 when spinning up a lambda based on an instance method reference.

    //  0: aload_0
    //  1: astore_1
    //  2: aload_1
    //  3: dup
    //  4: invokevirtual #3                  // Method java/lang/Object.getClass:()Ljava/lang/Class;
    //  7: pop
    //  8: invokedynamic #2,  0              // InvokeDynamic #0:x:(LTest;)LTest$FI1; (invokevirtual Test.bar:(Ljava/lang/Object;Ljava/lang/Object;)V)
    // 13: areturn
FI1 a1() { Test t1 = this; return t1::bar; }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions