Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

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

Open
Eskibear opened this issue Jul 16, 2020 · 6 comments
Open

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

Eskibear opened this issue Jul 16, 2020 · 6 comments
Labels

Comments

@Eskibear
Copy link
Contributor

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
Copy link
Contributor

The code does not compile, not an issue.

@sadikovi sadikovi reopened this Jul 16, 2020
@Eskibear
Copy link
Contributor Author

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
Copy link
Contributor

sadikovi commented Jul 16, 2020

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
Copy link
Contributor

sadikovi commented Jul 16, 2020

Also, shouldn't it be like this?

Foo.Bar example2 = new example1.Bar();   // <----

@Eskibear
Copy link
Contributor Author

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
Copy link
Contributor

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

@sadikovi sadikovi added bug and removed enhancement labels Jul 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants