From bc044a4cb649186d32251a378566921fabd9a8b5 Mon Sep 17 00:00:00 2001 From: Mickael Istria Date: Thu, 20 Jun 2024 16:15:43 +0200 Subject: [PATCH] Some more Javadoc fixes --- .../jdt/core/dom/JavacBindingResolver.java | 33 +++++++++++++++++-- .../eclipse/jdt/core/dom/JavacConverter.java | 19 +++++++++-- .../jdt/core/dom/JavadocConverter.java | 33 ++++++++++++++----- 3 files changed, 70 insertions(+), 15 deletions(-) diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacBindingResolver.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacBindingResolver.java index 33919fac008..b54046eb7ca 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacBindingResolver.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacBindingResolver.java @@ -517,9 +517,16 @@ IBinding resolveName(Name name) { if( tree != null ) { return resolveNameToJavac(name, tree); } - DocTreePath path = this.converter.findDocTreePath(name); // TODO - if (path != null && JavacTrees.instance(this.context).getElement(path) instanceof Symbol symbol) { - return this.bindings.getBinding(symbol, null); + DocTreePath path = this.converter.findDocTreePath(name); + if (path != null) { + if (JavacTrees.instance(this.context).getElement(path) instanceof Symbol symbol) { + return this.bindings.getBinding(symbol, null); + } + // try parent + path = path.getParentPath(); + if (JavacTrees.instance(this.context).getElement(path) instanceof Symbol symbol) { + return this.bindings.getBinding(symbol, null); + } } if (tree == null && (name.getFlags() & ASTNode.ORIGINAL) != 0) { tree = this.converter.domToJavac.get(name.getParent()); @@ -840,4 +847,24 @@ IAnnotationBinding resolveAnnotation(Annotation annotation) { } return null; } + + @Override + IBinding resolveReference(MethodRef ref) { + resolve(); + DocTreePath path = this.converter.findDocTreePath(ref); + if (path != null && JavacTrees.instance(this.context).getElement(path) instanceof Symbol symbol) { + return this.bindings.getBinding(symbol, null); + } + return null; + } + + @Override + IBinding resolveReference(MemberRef ref) { + resolve(); + DocTreePath path = this.converter.findDocTreePath(ref); + if (path != null && JavacTrees.instance(this.context).getElement(path) instanceof Symbol symbol) { + return this.bindings.getBinding(symbol, null); + } + return null; + } } 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 b1117784d82..907e3c4fe8f 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 @@ -2435,9 +2435,16 @@ private IfStatement convertIfStatement(JCIf javac) { return res; } + /** + * ⚠️ node position in JCTree must be absolute + * @param javac + * @return + */ Type convertToType(JCTree javac) { if (javac instanceof JCIdent ident) { - SimpleType res = this.ast.newSimpleType(convertName(ident.name)); + Name name = convertName(ident.name); + name.setSourceRange(ident.getStartPosition(), ident.name.length()); + SimpleType res = this.ast.newSimpleType(name); commonSettings(res, ident); return res; } @@ -2455,17 +2462,23 @@ Type convertToType(JCTree javac) { // case of not translatable name, eg because of generics // TODO find a better check instead of relying on exception Type qualifierType = convertToType(qualified.getExpression()); + SimpleName simpleName = (SimpleName)convertName(qualified.getIdentifier()); + int simpleNameStart = this.rawText.indexOf(simpleName.getIdentifier(), qualifierType.getStartPosition() + qualifierType.getLength()); + simpleName.setSourceRange(simpleNameStart, simpleName.getIdentifier().length()); if(qualifierType instanceof SimpleType simpleType && (ast.apiLevel() < AST.JLS8 || simpleType.annotations().isEmpty())) { simpleType.delete(); Name parentName = simpleType.getName(); parentName.setParent(null, null); - QualifiedName name = this.ast.newQualifiedName(simpleType.getName(), (SimpleName)convertName(qualified.getIdentifier())); + QualifiedName name = this.ast.newQualifiedName(simpleType.getName(), simpleName); commonSettings(name, javac); + int length = name.getName().getStartPosition() + name.getName().getLength() - name.getStartPosition(); + name.setSourceRange(name.getStartPosition(), length); SimpleType res = this.ast.newSimpleType(name); commonSettings(res, javac); + res.setSourceRange(name.getStartPosition(), length); return res; } else { - QualifiedType res = this.ast.newQualifiedType(qualifierType, (SimpleName)convertName(qualified.getIdentifier())); + QualifiedType res = this.ast.newQualifiedType(qualifierType, simpleName); commonSettings(res, qualified); return res; } diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavadocConverter.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavadocConverter.java index 0570c79bdf5..bbdc5771bd0 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavadocConverter.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavadocConverter.java @@ -25,16 +25,14 @@ import org.eclipse.core.runtime.ILog; -import com.sun.source.tree.CompilationUnitTree; -import com.sun.source.tree.Tree; import com.sun.source.util.DocTreePath; import com.sun.source.util.TreePath; import com.sun.tools.javac.parser.UnicodeReader; import com.sun.tools.javac.tree.DCTree; import com.sun.tools.javac.tree.DCTree.DCAuthor; import com.sun.tools.javac.tree.DCTree.DCBlockTag; -import com.sun.tools.javac.tree.DCTree.DCDeprecated; import com.sun.tools.javac.tree.DCTree.DCComment; +import com.sun.tools.javac.tree.DCTree.DCDeprecated; import com.sun.tools.javac.tree.DCTree.DCDocComment; import com.sun.tools.javac.tree.DCTree.DCEndElement; import com.sun.tools.javac.tree.DCTree.DCEntity; @@ -58,6 +56,7 @@ import com.sun.tools.javac.tree.DCTree.DCValue; import com.sun.tools.javac.tree.DCTree.DCVersion; import com.sun.tools.javac.tree.JCTree; +import com.sun.tools.javac.tree.TreeScanner; import com.sun.tools.javac.util.JCDiagnostic; class JavadocConverter { @@ -353,6 +352,7 @@ private Stream convertElement(DCTree javac) { name.setSourceRange(currentOffset, Math.max(0, reference.memberName.toString().length())); currentOffset += name.getLength(); res.setName(name); + this.converted.put(name, DocTreePath.getPath(this.contextTreePath, this.docComment, reference)); currentOffset++; // ( final int paramListOffset = currentOffset; List params = new ArrayList<>(); @@ -379,6 +379,7 @@ private Stream convertElement(DCTree javac) { commonSettings(res, javac); SimpleName name = this.ast.newSimpleName(reference.memberName.toString()); name.setSourceRange(this.docComment.getSourcePosition(javac.getStartPosition()), Math.max(0, reference.memberName.toString().length())); + this.converted.put(res, DocTreePath.getPath(this.contextTreePath, this.docComment, reference)); res.setName(name); if (reference.qualifierExpression != null) { Name qualifierExpressionName = toName(reference.qualifierExpression, res.getStartPosition()); @@ -390,7 +391,12 @@ private Stream convertElement(DCTree javac) { } else if (!signature.contains("#")) { Name res = this.ast.newName(signature); res.setSourceRange(this.docComment.getSourcePosition(javac.getStartPosition()), signature.length()); - this.converted.put(res, DocTreePath.getPath(this.contextTreePath, this.docComment, reference)); + res.accept(new ASTVisitor() { + @Override + public void preVisit(ASTNode node) { + JavadocConverter.this.converted.put(node, DocTreePath.getPath(JavadocConverter.this.contextTreePath, JavadocConverter.this.docComment, reference)); + } + }); return Stream.of(res); } } else if (javac instanceof DCStartElement || javac instanceof DCEndElement || javac instanceof DCEntity) { @@ -437,16 +443,25 @@ private MethodRefParameter toMethodRefParam(JCTree type, Region range, int param res.setSourceRange( range != null ? range.startOffset : paramListOffset + type.getStartPosition(), range != null ? range.length : type.toString().length()); + // Make positons absolute + var fixPositions = new TreeScanner() { + @Override + public void scan(JCTree tree) { + tree.setPos(tree.pos + paramListOffset); + super.scan(tree); + } + }; + fixPositions.scan(type); Type jdtType = this.javacConverter.convertToType(type); res.setType(jdtType); - jdtType.accept(new ASTVisitor(true) { + // some lengths may be missing + jdtType.accept(new ASTVisitor() { @Override public void preVisit(ASTNode node) { - if (node.getStartPosition() <= 0 && node.getParent() != null) { - node.setSourceRange(node.getParent().getStartPosition(), node.getLength() != 0 ? node.getLength() : node.toString().length()); - } else { - node.setSourceRange(node.getStartPosition() + paramListOffset, node.getLength() != 0 ? node.getLength() : node.toString().length()); + if (node.getLength() == 0 && node.getStartPosition() >= 0) { + node.setSourceRange(node.getStartPosition(), node.toString().length()); } + super.preVisit(node); } }); if (jdtType.getStartPosition() + jdtType.getLength() < res.getStartPosition() + res.getLength()) {