Skip to content

Commit

Permalink
[javac] fix quickfix to implement inherited abstract methods
Browse files Browse the repository at this point in the history
- add mapping for error id for missing abstract method implementations
- fixed an NPE that would prevent the error from being shown on
  anonymous classes
- fix binding logic for anonymous classes

example to try out: use the quickfix to add a stub implementation of
method() to the anonymous class:

```java
public class Parent {

  static interface IMethodable {
    void method();
  }

  public static void myMethod() {
    IMethodable methodable = new IMethodable() {
    };
    methodable.method();
  }

}
```

Future work: fix the diagnostic range on anonymous classes. We might
need access to the AST, since we ideally want to highlight `IMethodable`
from the constructor invocation.

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 committed May 7, 2024
1 parent 82cfa0f commit 8442e39
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@ ITypeBinding resolveType(EnumDeclaration enumDecl) {
return null;
}

@Override
ITypeBinding resolveType(AnonymousClassDeclaration anonymousClassDecl) {
resolve();
JCTree javacNode = this.converter.domToJavac.get(anonymousClassDecl);
if (javacNode instanceof JCClassDecl jcClassDecl) {
return new JavacTypeBinding(jcClassDecl.type, this);
}
return null;
}

public IBinding getBinding(final Symbol owner, final com.sun.tools.javac.code.Type type) {
if (owner instanceof final PackageSymbol other) {
return new JavacPackageBinding(other, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private static org.eclipse.jface.text.Position getDiagnosticPosition(JCDiagnosti

private static org.eclipse.jface.text.Position getDiagnosticPosition(String name, int startPosition, JCDiagnostic jcDiagnostic)
throws IOException {
if (name != null) {
if (name != null && !name.isEmpty()) {
DiagnosticSource source = jcDiagnostic.getDiagnosticSource();
JavaFileObject fileObject = source.getFile();
CharSequence charContent = fileObject.getCharContent(true);
Expand Down Expand Up @@ -149,6 +149,7 @@ public static int toProblemId(Diagnostic<? extends JavaFileObject> javacDiagnost
case "compiler.err.cant.apply.symbols" -> IProblem.UndefinedConstructor;
case "compiler.err.premature.eof" -> IProblem.ParsingErrorUnexpectedEOF; // syntax error
case "compiler.err.report.access" -> convertNotVisibleAccess(javacDiagnostic);
case "compiler.err.does.not.override.abstract" -> IProblem.AbstractMethodMustBeImplemented;
case COMPILER_WARN_MISSING_SVUID -> IProblem.MissingSerialVersion;
case COMPILER_WARN_NON_SERIALIZABLE_INSTANCE_FIELD -> 99999999; // JDT doesn't have this diagnostic
// TODO complete mapping list; dig in https://github.com/openjdk/jdk/blob/master/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
Expand Down

0 comments on commit 8442e39

Please sign in to comment.