From 7312c767be4000bc60166e3f8b9859b09752c4e3 Mon Sep 17 00:00:00 2001 From: Jinbo Wang Date: Wed, 8 May 2024 17:59:13 +0800 Subject: [PATCH] Map Javac problems to JDT problems --- .../diagnostics/Ambiguous/Ambiguous.java | 19 +++ .../diagnostics/Ambiguous/pkg1/A.java | 5 + .../diagnostics/Ambiguous/pkg2/A.java | 5 + .../diagnostics/AnnotationMember.java | 8 + .../diagnostics/BodyForAbstractMethod.java | 7 + .../diagnostics/CodeCannotBeReached.java | 7 + .../examples/diagnostics/File.java | 5 + .../diagnostics/FileNameAndClassName.java | 4 + .../IncompatibleExpInThrow/Sub.java | 9 ++ .../IncompatibleExpInThrow/Super.java | 8 + .../IncompatibleReturnType/Sub.java | 9 ++ .../IncompatibleReturnType/Super.java | 9 ++ ...nvalidUnionTypeReferenceSequenceCatch.java | 15 ++ .../diagnostics/MethodReturnsVoid.java | 11 ++ .../diagnostics/MissingReturnType.java | 7 + .../MissingValueForAnnotationMember/A.java | 9 ++ .../CustomAnnotation.java | 5 + .../diagnostics/NoMessageSendOnArrayType.java | 8 + .../diagnostics/NotVisibleConstructor/A.java | 8 + .../diagnostics/NotVisibleConstructor/B.java | 8 + .../Sub.java | 6 + .../Super.java | 7 + .../diagnostics/NotVisibleMethod/A.java | 7 + .../diagnostics/NotVisibleMethod/B.java | 9 ++ .../diagnostics/NotVisibleType/A.java | 7 + .../diagnostics/NotVisibleType/B.java | 8 + .../diagnostics/ParameterMismatch.java | 22 +++ .../examples/diagnostics/TypeMismatch.java | 29 ++++ .../examples/diagnostics/Undefined.java | 46 ++++++ .../Sub.java | 5 + .../Super.java | 7 + .../diagnostics/UnhandledException.java | 22 +++ .../Sub.java | 5 + .../Super.java | 7 + .../diagnostics/UnreachableCatch.java | 26 +++ .../examples/diagnostics/Unresolved.java | 33 ++++ .../diagnostics/UnterminatedString.java | 5 + .../diagnostics/VoidMethodReturnsValue.java | 7 + .../diagnostics/notVisibleField/A.java | 5 + .../diagnostics/notVisibleField/B.java | 9 ++ .../internal/javac/JavacProblemConverter.java | 151 ++++++++++++++++-- 41 files changed, 577 insertions(+), 12 deletions(-) create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/Ambiguous/Ambiguous.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/Ambiguous/pkg1/A.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/Ambiguous/pkg2/A.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/AnnotationMember.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/BodyForAbstractMethod.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/CodeCannotBeReached.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/File.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/FileNameAndClassName.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/IncompatibleExpInThrow/Sub.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/IncompatibleExpInThrow/Super.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/IncompatibleReturnType/Sub.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/IncompatibleReturnType/Super.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/InvalidUnionTypeReferenceSequenceCatch.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/MethodReturnsVoid.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/MissingReturnType.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/MissingValueForAnnotationMember/A.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/MissingValueForAnnotationMember/CustomAnnotation.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/NoMessageSendOnArrayType.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleConstructor/A.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleConstructor/B.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleConstructorInDefaultConstructor/Sub.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleConstructorInDefaultConstructor/Super.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleMethod/A.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleMethod/B.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleType/A.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleType/B.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/ParameterMismatch.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/TypeMismatch.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/Undefined.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/UndefinedConstructorInDefaultConstructor/Sub.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/UndefinedConstructorInDefaultConstructor/Super.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/UnhandledException.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/UnhandledExceptionInDefaultConstructor/Sub.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/UnhandledExceptionInDefaultConstructor/Super.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/UnreachableCatch.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/Unresolved.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/UnterminatedString.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/VoidMethodReturnsValue.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/notVisibleField/A.java create mode 100644 org.eclipse.jdt.core.javac/examples/diagnostics/notVisibleField/B.java diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/Ambiguous/Ambiguous.java b/org.eclipse.jdt.core.javac/examples/diagnostics/Ambiguous/Ambiguous.java new file mode 100644 index 00000000000..343d9d7996d --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/Ambiguous/Ambiguous.java @@ -0,0 +1,19 @@ +package Ambiguous; + +import Ambiguous.pkg1.*; +import Ambiguous.pkg2.*; + +public class Ambiguous { + private void testAmbiguous1() { + // compiler.err.ref.ambiguous -> AmbiguousType(16777220) + A a; + // compiler.err.ref.ambiguous -> AmbiguousMethod(67108966) + method(1, 2); + } + + void method(int i, double d) { + } + + void method(double d, int m) { + } +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/Ambiguous/pkg1/A.java b/org.eclipse.jdt.core.javac/examples/diagnostics/Ambiguous/pkg1/A.java new file mode 100644 index 00000000000..8bb7e0a6080 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/Ambiguous/pkg1/A.java @@ -0,0 +1,5 @@ +package Ambiguous.pkg1; + +public class A { + +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/Ambiguous/pkg2/A.java b/org.eclipse.jdt.core.javac/examples/diagnostics/Ambiguous/pkg2/A.java new file mode 100644 index 00000000000..3fbbbc7dd39 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/Ambiguous/pkg2/A.java @@ -0,0 +1,5 @@ +package Ambiguous.pkg2; + +public class A { + +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/AnnotationMember.java b/org.eclipse.jdt.core.javac/examples/diagnostics/AnnotationMember.java new file mode 100644 index 00000000000..d4bb143adee --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/AnnotationMember.java @@ -0,0 +1,8 @@ + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +// compiler.err.annotation.value.must.be.name.value -> UndefinedAnnotationMember(67109475) +@Retention(RetentionPolicy.RUNTIME, "error") +public @interface AnnotationMember { +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/BodyForAbstractMethod.java b/org.eclipse.jdt.core.javac/examples/diagnostics/BodyForAbstractMethod.java new file mode 100644 index 00000000000..2b8585612ff --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/BodyForAbstractMethod.java @@ -0,0 +1,7 @@ + +public abstract class BodyForAbstractMethod { + // compiler.err.abstract.meth.cant.have.body -> BodyForAbstractMethod(603979889) + abstract void testBodyForAbstractMethod() { + + } +} \ No newline at end of file diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/CodeCannotBeReached.java b/org.eclipse.jdt.core.javac/examples/diagnostics/CodeCannotBeReached.java new file mode 100644 index 00000000000..c101d7281b5 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/CodeCannotBeReached.java @@ -0,0 +1,7 @@ +public class CodeCannotBeReached { + public void testCodeCannotBeReached() { + return; + // compiler.err.unreachable.stmt -> CodeCannotBeReached(536871073) + String reach = ""; + } +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/File.java b/org.eclipse.jdt.core.javac/examples/diagnostics/File.java new file mode 100644 index 00000000000..51d69c33678 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/File.java @@ -0,0 +1,5 @@ +import java.io.File; +// compiler.err.already.defined.this.unit -> ConflictingImport(268435841) +public class File { + +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/FileNameAndClassName.java b/org.eclipse.jdt.core.javac/examples/diagnostics/FileNameAndClassName.java new file mode 100644 index 00000000000..7a13c247347 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/FileNameAndClassName.java @@ -0,0 +1,4 @@ + +// compiler.err.class.public.should.be.in.file -> PublicClassMustMatchFileName(16777541) +public class ClassName { +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/IncompatibleExpInThrow/Sub.java b/org.eclipse.jdt.core.javac/examples/diagnostics/IncompatibleExpInThrow/Sub.java new file mode 100644 index 00000000000..9c96295cf7b --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/IncompatibleExpInThrow/Sub.java @@ -0,0 +1,9 @@ +package IncompatibleExpInThrow; + +// compiler.err.override.meth.doesnt.throw -> IncompatibleExceptionInThrowsClause(67109266) +public class Sub extends Super { + @Override + void foo() throws Exception { + + } +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/IncompatibleExpInThrow/Super.java b/org.eclipse.jdt.core.javac/examples/diagnostics/IncompatibleExpInThrow/Super.java new file mode 100644 index 00000000000..6a4f71ecb62 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/IncompatibleExpInThrow/Super.java @@ -0,0 +1,8 @@ +package IncompatibleExpInThrow; + +import java.io.IOException; + +public class Super { + void foo() throws IOException { + } +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/IncompatibleReturnType/Sub.java b/org.eclipse.jdt.core.javac/examples/diagnostics/IncompatibleReturnType/Sub.java new file mode 100644 index 00000000000..9aec288019f --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/IncompatibleReturnType/Sub.java @@ -0,0 +1,9 @@ +package IncompatibleReturnType; + +// compiler.err.override.incompatible.ret -> UndefinedAnnotationMember(67109475) +public class Sub extends Super { + @Override + void foo() { + + } +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/IncompatibleReturnType/Super.java b/org.eclipse.jdt.core.javac/examples/diagnostics/IncompatibleReturnType/Super.java new file mode 100644 index 00000000000..bb0330e9d91 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/IncompatibleReturnType/Super.java @@ -0,0 +1,9 @@ +package IncompatibleReturnType; + +import java.io.IOException; + +public class Super { + String foo() { + return "foo"; + } +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/InvalidUnionTypeReferenceSequenceCatch.java b/org.eclipse.jdt.core.javac/examples/diagnostics/InvalidUnionTypeReferenceSequenceCatch.java new file mode 100644 index 00000000000..d2226f76d05 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/InvalidUnionTypeReferenceSequenceCatch.java @@ -0,0 +1,15 @@ + +import java.io.File; +import java.io.IOException; +import java.io.FileNotFoundException; + +public class InvalidUnionTypeReferenceSequenceCatch { + public void testInvalidUnionTypeReferenceSequence() { + try { + boolean success = new File("f").createNewFile(); + } catch (FileNotFoundException | IOException e) { + // compiler.err.multicatch.types.must.be.disjoint -> InvalidUnionTypeReferenceSequence(553649001) + + } + } +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/MethodReturnsVoid.java b/org.eclipse.jdt.core.javac/examples/diagnostics/MethodReturnsVoid.java new file mode 100644 index 00000000000..3c7b2981391 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/MethodReturnsVoid.java @@ -0,0 +1,11 @@ + +public class MethodReturnsVoid { + public void testVoidMethod() { + + } + + public String testMethodReturnsVoid() { + // compiler.err.prob.found.req -> MethodReturnsVoid(67108969) + return testVoidMethod(); + } +} \ No newline at end of file diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/MissingReturnType.java b/org.eclipse.jdt.core.javac/examples/diagnostics/MissingReturnType.java new file mode 100644 index 00000000000..3f376bb9df6 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/MissingReturnType.java @@ -0,0 +1,7 @@ + +public class MissingReturnType { + // compiler.err.invalid.meth.decl.ret.type.req -> MissingReturnType(16777327) + public testMissingReturnType() { + + } +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/MissingValueForAnnotationMember/A.java b/org.eclipse.jdt.core.javac/examples/diagnostics/MissingValueForAnnotationMember/A.java new file mode 100644 index 00000000000..75422c36e0c --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/MissingValueForAnnotationMember/A.java @@ -0,0 +1,9 @@ +package MissingValueForAnnotationMember; + +// compiler.err.annotation.missing.default.value -> MissingValueForAnnotationMember(16777825) +public class A { + @CustomAnnotation + public void testMissingValueForAnnotationMember() { + + } +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/MissingValueForAnnotationMember/CustomAnnotation.java b/org.eclipse.jdt.core.javac/examples/diagnostics/MissingValueForAnnotationMember/CustomAnnotation.java new file mode 100644 index 00000000000..bc44ce9c145 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/MissingValueForAnnotationMember/CustomAnnotation.java @@ -0,0 +1,5 @@ +package MissingValueForAnnotationMember; + +public @interface CustomAnnotation { + String name(); +} \ No newline at end of file diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/NoMessageSendOnArrayType.java b/org.eclipse.jdt.core.javac/examples/diagnostics/NoMessageSendOnArrayType.java new file mode 100644 index 00000000000..a80ca40913d --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/NoMessageSendOnArrayType.java @@ -0,0 +1,8 @@ + +public class NoMessageSendOnArrayType { + public void testNoMessageSendOnArrayType() { + String[] test = {"1", "2"}; + // compiler.err.cant.resolve.location.args -> NoMessageSendOnArrayType(67108980) + int size = test.size(); + } +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleConstructor/A.java b/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleConstructor/A.java new file mode 100644 index 00000000000..1a3255d660c --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleConstructor/A.java @@ -0,0 +1,8 @@ +package NotVisibleConstructor; + +public class A { + private String a; + private A(String a) { + this.a = a; + } +} \ No newline at end of file diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleConstructor/B.java b/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleConstructor/B.java new file mode 100644 index 00000000000..9e3fa1a9c49 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleConstructor/B.java @@ -0,0 +1,8 @@ +package NotVisibleConstructor; + +public class B { + public void testNotVisibleConstructor() { + // compiler.err.report.access -> NotVisibleConstructor(134217859) + A a = new A("a"); + } +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleConstructorInDefaultConstructor/Sub.java b/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleConstructorInDefaultConstructor/Sub.java new file mode 100644 index 00000000000..01931294a7f --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleConstructorInDefaultConstructor/Sub.java @@ -0,0 +1,6 @@ +package NotVisibleConstructorInDefaultConstructor; + +// compiler.err.report.access -> NotVisibleConstructorInDefaultConstructor(134217869) +public class Sub extends Super { + +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleConstructorInDefaultConstructor/Super.java b/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleConstructorInDefaultConstructor/Super.java new file mode 100644 index 00000000000..ab18a17e3a3 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleConstructorInDefaultConstructor/Super.java @@ -0,0 +1,7 @@ +package NotVisibleConstructorInDefaultConstructor; + +public class Super { + private Super() { + + } +} \ No newline at end of file diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleMethod/A.java b/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleMethod/A.java new file mode 100644 index 00000000000..b5d4b9b7d57 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleMethod/A.java @@ -0,0 +1,7 @@ +package NotVisibleMethod; + +public class A { + private void a() { + + } +} \ No newline at end of file diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleMethod/B.java b/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleMethod/B.java new file mode 100644 index 00000000000..4260a390801 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleMethod/B.java @@ -0,0 +1,9 @@ +package NotVisibleMethod; + +public class B { + public void testNotVisibleMethod() { + A a = new A(); + // compiler.err.report.access -> NotVisibleMethod(67108965) + a.a(); + } +} \ No newline at end of file diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleType/A.java b/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleType/A.java new file mode 100644 index 00000000000..a4a6eb7ab7a --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleType/A.java @@ -0,0 +1,7 @@ +package NotVisibleType; + +public class A { + private class Inner { + + } +} \ No newline at end of file diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleType/B.java b/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleType/B.java new file mode 100644 index 00000000000..ed5fa81a285 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/NotVisibleType/B.java @@ -0,0 +1,8 @@ +package NotVisibleType; + +public class B { + public void testNotVisibleType() { + // compiler.err.report.access -> NotVisibleType(16777219) + A.Inner i = new A.Inner(); + } +} \ No newline at end of file diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/ParameterMismatch.java b/org.eclipse.jdt.core.javac/examples/diagnostics/ParameterMismatch.java new file mode 100644 index 00000000000..d5a20ab22a6 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/ParameterMismatch.java @@ -0,0 +1,22 @@ + +public class ParameterMismatch { + + private String message; + + private void setMessage(String message) { + this.message = message; + } + + private void testMethodParameterMatch() { + // compiler.err.cant.apply.symbol -> ParameterMismatch(67108979) + this.setMessage(); + } + + void m(int i1) {} + void m(int i1, int i2) {} + + ParameterMismatch() { + // compiler.err.cant.apply.symbols -> ParameterMismatch(67108979) + this.m(); + } +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/TypeMismatch.java b/org.eclipse.jdt.core.javac/examples/diagnostics/TypeMismatch.java new file mode 100644 index 00000000000..e02347946fd --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/TypeMismatch.java @@ -0,0 +1,29 @@ + +import java.util.List; +import java.util.ArrayList; + +public class TypeMismatch { + private void testTypeMismatch() { + // compiler.err.illegal.initializer.for.type -> TypeMismatch(16777233) + String a = { "a", "b" }; + } + + private void testTypeMismatch1() { + // compiler.err.prob.found.req -> TypeMismatch(16777233) + String a = new String[] { "a", "b" }; + } + + private String testReturnTypeMismatch() { + // compiler.err.prob.found.req -> ReturnTypeMismatch(16777235) + return new String[] { "a", "b" }; + } + + + private void testIncompatibleTypesInForeach() { + List intList = new ArrayList<>(); + // compiler.err.prob.found.req -> IncompatibleTypesInForeach(16777796) + for (String s : intList) { + s.hashCode(); + } + } +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/Undefined.java b/org.eclipse.jdt.core.javac/examples/diagnostics/Undefined.java new file mode 100644 index 00000000000..b4c073a9bde --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/Undefined.java @@ -0,0 +1,46 @@ + +import java.util.List; + +public class Undefined { + Undefined(Integer x) {} + + private void testUndefinedConstructor1() { + // compiler.err.cant.apply.symbols -> UndefinedConstructor(134217858) + String l = new String("s", "t"); + } + + void testUndefinedConstructor2() { + // compiler.err.cant.resolve.args -> UndefinedConstructor(134217858) + new Undefined(""){}; + } + + private void testUndefinedType() { + // compiler.err.cant.resolve.location -> UndefinedType(16777218) + UndefinedType a = new UndefinedType(); + } + + private void testUndefinedMethod1() { + // compiler.err.cant.resolve.location.args -> UndefinedMethod(67108964) + test(); + } + + private void testUndefinedMethod2() { + // compiler.err.cant.resolve.args.params -> UndefinedMethod(67108964) + Object o = new Object() { + { this.m2(1, ""); } + }; + } + + private void testUndefinedMethod3() { + // compiler.err.cant.resolve.args -> UndefinedMethod(67108964) + new Runnable() { + { unknown(); } + public void run() { } + }; + } + + private void testUndefinedMethod4() { + // compiler.err.cant.resolve.location.args.params -> UndefinedMethod(67108964) + Object o = List.unknown(); + } +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/UndefinedConstructorInDefaultConstructor/Sub.java b/org.eclipse.jdt.core.javac/examples/diagnostics/UndefinedConstructorInDefaultConstructor/Sub.java new file mode 100644 index 00000000000..b6ed2188366 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/UndefinedConstructorInDefaultConstructor/Sub.java @@ -0,0 +1,5 @@ +package UndefinedConstructorInDefaultConstructor; + +// compiler.err.cant.apply.symbol -> UndefinedConstructorInDefaultConstructor(134217868) +public class Sub extends Super { +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/UndefinedConstructorInDefaultConstructor/Super.java b/org.eclipse.jdt.core.javac/examples/diagnostics/UndefinedConstructorInDefaultConstructor/Super.java new file mode 100644 index 00000000000..2ed456b91d1 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/UndefinedConstructorInDefaultConstructor/Super.java @@ -0,0 +1,7 @@ +package UndefinedConstructorInDefaultConstructor; + +public class Super { + Super(int a) { + + } +} \ No newline at end of file diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/UnhandledException.java b/org.eclipse.jdt.core.javac/examples/diagnostics/UnhandledException.java new file mode 100644 index 00000000000..a8e387bf659 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/UnhandledException.java @@ -0,0 +1,22 @@ + +import java.io.IOException; + +public class UnhandledException { + public void testUnhandledException() { + throw new IOException("IOExp"); + } + + public static class AutoCloseableClass implements AutoCloseable { + @Override + public void close() throws Exception { + System.out.println("close"); + } + } + + // compiler.err.unreported.exception.implicit.close -> UnhandledExceptionOnAutoClose(16778098) + public void testUnhandledExceptionOnAutoClose() { + try (AutoCloseableClass a = new AutoCloseableClass()) { + System.out.println("try-with-resource AutoCloseableClass"); + } + } +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/UnhandledExceptionInDefaultConstructor/Sub.java b/org.eclipse.jdt.core.javac/examples/diagnostics/UnhandledExceptionInDefaultConstructor/Sub.java new file mode 100644 index 00000000000..a9c0819fb86 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/UnhandledExceptionInDefaultConstructor/Sub.java @@ -0,0 +1,5 @@ +package UnhandledExceptionInDefaultConstructor; + +// compiler.err.unreported.exception.default.constructor -> UnhandledExceptionInDefaultConstructor(16777362) +public class Sub extends Super { +} \ No newline at end of file diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/UnhandledExceptionInDefaultConstructor/Super.java b/org.eclipse.jdt.core.javac/examples/diagnostics/UnhandledExceptionInDefaultConstructor/Super.java new file mode 100644 index 00000000000..b12ea257478 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/UnhandledExceptionInDefaultConstructor/Super.java @@ -0,0 +1,7 @@ +package UnhandledExceptionInDefaultConstructor; + +public class Super { + Super() throws Exception { + throw new Exception("Exp"); + } +} \ No newline at end of file diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/UnreachableCatch.java b/org.eclipse.jdt.core.javac/examples/diagnostics/UnreachableCatch.java new file mode 100644 index 00000000000..603f1554632 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/UnreachableCatch.java @@ -0,0 +1,26 @@ + +import java.io.File; +import java.io.IOException; +import java.io.FileNotFoundException; + +public class UnreachableCatch { + + public void testUnreachableCatch() { + try { + String a = "a"; + } catch (IOException e) { // compiler.err.except.never.thrown.in.try -> UnreachableCatch(83886247) + + + } + } + + public void testInvalidCatchBlockSequence() { + try { + boolean success = new File("f").createNewFile(); + } catch (IOException e) { + + } catch (FileNotFoundException e) { // compiler.err.except.already.caught -> InvalidCatchBlockSequence(553648315) + + } + } +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/Unresolved.java b/org.eclipse.jdt.core.javac/examples/diagnostics/Unresolved.java new file mode 100644 index 00000000000..7e94f7c6fac --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/Unresolved.java @@ -0,0 +1,33 @@ + +public class Unresolved { + private void testUndefinedField() { + // compiler.err.cant.resolve -> UndefinedField(33554502) + String test = this.str; + + } + + private void testUnresolvedVariable1() { + // compiler.err.cant.resolve.location -> UnresolvedVariable(33554515) + String test = str; + + Object o = new Object() { + // compiler.err.cant.resolve -> UnresolvedVariable(33554515) + int i = f; + }; + } + + private void testUndefinedName() { + // compiler.err.cant.resolve.location -> UndefinedName(570425394) + String test = K.Strin(); + } + +} + +@interface Anno { + String name() default "anon"; + String address() default "here"; +} + +// compiler.err.cant.resolve -> UnresolvedVariable(33554515) +@Anno(name == "fred", address = "there") +class X { } \ No newline at end of file diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/UnterminatedString.java b/org.eclipse.jdt.core.javac/examples/diagnostics/UnterminatedString.java new file mode 100644 index 00000000000..0a85adccbda --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/UnterminatedString.java @@ -0,0 +1,5 @@ + +public class UnterminatedString { + // compiler.err.unclosed.str.lit -> UnterminatedString(1610612995) + private String test = "Test'; +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/VoidMethodReturnsValue.java b/org.eclipse.jdt.core.javac/examples/diagnostics/VoidMethodReturnsValue.java new file mode 100644 index 00000000000..3cc5413adaf --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/VoidMethodReturnsValue.java @@ -0,0 +1,7 @@ + +public class VoidMethodReturnsValue { + public void testVoidMethodReturnsValue() { + // compiler.err.prob.found.req -> VoidMethodReturnsValue(67108969) + return "VoidMethodReturnsValue"; + } +} diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/notVisibleField/A.java b/org.eclipse.jdt.core.javac/examples/diagnostics/notVisibleField/A.java new file mode 100644 index 00000000000..528134a13f6 --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/notVisibleField/A.java @@ -0,0 +1,5 @@ +package notVisibleField; + +public class A { + private int a; +} \ No newline at end of file diff --git a/org.eclipse.jdt.core.javac/examples/diagnostics/notVisibleField/B.java b/org.eclipse.jdt.core.javac/examples/diagnostics/notVisibleField/B.java new file mode 100644 index 00000000000..091684b5a1f --- /dev/null +++ b/org.eclipse.jdt.core.javac/examples/diagnostics/notVisibleField/B.java @@ -0,0 +1,9 @@ +package notVisibleField; + +public class B { + public void testNotVisibleField() { + A a = new A(); + // compiler.err.report.access -> NotVisibleField(33554503) + a.a = 1; + } +} \ No newline at end of file diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/JavacProblemConverter.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/JavacProblemConverter.java index 3d5aa70763d..db614f05f44 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/JavacProblemConverter.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/JavacProblemConverter.java @@ -25,9 +25,11 @@ import com.sun.tools.javac.code.Kinds; import com.sun.tools.javac.code.Symbol; +import com.sun.tools.javac.code.Type; import com.sun.tools.javac.parser.Scanner; import com.sun.tools.javac.parser.ScannerFactory; import com.sun.tools.javac.parser.Tokens.Token; +import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree.JCClassDecl; import com.sun.tools.javac.tree.JCTree.JCVariableDecl; import com.sun.tools.javac.util.Context; @@ -177,26 +179,138 @@ private static int toSeverity(Diagnostic diagnostic) { * And the examples to reproduce the Javac problems: * https://github.com/openjdk/jdk/tree/master/test/langtools/tools/javac/diags/examples */ - public static int toProblemId(Diagnostic javacDiagnostic) { - String javacDiagnosticCode = javacDiagnostic.getCode(); - // better use a Map if there is a 1->0..1 mapping + public static int toProblemId(Diagnostic diagnostic) { + String javacDiagnosticCode = diagnostic.getCode(); return switch (javacDiagnosticCode) { case "compiler.err.expected" -> IProblem.ParsingErrorInsertTokenAfter; case "compiler.warn.raw.class.use" -> IProblem.RawTypeReference; - case "compiler.err.cant.resolve.location" -> convertUnresolvedSymbol(javacDiagnostic); - case "compiler.err.cant.resolve.location.args" -> IProblem.UndefinedMethod; - case "compiler.err.cant.resolve" -> IProblem.UndefinedField; - case "compiler.err.cant.apply.symbols" -> IProblem.UndefinedConstructor; + case "compiler.err.cant.resolve.location" -> convertUnresolvedSymbol(diagnostic); + case "compiler.err.cant.resolve.location.args" -> convertUndefinedMethod(diagnostic); + case "compiler.err.cant.resolve.location.args.params" -> IProblem.UndefinedMethod; + case "compiler.err.cant.resolve" -> convertUnresolvedVariable(diagnostic); + case "compiler.err.cant.resolve.args" -> convertUndefinedMethod(diagnostic); + case "compiler.err.cant.resolve.args.params" -> IProblem.UndefinedMethod; + case "compiler.err.cant.apply.symbols" -> convertInApplicableSymbols(diagnostic); + case "compiler.err.cant.apply.symbol" -> convertInApplicableSymbols(diagnostic); case "compiler.err.premature.eof" -> IProblem.ParsingErrorUnexpectedEOF; // syntax error - case "compiler.err.report.access" -> convertNotVisibleAccess(javacDiagnostic); + case "compiler.err.report.access" -> convertNotVisibleAccess(diagnostic); 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 - // for an exhaustive (but polluted) list, unless a better source can be found (spec?) + case "compiler.err.ref.ambiguous" -> convertAmbiguous(diagnostic); + case "compiler.err.illegal.initializer.for.type" -> IProblem.TypeMismatch; + case "compiler.err.prob.found.req" -> convertTypeMismatch(diagnostic); + case "compiler.err.invalid.meth.decl.ret.type.req" -> IProblem.MissingReturnType; + case "compiler.err.abstract.meth.cant.have.body" -> IProblem.BodyForAbstractMethod; + case "compiler.err.unreported.exception.default.constructor" -> IProblem.UnhandledExceptionInDefaultConstructor; + case "compiler.err.unreachable.stmt" -> IProblem.CodeCannotBeReached; + case "compiler.err.except.never.thrown.in.try" -> IProblem.UnreachableCatch; + case "compiler.err.except.already.caught" -> IProblem.InvalidCatchBlockSequence; + case "compiler.err.unclosed.str.lit" -> IProblem.UnterminatedString; + case "compiler.err.class.public.should.be.in.file" -> IProblem.PublicClassMustMatchFileName; + case "compiler.err.already.defined.this.unit" -> IProblem.ConflictingImport; + case "compiler.err.override.meth.doesnt.throw" -> IProblem.IncompatibleExceptionInThrowsClause; + case "compiler.err.override.incompatible.ret" -> IProblem.IncompatibleReturnType; + case "compiler.err.annotation.missing.default.value" -> IProblem.MissingValueForAnnotationMember; + case "compiler.err.annotation.value.must.be.name.value" -> IProblem.UndefinedAnnotationMember; + case "compiler.err.multicatch.types.must.be.disjoint" -> IProblem.InvalidUnionTypeReferenceSequence; + case "compiler.err.unreported.exception.implicit.close" -> IProblem.UnhandledExceptionOnAutoClose; default -> 0; }; } + // compiler.err.cant.resolve + private static int convertUnresolvedVariable(Diagnostic diagnostic) { + if (diagnostic instanceof JCDiagnostic jcDiagnostic) { + if (jcDiagnostic.getDiagnosticPosition() instanceof JCTree.JCFieldAccess) { + return IProblem.UndefinedField; + } + } + + return IProblem.UnresolvedVariable; + } + + private static int convertUndefinedMethod(Diagnostic diagnostic) { + Diagnostic diagnosticArg = getDiagnosticArgumentByType(diagnostic, Diagnostic.class); + if (diagnosticArg != null && "compiler.misc.location.1".equals(diagnosticArg.getCode())) { + return IProblem.NoMessageSendOnArrayType; + } + + if ("compiler.err.cant.resolve.args".equals(diagnostic.getCode())) { + Kinds.KindName kind = getDiagnosticArgumentByType(diagnostic, Kinds.KindName.class); + if (kind == Kinds.KindName.CONSTRUCTOR) { + return IProblem.UndefinedConstructor; + } + } + + return IProblem.UndefinedMethod; + } + + private static T getDiagnosticArgumentByType(Diagnostic diagnostic, Class type) { + if (!(diagnostic instanceof JCDiagnostic jcDiagnostic)) { + return null; + } + + Object[] args = jcDiagnostic.getArgs(); + if (args != null) { + for (Object arg : args) { + if (type.isInstance(arg)) { + return type.cast(arg); + } + } + } + + return null; + } + + private static Object[] getDiagnosticArguments(Diagnostic diagnostic) { + if (!(diagnostic instanceof JCDiagnostic jcDiagnostic)) { + return new Object[0]; + } + + return jcDiagnostic.getArgs(); + } + + private static int convertInApplicableSymbols(Diagnostic diagnostic) { + Kinds.KindName kind = getDiagnosticArgumentByType(diagnostic, Kinds.KindName.class); + if ("compiler.err.cant.apply.symbols".equals(diagnostic.getCode())) { + return switch (kind) { + case CONSTRUCTOR -> IProblem.UndefinedConstructor; + case METHOD -> IProblem.ParameterMismatch; + default -> 0; + }; + } else if ("compiler.err.cant.apply.symbol".equals(diagnostic.getCode())) { + return switch (kind) { + case CONSTRUCTOR -> IProblem.UndefinedConstructorInDefaultConstructor; + case METHOD -> IProblem.ParameterMismatch; + default -> 0; + }; + } + + return 0; + } + + // compiler.err.prob.found.req -> TypeMismatch, ReturnTypeMismatch, IllegalCast, VoidMethodReturnsValue... + private static int convertTypeMismatch(Diagnostic diagnostic) { + Diagnostic diagnosticArg = getDiagnosticArgumentByType(diagnostic, Diagnostic.class); + if (diagnosticArg != null) { + if ("compiler.misc.inconvertible.types".equals(diagnosticArg.getCode())) { + Object[] args = getDiagnosticArguments(diagnosticArg); + if (args != null && args.length > 0 + && args[0] instanceof Type.JCVoidType) { + return IProblem.MethodReturnsVoid; + } + + return IProblem.TypeMismatch; + } else if ("compiler.misc.unexpected.ret.val".equals(diagnosticArg.getCode())) { + return IProblem.VoidMethodReturnsValue; + } else if ("compiler.misc.missing.ret.val".equals(diagnosticArg.getCode())) { + return IProblem.ShouldReturnValue; + } + } + + return 0; + } + private static int convertUnresolvedSymbol(Diagnostic javacDiagnostic) { if (javacDiagnostic instanceof JCDiagnostic jcDiagnostic) { Object[] args = jcDiagnostic.getArgs(); @@ -217,12 +331,16 @@ private static int convertUnresolvedSymbol(Diagnostic return IProblem.UndefinedName; } - private static int convertNotVisibleAccess(Diagnostic javacDiagnostic) { - if (javacDiagnostic instanceof JCDiagnostic jcDiagnostic) { + private static int convertNotVisibleAccess(Diagnostic diagnostic) { + if (diagnostic instanceof JCDiagnostic jcDiagnostic) { Object[] args = jcDiagnostic.getArgs(); if (args != null && args.length > 0) { if (args[0] instanceof Symbol.MethodSymbol methodSymbol) { if (methodSymbol.isConstructor()) { + if (jcDiagnostic.getDiagnosticPosition() instanceof JCTree.JCIdent id + && id.getName() != null && id.getName().toString().equals("super")) { + return IProblem.NotVisibleConstructorInDefaultConstructor; + } return IProblem.NotVisibleConstructor; } @@ -237,4 +355,13 @@ private static int convertNotVisibleAccess(Diagnostic return 0; } + + private static int convertAmbiguous(Diagnostic diagnostic) { + Kinds.KindName kind = getDiagnosticArgumentByType(diagnostic, Kinds.KindName.class); + return switch (kind) { + case CLASS -> IProblem.AmbiguousType; + case METHOD -> IProblem.AmbiguousMethod; + default -> 0; + }; + } }