eclipse-jdt / eclipse-jdt/eclipse.jdt.core

ECJ treats non-local classes as local classes when considering modifiers

Open
#4,241 3 comments 0 reactions 1 assignee Claimed by @srikanth-sankaran View on GitHub
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

ECJ treats nested members of local classes as if they were local classes when considering allowed modifiers. For instance, consider:

```java
package test;

public class Test {
public static void main(String[] args) {
class LocalClass {
public class NotALocalClass {}
}
}
}
```

In this example, `LocalClass` is a local class but `NotALocalClass` is _not_; by the JLS:
> A local class is a nested class ([§8 (Classes)](https://docs.oracle.com/javase/specs/jls/se24/html/jls-8.html)) whose declaration is **immediately** contained by a block ([§14.2](https://docs.oracle.com/javase/specs/jls/se24/html/jls-14.html#jls-14.2)).

(from JLS [§14.3](https://docs.oracle.com/javase/specs/jls/se24/html/jls-14.html#jls-14.3); emphasis mine) which does not apply to `NotALocalClass`. Thus, `NotALocalClass` being public should not be an issue (and indeed javac compiles this without issue). However, ecj treats it as a local class for the purposes of determining if the `public` modifier is allowed here -- attempting to compile the example with the latest ecj (`3.42.50-SNAPSHOT`) gives:

```
1. ERROR in Test.java (at line 6)
public class NotALocalClass {}
^^^^^^^^^^^^^^
Illegal modifier for the local class NotALocalClass; only abstract or final is permitted
----------
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.