atom / language-java

Java package for Atom
Other
62 stars 58 forks source link

Wrong highlighting on 'new' keyword on inner classes #228

Open Eskibear opened 4 years ago

Eskibear commented 4 years ago

Description

creating a new instance of an inner class doesn't highlight the new keyword properly

inner

Steps to Reproduce

public class Foo {
    public class Bar {
    }
}

public class Main {
    public static void main(String[] args) {
        Foo example1 = new Foo();
        Foo.Bar example2 = example1.new Bar();   // <----
    }
}

Expected behavior:

keyword new is correctly highlighted.

Actual behavior: image

sadikovi commented 4 years ago

The code does not compile, not an issue.

Eskibear commented 4 years ago

It compiles.

$> java -version
openjdk version "14.0.1" 2020-04-14
OpenJDK Runtime Environment Zulu14.28+21-CA (build 14.0.1+8)
OpenJDK 64-Bit Server VM Zulu14.28+21-CA (build 14.0.1+8, mixed mode, sharing)

$> tree 
.
├── Foo$Bar.class
├── Foo.class
├── Foo.java
├── Main.class
└── Main.java

0 directories, 5 files

$> cat Foo.java 
public class Foo {

    public class Bar {

        void run() {
            System.out.println("It complies.");
        }
    }
}

$> cat Main.java 
public class Main {
    public static void main(String[] args) {
        Foo foo = new Foo();
        Foo.Bar bar = foo.new Bar();
        bar.run();
    }
}

$> java Main
It complies.
sadikovi commented 4 years ago

Is that OpenJDK 14? I compile on JDK 8, which is what we keep as a baseline. This is a new feature then. Please update the issue accordingly.

sadikovi commented 4 years ago

Also, shouldn't it be like this?

Foo.Bar example2 = new example1.Bar();   // <----
Eskibear commented 4 years ago

See https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html , section "Inner Classes".

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

OuterClass.InnerClass innerObject = outerObject.new InnerClass()

It's strange, and I don't know why new example1.Bar() compiles for you. It doesn't compiles for me, with either JDK 8 or 14.

$> `java_home -v 1.8`/bin/javac Main.java
Main.java:4: error: package example1 does not exist
        Foo.Bar example2 = new example1.Bar();
                                       ^
1 error

$> `java_home -v 14`/bin/javac Main.java 
Main.java:4: error: package example1 does not exist
        Foo.Bar example2 = new example1.Bar();
                                       ^
1 error
sadikovi commented 4 years ago

Yeah, I came up with a working example, similar to your code. Looks like a bug.