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

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

Open

Description

@Eskibear

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

Activity

  1. sadikovi commented on Jul 16, 2020

    @sadikovi
    Contributor

    The code does not compile, not an issue.

  2. Eskibear commented on Jul 16, 2020

    @Eskibear
    ContributorAuthor

    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.
    
  3. sadikovi commented on Jul 16, 2020

    @sadikovi
    Contributor

    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.

  4. sadikovi commented on Jul 16, 2020

    @sadikovi
    Contributor

    Also, shouldn't it be like this?

    Foo.Bar example2 = new example1.Bar();   // <----
  5. Eskibear commented on Jul 16, 2020

    @Eskibear
    ContributorAuthor

    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
    
  6. sadikovi commented on Jul 16, 2020

    @sadikovi
    Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions