From 4ba196c3845793bdcd0827f0dc4e3b28619eb862 Mon Sep 17 00:00:00 2001 From: Mickael Istria Date: Fri, 31 May 2024 00:09:54 +0200 Subject: [PATCH] Some convert/binding fixes --- .../jdt/core/dom/JavacBindingResolver.java | 4 ++ .../eclipse/jdt/core/dom/JavacConverter.java | 42 +++++++++++++------ .../jdt/core/dom/JavadocConverter.java | 2 +- .../javac/dom/JavacPackageBinding.java | 4 ++ .../internal/javac/dom/JavacTypeBinding.java | 11 ++++- .../javac/dom/JavacVariableBinding.java | 5 +++ .../jdt/internal/core/CompilationUnit.java | 5 +-- 7 files changed, 56 insertions(+), 17 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 1b0f8a38cae..3188c6466cd 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 @@ -45,6 +45,7 @@ import com.sun.tools.javac.code.Type.PackageType; import com.sun.tools.javac.code.Types; import com.sun.tools.javac.tree.JCTree; +import com.sun.tools.javac.tree.JCTree.JCAnnotatedType; import com.sun.tools.javac.tree.JCTree.JCArrayTypeTree; import com.sun.tools.javac.tree.JCTree.JCClassDecl; import com.sun.tools.javac.tree.JCTree.JCExpression; @@ -254,6 +255,9 @@ ITypeBinding resolveType(Type type) { if (jcTree instanceof JCTypeApply jcta && jcta.type != null) { return this.bindings.getTypeBinding(jcta.type); } + if (jcTree instanceof JCAnnotatedType annotated && annotated.type != null) { + return this.bindings.getTypeBinding(annotated.type); + } // return this.flowResult.stream().map(env -> env.enclClass) // .filter(Objects::nonNull) 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 21d56da1090..9eb8b689e80 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 @@ -631,7 +631,7 @@ private ASTNode convertBodyDeclaration(JCTree tree, ASTNode parent) { res.setBody(convertBlock(block)); return res; } - if (tree instanceof JCErroneous erroneous) { + if (tree instanceof JCErroneous erroneous || tree instanceof JCSkip) { return null; } ILog.get().error("Unsupported " + tree + " of type" + tree.getClass()); @@ -828,7 +828,7 @@ private AbstractTypeDeclaration findSurroundingTypeDeclaration(ASTNode parent) { } private VariableDeclaration convertVariableDeclarationForLambda(JCVariableDecl javac) { - if( javac.type == null ) { + if( javac.getType() == null ) { return createVariableDeclarationFragment(javac); } else { return convertVariableDeclaration(javac); @@ -869,8 +869,15 @@ private VariableDeclaration convertVariableDeclaration(JCVariableDecl javac) { res.setType(convertToType(unwrapDimensions(jcatt, dims))); } } else if ( (javac.mods.flags & VARARGS) != 0) { + JCTree type = javac.getType(); + if (type instanceof JCAnnotatedType annotatedType) { + annotatedType.getAnnotations().stream() + .map(this::convert) + .forEach(res.varargsAnnotations()::add); + type = annotatedType.getUnderlyingType(); + } // We have varity - if( javac.getType() instanceof JCArrayTypeTree arr) { + if(type instanceof JCArrayTypeTree arr) { res.setType(convertToType(arr.elemtype)); } if( this.ast.apiLevel > AST.JLS2_INTERNAL) { @@ -1372,6 +1379,9 @@ private Expression convertExpressionImpl(JCExpression javac) { .map(JCVariableDecl.class::cast) .map(this::convertVariableDeclarationForLambda) .forEach(res.parameters()::add); + int arrowIndex = this.rawText.indexOf("->", jcLambda.getStartPosition()); + int parenthesisIndex = this.rawText.indexOf(")", jcLambda.getStartPosition()); + res.setParentheses(parenthesisIndex >= 0 && parenthesisIndex < arrowIndex); ASTNode body = jcLambda.getBody() instanceof JCExpression expr ? convertExpression(expr) : jcLambda.getBody() instanceof JCStatement stmt ? convertStatement(stmt, res) : null; @@ -2173,13 +2183,18 @@ 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)convertName(qualified.getIdentifier())); - commonSettings(res, qualified); + Type qualifierType = convertToType(qualified.getExpression()); + 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())); + SimpleType res = this.ast.newSimpleType(name); + commonSettings(res, javac); return res; } else { - SimpleType res = this.ast.newSimpleType(toName(qualified)); - commonSettings(res, javac); + QualifiedType res = this.ast.newQualifiedType(qualifierType, (SimpleName)convertName(qualified.getIdentifier())); + commonSettings(res, qualified); return res; } } @@ -2252,7 +2267,7 @@ Type convertToType(JCTree javac) { if( createNameQualifiedType && this.ast.apiLevel >= AST.JLS8_INTERNAL) { JCExpression jcpe = jcAnnotatedType.underlyingType; if( jcpe instanceof JCFieldAccess jcfa2) { - if( jcfa2.selected instanceof JCAnnotatedType) { + if( jcfa2.selected instanceof JCAnnotatedType || jcfa2.selected instanceof JCTypeApply) { QualifiedType nameQualifiedType = new QualifiedType(this.ast); commonSettings(nameQualifiedType, javac); nameQualifiedType.setQualifier(convertToType(jcfa2.selected)); @@ -2265,13 +2280,16 @@ Type convertToType(JCTree javac) { nameQualifiedType.setName(this.ast.newSimpleName(jcfa2.name.toString())); res = nameQualifiedType; } + } else if (jcpe instanceof JCIdent simpleType) { + res = this.ast.newSimpleType(convertName(simpleType.getName())); + commonSettings(res, javac); } } else { - convertToType(jcAnnotatedType.getUnderlyingType()); + res = convertToType(jcAnnotatedType.getUnderlyingType()); } - if (res instanceof AnnotatableType annotatableType) { + if (res instanceof AnnotatableType annotatableType && this.ast.apiLevel() >= AST.JLS8) { for (JCAnnotation annotation : jcAnnotatedType.getAnnotations()) { - annotatableType.annotations.add(convert(annotation)); + annotatableType.annotations().add(convert(annotation)); } } else if (res instanceof ArrayType arrayType) { if (!arrayType.dimensions().isEmpty()) { 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 65bb561fab7..48b2a5024c0 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 @@ -330,7 +330,7 @@ private MethodRefParameter toMethodRefParam(JCTree type, int fromOffset) { res.accept(new ASTVisitor(true) { @Override public void preVisit(ASTNode node) { - node.setSourceRange(node.getStartPosition() + fromOffset, node.toString().length()); + node.setSourceRange(Math.max(0, node.getStartPosition()) + fromOffset, node.toString().length()); } }); return res; diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacPackageBinding.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacPackageBinding.java index 8b2583f2f27..6c0499f8903 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacPackageBinding.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacPackageBinding.java @@ -134,4 +134,8 @@ public String[] getNameComponents() { return isUnnamed()? new String[0] : this.packageSymbol.getQualifiedName().toString().split("."); //$NON-NLS-1$ } + @Override + public String toString() { + return "package " + getName(); + } } diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacTypeBinding.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacTypeBinding.java index 255a8ea8b92..3a107d16e23 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacTypeBinding.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacTypeBinding.java @@ -440,7 +440,16 @@ public String getQualifiedName() { return at.elemtype.tsym.getQualifiedName().toString() + "[]"; } - return this.typeSymbol.getQualifiedName().toString(); + StringBuilder res = new StringBuilder(this.type.toString()); + // remove annotations here + int annotationIndex = -1; + while ((annotationIndex = res.lastIndexOf("@")) >= 0) { + int nextSpace = res.indexOf(" ", annotationIndex); + if (nextSpace >= 0) { + res.delete(annotationIndex, nextSpace + 1); + } + } + return res.toString(); } @Override diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacVariableBinding.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacVariableBinding.java index bbca3591816..d0929c8e085 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacVariableBinding.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacVariableBinding.java @@ -281,4 +281,9 @@ private static int toModelFlags(int domModifiers, boolean isDeprecated) { if (isDeprecated) res |= org.eclipse.jdt.core.Flags.AccDeprecated; return res; } + + @Override + public String toString() { + return getType().getQualifiedName() + " " + getName(); + } } diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/CompilationUnit.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/CompilationUnit.java index bf71818040a..246a6dbd34a 100644 --- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/CompilationUnit.java +++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/CompilationUnit.java @@ -542,15 +542,14 @@ public org.eclipse.jdt.core.dom.CompilationUnit getOrBuildAST(WorkingCopyOwner w return this.ast; } Map options = getOptions(true); - int jlsLevel = Integer.parseInt(options.getOrDefault(JavaCore.COMPILER_SOURCE, Integer.toString(AST.getJLSLatest()))); - ASTParser parser = ASTParser.newParser(jlsLevel); + ASTParser parser = ASTParser.newParser(new AST(options).apiLevel()); // go through AST constructor to convert options to apiLevel parser.setWorkingCopyOwner(workingCopyOwner); parser.setSource(this); // greedily enable everything assuming the AST will be used extensively for edition parser.setResolveBindings(true); parser.setStatementsRecovery(true); parser.setBindingsRecovery(true); - parser.setCompilerOptions(getOptions(true)); + parser.setCompilerOptions(options); if (parser.createAST(null) instanceof org.eclipse.jdt.core.dom.CompilationUnit newAST) { if (storeAST) { return this.ast = newAST;