From 2bb0a2c5cea3e9e924bc12d568069924c90b3064 Mon Sep 17 00:00:00 2001 From: Rob Stryker Date: Thu, 9 May 2024 17:05:41 -0400 Subject: [PATCH] Partial fix for test0472 re javadoc blowing away stack Signed-off-by: Rob Stryker --- .../eclipse/jdt/core/dom/JavacConverter.java | 67 +++++++++++-------- .../jdt/core/dom/JavadocConverter.java | 51 ++++++++++++-- 2 files changed, 85 insertions(+), 33 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 a38485956f9..de7aedd6917 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 @@ -333,7 +333,7 @@ private ImportDeclaration convert(JCImport javac) { return res; } - private void commonSettings(ASTNode res, JCTree javac) { + void commonSettings(ASTNode res, JCTree javac) { if( javac != null ) { if (javac.getStartPosition() >= 0) { int length = javac.getEndPosition(this.javacCompilationUnit.endPositions) - javac.getStartPosition(); @@ -344,23 +344,30 @@ private void commonSettings(ASTNode res, JCTree javac) { } } - - Name toName(JCTree expression) { + public interface CommonSettingsOperator { + public void op(ASTNode res, JCTree javac); + } + private Name toName(JCTree expression) { + return toName(expression, (a,b) -> commonSettings(a,b)); + } + Name toName(JCTree expression, CommonSettingsOperator commonSettings ) { if (expression instanceof JCIdent ident) { - Name res = convert(ident.getName()); - commonSettings(res, ident); + Name res = convertName(ident.getName()); + commonSettings.op(res, ident); return res; } if (expression instanceof JCFieldAccess fieldAccess) { - QualifiedName res = this.ast.newQualifiedName(toName(fieldAccess.getExpression()), (SimpleName)convert(fieldAccess.getIdentifier())); - commonSettings(res, fieldAccess); + Name qualifier = toName(fieldAccess.getExpression()); + SimpleName n = (SimpleName)convertName(fieldAccess.getIdentifier()); + QualifiedName res = this.ast.newQualifiedName(qualifier, n); + commonSettings.op(res, fieldAccess); return res; } if (expression instanceof JCAnnotatedType jcat) { - return toName(jcat.underlyingType); + return toName(jcat.underlyingType, commonSettings); } if (expression instanceof JCTypeApply jcta) { - return toName(jcta.clazz); + return toName(jcta.clazz, commonSettings); } throw new UnsupportedOperationException("toName for " + expression + " (" + expression.getClass().getName() + ")"); } @@ -392,7 +399,7 @@ private AbstractTypeDeclaration convertClassDecl(JCClassDecl javacClassDecl, AST private AbstractTypeDeclaration convertClassDecl(JCClassDecl javacClassDecl, ASTNode parent, AbstractTypeDeclaration res) { commonSettings(res, javacClassDecl); - SimpleName simpName = (SimpleName)convert(javacClassDecl.getSimpleName()); + SimpleName simpName = (SimpleName)convertName(javacClassDecl.getSimpleName()); if( simpName != null ) res.setName(simpName); if( this.ast.apiLevel != AST.JLS2_INTERNAL) { @@ -618,7 +625,7 @@ private ASTNode convertMethodInAnnotationTypeDecl(JCMethodDecl javac, ASTNode pa if( javac.defaultValue != null) { res.setDefault(convertExpression(javac.defaultValue)); } - if (convert(javac.getName()) instanceof SimpleName simpleName) { + if (convertName(javac.getName()) instanceof SimpleName simpleName) { res.setName(simpleName); } return res; @@ -805,7 +812,7 @@ private VariableDeclaration convertVariableDeclaration(JCVariableDecl javac) { // if (singleDecl) { SingleVariableDeclaration res = this.ast.newSingleVariableDeclaration(); commonSettings(res, javac); - if (convert(javac.getName()) instanceof SimpleName simpleName) { + if (convertName(javac.getName()) instanceof SimpleName simpleName) { res.setName(simpleName); } if( this.ast.apiLevel != AST.JLS2_INTERNAL) { @@ -874,7 +881,7 @@ private VariableDeclarationFragment createVariableDeclarationFragment(JCVariable } fragment.setSourceRange(fragmentStart, Math.max(0, fragmentLength)); - if (convert(javac.getName()) instanceof SimpleName simpleName) { + if (convertName(javac.getName()) instanceof SimpleName simpleName) { fragment.setName(simpleName); } if( javac.getType() instanceof JCArrayTypeTree jcatt && javac.vartype.pos > javac.pos ) { @@ -974,7 +981,9 @@ private void setJavadocForNode(JCTree javac, ASTNode node) { moduleDeclaration.setJavadoc(javadoc); moduleDeclaration.setSourceRange(javadoc.getStartPosition(), moduleDeclaration.getStartPosition() + moduleDeclaration.getLength() - javadoc.getStartPosition()); } else if (node instanceof PackageDeclaration packageDeclaration) { - packageDeclaration.setJavadoc(javadoc); + if( this.ast.apiLevel != AST.JLS2_INTERNAL) { + packageDeclaration.setJavadoc(javadoc); + } packageDeclaration.setSourceRange(javadoc.getStartPosition(), packageDeclaration.getStartPosition() + packageDeclaration.getLength() - javadoc.getStartPosition()); } } @@ -1009,19 +1018,19 @@ private Expression convertExpression(JCExpression javac) { SuperFieldAccess res = this.ast.newSuperFieldAccess(); commonSettings(res, javac); res.setQualifier(toName(parentFieldAccess.getExpression())); - res.setName((SimpleName)convert(fieldAccess.getIdentifier())); + res.setName((SimpleName)convertName(fieldAccess.getIdentifier())); return res; } if (fieldAccess.getExpression() instanceof JCIdent parentFieldAccess && Objects.equals(Names.instance(this.context)._super, parentFieldAccess.getName())) { SuperFieldAccess res = this.ast.newSuperFieldAccess(); commonSettings(res, javac); - res.setName((SimpleName)convert(fieldAccess.getIdentifier())); + res.setName((SimpleName)convertName(fieldAccess.getIdentifier())); return res; } FieldAccess res = this.ast.newFieldAccess(); commonSettings(res, javac); res.setExpression(convertExpression(fieldAccess.getExpression())); - if (convert(fieldAccess.getIdentifier()) instanceof SimpleName name) { + if (convertName(fieldAccess.getIdentifier()) instanceof SimpleName name) { res.setName(name); } return res; @@ -1043,7 +1052,7 @@ private Expression convertExpression(JCExpression javac) { if( superCall1 ) { res2.setQualifier(toName(fa.getExpression())); } - res2.setName((SimpleName)convert(access.getIdentifier())); + res2.setName((SimpleName)convertName(access.getIdentifier())); return res2; } } @@ -1054,7 +1063,7 @@ private Expression convertExpression(JCExpression javac) { if (Objects.equals(ident.getName(), Names.instance(this.context)._super)) { return convertSuperMethodInvocation(methodInvocation); } - SimpleName name = (SimpleName)convert(ident.getName()); + SimpleName name = (SimpleName)convertName(ident.getName()); commonSettings(name, ident); res.setName(name); } else if (nameExpr instanceof JCFieldAccess access) { @@ -1071,10 +1080,10 @@ private Expression convertExpression(JCExpression javac) { if( superCall1 ) { res2.setQualifier(toName(fa.getExpression())); } - res2.setName((SimpleName)convert(access.getIdentifier())); + res2.setName((SimpleName)convertName(access.getIdentifier())); return res2; } - if (convert(access.getIdentifier()) instanceof SimpleName simpleName) { + if (convertName(access.getIdentifier()) instanceof SimpleName simpleName) { res.setName(simpleName); } res.setExpression(convertExpression(access.getExpression())); @@ -1291,7 +1300,7 @@ private Expression convertExpression(JCExpression javac) { ExpressionMethodReference res = this.ast.newExpressionMethodReference(); commonSettings(res, javac); res.setExpression(convertExpression(jcMemberReference.getQualifierExpression())); - res.setName((SimpleName)convert(jcMemberReference.getName())); + res.setName((SimpleName)convertName(jcMemberReference.getName())); if (jcMemberReference.getTypeArguments() != null) { jcMemberReference.getTypeArguments().map(this::convertToType).forEach(res.typeArguments()::add); } @@ -1671,7 +1680,7 @@ private Statement convertStatement(JCStatement javac, ASTNode parent) { BreakStatement res = this.ast.newBreakStatement(); commonSettings(res, javac); if (jcBreak.getLabel() != null) { - res.setLabel((SimpleName)convert(jcBreak.getLabel())); + res.setLabel((SimpleName)convertName(jcBreak.getLabel())); } return res; } @@ -1753,14 +1762,14 @@ private Statement convertStatement(JCStatement javac, ASTNode parent) { ContinueStatement res = this.ast.newContinueStatement(); commonSettings(res, javac); if (jcContinue.getLabel() != null) { - res.setLabel((SimpleName)convert(jcContinue.getLabel())); + res.setLabel((SimpleName)convertName(jcContinue.getLabel())); } return res; } if (javac instanceof JCLabeledStatement jcLabel) { LabeledStatement res = this.ast.newLabeledStatement(); commonSettings(res, javac); - res.setLabel((SimpleName)convert(jcLabel.getLabel())); + res.setLabel((SimpleName)convertName(jcLabel.getLabel())); Statement stmt = convertStatement(jcLabel.getStatement(), res); if( stmt != null ) res.setBody(stmt); @@ -1890,7 +1899,7 @@ private IfStatement convertIfStatement(JCIf javac) { private Type convertToType(JCTree javac) { if (javac instanceof JCIdent ident) { - SimpleType res = this.ast.newSimpleType(convert(ident.name)); + SimpleType res = this.ast.newSimpleType(convertName(ident.name)); commonSettings(res, ident); return res; } @@ -1907,7 +1916,7 @@ private Type convertToType(JCTree javac) { // case of not translatable name, eg because of generics // TODO find a better check instead of relying on exception if( this.ast.apiLevel > AST.JLS2_INTERNAL) { - QualifiedType res = this.ast.newQualifiedType(convertToType(qualified.getExpression()), (SimpleName)convert(qualified.getIdentifier())); + QualifiedType res = this.ast.newQualifiedType(convertToType(qualified.getExpression()), (SimpleName)convertName(qualified.getIdentifier())); commonSettings(res, qualified); return res; } else { @@ -2259,7 +2268,7 @@ private Modifier convert(javax.lang.model.element.Modifier javac, int startPos, } - private Name convert(com.sun.tools.javac.util.Name javac) { + private Name convertName(com.sun.tools.javac.util.Name javac) { if (javac == null || Objects.equals(javac, Names.instance(this.context).error) || Objects.equals(javac, Names.instance(this.context).empty)) { return null; } @@ -2268,7 +2277,7 @@ private Name convert(com.sun.tools.javac.util.Name javac) { if (lastDot < 0) { return this.ast.newSimpleName(nameString); } else { - return this.ast.newQualifiedName(convert(javac.subName(0, lastDot)), (SimpleName)convert(javac.subName(lastDot + 1, javac.length() - 1))); + return this.ast.newQualifiedName(convertName(javac.subName(0, lastDot)), (SimpleName)convertName(javac.subName(lastDot + 1, javac.length() - 1))); } // position is set later, in FixPositions, as computing them depends on the sibling } 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 7f33cd090e6..f926ecd4742 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 @@ -16,6 +16,7 @@ import org.eclipse.core.runtime.ILog; +import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.DCTree; import com.sun.tools.javac.tree.DCTree.DCAuthor; import com.sun.tools.javac.tree.DCTree.DCBlockTag; @@ -62,10 +63,20 @@ private void commonSettings(ASTNode res, DCTree javac) { } //this.domToJavac.put(res, javac); } + private void commonSettings(ASTNode res, JCTree javac) { + int start = this.docComment.getSourcePosition(javac.getStartPosition()); + int length = javac.toString().length(); + res.setSourceRange(start, Math.max(0,length)); + } Javadoc convertJavadoc() { Javadoc res = this.ast.newJavadoc(); res.setSourceRange(this.initialOffset, this.endOffset - this.initialOffset); + String rawContent2 = this.javacConverter.rawText.substring(this.initialOffset, this.endOffset); + if( rawContent2 != null && rawContent2.contains("@see junit.framework.TestListener#addError()")) { + int z = 5; + int a = 21; + } if( this.javacConverter.ast.apiLevel == AST.JLS2_INTERNAL) { String rawContent = this.javacConverter.rawText.substring(this.initialOffset, this.endOffset); res.setComment(rawContent); @@ -85,6 +96,9 @@ Javadoc convertJavadoc() { } else { if (host == null) { host = this.ast.newTagElement(); + if( elements[i] instanceof ASTNode astn) { + host.setSourceRange(astn.getStartPosition(), astn.getLength()); + } } host.fragments().add(elements[i]); } @@ -169,6 +183,32 @@ private Optional convertInlineTag(DCTree javac) { } return Optional.of(res); } + + private Name toName(JCTree expression) { + Name n = this.javacConverter.toName(expression, (a,b) -> commonSettings(a,b)); + // We need to clean all the sub-names + if( n instanceof QualifiedName qn ) { + SimpleName sn = qn.getName(); + if( sn.getStartPosition() == 0 || sn.getStartPosition() == -1) { + int qnEnd = qn.getStartPosition() + qn.getLength(); + int start = qnEnd - sn.toString().length(); + sn.setSourceRange(start, qnEnd-start); + } + cleanNameQualifierLocations(qn); + } + return n; + } + + private void cleanNameQualifierLocations(QualifiedName qn) { + Name qualifier = qn.getQualifier(); + if( qualifier != null ) { + qualifier.setSourceRange(qn.getStartPosition(), qualifier.toString().length()); + if( qualifier instanceof QualifiedName qn2) { + cleanNameQualifierLocations(qn2); + } + } + } + private IDocElement convertElement(DCTree javac) { if (javac instanceof DCText text) { JavaDocTextElement res = this.ast.newJavaDocTextElement(); @@ -186,11 +226,12 @@ private IDocElement convertElement(DCTree javac) { commonSettings(res, javac); if (reference.memberName != null) { SimpleName name = this.ast.newSimpleName(reference.memberName.toString()); - // TODO set range + name.setSourceRange(this.docComment.getSourcePosition(javac.getStartPosition()), Math.max(0, reference.memberName.toString().length())); res.setName(name); } if (reference.qualifierExpression != null) { - res.setQualifier(this.javacConverter.toName(reference.qualifierExpression)); + Name n = toName(reference.qualifierExpression); + res.setQualifier(n); } // TODO here: fix // reference.paramTypes.stream().map(this.javacConverter::toName).forEach(res.parameters()::add); @@ -200,10 +241,12 @@ private IDocElement convertElement(DCTree javac) { commonSettings(res, javac); if (reference.memberName != null) { SimpleName name = this.ast.newSimpleName(reference.memberName.toString()); - // TODO set range + name.setSourceRange(this.docComment.getSourcePosition(javac.getStartPosition()), Math.max(0, reference.memberName.toString().length())); res.setName(name); } - res.setQualifier(this.javacConverter.toName(reference.qualifierExpression)); + Name n = toName(reference.qualifierExpression); + n.setSourceRange(this.docComment.getSourcePosition(reference.pos), Math.max(0, reference.qualifierExpression.toString().length())); + res.setQualifier(n); return res; } } else if (javac instanceof DCStartElement || javac instanceof DCEndElement || javac instanceof DCEntity) {