From 116ed6cb44112f8c908e57c892e72c14c096284d Mon Sep 17 00:00:00 2001 From: Sarika Sinha Date: Fri, 9 Jul 2021 11:42:20 +0530 Subject: Bug 561726 - [dom ast] the ast target level not percolated down to codegen Change-Id: Id61af9dbcc2334a2b9ac1322ea7e91fb46853bd4 Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/182929 Tested-by: JDT Bot Reviewed-by: Sarika Sinha --- .../dom/org/eclipse/jdt/core/dom/ASTParser.java | 54 ++++++++++++++++++++-- .../jdt/internal/core/dom/util/DOMASTUtil.java | 18 ++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) (limited to 'org.eclipse.jdt.core/dom/org/eclipse/jdt') diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java index bf3d8f87c8..932c3cca07 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java @@ -27,8 +27,8 @@ import org.eclipse.core.runtime.SubMonitor; import org.eclipse.jdt.core.IClassFile; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IJavaElement; -import org.eclipse.jdt.core.ITypeRoot; import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.ITypeRoot; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.WorkingCopyOwner; @@ -37,14 +37,18 @@ import org.eclipse.jdt.core.compiler.CharOperation; import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration; import org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall; -import org.eclipse.jdt.internal.compiler.batch.Main; import org.eclipse.jdt.internal.compiler.batch.FileSystem.Classpath; +import org.eclipse.jdt.internal.compiler.batch.Main; import org.eclipse.jdt.internal.compiler.env.IBinaryType; import org.eclipse.jdt.internal.compiler.parser.RecoveryScanner; import org.eclipse.jdt.internal.compiler.parser.RecoveryScannerData; import org.eclipse.jdt.internal.compiler.parser.Scanner; import org.eclipse.jdt.internal.compiler.util.SuffixConstants; -import org.eclipse.jdt.internal.core.*; +import org.eclipse.jdt.internal.core.BasicCompilationUnit; +import org.eclipse.jdt.internal.core.BinaryType; +import org.eclipse.jdt.internal.core.ClassFileWorkingCopy; +import org.eclipse.jdt.internal.core.DefaultWorkingCopyOwner; +import org.eclipse.jdt.internal.core.PackageFragment; import org.eclipse.jdt.internal.core.dom.util.DOMASTUtil; import org.eclipse.jdt.internal.core.util.CodeSnippetParsingUtil; import org.eclipse.jdt.internal.core.util.RecordedParsingInformation; @@ -656,6 +660,50 @@ public class ASTParser { } } + + /** + * Sets the source code to be parsed. + * + * + *

This method automatically sets the project (and compiler + * options) based on the given compilation unit of class file, in a manner + * equivalent to {@link #setProject(IJavaProject) setProject(source.getJavaProject())}.

+ *

If the source is a class file without source attachment, the creation of the + * ast will fail with an {@link IllegalStateException}.

+ * + *

If this method is used, the user need not specify compiler options explicitly. + * The @param astLevel will be used for setting the corresponding values for the compiler + * options: {@link JavaCore#COMPILER_SOURCE}, {@link JavaCore#COMPILER_CODEGEN_TARGET_PLATFORM} + * and {@link JavaCore#COMPILER_COMPLIANCE}.

+ * + *

This source is not used when the AST is built using + * {@link #createASTs(ICompilationUnit[], String[], ASTRequestor, IProgressMonitor)}.

+ * + *

This astLevel will be used as the + * {@link #createASTs(ICompilationUnit[], String[], ASTRequestor, IProgressMonitor)}.

+ * + * @param source the Java model compilation unit or class file whose corresponding source code + * is to be parsed, or null if none + * @param astLevel the API level; one of the JLS* level constants + * declared on {@link AST} + * @since 3.27 + */ + public void setSource(ITypeRoot source, int astLevel) { + this.typeRoot = source; + // clear the raw source + this.rawSource = null; + if (source != null) { + this.project = source.getJavaProject(); + Map options = this.project.getOptions(true); + options.remove(JavaCore.COMPILER_TASK_TAGS); // no need to parse task tags + this.compilerOptions = options; + String compliance = DOMASTUtil.getCompliance(astLevel); + this.compilerOptions.put(JavaCore.COMPILER_COMPLIANCE, compliance); + this.compilerOptions.put(JavaCore.COMPILER_SOURCE, compliance); + this.compilerOptions.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, compliance); + } + } + /** * Sets the subrange of the source code to be parsed. * By default, the entire source string will be parsed diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/util/DOMASTUtil.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/util/DOMASTUtil.java index d0c1783dee..99771177d8 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/util/DOMASTUtil.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/util/DOMASTUtil.java @@ -14,6 +14,7 @@ package org.eclipse.jdt.internal.core.dom.util; +import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.Modifier; @@ -160,4 +161,21 @@ public class DOMASTUtil { throw new IllegalArgumentException(Integer.toString(level)); } + private static final String[] AST_COMPLIANCE_MAP = {"-1","-1",JavaCore.VERSION_1_2, JavaCore.VERSION_1_3, JavaCore.VERSION_1_7, //$NON-NLS-1$ //$NON-NLS-2$ + JavaCore.VERSION_1_7, JavaCore.VERSION_1_7, JavaCore.VERSION_1_7, JavaCore.VERSION_1_8, JavaCore.VERSION_9, JavaCore.VERSION_10, + JavaCore.VERSION_11, JavaCore.VERSION_12, JavaCore.VERSION_13, JavaCore.VERSION_14, JavaCore.VERSION_15, JavaCore.VERSION_16}; + + /** + * Calculates the JavaCore Option value string corresponding to the input ast level. + * AST Level 4 is used for Java versions 1.4 to 1.7 and is converted to compliance level 7 + * if input ast level is out of boundary, latest compliance will be returned + * @param astLevel + * @return JavaCore Option value string corresponding to the ast level + */ + @SuppressWarnings("deprecation") + public static String getCompliance(int astLevel) { + if (astLevel < AST.JLS2 && astLevel > AST.getJLSLatest()) return JavaCore.latestSupportedJavaVersion(); + return AST_COMPLIANCE_MAP[astLevel]; + } + } -- cgit v1.2.3 From 988d081198f40365cb4e90928cae635fffc40f5f Mon Sep 17 00:00:00 2001 From: Jay Arthanareeswaran Date: Wed, 21 Jul 2021 11:02:24 +0530 Subject: Bug 574929 - ITypeBinding.getTypeBounds() returns an empty array for an intersection type Change-Id: Ifbfc51c7e14d7be4db2955eab1b1492a89ef42f4 Signed-off-by: Jay Arthanareeswaran Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/183211 Tested-by: JDT Bot --- .../jdt/core/tests/dom/ASTConverter_16Test.java | 40 ++++++++++++++++++++-- .../jdt/core/tests/dom/RunConverterTests.java | 3 +- .../dom/org/eclipse/jdt/core/dom/TypeBinding.java | 23 ++++++++----- 3 files changed, 55 insertions(+), 11 deletions(-) (limited to 'org.eclipse.jdt.core/dom/org/eclipse/jdt') diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_16Test.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_16Test.java index 576bdf8b1f..031ba91eb4 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_16Test.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_16Test.java @@ -16,6 +16,7 @@ package org.eclipse.jdt.core.tests.dom; import static org.junit.Assert.assertNotEquals; import java.util.List; +import java.util.Map; import org.eclipse.core.runtime.CoreException; import org.eclipse.jdt.core.ICompilationUnit; @@ -26,6 +27,7 @@ import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.ASTParser; +import org.eclipse.jdt.core.dom.ASTVisitor; import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.Expression; @@ -43,6 +45,7 @@ import org.eclipse.jdt.core.dom.SimpleName; import org.eclipse.jdt.core.dom.SingleVariableDeclaration; import org.eclipse.jdt.core.dom.TypeDeclaration; import org.eclipse.jdt.core.dom.TypeDeclarationStatement; +import org.eclipse.jdt.core.dom.VariableDeclarationFragment; import junit.framework.Test; @@ -772,6 +775,39 @@ public class ASTConverter_16Test extends ConverterTestSetup { node = getASTNode(compilationUnit, 0, 0, 0); assertEquals("Not an enum statement", ASTNode.ENUM_DECLARATION, ((TypeDeclarationStatement)node).getDeclaration().getNodeType()); } - - + public void testTypeBindingMethods() { + var parser = ASTParser.newParser(AST.getJLSLatest()); + parser.setResolveBindings(true); + parser.setEnvironment(null, null, null, true); + parser.setCompilerOptions( + Map.of( + JavaCore.COMPILER_RELEASE, "enabled", + JavaCore.COMPILER_SOURCE, "16", + JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, "16" + ) + ); + parser.setBindingsRecovery(true); + parser.setStatementsRecovery(true); + parser.setKind(ASTParser.K_COMPILATION_UNIT); + parser.setUnitName("X.java"); + parser.setSource("class X {void main() {var x = java.util.List.of(1, 1.0);}}".toCharArray()); + var cu = (CompilationUnit) parser.createAST(null); + cu.accept(new ASTVisitor() { + @Override + public boolean visit(VariableDeclarationFragment node) { + var varBinding = node.resolveBinding(); + assertNotNull(varBinding); + var typeBinding = varBinding.getType(); + assertNotNull(typeBinding); + assertTrue(typeBinding.isParameterizedType()); + assertEquals(1, typeBinding.getTypeArguments().length); + var parameterType = typeBinding.getTypeArguments()[0]; + assertNotNull(parameterType); + assertTrue(parameterType.isIntersectionType()); + var bounds = parameterType.getTypeBounds(); + assertTrue("size of type bounds should be > 1 but actual size is " + bounds.length, bounds.length > 1); + return super.visit(node); + } + }); + } } diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/RunConverterTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/RunConverterTests.java index 14ff83d06e..c428626c32 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/RunConverterTests.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/RunConverterTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2020 IBM Corporation and others. + * Copyright (c) 2000, 2021 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -56,6 +56,7 @@ public static Class[] getAllTestClasses() { ASTConverter9Test.class, ASTConverter14Test.class, ASTConverter_15Test.class, + ASTConverter_16Test.class, }; } public static Test suite() { diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/TypeBinding.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/TypeBinding.java index 3ac7e77814..b11bc305a5 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/TypeBinding.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/TypeBinding.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2020 IBM Corporation and others. + * Copyright (c) 2000, 2021 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -539,13 +539,11 @@ class TypeBinding implements ITypeBinding { private ITypeBinding[] getIntersectingTypes() { ITypeBinding[] intersectionBindings = TypeBinding.NO_TYPE_BINDINGS; - if (this.binding instanceof IntersectionTypeBinding18) { - ReferenceBinding[] intersectingTypes = this.binding.getIntersectingTypes(); - int l = intersectingTypes.length; - intersectionBindings = new ITypeBinding[l]; - for (int i = 0; i < l; ++i) { - intersectionBindings[i] = this.resolver.getTypeBinding(intersectingTypes[i]); - } + ReferenceBinding[] intersectingTypes = this.binding.getIntersectingTypes(); + int l = intersectingTypes.length; + intersectionBindings = new ITypeBinding[l]; + for (int i = 0; i < l; ++i) { + intersectionBindings[i] = this.resolver.getTypeBinding(intersectingTypes[i]); } return intersectionBindings; } @@ -911,6 +909,15 @@ class TypeBinding implements ITypeBinding { } else if (this.binding instanceof WildcardBinding) { WildcardBinding wildcardBinding = (WildcardBinding) this.binding; typeVariableBinding = wildcardBinding.typeVariable(); + if (typeVariableBinding == null) { + org.eclipse.jdt.internal.compiler.lookup.TypeBinding allBounds = wildcardBinding.allBounds(); + if (allBounds instanceof IntersectionTypeBinding18) { // doubles up as null check + ITypeBinding typeBinding = this.resolver.getTypeBinding(allBounds); + if (typeBinding instanceof TypeBinding) { // doubles up as null check + return ((TypeBinding) typeBinding).getIntersectingTypes(); + } + } + } } else if (this.binding instanceof IntersectionTypeBinding18) { return this.bounds = getIntersectingTypes(); } -- cgit v1.2.3