Skip to content

Commit

Permalink
Some fixes in problem conversion
Browse files Browse the repository at this point in the history
* Javadoc stuff
* ranges for FieldAccess that are not packages
* Improve support for error type
  • Loading branch information
mickaelistria committed Jul 1, 2024
1 parent 8ebdc0d commit 206803d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public JavacPackageBinding getPackageBinding(PackageSymbol packageSymbol) {
//
private Map<String, JavacTypeBinding> typeBinding = new HashMap<>();
public JavacTypeBinding getTypeBinding(com.sun.tools.javac.code.Type type) {
if (type instanceof ErrorType errorType && (errorType.getOriginalType() != com.sun.tools.javac.code.Type.noType)) {
if (type instanceof ErrorType errorType && errorType.tsym == null && (errorType.getOriginalType() != com.sun.tools.javac.code.Type.noType)) {
return getTypeBinding(errorType.getOriginalType());
}
JavacTypeBinding newInstance = new JavacTypeBinding(type, type.tsym, JavacBindingResolver.this) { };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@
import org.eclipse.jdt.internal.compiler.problem.ProblemSeverities;

import com.sun.tools.javac.code.Kinds;
import com.sun.tools.javac.code.Kinds.KindName;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.TypeTag;
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.parser.Tokens.TokenKind;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCBlock;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.Context;
Expand Down Expand Up @@ -87,7 +90,7 @@ public JavacProblem createJavacProblem(Diagnostic<? extends JavaFileObject> diag
new String[0],
severity,
diagnosticPosition.getOffset(),
diagnosticPosition.getOffset() + diagnosticPosition.getLength(),
diagnosticPosition.getOffset() + diagnosticPosition.getLength() - 1,
(int) diagnostic.getLineNumber(),
(int) diagnostic.getColumnNumber());
}
Expand All @@ -96,31 +99,27 @@ private static org.eclipse.jface.text.Position getDiagnosticPosition(Diagnostic<
if (diagnostic.getCode().contains(".dc")) { //javadoc
return getDefaultPosition(diagnostic);
}
switch (diagnostic) {
case JCDiagnostic jcDiagnostic -> {
if (diagnostic instanceof JCDiagnostic jcDiagnostic) {
switch (jcDiagnostic.getDiagnosticPosition()) {
case JCClassDecl jcClassDecl -> {
return getDiagnosticPosition(jcDiagnostic, jcClassDecl);
}
case JCVariableDecl jcVariableDecl -> {
return getDiagnosticPosition(jcDiagnostic, jcVariableDecl);
}
case JCMethodDecl jcMethodDecl -> {
return getDiagnosticPosition(jcDiagnostic, jcMethodDecl);
}
default -> {
org.eclipse.jface.text.Position result = getMissingReturnMethodDiagnostic(jcDiagnostic, context);
if (result != null) {
return result;
}
if (jcDiagnostic.getStartPosition() == jcDiagnostic.getEndPosition()) {
return getPositionUsingScanner(jcDiagnostic, context);
}
}
case JCClassDecl jcClassDecl: return getDiagnosticPosition(jcDiagnostic, jcClassDecl);
case JCVariableDecl jcVariableDecl: return getDiagnosticPosition(jcDiagnostic, jcVariableDecl);
case JCMethodDecl jcMethodDecl: return getDiagnosticPosition(jcDiagnostic, jcMethodDecl);
case JCFieldAccess jcFieldAccess:
if (getDiagnosticArgumentByType(jcDiagnostic, KindName.class) != KindName.PACKAGE) {
// TODO here, instead of recomputing a position, get the JDT DOM node and call the Name (which has a position)
return new org.eclipse.jface.text.Position(jcFieldAccess.getPreferredPosition() + 1, jcFieldAccess.getIdentifier().length());
}
// else: fail-through
default:
org.eclipse.jface.text.Position result = getMissingReturnMethodDiagnostic(jcDiagnostic, context);
if (result != null) {
return result;
}
if (jcDiagnostic.getStartPosition() == jcDiagnostic.getEndPosition()) {
return getPositionUsingScanner(jcDiagnostic, context);
}
}
}
default -> {}
}
return getDefaultPosition(diagnostic);
}

Expand Down Expand Up @@ -342,8 +341,12 @@ public static int toProblemId(Diagnostic<? extends JavaFileObject> diagnostic) {
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.cant.apply.symbols", "compiler.err.cant.apply.symbol" ->
switch (getDiagnosticArgumentByType(diagnostic, Kinds.KindName.class)) {
case CONSTRUCTOR -> IProblem.UndefinedConstructor;
case METHOD -> IProblem.ParameterMismatch;
default -> 0;
};
case "compiler.err.premature.eof" -> IProblem.ParsingErrorUnexpectedEOF; // syntax error
case "compiler.err.report.access" -> convertNotVisibleAccess(diagnostic);
case "compiler.err.does.not.override.abstract" -> IProblem.AbstractMethodMustBeImplemented;
Expand Down Expand Up @@ -402,7 +405,7 @@ public static int toProblemId(Diagnostic<? extends JavaFileObject> diagnostic) {
case "compiler.err.dc.unterminated.signature" -> IProblem.JavadocUnexpectedText;
case "compiler.err.dc.unterminated.string" -> IProblem.JavadocUnexpectedText;
case "compiler.err.dc.ref.annotations.not.allowed" -> IProblem.JavadocUnexpectedText;
case "compiler.warn.proc.messager" -> {
case "compiler.warn.proc.messager", "compiler.err.proc.messager" -> {
// probably some javadoc comment, we didn't find a good way to get javadoc
// code/ids: there are lost in the diagnostic when going through
// jdk.javadoc.internal.doclint.Messages.report(...) and we cannot override
Expand All @@ -415,6 +418,9 @@ public static int toProblemId(Diagnostic<? extends JavaFileObject> diagnostic) {
if (message.contains("no @return")) {
yield IProblem.JavadocMissingReturnTag;
}
if (message.contains("@param name not found")) {
yield IProblem.JavadocInvalidParamName;
}
// most others are ignored
yield 0;
}
Expand All @@ -437,9 +443,12 @@ private static int convertUnresolvedVariable(Diagnostic<?> diagnostic) {
}

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;
JCDiagnostic diagnosticArg = getDiagnosticArgumentByType(diagnostic, JCDiagnostic.class);
if (diagnosticArg != null) {
Type receiverArg = getDiagnosticArgumentByType(diagnosticArg, Type.class);
if (receiverArg.hasTag(TypeTag.ARRAY)) {
return IProblem.NoMessageSendOnArrayType;
}
}

if ("compiler.err.cant.resolve.args".equals(diagnostic.getCode())) {
Expand Down Expand Up @@ -477,25 +486,6 @@ private static Object[] getDiagnosticArguments(Diagnostic<?> diagnostic) {
return jcDiagnostic.getArgs();
}

private static int convertInApplicableSymbols(Diagnostic<? extends JavaFileObject> 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);
Expand Down

0 comments on commit 206803d

Please sign in to comment.