From 13b1a0686fe6beb7e59fec889a02c38b025334d2 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Thu, 23 May 2024 16:51:01 -0400 Subject: [PATCH] MethodBinding fix related to JLS compliance, QualifiedName fix Signed-off-by: David Thompson --- .../eclipse/jdt/core/dom/JavacConverter.java | 30 ++++++++++++++----- .../javac/dom/JavacMethodBinding.java | 3 +- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacConverter.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacConverter.java index 1e1498f88af..3511cc4af17 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacConverter.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacConverter.java @@ -1046,6 +1046,22 @@ private Expression convertExpressionImpl(JCExpression javac) { res.setName((SimpleName)convertName(fieldAccess.getIdentifier())); return res; } + if (fieldAccess.getExpression() instanceof JCIdent parentFieldAccess && Objects.equals(Names.instance(this.context)._this, parentFieldAccess.getName())) { + FieldAccess res = this.ast.newFieldAccess(); + commonSettings(res, javac); + res.setExpression(convertExpression(parentFieldAccess)); + if (convertName(fieldAccess.getIdentifier()) instanceof SimpleName name) { + res.setName(name); + } + return res; + } + if (fieldAccess.getExpression() instanceof JCIdent qualifier) { + Name qualifierName = convertName(qualifier.getName()); + SimpleName qualifiedName = (SimpleName)convertName(fieldAccess.getIdentifier()); + QualifiedName res = this.ast.newQualifiedName(qualifierName, qualifiedName); + commonSettings(res, javac); + return res; + } FieldAccess res = this.ast.newFieldAccess(); commonSettings(res, javac); res.setExpression(convertExpression(fieldAccess.getExpression())); @@ -1386,7 +1402,7 @@ private Expression convertExpressionImpl(JCExpression javac) { switchExpr = jcp.getExpression(); } res.setExpression(convertExpression(switchExpr)); - + List cases = jcSwitch.getCases(); Iterator it = cases.iterator(); ArrayList bodyList = new ArrayList<>(); @@ -1401,7 +1417,7 @@ private Expression convertExpressionImpl(JCExpression javac) { bodyList.add(switchCase.getBody()); } } - + Iterator stmtIterator = bodyList.iterator(); while(stmtIterator.hasNext()) { JCTree next = stmtIterator.next(); @@ -1425,16 +1441,16 @@ private Expression convertExpressionImpl(JCExpression javac) { } return null; } - + private Expression convertExpressionOrNull(JCExpression javac) { return convertExpressionImpl(javac); } - + private Expression convertExpression(JCExpression javac) { Expression ret = convertExpressionImpl(javac); if( ret != null ) return ret; - + // Handle errors or default situation if (javac instanceof JCErroneous error) { if (error.getErrorTrees().size() == 1) { @@ -2346,7 +2362,7 @@ private void sortModifierNodesByPosition(List l) { return a1.getStartPosition() - a2.getStartPosition(); }); } - + private void convertModifiers(JCModifiers modifiers, ASTNode parent, List res) { Iterator mods = modifiers.getFlags().iterator(); while(mods.hasNext()) { @@ -2361,7 +2377,7 @@ private List convertModifierAnnotations(JCModifiers modifier sortModifierNodesByPosition(res); return res; } - + private void convertModifierAnnotations(JCModifiers modifiers, ASTNode parent, List res) { modifiers.getAnnotations().stream().map(this::convert).forEach(res::add); } diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacMethodBinding.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacMethodBinding.java index f5260336352..cfd8f28c73d 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacMethodBinding.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacMethodBinding.java @@ -17,6 +17,7 @@ import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.Signature; +import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.IAnnotationBinding; import org.eclipse.jdt.core.dom.IBinding; @@ -287,7 +288,7 @@ public ITypeBinding getReturnType() { @Override public ITypeBinding[] getExceptionTypes() { ASTNode node = this.resolver.findNode(this.methodSymbol); - if (node instanceof MethodDeclaration method) { + if (node.getAST().apiLevel() >= AST.JLS8 && node instanceof MethodDeclaration method) { return ((List)method.thrownExceptionTypes()).stream() .map(Type::resolveBinding) .toArray(ITypeBinding[]::new);