Skip to content

Commit

Permalink
Better map UndefinedConstructor problem
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelistria committed Sep 4, 2024
1 parent b78227f commit 31d289e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.tree.JCTree.JCWildcard;
import com.sun.tools.javac.tree.TreeInfo;
import com.sun.tools.javac.util.Context;

/**
Expand Down Expand Up @@ -409,7 +410,7 @@ private void resolve() {
return;
}
synchronized (this.javac) { // prevents from multiple `analyze` for the same task
boolean alreadyAnalyzed = this.converter.domToJavac.values().stream().map(this::symbol).anyMatch(Optional::isPresent);
boolean alreadyAnalyzed = this.converter.domToJavac.values().stream().map(JavacBindingResolver::symbol).anyMatch(Optional::isPresent);
if (!alreadyAnalyzed) {
// symbols not already present: analyze
try {
Expand Down Expand Up @@ -485,7 +486,7 @@ public ASTNode findNode(Symbol symbol) {
return null;
}

private Optional<Symbol> symbol(JCTree value) {
public static Optional<Symbol> symbol(JCTree value) {
if (value instanceof JCClassDecl jcClassDecl) {
return Optional.ofNullable(jcClassDecl.sym);
}
Expand All @@ -504,6 +505,18 @@ private Optional<Symbol> symbol(JCTree value) {
if (value instanceof JCIdent ident) {
return Optional.ofNullable(ident.sym);
}
if (value instanceof JCFieldAccess fieldAccess) {
return Optional.ofNullable(fieldAccess.sym);
}
if (value instanceof JCMethodInvocation method) {
return symbol(method.getMethodSelect());
}
if (value instanceof JCNewClass jcNewClass) {
return Optional.ofNullable(jcNewClass.constructor);
}
if (value instanceof JCMemberReference jcMemberReference) {
return Optional.ofNullable(jcMemberReference.sym);
}
// TODO fields, methods, variables...
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import org.eclipse.core.runtime.ILog;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.JavacBindingResolver;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
Expand Down Expand Up @@ -73,6 +74,7 @@
import com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree;
import com.sun.tools.javac.tree.JCTree.JCReturn;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.tree.TreeInfo;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.DiagnosticSource;
import com.sun.tools.javac.util.JCDiagnostic;
Expand Down Expand Up @@ -299,6 +301,10 @@ private org.eclipse.jface.text.Position getDiagnosticPosition(Diagnostic<? exten
} else if (problemId == IProblem.SwitchExpressionsYieldMissingDefaultCase
&& diagnosticPath.getLeaf() instanceof JCTree.JCSwitchExpression switchExpr) {
return getPositionByNodeRangeOnly(jcDiagnostic, switchExpr.selector instanceof JCTree.JCParens parens? parens.expr : switchExpr.selector);
} else if (problemId == IProblem.UndefinedConstructor
&& diagnosticPath.getParentPath() != null
&& diagnosticPath.getParentPath().getLeaf() instanceof JCNewClass newClass) {
return getPositionByNodeRangeOnly(jcDiagnostic, newClass);
}
}

Expand Down Expand Up @@ -674,7 +680,7 @@ public int toProblemId(Diagnostic<? extends JavaFileObject> diagnostic) {
switch (getDiagnosticArgumentByType(diagnostic, Kinds.KindName.class)) {
case CONSTRUCTOR -> {
TreePath treePath = getTreePath((JCDiagnostic)diagnostic);
while (!(treePath.getLeaf() instanceof JCMethodDecl) && treePath != null) {
while (treePath != null && !(treePath.getLeaf() instanceof JCMethodDecl) && treePath != null) {
treePath = treePath.getParentPath();
}
if (treePath == null || !(treePath.getLeaf() instanceof JCMethodDecl methodDecl)) {
Expand Down Expand Up @@ -1225,6 +1231,11 @@ private int convertTypeMismatch(Diagnostic<?> diagnostic) {
&& args[1] instanceof Type.JCVoidType) {
return IProblem.MethodReturnsVoid;
}
TreePath path = getTreePath(diagnostic);
if (path != null && path.getParentPath() != null
&& path.getParentPath().getLeaf() instanceof JCNewClass) {
return IProblem.UndefinedConstructor;
}
} else if ("compiler.misc.unexpected.ret.val".equals(diagnosticArg.getCode())) {
return IProblem.VoidMethodReturnsValue;
} else if ("compiler.misc.missing.ret.val".equals(diagnosticArg.getCode())) {
Expand Down

0 comments on commit 31d289e

Please sign in to comment.