diff options
author | M N Palat | 2013-09-25 09:16:04 +0000 |
---|---|---|
committer | M N Palat | 2013-09-25 09:16:04 +0000 |
commit | b3031b3455034eac76b444fd20cbebeea6a2f5ff (patch) | |
tree | 57bc4033ef8687b179e6c3cbce68ddbd8677cb34 | |
parent | b42e8d4e3737175b925c20ff2d83cccd857d4944 (diff) | |
download | eclipse.jdt.core-b3031b3455034eac76b444fd20cbebeea6a2f5ff.tar.gz eclipse.jdt.core-b3031b3455034eac76b444fd20cbebeea6a2f5ff.tar.xz eclipse.jdt.core-b3031b3455034eac76b444fd20cbebeea6a2f5ff.zip |
Fix for 417659 - [dom][ast][1.8] test failures - ast structure - when
debug tracing is on (fixes 5 rewrite failures)
27 files changed, 1188 insertions, 600 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15JLS4Test.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15JLS4Test.java index f0161190bd..dbc6d3e3a8 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15JLS4Test.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15JLS4Test.java @@ -1,10 +1,14 @@ /******************************************************************************* - * Copyright (c) 2011, 2012 IBM Corporation and others. + * Copyright (c) 2011, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation * Stephan Herrmann - Contribution for bug 186342 - [compiler][null] Using annotations for null checking @@ -64,6 +68,13 @@ public class ASTConverter15JLS4Test extends ConverterTestSetup { } } + /** + * @deprecated + */ + private Type componentType(ArrayType array) { + return array.getComponentType(); + } + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=234609 BindingKey#toSignature() fails with key from createWilcardTypeBindingKey(..) public void test234609() throws JavaModelException { @@ -1704,7 +1715,7 @@ public class ASTConverter15JLS4Test extends ConverterTestSetup { checkSourceRange(type, "String[]", source); assertTrue("not an array type", type.isArrayType()); ArrayType arrayType = (ArrayType) type; - checkSourceRange(arrayType.getComponentType(), "String", source); + checkSourceRange(componentType(arrayType), "String", source); assertEquals("Wrong extra dimensions", 1, singleVariableDeclaration.getExtraDimensions()); } @@ -1732,7 +1743,7 @@ public class ASTConverter15JLS4Test extends ConverterTestSetup { checkSourceRange(type, "String[]", source); assertTrue("not an array type", type.isArrayType()); ArrayType arrayType = (ArrayType) type; - checkSourceRange(arrayType.getComponentType(), "String", source); + checkSourceRange(componentType(arrayType), "String", source); assertEquals("Wrong extra dimensions", 0, singleVariableDeclaration.getExtraDimensions()); } /** @@ -2423,11 +2434,11 @@ public class ASTConverter15JLS4Test extends ConverterTestSetup { checkSourceRange(type, "Map<String, Double>[][]", source); assertEquals("wrong type", ASTNode.ARRAY_TYPE, type.getNodeType()); ArrayType arrayType = (ArrayType) type; - type = arrayType.getComponentType(); + type = componentType(arrayType); checkSourceRange(type, "Map<String, Double>[]", source); assertEquals("wrong type", ASTNode.ARRAY_TYPE, type.getNodeType()); arrayType = (ArrayType) type; - type = arrayType.getComponentType(); + type = componentType(arrayType); checkSourceRange(type, "Map<String, Double>", source); } @@ -2450,11 +2461,11 @@ public class ASTConverter15JLS4Test extends ConverterTestSetup { checkSourceRange(type, "java.util.Map<String, Double>[][]", source); assertEquals("wrong type", ASTNode.ARRAY_TYPE, type.getNodeType()); ArrayType arrayType = (ArrayType) type; - type = arrayType.getComponentType(); + type = componentType(arrayType); checkSourceRange(type, "java.util.Map<String, Double>[]", source); assertEquals("wrong type", ASTNode.ARRAY_TYPE, type.getNodeType()); arrayType = (ArrayType) type; - type = arrayType.getComponentType(); + type = componentType(arrayType); checkSourceRange(type, "java.util.Map<String, Double>", source); } @@ -4114,7 +4125,7 @@ public class ASTConverter15JLS4Test extends ConverterTestSetup { assertEquals("wrong dimensions", 1, typeBinding.getDimensions()); ArrayType arrayType = (ArrayType) type; assertEquals("Wrong dimension", 1, arrayType.getDimensions()); - type = arrayType.getComponentType(); + type = componentType(arrayType); assertTrue("Not a simple type", type.isSimpleType()); checkSourceRange(type, "String", contents); assertEquals("Wrong extra dimension", 1, singleVariableDeclaration.getExtraDimensions()); @@ -4498,7 +4509,7 @@ public class ASTConverter15JLS4Test extends ConverterTestSetup { assertEquals("wrong dimensions", 1, typeBinding.getDimensions()); ArrayType arrayType = (ArrayType) type; assertEquals("Wrong dimension", 1, arrayType.getDimensions()); - type = arrayType.getComponentType(); + type = componentType(arrayType); assertTrue("Not a simple type", type.isSimpleType()); checkSourceRange(type, "String", contents); assertEquals("Wrong extra dimension", 0, singleVariableDeclaration.getExtraDimensions()); @@ -11182,7 +11193,7 @@ public class ASTConverter15JLS4Test extends ConverterTestSetup { ITypeBinding binding = type.resolveBinding(); assertNotNull("No binding", binding); assertEquals("Wrong qualified name", "test0347.Outer<java.lang.Integer>.Inner<java.lang.Double>[]", binding.getQualifiedName()); - Type componentType = type.getComponentType(); + Type componentType = componentType(type); binding = componentType.resolveBinding(); assertNotNull("No binding", binding); assertEquals("Wrong qualified name", "test0347.Outer<java.lang.Integer>.Inner<java.lang.Double>", binding.getQualifiedName()); diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15JLS8Test.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15JLS8Test.java index f4b1528d51..e2c4404870 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15JLS8Test.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15JLS8Test.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011, 2012 IBM Corporation and others. + * Copyright (c) 2011, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -1708,7 +1708,7 @@ public class ASTConverter15JLS8Test extends ConverterTestSetup { checkSourceRange(type, "String[]", source); assertTrue("not an array type", type.isArrayType()); ArrayType arrayType = (ArrayType) type; - checkSourceRange(arrayType.getComponentType(), "String", source); + checkSourceRange(arrayType.getElementType(), "String", source); assertEquals("Wrong extra dimensions", 1, singleVariableDeclaration.getExtraDimensions()); } @@ -1736,7 +1736,7 @@ public class ASTConverter15JLS8Test extends ConverterTestSetup { checkSourceRange(type, "String[]", source); assertTrue("not an array type", type.isArrayType()); ArrayType arrayType = (ArrayType) type; - checkSourceRange(arrayType.getComponentType(), "String", source); + checkSourceRange(arrayType.getElementType(), "String", source); assertEquals("Wrong extra dimensions", 0, singleVariableDeclaration.getExtraDimensions()); } /** @@ -2427,12 +2427,9 @@ public class ASTConverter15JLS8Test extends ConverterTestSetup { checkSourceRange(type, "Map<String, Double>[][]", source); assertEquals("wrong type", ASTNode.ARRAY_TYPE, type.getNodeType()); ArrayType arrayType = (ArrayType) type; - type = arrayType.getComponentType(); - checkSourceRange(type, "Map<String, Double>[]", source); - assertEquals("wrong type", ASTNode.ARRAY_TYPE, type.getNodeType()); - arrayType = (ArrayType) type; - type = arrayType.getComponentType(); + type = arrayType.getElementType(); checkSourceRange(type, "Map<String, Double>", source); + assertEquals("wrong type", ASTNode.PARAMETERIZED_TYPE, type.getNodeType()); } /* @@ -2454,12 +2451,9 @@ public class ASTConverter15JLS8Test extends ConverterTestSetup { checkSourceRange(type, "java.util.Map<String, Double>[][]", source); assertEquals("wrong type", ASTNode.ARRAY_TYPE, type.getNodeType()); ArrayType arrayType = (ArrayType) type; - type = arrayType.getComponentType(); - checkSourceRange(type, "java.util.Map<String, Double>[]", source); - assertEquals("wrong type", ASTNode.ARRAY_TYPE, type.getNodeType()); - arrayType = (ArrayType) type; - type = arrayType.getComponentType(); + type = arrayType.getElementType(); checkSourceRange(type, "java.util.Map<String, Double>", source); + assertEquals("wrong type", ASTNode.PARAMETERIZED_TYPE, type.getNodeType()); } /* @@ -4118,7 +4112,7 @@ public class ASTConverter15JLS8Test extends ConverterTestSetup { assertEquals("wrong dimensions", 1, typeBinding.getDimensions()); ArrayType arrayType = (ArrayType) type; assertEquals("Wrong dimension", 1, arrayType.getDimensions()); - type = arrayType.getComponentType(); + type = arrayType.getElementType(); assertTrue("Not a simple type", type.isSimpleType()); checkSourceRange(type, "String", contents); assertEquals("Wrong extra dimension", 1, singleVariableDeclaration.getExtraDimensions()); @@ -4502,7 +4496,7 @@ public class ASTConverter15JLS8Test extends ConverterTestSetup { assertEquals("wrong dimensions", 1, typeBinding.getDimensions()); ArrayType arrayType = (ArrayType) type; assertEquals("Wrong dimension", 1, arrayType.getDimensions()); - type = arrayType.getComponentType(); + type = arrayType.getElementType(); assertTrue("Not a simple type", type.isSimpleType()); checkSourceRange(type, "String", contents); assertEquals("Wrong extra dimension", 0, singleVariableDeclaration.getExtraDimensions()); @@ -11186,7 +11180,7 @@ public class ASTConverter15JLS8Test extends ConverterTestSetup { ITypeBinding binding = type.resolveBinding(); assertNotNull("No binding", binding); assertEquals("Wrong qualified name", "test0347.Outer<java.lang.Integer>.Inner<java.lang.Double>[]", binding.getQualifiedName()); - Type componentType = type.getComponentType(); + Type componentType = type.getElementType(); binding = componentType.resolveBinding(); assertNotNull("No binding", binding); assertEquals("Wrong qualified name", "test0347.Outer<java.lang.Integer>.Inner<java.lang.Double>", binding.getQualifiedName()); diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15Test.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15Test.java index aec1a3c984..094d813586 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15Test.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15Test.java @@ -78,6 +78,13 @@ public class ASTConverter15Test extends ConverterTestSetup { } } + /** + * @deprecated + */ + private Type componentType(ArrayType array) { + return array.getComponentType(); + } + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=234609 BindingKey#toSignature() fails with key from createWilcardTypeBindingKey(..) public void test234609() throws JavaModelException { @@ -1718,7 +1725,7 @@ public class ASTConverter15Test extends ConverterTestSetup { checkSourceRange(type, "String[]", source); assertTrue("not an array type", type.isArrayType()); ArrayType arrayType = (ArrayType) type; - checkSourceRange(arrayType.getComponentType(), "String", source); + checkSourceRange(componentType(arrayType), "String", source); assertEquals("Wrong extra dimensions", 1, singleVariableDeclaration.getExtraDimensions()); } @@ -1746,7 +1753,7 @@ public class ASTConverter15Test extends ConverterTestSetup { checkSourceRange(type, "String[]", source); assertTrue("not an array type", type.isArrayType()); ArrayType arrayType = (ArrayType) type; - checkSourceRange(arrayType.getComponentType(), "String", source); + checkSourceRange(componentType(arrayType), "String", source); assertEquals("Wrong extra dimensions", 0, singleVariableDeclaration.getExtraDimensions()); } /** @@ -2437,11 +2444,11 @@ public class ASTConverter15Test extends ConverterTestSetup { checkSourceRange(type, "Map<String, Double>[][]", source); assertEquals("wrong type", ASTNode.ARRAY_TYPE, type.getNodeType()); ArrayType arrayType = (ArrayType) type; - type = arrayType.getComponentType(); + type = componentType(arrayType); checkSourceRange(type, "Map<String, Double>[]", source); assertEquals("wrong type", ASTNode.ARRAY_TYPE, type.getNodeType()); arrayType = (ArrayType) type; - type = arrayType.getComponentType(); + type = componentType(arrayType); checkSourceRange(type, "Map<String, Double>", source); } @@ -2464,11 +2471,11 @@ public class ASTConverter15Test extends ConverterTestSetup { checkSourceRange(type, "java.util.Map<String, Double>[][]", source); assertEquals("wrong type", ASTNode.ARRAY_TYPE, type.getNodeType()); ArrayType arrayType = (ArrayType) type; - type = arrayType.getComponentType(); + type = componentType(arrayType); checkSourceRange(type, "java.util.Map<String, Double>[]", source); assertEquals("wrong type", ASTNode.ARRAY_TYPE, type.getNodeType()); arrayType = (ArrayType) type; - type = arrayType.getComponentType(); + type = componentType(arrayType); checkSourceRange(type, "java.util.Map<String, Double>", source); } @@ -4128,7 +4135,7 @@ public class ASTConverter15Test extends ConverterTestSetup { assertEquals("wrong dimensions", 1, typeBinding.getDimensions()); ArrayType arrayType = (ArrayType) type; assertEquals("Wrong dimension", 1, arrayType.getDimensions()); - type = arrayType.getComponentType(); + type = componentType(arrayType); assertTrue("Not a simple type", type.isSimpleType()); checkSourceRange(type, "String", contents); assertEquals("Wrong extra dimension", 1, singleVariableDeclaration.getExtraDimensions()); @@ -4512,7 +4519,7 @@ public class ASTConverter15Test extends ConverterTestSetup { assertEquals("wrong dimensions", 1, typeBinding.getDimensions()); ArrayType arrayType = (ArrayType) type; assertEquals("Wrong dimension", 1, arrayType.getDimensions()); - type = arrayType.getComponentType(); + type = componentType(arrayType); assertTrue("Not a simple type", type.isSimpleType()); checkSourceRange(type, "String", contents); assertEquals("Wrong extra dimension", 0, singleVariableDeclaration.getExtraDimensions()); @@ -11197,7 +11204,7 @@ public class ASTConverter15Test extends ConverterTestSetup { ITypeBinding binding = type.resolveBinding(); assertNotNull("No binding", binding); assertEquals("Wrong qualified name", "test0347.Outer<java.lang.Integer>.Inner<java.lang.Double>[]", binding.getQualifiedName()); - Type componentType = type.getComponentType(); + Type componentType = componentType(type); binding = componentType.resolveBinding(); assertNotNull("No binding", binding); assertEquals("Wrong qualified name", "test0347.Outer<java.lang.Integer>.Inner<java.lang.Double>", binding.getQualifiedName()); @@ -11323,7 +11330,7 @@ public class ASTConverter15Test extends ConverterTestSetup { assertTrue("Not a compilation unit", result.getNodeType() == ASTNode.COMPILATION_UNIT); CompilationUnit unit = (CompilationUnit) result; MethodDeclaration methodDeclaration = (MethodDeclaration) getASTNode(unit, 0, 0); - Type componentType = ((ArrayType)methodDeclaration.getReturnType2()).getComponentType(); + Type componentType = componentType(((ArrayType)methodDeclaration.getReturnType2())); ITypeBinding typeBinding = componentType.resolveBinding(); assertEquals("Wrong fully qualified name", "test0351.I1", typeBinding.getQualifiedName()); } diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter18Test.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter18Test.java index a5c947092a..31b5bc8b84 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter18Test.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter18Test.java @@ -273,13 +273,13 @@ public class ASTConverter18Test extends ConverterTestSetup { ITypeBinding binding = type.resolveBinding(); assertNotNull("No binding", binding); assertEquals("Wrong qualified name", "test0004.Outer<java.lang.Integer>.Inner<java.lang.Double>[]", binding.getQualifiedName()); - Type componentType = type.getComponentType(); - binding = componentType.resolveBinding(); + Type elementType = type.getElementType(); + binding = elementType.resolveBinding(); assertNotNull("No binding", binding); assertEquals("Wrong qualified name", "test0004.Outer<java.lang.Integer>.Inner<java.lang.Double>", binding.getQualifiedName()); - assertTrue("Not parameterized", componentType.isParameterizedType()); - ParameterizedType parameterizedType = (ParameterizedType) componentType; + assertTrue("Not parameterized", elementType.isParameterizedType()); + ParameterizedType parameterizedType = (ParameterizedType) elementType; Type type2 = parameterizedType.getType(); assertTrue("Not qualified", type2.isQualifiedType()); QualifiedType qualifiedType = (QualifiedType) type2; @@ -1139,13 +1139,12 @@ public class ASTConverter18Test extends ConverterTestSetup { Type type = creation.getType(); assertEquals("Incorrect type", true, type.isArrayType()); checkSourceRange(type, "@Marker2 int @Marker @Marker2 [2] @Marker2 @Marker3 [bar()] @Marker3 @Marker []", contents.toCharArray()); - assertEquals("Incorrect annotations", "@Marker3 @Marker ", convertAnnotationsList(((ArrayType) type).annotations())); - type = ((ArrayType) type).getComponentType(); - assertEquals("Incorrect type", true, type.isArrayType()); - assertEquals("Incorrect annotations", "@Marker2 @Marker3 ", convertAnnotationsList(((ArrayType) type).annotations())); - type = ((ArrayType) type).getComponentType(); - assertEquals("Incorrect type", true, type.isArrayType()); - assertEquals("Incorrect annotations", "@Marker @Marker2 ", convertAnnotationsList(((ArrayType) type).annotations())); + ExtraDimension extraDimension = ((ArrayType) type).getDimensionAt(0); + assertEquals("Incorrect annotations", "@Marker3 @Marker ", convertAnnotationsList(extraDimension.annotations())); + extraDimension = ((ArrayType) type).getDimensionAt(1); + assertEquals("Incorrect annotations", "@Marker2 @Marker3 ", convertAnnotationsList(extraDimension.annotations())); + extraDimension = ((ArrayType) type).getDimensionAt(2); + assertEquals("Incorrect annotations", "@Marker @Marker2 ", convertAnnotationsList(extraDimension.annotations())); List dimensions = creation.dimensions(); assertEquals("Incorrect expressions", 2, dimensions.size()); assertEquals("Incorrect expressions", "2", dimensions.get(0).toString()); @@ -1159,13 +1158,12 @@ public class ASTConverter18Test extends ConverterTestSetup { type = creation.getType(); assertEquals("Incorrect type", true, type.isArrayType()); - assertEquals("Incorrect annotations", "@Marker2 @Marker3 ", convertAnnotationsList(((ArrayType) type).annotations())); - type = ((ArrayType) type).getComponentType(); - assertEquals("Incorrect type", true, type.isArrayType()); - assertEquals("Incorrect annotations", "@Marker @Marker2 ", convertAnnotationsList(((ArrayType) type).annotations())); - type = ((ArrayType) type).getComponentType(); - assertEquals("Incorrect type", true, type.isArrayType()); - assertEquals("Incorrect annotations", "@Marker3 @Marker ", convertAnnotationsList(((ArrayType) type).annotations())); + extraDimension = ((ArrayType) type).getDimensionAt(0); + assertEquals("Incorrect annotations", "@Marker2 @Marker3 ", convertAnnotationsList(extraDimension.annotations())); + extraDimension = ((ArrayType) type).getDimensionAt(1); + assertEquals("Incorrect annotations", "@Marker @Marker2 ", convertAnnotationsList(extraDimension.annotations())); + extraDimension = ((ArrayType) type).getDimensionAt(2); + assertEquals("Incorrect annotations", "@Marker3 @Marker ", convertAnnotationsList(extraDimension.annotations())); dimensions = creation.dimensions(); assertEquals("Incorrect expressions", 2, dimensions.size()); assertEquals("Incorrect expressions", "2", dimensions.get(0).toString()); @@ -1238,12 +1236,6 @@ public class ASTConverter18Test extends ConverterTestSetup { Type type = creation.getType(); assertEquals("Incorrect type", true, type.isArrayType()); assertEquals("Type should be malformed", ASTNode.MALFORMED, (type.getFlags() & ASTNode.MALFORMED)); - type = ((ArrayType) type).getComponentType(); - assertEquals("Incorrect type", true, type.isArrayType()); - assertEquals("Type should be malformed", ASTNode.MALFORMED, (type.getFlags() & ASTNode.MALFORMED)); - type = ((ArrayType) type).getComponentType(); - assertEquals("Incorrect type", true, type.isArrayType()); - assertEquals("Type should be malformed", ASTNode.MALFORMED, (type.getFlags() & ASTNode.MALFORMED)); } /** * https://bugs.eclipse.org/bugs/show_bug.cgi?id=399768 @@ -1614,14 +1606,14 @@ public class ASTConverter18Test extends ConverterTestSetup { Expression expression = fragment.getInitializer(); assertTrue(expression instanceof LambdaExpression); LambdaExpression lambdaExpression = (LambdaExpression)expression; - assertEquals("(int[] ia) -> {\n return ia.clone();\n}\n", lambdaExpression.toString()); + assertEquals("(int [] ia) -> {\n return ia.clone();\n}\n", lambdaExpression.toString()); IMethodBinding binding = lambdaExpression.resolveMethodBinding(); assertEquals("private static java.lang.Object lambda$0(int[]) ", binding.toString()); assertTrue(lambdaExpression.parameters().size() == 1); VariableDeclaration variableDeclaration = (VariableDeclaration) lambdaExpression.parameters().get(0); assertTrue(variableDeclaration instanceof SingleVariableDeclaration); SingleVariableDeclaration singleVariableDeclaration = (SingleVariableDeclaration)variableDeclaration; - assertEquals("int[] ia", singleVariableDeclaration.toString()); + assertEquals("int [] ia", singleVariableDeclaration.toString()); } /** @@ -2653,5 +2645,232 @@ public class ASTConverter18Test extends ConverterTestSetup { typeBinding = type.resolveBinding(); assertFalse("A Functional interface", typeBinding.isFunctionalInterface()); } + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=413942 + // also refer https://bugs.eclipse.org/bugs/show_bug.cgi?id=413569 + public void testBug413942() throws JavaModelException { + String contents = + "public class X extends @NonNull(int[].class) Object {\n" + + " Object field = new ArrayList< @NonEmpty(0) int @NonNull(value1 = 1) [] @NonEmpty(1) [ ]>() ;\n" + + " @Annot int @Annot1 [] a1 @Annot2 @Annot3 @NonNull (value = int[].class, value1 = 0)[/* [] */ ] @Annot3 @Annot2 [] @Annot4 [];\n" + + " int[] xxx[];\n" + + " int [][] ii = new int[2][3];" + + " ArrayList<int[]> [][] yyy; // source ranges already broken in AST.JLS4\n" + + " ArrayList<int[][]> [][][][] zzz;\n" + + " ArrayList<Float> [][][] zzz2;\n" + + " Object a = new ArrayList< @TakeType(int[][].class) int @TakeType(float.class) [] @TakeType(double.class) []>() ;\n" + + " Object b = new @NonNull(value1 = Math.PI) ArrayList< >() ; \n" + + " Object c = new ArrayList<@NonNull(value1= Math.PI ) Object[]>() ;\n" + + "\n" + + " int foo(@TakeType(int[].class)int i ) @TakeType(int[].class) [] {\n" + + " int[] arr = new int[2];\n" + + " for (String tab @TakeType(int[].class) [] = null;; ++i) { break; }\n" + + " for (@Deprecated String tab@TakeType(int[].class) [][] = null;; ++i) {}\n" + + " }\n" + + " int bar(int [] /*@TakeType(int[].class)*/ [] a ) {\n" + + " return 0;\n" + + " }\n" + + "public int var1(int @TakeType(int[].class)... args) { return 0;}\n" + + "public int var2(int @Annot ... args) { return 0;}\n" + + "}\n" + + "\n" + + "@Target(ElementType.TYPE_USE)\n" + + "@Retention(RetentionPolicy.RUNTIME)\n" + + "@Documented\n" + + "@interface NonNull {\n" + + " Class value() default int.class;\n" + + " double value1() default 0;\n" + + "}\n" + + "\n" + + "@Target(ElementType.TYPE_USE)\n" + + "@Retention(RetentionPolicy.RUNTIME)\n" + + "@Documented\n" + + "@interface NonEmpty {\n" + + " int value() default 0;\n" + + "}\n" + + "\n" + + "@Target(ElementType.TYPE_USE)\n" + + "@Retention(RetentionPolicy.RUNTIME)\n" + + "@Documented\n" + + "@interface TakeType {\n" + + " Class value() default int[].class;\n" + + "}\n" + + "\n" + + "@Target(ElementType.TYPE_USE)\n" + + "@Retention(RetentionPolicy.RUNTIME)\n" + + "@Documented\n" + + "@interface Annot {}\n" + + "\n" + + "@Target(ElementType.TYPE_USE)\n" + + "@Retention(RetentionPolicy.RUNTIME)\n" + + "@Documented\n" + + "@interface Annot1 {}\n" + + "\n" + + "@Target(ElementType.TYPE_USE)\n" + + "@Retention(RetentionPolicy.RUNTIME)\n" + + "@Documented\n" + + "@interface Annot2 {}\n" + + "\n" + + "@Target(ElementType.TYPE_USE)\n" + + "@Retention(RetentionPolicy.RUNTIME)\n" + + "@Documented\n" + + "@interface Annot3 {}\n" + + "\n" + + "@Target(ElementType.TYPE_USE)\n" + + "@Retention(RetentionPolicy.RUNTIME)\n" + + "@Documented\n" + + "@interface Annot4 {}\n" + + "\n"; + this.workingCopy = getWorkingCopy("/Converter18/src/X.java", true); + ASTNode node = buildAST(contents, this.workingCopy, false); + assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType()); + CompilationUnit unit = (CompilationUnit) node; + + TypeDeclaration type = (TypeDeclaration) unit.types().get(0); + SimpleType simpleType = (SimpleType) type.getSuperclassType(); + checkSourceRange(simpleType, "@NonNull(int[].class) Object", contents); + SingleMemberAnnotation singleMemberAnnotation = (SingleMemberAnnotation) simpleType.annotations().get(0); + checkSourceRange(singleMemberAnnotation, "@NonNull(int[].class)", contents); + TypeLiteral typeLiteral = (TypeLiteral) singleMemberAnnotation.getValue(); + checkSourceRange(typeLiteral, "int[].class", contents); + ArrayType arrayType = (ArrayType) typeLiteral.getType(); + checkSourceRange(arrayType, "int[]", contents); + + int count = 0; + FieldDeclaration field = (FieldDeclaration) type.bodyDeclarations().get(count++); + checkSourceRange(field, "Object field = new ArrayList< @NonEmpty(0) int @NonNull(value1 = 1) [] @NonEmpty(1) [ ]>() ;", contents); + VariableDeclarationFragment fragment = (VariableDeclarationFragment) field.fragments().get(0); + ClassInstanceCreation instance = (ClassInstanceCreation) fragment.getInitializer(); + ParameterizedType parameterizedType = (ParameterizedType) instance.getType(); + arrayType = (ArrayType) parameterizedType.typeArguments().get(0); + checkSourceRange(arrayType, "@NonEmpty(0) int @NonNull(value1 = 1) [] @NonEmpty(1) [ ]", contents); + PrimitiveType primitiveType = (PrimitiveType) arrayType.getElementType(); + checkSourceRange(primitiveType, "@NonEmpty(0) int", contents); + ExtraDimension extraDimension = arrayType.getDimensionAt(1); + checkSourceRange(extraDimension, "@NonNull(value1 = 1) []", contents); + extraDimension = arrayType.getDimensionAt(0); + checkSourceRange(extraDimension, "@NonEmpty(1) [ ]", contents); + + field = (FieldDeclaration) type.bodyDeclarations().get(count++); + checkSourceRange(field, "@Annot int @Annot1 [] a1 @Annot2 @Annot3 @NonNull (value = int[].class, value1 = 0)[/* [] */ ] @Annot3 @Annot2 [] @Annot4 [];", contents); + arrayType = (ArrayType) field.getType(); + checkSourceRange(arrayType, "int @Annot1 []", contents); + fragment = (VariableDeclarationFragment) field.fragments().get(0); + extraDimension = (ExtraDimension) fragment.extraDimensions().get(0); + checkSourceRange(extraDimension, "@Annot2 @Annot3 @NonNull (value = int[].class, value1 = 0)[/* [] */ ]", contents); + extraDimension = (ExtraDimension) fragment.extraDimensions().get(1); + checkSourceRange(extraDimension, "@Annot3 @Annot2 []", contents); + + field = (FieldDeclaration) type.bodyDeclarations().get(count++); + checkSourceRange(field, "int[] xxx[];", contents); + assertTrue(field.getType().isArrayType()); + arrayType = (ArrayType) field.getType(); + checkSourceRange(arrayType, "int[]", contents); + + field = (FieldDeclaration) type.bodyDeclarations().get(count++); + checkSourceRange(field, "int [][] ii = new int[2][3];", contents); + arrayType = (ArrayType) field.getType(); + checkSourceRange(arrayType, "int [][]", contents); + fragment = (VariableDeclarationFragment) field.fragments().get(0); + ArrayCreation arrayCreation = (ArrayCreation) fragment.getInitializer(); + arrayType = arrayCreation.getType(); + assertTrue(arrayType.getElementType().isPrimitiveType()); + assertTrue(arrayType.getDimensions() == 2); + + field = (FieldDeclaration) type.bodyDeclarations().get(count++); + checkSourceRange(field, "ArrayList<int[]> [][] yyy;", contents); + arrayType = (ArrayType) field.getType(); + checkSourceRange(arrayType, "ArrayList<int[]> [][]", contents); + + field = (FieldDeclaration) type.bodyDeclarations().get(count++); + checkSourceRange(field, "ArrayList<int[][]> [][][][] zzz;", contents); + arrayType = (ArrayType) field.getType(); + assertTrue(arrayType.getElementType().isParameterizedType()); + assertTrue(arrayType.getDimensions() == 4); + + field = (FieldDeclaration) type.bodyDeclarations().get(count++); + checkSourceRange(field, "ArrayList<Float> [][][] zzz2;", contents); + arrayType = (ArrayType) field.getType(); + assertTrue(arrayType.getElementType().isParameterizedType()); + assertTrue(arrayType.getDimensions() == 3); + + field = (FieldDeclaration) type.bodyDeclarations().get(count++); + checkSourceRange(field, "Object a = new ArrayList< @TakeType(int[][].class) int @TakeType(float.class) [] @TakeType(double.class) []>() ;", contents); + fragment = (VariableDeclarationFragment) field.fragments().get(0); + ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) fragment.getInitializer(); + parameterizedType = (ParameterizedType) classInstanceCreation.getType(); + arrayType = (ArrayType) parameterizedType.typeArguments().get(0); + checkSourceRange(arrayType, "@TakeType(int[][].class) int @TakeType(float.class) [] @TakeType(double.class) []", contents); + checkSourceRange(arrayType.getElementType(), "@TakeType(int[][].class) int", contents); + assertTrue(arrayType.getElementType().isPrimitiveType()); + extraDimension = arrayType.getDimensionAt(1); + checkSourceRange(extraDimension, "@TakeType(float.class) []", contents); + extraDimension = arrayType.getDimensionAt(0); + Annotation annotation = (Annotation) extraDimension.annotations().get(0); + assertTrue(annotation.isSingleMemberAnnotation()); + singleMemberAnnotation = (SingleMemberAnnotation) annotation; + typeLiteral = (TypeLiteral) singleMemberAnnotation.getValue(); + checkSourceRange(typeLiteral, "double.class", contents); + + field = (FieldDeclaration) type.bodyDeclarations().get(count++); + checkSourceRange(field, "Object b = new @NonNull(value1 = Math.PI) ArrayList< >() ;", contents); + fragment = (VariableDeclarationFragment) field.fragments().get(0); + classInstanceCreation = (ClassInstanceCreation) fragment.getInitializer(); + parameterizedType = (ParameterizedType) classInstanceCreation.getType(); + checkSourceRange(parameterizedType.getType(), "@NonNull(value1 = Math.PI) ArrayList", contents); + + field = (FieldDeclaration) type.bodyDeclarations().get(count++); + checkSourceRange(field, "Object c = new ArrayList<@NonNull(value1= Math.PI ) Object[]>() ;", contents); + fragment = (VariableDeclarationFragment) field.fragments().get(0); + classInstanceCreation = (ClassInstanceCreation) fragment.getInitializer(); + parameterizedType = (ParameterizedType) classInstanceCreation.getType(); + arrayType = (ArrayType) parameterizedType.typeArguments().get(0); + assertTrue(arrayType.getDimensions() == 1); + + MethodDeclaration method = (MethodDeclaration) type.bodyDeclarations().get(count++); + extraDimension = (ExtraDimension) method.extraDimensions().get(0); + checkSourceRange(extraDimension, "@TakeType(int[].class) []", contents); + singleMemberAnnotation = (SingleMemberAnnotation) extraDimension.annotations().get(0); + typeLiteral = (TypeLiteral) singleMemberAnnotation.getValue(); + arrayType = (ArrayType) typeLiteral.getType(); + assertTrue(arrayType.getElementType().isPrimitiveType()); + assertTrue(arrayType.getDimensions() == 1); + SingleVariableDeclaration singleVariableDeclaration = (SingleVariableDeclaration) method.parameters().get(0); + singleMemberAnnotation = (SingleMemberAnnotation) singleVariableDeclaration.modifiers().get(0); + checkSourceRange(singleMemberAnnotation, "@TakeType(int[].class)", contents); + typeLiteral = (TypeLiteral) singleMemberAnnotation.getValue(); + arrayType = (ArrayType) typeLiteral.getType(); + assertTrue(arrayType.getElementType().isPrimitiveType()); + assertTrue(arrayType.getDimensions() == 1); + ForStatement forStatement = (ForStatement) method.getBody().statements().get(1); + VariableDeclarationExpression variableDeclarationExpression = (VariableDeclarationExpression) forStatement.initializers().get(0); + fragment = (VariableDeclarationFragment) variableDeclarationExpression.fragments().get(0); + extraDimension = (ExtraDimension) fragment.extraDimensions().get(0); + checkSourceRange(extraDimension, "@TakeType(int[].class) []", contents); + forStatement = (ForStatement) method.getBody().statements().get(1); + variableDeclarationExpression = (VariableDeclarationExpression) forStatement.initializers().get(0); + fragment = (VariableDeclarationFragment) variableDeclarationExpression.fragments().get(0); + extraDimension = (ExtraDimension) fragment.extraDimensions().get(0); + checkSourceRange(extraDimension, "@TakeType(int[].class) []", contents); + + method = (MethodDeclaration) type.bodyDeclarations().get(count++); + singleVariableDeclaration = (SingleVariableDeclaration) method.parameters().get(0); + // test case active only after bug 417660 is fixed (uncomment) + checkSourceRange(singleVariableDeclaration, "int [] /*@TakeType(int[].class)*/ [] a", contents); + + method = (MethodDeclaration) type.bodyDeclarations().get(count++); + singleVariableDeclaration = (SingleVariableDeclaration) method.parameters().get(0); + checkSourceRange(singleVariableDeclaration, "int @TakeType(int[].class)... args", contents); + singleMemberAnnotation = (SingleMemberAnnotation) singleVariableDeclaration.varargsAnnotations().get(0); + typeLiteral = (TypeLiteral) singleMemberAnnotation.getValue(); + arrayType = (ArrayType) typeLiteral.getType(); + assertTrue(arrayType.getElementType().isPrimitiveType()); + assertTrue(arrayType.getDimensions() == 1); + + method = (MethodDeclaration) type.bodyDeclarations().get(count++); + singleVariableDeclaration = (SingleVariableDeclaration) method.parameters().get(0); + checkSourceRange(singleVariableDeclaration, "int @Annot ... args", contents); + assertTrue(singleVariableDeclaration.varargsAnnotations().size() == 1); + + } } diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterAST3Test.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterAST3Test.java index 9d77f72f8f..a62bafd224 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterAST3Test.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterAST3Test.java @@ -59,6 +59,13 @@ public class ASTConverterAST3Test extends ConverterTestSetup { return methodDeclaration.thrownExceptions(); } + /** + * @deprecated + */ + private Type componentType(ArrayType array) { + return array.getComponentType(); + } + public void test0001() throws JavaModelException { ICompilationUnit sourceUnit = getCompilationUnit("Converter" , "src", "test0001", "Test.java"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ char[] source = sourceUnit.getSource().toCharArray(); @@ -7659,13 +7666,13 @@ public class ASTConverterAST3Test extends ConverterTestSetup { ITypeBinding typeBinding = arrayType.resolveBinding(); checkSourceRange(type, "java.lang.Object[][]", source); //$NON-NLS-1$ assertNotNull("No type binding", typeBinding); //$NON-NLS-1$ - Type elementType = arrayType.getComponentType(); + Type elementType = componentType(arrayType); ITypeBinding typeBinding2 = elementType.resolveBinding(); assertNotNull("No type binding2", typeBinding2); //$NON-NLS-1$ assertEquals("wrong dimension", 1, typeBinding2.getDimensions()); //$NON-NLS-1$ assertEquals("wrong name", "Object[]", typeBinding2.getName()); //$NON-NLS-1$ //$NON-NLS-2$ assertTrue("Not an array type", elementType.isArrayType()); //$NON-NLS-1$ - Type elementType2 = ((ArrayType) elementType).getComponentType(); + Type elementType2 = componentType(((ArrayType) elementType)); assertTrue("Not a simple type", elementType2.isSimpleType()); //$NON-NLS-1$ ITypeBinding typeBinding3 = elementType2.resolveBinding(); assertNotNull("No type binding3", typeBinding3); //$NON-NLS-1$ @@ -8006,7 +8013,7 @@ public class ASTConverterAST3Test extends ConverterTestSetup { ITypeBinding typeBinding3 = simpleType.resolveBinding(); assertNotNull("no type binding3", typeBinding3); //$NON-NLS-1$ assertEquals("wrong name", "Object", typeBinding3.getName()); //$NON-NLS-1$ //$NON-NLS-2$ - type = arrayType.getComponentType(); + type = componentType(arrayType); assertTrue("Not an array type", type instanceof ArrayType); //$NON-NLS-1$ ArrayType arrayType2 = (ArrayType) type; ITypeBinding typeBinding4 = arrayType2.resolveBinding(); @@ -8048,14 +8055,14 @@ public class ASTConverterAST3Test extends ConverterTestSetup { ITypeBinding typeBinding3 = simpleType.resolveBinding(); assertNotNull("no type binding3", typeBinding3); //$NON-NLS-1$ assertEquals("wrong name", "Object", typeBinding3.getName()); //$NON-NLS-1$ //$NON-NLS-2$ - type = arrayType.getComponentType(); + type = componentType(arrayType); assertTrue("Not an array type", type instanceof ArrayType); //$NON-NLS-1$ ArrayType arrayType2 = (ArrayType) type; checkSourceRange(arrayType2, "Object[10][]", source); //$NON-NLS-1$ ITypeBinding typeBinding4 = arrayType2.resolveBinding(); assertNotNull("no type binding4", typeBinding4); //$NON-NLS-1$ assertEquals("wrong name", "Object[][]", typeBinding4.getName()); //$NON-NLS-1$ //$NON-NLS-2$ - type = arrayType2.getComponentType(); + type = componentType(arrayType2); assertTrue("Not an array type", type instanceof ArrayType); //$NON-NLS-1$ ArrayType arrayType3 = (ArrayType) type; ITypeBinding typeBinding5 = arrayType3.resolveBinding(); diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterAST4Test.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterAST4Test.java index 8f41012bf3..c58a21833e 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterAST4Test.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterAST4Test.java @@ -59,6 +59,13 @@ public class ASTConverterAST4Test extends ConverterTestSetup { return methodDeclaration.thrownExceptions(); } + /** + * @deprecated + */ + private Type componentType(ArrayType array) { + return array.getComponentType(); + } + public void test0001() throws JavaModelException { ICompilationUnit sourceUnit = getCompilationUnit("Converter" , "src", "test0001", "Test.java"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ char[] source = sourceUnit.getSource().toCharArray(); @@ -7659,13 +7666,13 @@ public class ASTConverterAST4Test extends ConverterTestSetup { ITypeBinding typeBinding = arrayType.resolveBinding(); checkSourceRange(type, "java.lang.Object[][]", source); //$NON-NLS-1$ assertNotNull("No type binding", typeBinding); //$NON-NLS-1$ - Type elementType = arrayType.getComponentType(); + Type elementType = componentType(arrayType); ITypeBinding typeBinding2 = elementType.resolveBinding(); assertNotNull("No type binding2", typeBinding2); //$NON-NLS-1$ assertEquals("wrong dimension", 1, typeBinding2.getDimensions()); //$NON-NLS-1$ assertEquals("wrong name", "Object[]", typeBinding2.getName()); //$NON-NLS-1$ //$NON-NLS-2$ assertTrue("Not an array type", elementType.isArrayType()); //$NON-NLS-1$ - Type elementType2 = ((ArrayType) elementType).getComponentType(); + Type elementType2 = componentType(((ArrayType) elementType)); assertTrue("Not a simple type", elementType2.isSimpleType()); //$NON-NLS-1$ ITypeBinding typeBinding3 = elementType2.resolveBinding(); assertNotNull("No type binding3", typeBinding3); //$NON-NLS-1$ @@ -8006,7 +8013,7 @@ public class ASTConverterAST4Test extends ConverterTestSetup { ITypeBinding typeBinding3 = simpleType.resolveBinding(); assertNotNull("no type binding3", typeBinding3); //$NON-NLS-1$ assertEquals("wrong name", "Object", typeBinding3.getName()); //$NON-NLS-1$ //$NON-NLS-2$ - type = arrayType.getComponentType(); + type = componentType(arrayType); assertTrue("Not an array type", type instanceof ArrayType); //$NON-NLS-1$ ArrayType arrayType2 = (ArrayType) type; ITypeBinding typeBinding4 = arrayType2.resolveBinding(); @@ -8048,14 +8055,14 @@ public class ASTConverterAST4Test extends ConverterTestSetup { ITypeBinding typeBinding3 = simpleType.resolveBinding(); assertNotNull("no type binding3", typeBinding3); //$NON-NLS-1$ assertEquals("wrong name", "Object", typeBinding3.getName()); //$NON-NLS-1$ //$NON-NLS-2$ - type = arrayType.getComponentType(); + type = componentType(arrayType); assertTrue("Not an array type", type instanceof ArrayType); //$NON-NLS-1$ ArrayType arrayType2 = (ArrayType) type; checkSourceRange(arrayType2, "Object[10][]", source); //$NON-NLS-1$ ITypeBinding typeBinding4 = arrayType2.resolveBinding(); assertNotNull("no type binding4", typeBinding4); //$NON-NLS-1$ assertEquals("wrong name", "Object[][]", typeBinding4.getName()); //$NON-NLS-1$ //$NON-NLS-2$ - type = arrayType2.getComponentType(); + type = componentType(arrayType2); assertTrue("Not an array type", type instanceof ArrayType); //$NON-NLS-1$ ArrayType arrayType3 = (ArrayType) type; ITypeBinding typeBinding5 = arrayType3.resolveBinding(); diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterAST8Test.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterAST8Test.java index ce0fcc42af..d4130e51ec 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterAST8Test.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterAST8Test.java @@ -7662,15 +7662,9 @@ public class ASTConverterAST8Test extends ConverterTestSetup { ITypeBinding typeBinding = arrayType.resolveBinding(); checkSourceRange(type, "java.lang.Object[][]", source); //$NON-NLS-1$ assertNotNull("No type binding", typeBinding); //$NON-NLS-1$ - Type elementType = arrayType.getComponentType(); - ITypeBinding typeBinding2 = elementType.resolveBinding(); - assertNotNull("No type binding2", typeBinding2); //$NON-NLS-1$ - assertEquals("wrong dimension", 1, typeBinding2.getDimensions()); //$NON-NLS-1$ - assertEquals("wrong name", "Object[]", typeBinding2.getName()); //$NON-NLS-1$ //$NON-NLS-2$ - assertTrue("Not an array type", elementType.isArrayType()); //$NON-NLS-1$ - Type elementType2 = ((ArrayType) elementType).getComponentType(); - assertTrue("Not a simple type", elementType2.isSimpleType()); //$NON-NLS-1$ - ITypeBinding typeBinding3 = elementType2.resolveBinding(); + Type elementType = arrayType.getElementType(); + assertTrue("Not a simple type", elementType.isSimpleType()); //$NON-NLS-1$ + ITypeBinding typeBinding3 = elementType.resolveBinding(); assertNotNull("No type binding3", typeBinding3); //$NON-NLS-1$ assertEquals("wrong dimension", 0, typeBinding3.getDimensions()); //$NON-NLS-1$ assertEquals("wrong name", "Object", typeBinding3.getName()); //$NON-NLS-1$ //$NON-NLS-2$ @@ -8009,12 +8003,10 @@ public class ASTConverterAST8Test extends ConverterTestSetup { ITypeBinding typeBinding3 = simpleType.resolveBinding(); assertNotNull("no type binding3", typeBinding3); //$NON-NLS-1$ assertEquals("wrong name", "Object", typeBinding3.getName()); //$NON-NLS-1$ //$NON-NLS-2$ - type = arrayType.getComponentType(); - assertTrue("Not an array type", type instanceof ArrayType); //$NON-NLS-1$ - ArrayType arrayType2 = (ArrayType) type; - ITypeBinding typeBinding4 = arrayType2.resolveBinding(); + type = arrayType.getElementType(); + ITypeBinding typeBinding4 = type.resolveBinding(); assertNotNull("no type binding4", typeBinding4); //$NON-NLS-1$ - assertEquals("wrong name", "Object[]", typeBinding4.getName()); //$NON-NLS-1$ //$NON-NLS-2$ + assertEquals("wrong name", "Object", typeBinding4.getName()); //$NON-NLS-1$ //$NON-NLS-2$ } /** @@ -8051,20 +8043,10 @@ public class ASTConverterAST8Test extends ConverterTestSetup { ITypeBinding typeBinding3 = simpleType.resolveBinding(); assertNotNull("no type binding3", typeBinding3); //$NON-NLS-1$ assertEquals("wrong name", "Object", typeBinding3.getName()); //$NON-NLS-1$ //$NON-NLS-2$ - type = arrayType.getComponentType(); - assertTrue("Not an array type", type instanceof ArrayType); //$NON-NLS-1$ - ArrayType arrayType2 = (ArrayType) type; - checkSourceRange(arrayType2, "Object[10][]", source); //$NON-NLS-1$ - ITypeBinding typeBinding4 = arrayType2.resolveBinding(); + type = arrayType.getElementType(); + ITypeBinding typeBinding4 = type.resolveBinding(); assertNotNull("no type binding4", typeBinding4); //$NON-NLS-1$ - assertEquals("wrong name", "Object[][]", typeBinding4.getName()); //$NON-NLS-1$ //$NON-NLS-2$ - type = arrayType2.getComponentType(); - assertTrue("Not an array type", type instanceof ArrayType); //$NON-NLS-1$ - ArrayType arrayType3 = (ArrayType) type; - ITypeBinding typeBinding5 = arrayType3.resolveBinding(); - assertNotNull("no type binding5", typeBinding5); //$NON-NLS-1$ - assertEquals("wrong name", "Object[]", typeBinding5.getName()); //$NON-NLS-1$ //$NON-NLS-2$ - checkSourceRange(arrayType3, "Object[10]", source); //$NON-NLS-1$ + assertEquals("wrong name", "Object", typeBinding4.getName()); //$NON-NLS-1$ //$NON-NLS-2$ } diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterJavadocFlattener.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterJavadocFlattener.java index d58dbdcd65..3f396b9c94 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterJavadocFlattener.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterJavadocFlattener.java @@ -1,10 +1,14 @@ /******************************************************************************* - * Copyright (c) 2000, 2008 IBM Corporation and others. + * Copyright (c) 2000, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ @@ -33,6 +37,13 @@ ASTConverterJavadocFlattener(String comment) { } /** + * @deprecated + */ +private Type componentType(ArrayType array) { + return array.getComponentType(); +} + +/** * Returns the string accumulated in the visit. * * @return the serialized @@ -52,8 +63,15 @@ public void reset() { * @see ASTVisitor#visit(ArrayType) */ public boolean visit(ArrayType node) { - node.getComponentType().accept(this); - this.buffer.append("[]");//$NON-NLS-1$ + if (node.getAST().apiLevel() < AST.JLS8) { + componentType(node).accept(this); + this.buffer.append("[]");//$NON-NLS-1$ + } else { + node.getElementType().accept(this); + for (int i = 0; i < node.getDimensions(); ++i) { + ((ExtraDimension) node.dimensions().get(i)).accept(this); + } + } return false; } diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterTest.java index 8d60553d0d..ac6c02c370 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterTest.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterTest.java @@ -52,6 +52,13 @@ public class ASTConverterTest extends ConverterTestSetup { return methodDeclaration.thrownExceptions(); } + /** + * @deprecated + */ + private Type componentType(ArrayType array) { + return array.getComponentType(); + } + /** @deprecated using deprecated code */ public void test0001() throws JavaModelException { ICompilationUnit sourceUnit = getCompilationUnit("Converter" , "src", "test0001", "Test.java"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ @@ -7821,13 +7828,13 @@ public class ASTConverterTest extends ConverterTestSetup { ITypeBinding typeBinding = arrayType.resolveBinding(); checkSourceRange(type, "java.lang.Object[][]", source); //$NON-NLS-1$ assertNotNull("No type binding", typeBinding); //$NON-NLS-1$ - Type elementType = arrayType.getComponentType(); + Type elementType = componentType(arrayType); ITypeBinding typeBinding2 = elementType.resolveBinding(); assertNotNull("No type binding2", typeBinding2); //$NON-NLS-1$ assertEquals("wrong dimension", 1, typeBinding2.getDimensions()); //$NON-NLS-1$ assertEquals("wrong name", "Object[]", typeBinding2.getName()); //$NON-NLS-1$ //$NON-NLS-2$ assertTrue("Not an array type", elementType.isArrayType()); //$NON-NLS-1$ - Type elementType2 = ((ArrayType) elementType).getComponentType(); + Type elementType2 = componentType(((ArrayType) elementType)); assertTrue("Not a simple type", elementType2.isSimpleType()); //$NON-NLS-1$ ITypeBinding typeBinding3 = elementType2.resolveBinding(); assertNotNull("No type binding3", typeBinding3); //$NON-NLS-1$ @@ -8168,7 +8175,7 @@ public class ASTConverterTest extends ConverterTestSetup { ITypeBinding typeBinding3 = simpleType.resolveBinding(); assertNotNull("no type binding3", typeBinding3); //$NON-NLS-1$ assertEquals("wrong name", "Object", typeBinding3.getName()); //$NON-NLS-1$ //$NON-NLS-2$ - type = arrayType.getComponentType(); + type = componentType(arrayType); assertTrue("Not an array type", type instanceof ArrayType); //$NON-NLS-1$ ArrayType arrayType2 = (ArrayType) type; ITypeBinding typeBinding4 = arrayType2.resolveBinding(); @@ -8210,14 +8217,14 @@ public class ASTConverterTest extends ConverterTestSetup { ITypeBinding typeBinding3 = simpleType.resolveBinding(); assertNotNull("no type binding3", typeBinding3); //$NON-NLS-1$ assertEquals("wrong name", "Object", typeBinding3.getName()); //$NON-NLS-1$ //$NON-NLS-2$ - type = arrayType.getComponentType(); + type = componentType(arrayType); assertTrue("Not an array type", type instanceof ArrayType); //$NON-NLS-1$ ArrayType arrayType2 = (ArrayType) type; checkSourceRange(arrayType2, "Object[10][]", source); //$NON-NLS-1$ ITypeBinding typeBinding4 = arrayType2.resolveBinding(); assertNotNull("no type binding4", typeBinding4); //$NON-NLS-1$ assertEquals("wrong name", "Object[][]", typeBinding4.getName()); //$NON-NLS-1$ //$NON-NLS-2$ - type = arrayType2.getComponentType(); + type = componentType(arrayType2); assertTrue("Not an array type", type instanceof ArrayType); //$NON-NLS-1$ ArrayType arrayType3 = (ArrayType) type; ITypeBinding typeBinding5 = arrayType3.resolveBinding(); diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterTest2.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterTest2.java index 12ad560617..6617fbbb14 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterTest2.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterTest2.java @@ -4,6 +4,10 @@ * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html + * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. * * Contributors: * IBM Corporation - initial API and implementation @@ -57,6 +61,13 @@ public class ASTConverterTest2 extends ConverterTestSetup { } /** + * @deprecated + */ + private Type componentType(ArrayType array) { + return array.getComponentType(); + } + + /** * http://dev.eclipse.org/bugs/show_bug.cgi?id=22560 * @deprecated using deprecated code */ @@ -2631,11 +2642,11 @@ public class ASTConverterTest2 extends ConverterTestSetup { checkSourceRange(type, "Class[][]", source); assertTrue("not an array type", type.isArrayType()); //$NON-NLS-1$ ArrayType arrayType = (ArrayType) type; - Type componentType = arrayType.getComponentType(); + Type componentType = componentType(arrayType); assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class[]", source); arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); + componentType = componentType(arrayType); assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class", source); } @@ -2657,15 +2668,15 @@ public class ASTConverterTest2 extends ConverterTestSetup { checkSourceRange(type, "Class[][][]", source); assertTrue("not an array type", type.isArrayType()); //$NON-NLS-1$ ArrayType arrayType = (ArrayType) type; - Type componentType = arrayType.getComponentType(); + Type componentType = componentType(arrayType); assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class[][]", source); arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); + componentType = componentType(arrayType); assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class[]", source); arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); + componentType = componentType(arrayType); assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class", source); } @@ -2687,11 +2698,11 @@ public class ASTConverterTest2 extends ConverterTestSetup { checkSourceRange(type, "Class[][]", source); assertTrue("not an array type", type.isArrayType()); //$NON-NLS-1$ ArrayType arrayType = (ArrayType) type; - Type componentType = arrayType.getComponentType(); + Type componentType = componentType(arrayType); assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class[]", source); arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); + componentType = componentType(arrayType); assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class", source); List fragments = fieldDeclaration.fragments(); @@ -2717,19 +2728,19 @@ public class ASTConverterTest2 extends ConverterTestSetup { checkSourceRange(type, "Class[][][][]", source); assertTrue("not an array type", type.isArrayType()); //$NON-NLS-1$ ArrayType arrayType = (ArrayType) type; - Type componentType = arrayType.getComponentType(); + Type componentType = componentType(arrayType); assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class[][][]", source); arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); + componentType = componentType(arrayType); assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class[][]", source); arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); + componentType = componentType(arrayType); assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class[]", source); arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); + componentType = componentType(arrayType); assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class", source); } @@ -2751,7 +2762,7 @@ public class ASTConverterTest2 extends ConverterTestSetup { checkSourceRange(type, "Class[]", source); assertTrue("not an array type", type.isArrayType()); //$NON-NLS-1$ ArrayType arrayType = (ArrayType) type; - Type componentType = arrayType.getComponentType(); + Type componentType = componentType(arrayType); assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class", source); } diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterTestAST3_2.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterTestAST3_2.java index 0b9a1772ae..7b49755b95 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterTestAST3_2.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterTestAST3_2.java @@ -143,6 +143,13 @@ public class ASTConverterTestAST3_2 extends ConverterTestSetup { } /** + * @deprecated + */ + private Type componentType(ArrayType array) { + return array.getComponentType(); + } + + /** * http://dev.eclipse.org/bugs/show_bug.cgi?id=22560 */ public void test0401() throws JavaModelException { @@ -2718,11 +2725,11 @@ public class ASTConverterTestAST3_2 extends ConverterTestSetup { checkSourceRange(type, "Class[][]", source); assertTrue("not an array type", type.isArrayType()); //$NON-NLS-1$ ArrayType arrayType = (ArrayType) type; - Type componentType = arrayType.getComponentType(); + Type componentType = componentType(arrayType); assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class[]", source); arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); + componentType = componentType(arrayType); assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class", source); } @@ -2744,15 +2751,15 @@ public class ASTConverterTestAST3_2 extends ConverterTestSetup { checkSourceRange(type, "Class[][][]", source); assertTrue("not an array type", type.isArrayType()); //$NON-NLS-1$ ArrayType arrayType = (ArrayType) type; - Type componentType = arrayType.getComponentType(); + Type componentType = componentType(arrayType); assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class[][]", source); arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); + componentType = componentType(arrayType); assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class[]", source); arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); + componentType = componentType(arrayType); assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class", source); } @@ -2774,11 +2781,11 @@ public class ASTConverterTestAST3_2 extends ConverterTestSetup { checkSourceRange(type, "Class[][]", source); assertTrue("not an array type", type.isArrayType()); //$NON-NLS-1$ ArrayType arrayType = (ArrayType) type; - Type componentType = arrayType.getComponentType(); + Type componentType = componentType(arrayType); assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class[]", source); arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); + componentType = componentType(arrayType); assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class", source); List fragments = fieldDeclaration.fragments(); @@ -2804,19 +2811,19 @@ public class ASTConverterTestAST3_2 extends ConverterTestSetup { checkSourceRange(type, "Class[][][][]", source); assertTrue("not an array type", type.isArrayType()); //$NON-NLS-1$ ArrayType arrayType = (ArrayType) type; - Type componentType = arrayType.getComponentType(); + Type componentType = componentType(arrayType); assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class[][][]", source); arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); + componentType = componentType(arrayType); assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class[][]", source); arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); + componentType = componentType(arrayType); assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class[]", source); arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); + componentType = componentType(arrayType); assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class", source); } @@ -2838,7 +2845,7 @@ public class ASTConverterTestAST3_2 extends ConverterTestSetup { checkSourceRange(type, "Class[]", source); assertTrue("not an array type", type.isArrayType()); //$NON-NLS-1$ ArrayType arrayType = (ArrayType) type; - Type componentType = arrayType.getComponentType(); + Type componentType = componentType(arrayType); assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class", source); } @@ -6549,7 +6556,7 @@ public class ASTConverterTestAST3_2 extends ConverterTestSetup { checkSourceRange(type, "boolean[]", contents); assertTrue("Not an array type", type.isArrayType()); ArrayType arrayType = (ArrayType) type; - Type componentType = arrayType.getComponentType(); + Type componentType = componentType(arrayType); assertTrue("Not a primitive type", componentType.isPrimitiveType()); PrimitiveType primitiveType = (PrimitiveType) componentType; assertEquals("Not boolean", PrimitiveType.BOOLEAN, primitiveType.getPrimitiveTypeCode()); @@ -9541,11 +9548,11 @@ public class ASTConverterTestAST3_2 extends ConverterTestSetup { ArrayCreation arrayCreation = (ArrayCreation) node; ArrayType arrayType = arrayCreation.getType(); checkSourceRange(arrayType, "String[0][b[10]][]", sourceUnit.getSource()); - node = arrayType.getComponentType(); + node = componentType(arrayType); assertEquals("Not an array type", ASTNode.ARRAY_TYPE, node.getNodeType()); arrayType = (ArrayType)node; checkSourceRange(arrayType, "String[0][b[10]]", sourceUnit.getSource()); - node = arrayType.getComponentType(); + node = componentType(arrayType); assertEquals("Not an array type", ASTNode.ARRAY_TYPE, node.getNodeType()); arrayType = (ArrayType)node; checkSourceRange(arrayType, "String[0]", sourceUnit.getSource()); diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterTestAST4_2.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterTestAST4_2.java index acf08ef71d..25f88a852b 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterTestAST4_2.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterTestAST4_2.java @@ -141,6 +141,13 @@ public class ASTConverterTestAST4_2 extends ConverterTestSetup { } /** + * @deprecated + */ + private Type componentType(ArrayType array) { + return array.getComponentType(); + } + + /** * http://dev.eclipse.org/bugs/show_bug.cgi?id=22560 */ public void test0401() throws JavaModelException { @@ -2716,11 +2723,11 @@ public class ASTConverterTestAST4_2 extends ConverterTestSetup { checkSourceRange(type, "Class[][]", source); assertTrue("not an array type", type.isArrayType()); //$NON-NLS-1$ ArrayType arrayType = (ArrayType) type; - Type componentType = arrayType.getComponentType(); + Type componentType = componentType(arrayType); assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class[]", source); arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); + componentType = componentType(arrayType); assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class", source); } @@ -2742,15 +2749,15 @@ public class ASTConverterTestAST4_2 extends ConverterTestSetup { checkSourceRange(type, "Class[][][]", source); assertTrue("not an array type", type.isArrayType()); //$NON-NLS-1$ ArrayType arrayType = (ArrayType) type; - Type componentType = arrayType.getComponentType(); + Type componentType = componentType(arrayType); assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class[][]", source); arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); + componentType = componentType(arrayType); assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class[]", source); arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); + componentType = componentType(arrayType); assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class", source); } @@ -2772,11 +2779,11 @@ public class ASTConverterTestAST4_2 extends ConverterTestSetup { checkSourceRange(type, "Class[][]", source); assertTrue("not an array type", type.isArrayType()); //$NON-NLS-1$ ArrayType arrayType = (ArrayType) type; - Type componentType = arrayType.getComponentType(); + Type componentType = componentType(arrayType); assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class[]", source); arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); + componentType = componentType(arrayType); assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class", source); List fragments = fieldDeclaration.fragments(); @@ -2802,19 +2809,19 @@ public class ASTConverterTestAST4_2 extends ConverterTestSetup { checkSourceRange(type, "Class[][][][]", source); assertTrue("not an array type", type.isArrayType()); //$NON-NLS-1$ ArrayType arrayType = (ArrayType) type; - Type componentType = arrayType.getComponentType(); + Type componentType = componentType(arrayType); assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class[][][]", source); arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); + componentType = componentType(arrayType); assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class[][]", source); arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); + componentType = componentType(arrayType); assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class[]", source); arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); + componentType = componentType(arrayType); assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class", source); } @@ -2836,7 +2843,7 @@ public class ASTConverterTestAST4_2 extends ConverterTestSetup { checkSourceRange(type, "Class[]", source); assertTrue("not an array type", type.isArrayType()); //$NON-NLS-1$ ArrayType arrayType = (ArrayType) type; - Type componentType = arrayType.getComponentType(); + Type componentType = componentType(arrayType); assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ checkSourceRange(componentType, "Class", source); } @@ -6547,7 +6554,7 @@ public class ASTConverterTestAST4_2 extends ConverterTestSetup { checkSourceRange(type, "boolean[]", contents); assertTrue("Not an array type", type.isArrayType()); ArrayType arrayType = (ArrayType) type; - Type componentType = arrayType.getComponentType(); + Type componentType = componentType(arrayType); assertTrue("Not a primitive type", componentType.isPrimitiveType()); PrimitiveType primitiveType = (PrimitiveType) componentType; assertEquals("Not boolean", PrimitiveType.BOOLEAN, primitiveType.getPrimitiveTypeCode()); @@ -9539,11 +9546,11 @@ public class ASTConverterTestAST4_2 extends ConverterTestSetup { ArrayCreation arrayCreation = (ArrayCreation) node; ArrayType arrayType = arrayCreation.getType(); checkSourceRange(arrayType, "String[0][b[10]][]", sourceUnit.getSource()); - node = arrayType.getComponentType(); + node = componentType(arrayType); assertEquals("Not an array type", ASTNode.ARRAY_TYPE, node.getNodeType()); arrayType = (ArrayType)node; checkSourceRange(arrayType, "String[0][b[10]]", sourceUnit.getSource()); - node = arrayType.getComponentType(); + node = componentType(arrayType); assertEquals("Not an array type", ASTNode.ARRAY_TYPE, node.getNodeType()); arrayType = (ArrayType)node; checkSourceRange(arrayType, "String[0]", sourceUnit.getSource()); diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterTestAST8_2.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterTestAST8_2.java index fb32e75115..2e74436cc0 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterTestAST8_2.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterTestAST8_2.java @@ -141,6 +141,13 @@ public class ASTConverterTestAST8_2 extends ConverterTestSetup { } /** + * @deprecated + */ + private Type componentType(ArrayType array) { + return array.getComponentType(); + } + + /** * http://dev.eclipse.org/bugs/show_bug.cgi?id=22560 */ public void test0401() throws JavaModelException { @@ -2725,13 +2732,18 @@ public class ASTConverterTestAST8_2 extends ConverterTestSetup { checkSourceRange(type, "Class[][]", source); assertTrue("not an array type", type.isArrayType()); //$NON-NLS-1$ ArrayType arrayType = (ArrayType) type; - Type componentType = arrayType.getComponentType(); - assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ - checkSourceRange(componentType, "Class[]", source); - arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); - assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ - checkSourceRange(componentType, "Class", source); + if (this.ast.apiLevel() < AST.JLS8) { + Type componentType = componentType(arrayType); + assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ + checkSourceRange(componentType, "Class[]", source); + arrayType = (ArrayType) componentType; + componentType = componentType(arrayType); + assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ + checkSourceRange(componentType, "Class", source); + } else { + Type elementType = arrayType.getElementType(); + checkSourceRange(elementType, "Class", source); + } } /** @@ -2751,17 +2763,22 @@ public class ASTConverterTestAST8_2 extends ConverterTestSetup { checkSourceRange(type, "Class[][][]", source); assertTrue("not an array type", type.isArrayType()); //$NON-NLS-1$ ArrayType arrayType = (ArrayType) type; - Type componentType = arrayType.getComponentType(); - assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ - checkSourceRange(componentType, "Class[][]", source); - arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); - assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ - checkSourceRange(componentType, "Class[]", source); - arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); - assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ - checkSourceRange(componentType, "Class", source); + if (this.ast.apiLevel() < AST.JLS8) { + Type componentType = componentType(arrayType); + assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ + checkSourceRange(componentType, "Class[][]", source); + arrayType = (ArrayType) componentType; + componentType = componentType(arrayType); + assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ + checkSourceRange(componentType, "Class[]", source); + arrayType = (ArrayType) componentType; + componentType = componentType(arrayType); + assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ + checkSourceRange(componentType, "Class", source); + } else { + Type elementType = arrayType.getElementType(); + checkSourceRange(elementType, "Class", source); + } } /** @@ -2781,13 +2798,18 @@ public class ASTConverterTestAST8_2 extends ConverterTestSetup { checkSourceRange(type, "Class[][]", source); assertTrue("not an array type", type.isArrayType()); //$NON-NLS-1$ ArrayType arrayType = (ArrayType) type; - Type componentType = arrayType.getComponentType(); - assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ - checkSourceRange(componentType, "Class[]", source); - arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); - assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ - checkSourceRange(componentType, "Class", source); + if (this.ast.apiLevel() < AST.JLS8) { + Type componentType = componentType(arrayType); + assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ + checkSourceRange(componentType, "Class[]", source); + arrayType = (ArrayType) componentType; + componentType = componentType(arrayType); + assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ + checkSourceRange(componentType, "Class", source); + } else { + Type elementType = arrayType.getElementType(); + checkSourceRange(elementType, "Class", source); + } List fragments = fieldDeclaration.fragments(); assertEquals("wrong size", 1, fragments.size()); VariableDeclarationFragment fragment = (VariableDeclarationFragment) fragments.get(0); @@ -2811,21 +2833,26 @@ public class ASTConverterTestAST8_2 extends ConverterTestSetup { checkSourceRange(type, "Class[][][][]", source); assertTrue("not an array type", type.isArrayType()); //$NON-NLS-1$ ArrayType arrayType = (ArrayType) type; - Type componentType = arrayType.getComponentType(); - assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ - checkSourceRange(componentType, "Class[][][]", source); - arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); - assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ - checkSourceRange(componentType, "Class[][]", source); - arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); - assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ - checkSourceRange(componentType, "Class[]", source); - arrayType = (ArrayType) componentType; - componentType = arrayType.getComponentType(); - assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ - checkSourceRange(componentType, "Class", source); + if (this.ast.apiLevel() < AST.JLS8) { + Type componentType = componentType(arrayType); + assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ + checkSourceRange(componentType, "Class[][][]", source); + arrayType = (ArrayType) componentType; + componentType = componentType(arrayType); + assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ + checkSourceRange(componentType, "Class[][]", source); + arrayType = (ArrayType) componentType; + componentType = componentType(arrayType); + assertTrue("not an array type", componentType.isArrayType()); //$NON-NLS-1$ + checkSourceRange(componentType, "Class[]", source); + arrayType = (ArrayType) componentType; + componentType = componentType(arrayType); + assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ + checkSourceRange(componentType, "Class", source); + } else { + Type elementType = arrayType.getElementType(); + checkSourceRange(elementType, "Class", source); + } } /** @@ -2845,9 +2872,9 @@ public class ASTConverterTestAST8_2 extends ConverterTestSetup { checkSourceRange(type, "Class[]", source); assertTrue("not an array type", type.isArrayType()); //$NON-NLS-1$ ArrayType arrayType = (ArrayType) type; - Type componentType = arrayType.getComponentType(); - assertTrue("is an array type", !componentType.isArrayType()); //$NON-NLS-1$ - checkSourceRange(componentType, "Class", source); + type = this.ast.apiLevel() < AST.JLS8 ? componentType(arrayType) : arrayType.getElementType(); + assertTrue("is an array type", !type.isArrayType()); //$NON-NLS-1$ + checkSourceRange(type, "Class", source); } /** @@ -6556,7 +6583,7 @@ public class ASTConverterTestAST8_2 extends ConverterTestSetup { checkSourceRange(type, "boolean[]", contents); assertTrue("Not an array type", type.isArrayType()); ArrayType arrayType = (ArrayType) type; - Type componentType = arrayType.getComponentType(); + Type componentType = componentType(arrayType); assertTrue("Not a primitive type", componentType.isPrimitiveType()); PrimitiveType primitiveType = (PrimitiveType) componentType; assertEquals("Not boolean", PrimitiveType.BOOLEAN, primitiveType.getPrimitiveTypeCode()); @@ -9548,14 +9575,19 @@ public class ASTConverterTestAST8_2 extends ConverterTestSetup { ArrayCreation arrayCreation = (ArrayCreation) node; ArrayType arrayType = arrayCreation.getType(); checkSourceRange(arrayType, "String[0][b[10]][]", sourceUnit.getSource()); - node = arrayType.getComponentType(); - assertEquals("Not an array type", ASTNode.ARRAY_TYPE, node.getNodeType()); - arrayType = (ArrayType)node; - checkSourceRange(arrayType, "String[0][b[10]]", sourceUnit.getSource()); - node = arrayType.getComponentType(); - assertEquals("Not an array type", ASTNode.ARRAY_TYPE, node.getNodeType()); - arrayType = (ArrayType)node; - checkSourceRange(arrayType, "String[0]", sourceUnit.getSource()); + if (this.ast.apiLevel() < AST.JLS8) { + node = componentType(arrayType); + assertEquals("Not an array type", ASTNode.ARRAY_TYPE, node.getNodeType()); + arrayType = (ArrayType)node; + checkSourceRange(arrayType, "String[0][b[10]]", sourceUnit.getSource()); + node = componentType(arrayType); + assertEquals("Not an array type", ASTNode.ARRAY_TYPE, node.getNodeType()); + arrayType = (ArrayType)node; + checkSourceRange(arrayType, "String[0]", sourceUnit.getSource()); + } else { + node = arrayType.getElementType(); + checkSourceRange(node, "String", sourceUnit.getSource()); + } } /** diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTStructuralPropertyTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTStructuralPropertyTest.java index 2bf4b8720c..b392811344 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTStructuralPropertyTest.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTStructuralPropertyTest.java @@ -172,7 +172,7 @@ public class ASTStructuralPropertyTest extends org.eclipse.jdt.core.tests.junit. break; case AST.JLS8 : assertEquals("Wrong number of visited node classes", 84, nodeClasses.size()); - assertEquals("Wrong number of visited properties", 105, visitedProperties.size()); + assertEquals("Wrong number of visited properties", 106, visitedProperties.size()); assertEquals("Wrong number of simple properties", 21, simpleProperties.size()); assertEquals("Wrong number of child properties", 118, childProperties.size()); assertEquals("Wrong number of child list properties", 66, childListProperties.size()); diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTTest.java index 6eb30a992b..df737c8b6f 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTTest.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTTest.java @@ -2057,6 +2057,18 @@ public class ASTTest extends org.eclipse.jdt.core.tests.junit.extension.TestCase assertTrue(PrimitiveType.toCode("not-a-type") == null); //$NON-NLS-1$ } + /** + * @deprecated + */ + Type getArrayComponentType(ArrayType array) { + return array.getComponentType(); + } + /** + * @deprecated + */ + void setArrayComponentType(ArrayType array, Type type) { + array.setComponentType(type); + } public void testArrayType() { SimpleName x1 = this.ast.newSimpleName("String"); //$NON-NLS-1$ SimpleType x2 = this.ast.newSimpleType(x1); @@ -2066,7 +2078,11 @@ public class ASTTest extends org.eclipse.jdt.core.tests.junit.extension.TestCase previousCount = this.ast.modificationCount(); assertTrue(x.getAST() == this.ast); assertTrue(x.getParent() == null); - assertTrue(x.getComponentType().getParent() == x); + if (this.ast.apiLevel() < AST.JLS8) { + assertTrue(getArrayComponentType(x).getParent() == x); + } else { + assertTrue(x.getElementType().getParent() == x); + } // make sure that reading did not change modification count assertTrue(this.ast.modificationCount() == previousCount); assertTrue(!x.isSimpleType()); @@ -2081,39 +2097,69 @@ public class ASTTest extends org.eclipse.jdt.core.tests.junit.extension.TestCase assertTrue(x.getDimensions() == 1); assertTrue(x.getElementType() == x2); - genericPropertyTest(x, new Property("ComponentType", true, Type.class) { //$NON-NLS-1$ - public ASTNode sample(AST targetAst, boolean parented) { - SimpleType result = targetAst.newSimpleType( - targetAst.newSimpleName("a")); //$NON-NLS-1$ - if (parented) { - targetAst.newArrayType(result); + if (this.ast.apiLevel() < AST.JLS8) { + genericPropertyTest(x, new Property("ComponentType", true, Type.class) { //$NON-NLS-1$ + public ASTNode sample(AST targetAst, boolean parented) { + SimpleType result = targetAst.newSimpleType( + targetAst.newSimpleName("a")); //$NON-NLS-1$ + if (parented) { + targetAst.newArrayType(result); + } + return result; } - return result; - } - public ASTNode wrap() { - ArrayType result = ASTTest.this.ast.newArrayType(x); - return result; - } - public void unwrap() { - ArrayType a = (ArrayType) x.getParent(); - a.setComponentType(ASTTest.this.ast.newPrimitiveType(PrimitiveType.INT)); - } - public ASTNode get() { - return x.getComponentType(); - } - public void set(ASTNode value) { - x.setComponentType((Type) value); - } - }); + public ASTNode wrap() { + ArrayType result = ASTTest.this.ast.newArrayType(x); + return result; + } + public void unwrap() { + ArrayType a = (ArrayType) x.getParent(); + setArrayComponentType(a, ASTTest.this.ast.newPrimitiveType(PrimitiveType.INT)); + } + public ASTNode get() { + return getArrayComponentType(x); + } + public void set(ASTNode value) { + setArrayComponentType(x, (Type) value); + } + }); + + setArrayComponentType(x, + this.ast.newArrayType(this.ast.newPrimitiveType(PrimitiveType.INT), 4)); + assertTrue(x.getDimensions() == 5); + assertTrue(x.getElementType().isPrimitiveType()); + final ArrayType x3 = this.ast.newArrayType(x, 2); + assertTrue(x3.getDimensions() == 7); + } else { + genericPropertyTest(x, new Property("ElementType", true, Type.class) { //$NON-NLS-1$ + public ASTNode sample(AST targetAst, boolean parented) { + SimpleType result = targetAst.newSimpleType( + targetAst.newSimpleName("a")); //$NON-NLS-1$ + if (parented) { + targetAst.newArrayType(result); + } + return result; + } + public ASTNode wrap() { + ArrayType result = ASTTest.this.ast.newArrayType(x, 5); + return result; + } + public void unwrap() { + ArrayType a = (ArrayType) x.getParent(); + a.setElementType(ASTTest.this.ast.newPrimitiveType(PrimitiveType.INT)); + } + public ASTNode get() { + return x.getElementType(); + } + public void set(ASTNode value) { + x.setElementType((Type) value); + } + }); - x.setComponentType( - this.ast.newArrayType(this.ast.newPrimitiveType(PrimitiveType.INT), 4)); + x.setElementType(this.ast.newPrimitiveType(PrimitiveType.INT)); + assertTrue(x.getDimensions() == 1); + assertTrue(x.getElementType().isPrimitiveType()); + } - assertTrue(x.getDimensions() == 5); - assertTrue(x.getElementType().isPrimitiveType()); - final ArrayType x3 = this.ast.newArrayType(x, 2); - assertTrue(x3.getDimensions() == 7); - try { this.ast.newArrayType(null, 2); } catch(IllegalArgumentException e) { diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTVisitorTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTVisitorTest.java index a94cb24b80..076d46ae89 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTVisitorTest.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTVisitorTest.java @@ -1157,7 +1157,8 @@ public class ASTVisitorTest extends org.eclipse.jdt.core.tests.junit.extension.T this.b.setLength(0); x1.accept(v1); String result = this.b.toString(); - assertTrue("[(tA[(tPcharchartP)]tA)]".equals(result)); //$NON-NLS-1$ + String expected = this.ast.apiLevel() < AST.JLS8 ? "[(tA[(tPcharchartP)]tA)]" : "[(tA[(tPcharchartP)][(@ED@ED)]tA)]"; + assertTrue(expected.equals(result)); //$NON-NLS-1$ } /** @deprecated using deprecated code */ @@ -1254,7 +1255,8 @@ public class ASTVisitorTest extends org.eclipse.jdt.core.tests.junit.extension.T this.b.setLength(0); x1.accept(v1); String result = this.b.toString(); - assertTrue(result.equals("[(eAC"+"[(tA"+this.T1S+"tA)]"+this.E1S+this.E2S+"[(eAIeAI)]eAC)]")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + String dim = this.ast.apiLevel() < AST.JLS8 ? "" : "[(@ED@ED)]"; + assertTrue(result.equals("[(eAC"+"[(tA"+this.T1S+ dim +"tA)]"+this.E1S+this.E2S+"[(eAIeAI)]eAC)]")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ } public void testArrayInitializer() { ArrayInitializer x1 = this.ast.newArrayInitializer(); diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/TypeAnnotationsConverterTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/TypeAnnotationsConverterTest.java index 1ca0cb33b2..c7efeb04bd 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/TypeAnnotationsConverterTest.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/TypeAnnotationsConverterTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2012 IBM Corporation and others. + * Copyright (c) 2000, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -162,7 +162,7 @@ public class TypeAnnotationsConverterTest extends ConverterTestSetup { "class Z<Q> {\n" + " }\n" + " }\n" + - " Object o=(@Marker X<@Marker String>.@Marker Y<@Marker Integer>.@Marker Z<@Marker Object>@Marker [][]@Marker [][])null;\n" + + " Object o=(@Marker X<@Marker String>.@Marker Y<@Marker Integer>.@Marker Z<@Marker Object> @Marker [] [] @Marker [] [])null;\n" + " @java.lang.annotation.Target(java.lang.annotation.ElementType.TYPE_USE) @interface Marker {}\n" + "}\n"; assertASTNodeEquals(expectedOutput, node); @@ -191,7 +191,7 @@ public class TypeAnnotationsConverterTest extends ConverterTestSetup { "class Z<Q> {\n" + " }\n" + " }\n" + - " Object o=(@Marker X<@Marker String>.@Marker Y<@Marker Integer>.@Marker Z<@Marker Object>[]@Marker [][]@Marker [])null;\n" + + " Object o=(@Marker X<@Marker String>.@Marker Y<@Marker Integer>.@Marker Z<@Marker Object> [] @Marker [] [] @Marker [])null;\n" + " @java.lang.annotation.Target(java.lang.annotation.ElementType.TYPE_USE) @interface Marker {}\n" + "}\n"; assertASTNodeEquals(expectedOutput, node); @@ -220,7 +220,7 @@ public class TypeAnnotationsConverterTest extends ConverterTestSetup { "class Z<Q> {\n" + " }\n" + " }\n" + - " int[][][][] o=(@One int[]@Two [][]@Three [])null;\n" + + " int [] [] [] [] o=(@One int [] @Two [] [] @Three [])null;\n" + " @java.lang.annotation.Target(java.lang.annotation.ElementType.TYPE_USE) @interface Marker {}\n" + "}\n"; assertASTNodeEquals(expectedOutput, node); @@ -249,7 +249,7 @@ public class TypeAnnotationsConverterTest extends ConverterTestSetup { "class Z<Q> {\n" + " }\n" + " }\n" + - " String[][][][] o=(@One String[]@Two [][]@Three [])null;\n" + + " String [] [] [] [] o=(@One String [] @Two [] [] @Three [])null;\n" + " @java.lang.annotation.Target(java.lang.annotation.ElementType.TYPE_USE) @interface Marker {}\n" + "}\n"; assertASTNodeEquals(expectedOutput, node); @@ -278,7 +278,7 @@ public class TypeAnnotationsConverterTest extends ConverterTestSetup { "class Z<Q> {\n" + " }\n" + " }\n" + - " Object o=(@One X<String>[]@Two [][]@Three [])null;\n" + + " Object o=(@One X<String> [] @Two [] [] @Three [])null;\n" + " @java.lang.annotation.Target(java.lang.annotation.ElementType.TYPE_USE) @interface Marker {}\n" + "}\n"; assertASTNodeEquals(expectedOutput, node); diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingExpressionsTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingExpressionsTest.java index 0aff4f8776..835fb8b942 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingExpressionsTest.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingExpressionsTest.java @@ -263,23 +263,25 @@ public class ASTRewritingExpressionsTest extends ASTRewritingTest { memberValuePair.setName(ast.newSimpleName("v")); memberValuePair.setValue(ast.newNumberLiteral("99")); annotationC.values().add(memberValuePair); - arrayType.annotations().add(annotationC); + ExtraDimension dim0 = arrayType.getDimensionAt(0); + dim0.annotations().add(annotationC); - arrayType= ast.newArrayType(arrayType); SingleMemberAnnotation annotationB= ast.newSingleMemberAnnotation(); annotationB.setTypeName(ast.newSimpleName("B")); annotationB.setValue(ast.newNumberLiteral("0")); - arrayType.annotations().add(annotationB); + ExtraDimension dim1 = ast.newExtraDimension(); + dim1.annotations().add(annotationB); + arrayType.dimensions().add(0, dim1); - arrayType= ast.newArrayType(arrayType); MarkerAnnotation annotationA= ast.newMarkerAnnotation(); annotationA.setTypeName(ast.newSimpleName("A")); - arrayType.annotations().add(annotationA); - - arrayCreation.setType(arrayType); + ExtraDimension dim2 = ast.newExtraDimension(); + dim2.annotations().add(annotationA); + arrayType.dimensions().add(0, dim2); arrayCreation.dimensions().add(ast.newNumberLiteral("1")); arrayCreation.dimensions().add(ast.newNumberLiteral("2")); + arrayCreation.setType(arrayType); rewrite.getListRewrite(invocation, MethodInvocation.ARGUMENTS_PROPERTY).insertLast(arrayCreation, null); } diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingMethodDeclTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingMethodDeclTest.java index ac133dd7fb..55bd87404d 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingMethodDeclTest.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingMethodDeclTest.java @@ -111,7 +111,7 @@ public class ASTRewritingMethodDeclTest extends ASTRewritingTest { listRewrite.remove(extraDimension, null); } for (int i= 0; i < extraDimensions; i++) { - listRewrite.insertLast(methodDecl.getAST().newExtraDimension(), null); + listRewrite.insertFirst(methodDecl.getAST().newExtraDimension(), null); } } } diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingStatementsTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingStatementsTest.java index 082fa57a5c..719b026242 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingStatementsTest.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingStatementsTest.java @@ -5942,24 +5942,25 @@ public class ASTRewritingStatementsTest extends ASTRewritingTest { ListRewrite listRewrite= rewrite.getListRewrite(fragment, VariableDeclarationFragment.EXTRA_DIMENSIONS2_PROPERTY); ExtraDimension dim= ast.newExtraDimension(); MarkerAnnotation markerAnnotation; - listRewrite.insertAt(dim, 0, null); + listRewrite.insertFirst(dim, null); ArrayType creationType = creation.getType(); - ArrayType newArrayType = ast.newArrayType((ArrayType) ASTNode.copySubtree(ast, creationType)); - newArrayType = ast.newArrayType(newArrayType); - rewrite.set(creation, ArrayCreation.TYPE_PROPERTY, newArrayType, null); - - listRewrite= rewrite.getListRewrite(newArrayType, ArrayType.ANNOTATIONS_PROPERTY); + ArrayType newArrayType = (ArrayType) ASTNode.copySubtree(ast, creationType); + newArrayType.dimensions().add(ast.newExtraDimension()); + + ExtraDimension dim0 = ast.newExtraDimension(); markerAnnotation= ast.newMarkerAnnotation(); markerAnnotation.setTypeName(ast.newSimpleName("Annot3")); - listRewrite.insertAt(markerAnnotation, 0, null); - newArrayType.annotations().add(markerAnnotation); + dim0.annotations().add(markerAnnotation); markerAnnotation= ast.newMarkerAnnotation(); markerAnnotation.setTypeName(ast.newSimpleName("Annot2")); - listRewrite.insertAt(markerAnnotation, 1, null); + dim0.annotations().add(markerAnnotation); markerAnnotation= ast.newMarkerAnnotation(); markerAnnotation.setTypeName(ast.newSimpleName("Annot1")); - listRewrite.insertAt(markerAnnotation, 2, null); + dim0.annotations().add(markerAnnotation); + newArrayType.dimensions().add(dim0); + rewrite.set(creation, ArrayCreation.TYPE_PROPERTY, newArrayType, null); + } { statement = (VariableDeclarationStatement) statements.get(1); @@ -5978,43 +5979,44 @@ public class ASTRewritingStatementsTest extends ASTRewritingTest { Expression exp = (Expression) expressions.get(1); listRewrite.remove(exp, null); - listRewrite = rewrite.getListRewrite(creationType, ArrayType.ANNOTATIONS_PROPERTY); - MarkerAnnotation annotation = (MarkerAnnotation) creationType.annotations().get(0); + ExtraDimension dim = creationType.getDimensionAt(2); + listRewrite = rewrite.getListRewrite(dim, ExtraDimension.ANNOTATIONS_PROPERTY); + MarkerAnnotation annotation = (MarkerAnnotation) dim.annotations().get(0); listRewrite.remove(annotation, null); - creationType = (ArrayType) creationType.getComponentType(); - listRewrite = rewrite.getListRewrite(creationType, ArrayType.ANNOTATIONS_PROPERTY); - annotation = (MarkerAnnotation) creationType.annotations().get(1); + dim = creationType.getDimensionAt(1); + listRewrite = rewrite.getListRewrite(dim, ExtraDimension.ANNOTATIONS_PROPERTY); + annotation = (MarkerAnnotation) dim.annotations().get(1); listRewrite.remove(annotation, null); - creationType = (ArrayType) creationType.getComponentType(); - listRewrite = rewrite.getListRewrite(creationType, ArrayType.ANNOTATIONS_PROPERTY); - annotation = (MarkerAnnotation) creationType.annotations().get(1); + dim = creationType.getDimensionAt(0); + listRewrite = rewrite.getListRewrite(dim, ExtraDimension.ANNOTATIONS_PROPERTY); + annotation = (MarkerAnnotation) dim.annotations().get(1); listRewrite.remove(annotation, null); fragment = (VariableDeclarationFragment) fragments.get(1); creation = (ArrayCreation) fragment.getInitializer(); creationType = creation.getType(); - - listRewrite = rewrite.getListRewrite(creationType, ArrayType.ANNOTATIONS_PROPERTY); - annotation = (MarkerAnnotation) creationType.annotations().get(0); + dim = creationType.getDimensionAt(2); + listRewrite = rewrite.getListRewrite(dim, ExtraDimension.ANNOTATIONS_PROPERTY); + annotation = (MarkerAnnotation) dim.annotations().get(1); listRewrite.remove(annotation, null); - annotation = (MarkerAnnotation) creationType.annotations().get(1); + annotation = (MarkerAnnotation) dim.annotations().get(0); listRewrite.remove(annotation, null); - creationType = (ArrayType) creationType.getComponentType(); - listRewrite = rewrite.getListRewrite(creationType, ArrayType.ANNOTATIONS_PROPERTY); - annotation = (MarkerAnnotation) creationType.annotations().get(0); + dim = creationType.getDimensionAt(1); + listRewrite = rewrite.getListRewrite(dim, ExtraDimension.ANNOTATIONS_PROPERTY); + annotation = (MarkerAnnotation) dim.annotations().get(1); listRewrite.remove(annotation, null); - annotation = (MarkerAnnotation) creationType.annotations().get(1); + annotation = (MarkerAnnotation) dim.annotations().get(0); listRewrite.remove(annotation, null); - creationType = (ArrayType) creationType.getComponentType(); - listRewrite = rewrite.getListRewrite(creationType, ArrayType.ANNOTATIONS_PROPERTY); - annotation = (MarkerAnnotation) creationType.annotations().get(0); + dim = creationType.getDimensionAt(0); + listRewrite = rewrite.getListRewrite(dim, ExtraDimension.ANNOTATIONS_PROPERTY); + annotation = (MarkerAnnotation) dim.annotations().get(1); listRewrite.remove(annotation, null); - annotation = (MarkerAnnotation) creationType.annotations().get(1); + annotation = (MarkerAnnotation) dim.annotations().get(0); listRewrite.remove(annotation, null); expressions = creation.dimensions(); @@ -6035,8 +6037,8 @@ public class ASTRewritingStatementsTest extends ASTRewritingTest { buf.append("import java.lang.annotation.ElementType;\n"); buf.append("public class E {\n"); buf.append(" public void foo() {\n"); - buf.append(" int [] i [] @Annot1 @Annot2 [] @Annot1 @Annot3 [] = new int @Annot1 @Annot2 [2] @Annot2 @Annot3 [size()] @Annot2 @Annot1 [][] @Annot3 @Annot2 @Annot1[];\n"); - buf.append(" int [] j [][] = new int @Annot1 [2] @Annot2 [] @Annot3 [], k [][] = new int[2][10][size()];\n"); + buf.append(" int [] i [] @Annot1 @Annot2 [] @Annot1 @Annot3 [] = new int @Annot1 @Annot2 [2] @Annot2 @Annot3 [size()] @Annot2 @Annot1 [][]@Annot3 @Annot2 @Annot1 [];\n"); + buf.append(" int [] j [][] = new int @Annot2 [2] @Annot2 [] @Annot1 [], k [][] = new int [2] [10] [size()];\n"); buf.append(" }\n"); buf.append(" public int size() { return 2; }\n"); buf.append("}\n"); @@ -6086,10 +6088,10 @@ public class ASTRewritingStatementsTest extends ASTRewritingTest { ArrayCreation creation = (ArrayCreation) fragment.getInitializer(); ArrayType arrayType = creation.getType(); - arrayType = (ArrayType) arrayType.getComponentType(); - ListRewrite listRewrite= rewrite.getListRewrite(arrayType, ArrayType.ANNOTATIONS_PROPERTY); - listRewrite.remove((ASTNode)arrayType.annotations().get(0), null); - listRewrite.remove((ASTNode)arrayType.annotations().get(1), null); + ExtraDimension dim = arrayType.getDimensionAt(1); + ListRewrite listRewrite= rewrite.getListRewrite(dim, ExtraDimension.ANNOTATIONS_PROPERTY); + listRewrite.remove((ASTNode)dim.annotations().get(0), null); + listRewrite.remove((ASTNode)dim.annotations().get(1), null); rewrite.set(creation, ArrayCreation.TYPE_PROPERTY, arrayType, null); } { @@ -6101,16 +6103,12 @@ public class ASTRewritingStatementsTest extends ASTRewritingTest { assertEquals("Incorrect type", ASTNode.ARRAY_TYPE, type.getNodeType()); ArrayCreation creation = (ArrayCreation) fragment.getInitializer(); ArrayType creationType = creation.getType(); - - ArrayType newArrayType = (ArrayType) creationType.getComponentType(); - rewrite.set(creation, ArrayCreation.TYPE_PROPERTY, newArrayType, null); - + rewrite.remove(creationType.getDimensionAt(0), null); fragment = (VariableDeclarationFragment) fragments.get(1); creation = (ArrayCreation) fragment.getInitializer(); creationType = creation.getType(); - newArrayType = (ArrayType) creationType.getComponentType(); - rewrite.set(creation, ArrayCreation.TYPE_PROPERTY, newArrayType, null); + rewrite.remove(creationType.getDimensionAt(0), null); } // Get new code String preview= evaluateRewrite(cu, rewrite); @@ -6120,7 +6118,7 @@ public class ASTRewritingStatementsTest extends ASTRewritingTest { buf.append("import java.lang.annotation.ElementType;\n"); buf.append("public class E {\n"); buf.append(" public void foo() {\n"); - buf.append(" int [] i [][] = new int @Annot1 @Annot2 [2][size(new int[][]{})];\n"); + buf.append(" int [] i [][] = new int @Annot1 @Annot2 [2] [size(new int[][]{})] [];\n"); buf.append(" int [] j [][] = new int @Annot1 @Annot2 [2] @Annot2 @Annot3 [size(new int[]{})], k [][] = new int @Annot1 @Annot2 [2] @Annot2 @Annot3 [10];\n"); buf.append(" }\n"); buf.append(" public int size(Object obj) { return 2; }\n"); diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AST.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AST.java index 78f89cfcc2..70e3f7c3dd 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AST.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AST.java @@ -999,6 +999,14 @@ public final class AST { } /** + * A local method to workaround calling deprecated method in array type. + * @deprecated + */ + private void setArrayComponentType(ArrayType arrayType, Type type) { + arrayType.setComponentType(type); + } + + /** * Creates and returns a new unparented annotation type declaration * node for an unspecified, but legal, name; no modifiers; no javadoc; * and an empty list of member declarations. @@ -1109,19 +1117,31 @@ public final class AST { /** * Creates and returns a new unparented array type node with the given - * component type, which may be another array type. + * component type, which may be another array type for levels less than JLS8. + * For JLS8 and above this type has to be an annotatable type. * - * @param componentType the component type (possibly another array type) + * @param type the component type (possibly another array type) for level less than JLS8, + * a <code>AnnotatableType</code> for JLS8 and above * @return a new unparented array type node * @exception IllegalArgumentException if: * <ul> * <li>the node belongs to a different AST</li> * <li>the node already has a parent</li> + * <li> level is greater than or equal to JLS8 and type not an array type</li> * </ul> */ - public ArrayType newArrayType(Type componentType) { - ArrayType result = new ArrayType(this); - result.setComponentType(componentType); + public ArrayType newArrayType(Type type) { + ArrayType result; + if (this.apiLevel < AST.JLS8) { + result = new ArrayType(this); + setArrayComponentType(result, type); + return result; + } + if (type.isArrayType()) { + throw new IllegalArgumentException(); + } + result = new ArrayType(this); + result.setElementType(type); return result; } @@ -1133,7 +1153,7 @@ public final class AST { * element type of the result will not be the same as what was passed in. * </p> * - * @param elementType the element type (can be an array type) + * @param elementType the element type (can be an array type for JLS8. For level JLS8 and above this should be an <code>AnnotatableType</code>) * @param dimensions the number of dimensions, a positive number * @return a new unparented array type node * @exception IllegalArgumentException if: @@ -1143,6 +1163,7 @@ public final class AST { * <li>the element type is null</li> * <li>the number of dimensions is lower than 1</li> * <li>the number of dimensions is greater than 1000</li> + * <li>for levels from JLS8 and later, if the element type is not an array type </li> * </ul> */ public ArrayType newArrayType(Type elementType, int dimensions) { @@ -1153,10 +1174,24 @@ public final class AST { // we would blow our stacks anyway with a 1000-D array throw new IllegalArgumentException(); } - ArrayType result = new ArrayType(this); - result.setComponentType(elementType); - for (int i = 2; i <= dimensions; i++) { - result = newArrayType(result); + ArrayType result; + if (this.apiLevel < AST.JLS8) { + result = new ArrayType(this); + setArrayComponentType(result, elementType); + for (int i = 2; i <= dimensions; i++) { + result = newArrayType(result); + } + return result; + } + //level >= JLS8 + if (elementType.isArrayType()) { + throw new IllegalArgumentException(); + } + result = new ArrayType(this); + result.setElementType(elementType); + // index starting from 1 since there is a dimension already available by default. + for (int i = 1; i < dimensions; ++i) { + result.dimensions().add(new ExtraDimension(this)); } return result; diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java index 79f9331b11..990729ed77 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java @@ -351,6 +351,13 @@ class ASTConverter { } } } + + private void checkAndSetMalformed(ASTNode oldASTNode, ASTNode newASTNode) { + if ((oldASTNode.getFlags() & ASTNode.MALFORMED) != 0) { + newASTNode.setFlags(newASTNode.getFlags() | ASTNode.MALFORMED); + } + } + /** * Internal access method to SingleVariableDeclaration#setExtraDimensions() for avoiding deprecated warnings * @@ -465,15 +472,27 @@ class ASTConverter { protected void completeRecord(ArrayType arrayType, org.eclipse.jdt.internal.compiler.ast.ASTNode astNode) { ArrayType array = arrayType; + this.recordNodes(arrayType, astNode); + if (this.ast.apiLevel() >= AST.JLS8) { + this.recordNodes(arrayType.getElementType(), astNode); + return; + } int dimensions = array.getDimensions(); for (int i = 0; i < dimensions; i++) { - Type componentType = array.getComponentType(); + Type componentType = componentType(array); this.recordNodes(componentType, astNode); if (componentType.isArrayType()) { array = (ArrayType) componentType; } } } + + /** + * @deprecated + */ + private Type componentType(ArrayType array) { + return array.getComponentType(); + } public ASTNode convert(boolean isInterface, org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration methodDeclaration) { checkCanceled(); @@ -909,12 +928,18 @@ class ASTConverter { if (isVarArgs) { setTypeForSingleVariableDeclaration(variableDecl, type, extraDimensions + 1); // https://bugs.eclipse.org/bugs/show_bug.cgi?id=391898 - if (type.isAnnotatable()) { - AnnotatableType annotatableType = (AnnotatableType) type; - if (this.ast.apiLevel() >= AST.JLS8 && !annotatableType.annotations().isEmpty()) { - Iterator annotations = annotatableType.annotations().iterator(); - while (annotations.hasNext()) { - Annotation annotation = (Annotation) annotations.next(); + if (this.ast.apiLevel() >= AST.JLS8) { + List annotations = null; + if (type.isAnnotatable()) { + annotations = ((AnnotatableType) type).annotations(); + } else if (type.isArrayType()) { + ArrayType arrayType = (ArrayType) type; + annotations = arrayType.dimensions().isEmpty() ? null : ((arrayType.getDimensionAt(0)).annotations()); + } + if (annotations != null) { + Iterator iter = annotations.iterator(); + while (iter.hasNext()) { + Annotation annotation = (Annotation) iter.next(); annotation.setParent(null, null); variableDecl.varargsAnnotations().add(annotation); } @@ -981,27 +1006,19 @@ class ASTConverter { ArrayType arrayType = null; if (type.isArrayType()) { arrayType = (ArrayType) type; - } else { - arrayType = this.ast.newArrayType(type, dimensionsLength); - if (this.resolveBindings) { - completeRecord(arrayType, expression); - } - int start = type.getStartPosition(); - int end = type.getStartPosition() + type.getLength(); - int previousSearchStart = end - 1; - ArrayType componentType = (ArrayType) type.getParent(); - for (int i = 0; i < dimensionsLength; i++) { - previousSearchStart = retrieveRightBracketPosition(previousSearchStart + 1, this.compilationUnitSourceLength); - componentType.setSourceRange(start, previousSearchStart - start + 1); - componentType = (ArrayType) componentType.getParent(); + if (expression.annotationsOnDimensions != null) { + if (this.ast.apiLevel() < AST.JLS8) { + arrayType.setFlags(arrayType.getFlags() | ASTNode.MALFORMED); + } else { + setArrayTypeAnnotationsAndSource(arrayType, expression.annotationsOnDimensions); + } } - } - if (expression.annotationsOnDimensions != null) { - annotateType(arrayType, expression.annotationsOnDimensions); + } else { + arrayType = convertToArray(type, type.getStartPosition(), -1, dimensionsLength, expression.annotationsOnDimensions); } arrayCreation.setType(arrayType); if (this.resolveBindings) { - recordNodes(arrayType, expression); + completeRecord(arrayType, expression); } if (expression.initializer != null) { arrayCreation.setInitializer(convert(expression.initializer)); @@ -3121,6 +3138,31 @@ class ASTConverter { return packageDeclaration; } + private ArrayType convertToArray(Type subType, int sourceStart, int length, int noOfDimensions, + org.eclipse.jdt.internal.compiler.ast.Annotation[][] annotationsOnDims) { + ArrayType arrayType = this.ast.newArrayType(subType, noOfDimensions); + if (length > 0) arrayType.setSourceRange(sourceStart, length); + if (this.ast.apiLevel() < AST.JLS8) { + if (annotationsOnDims != null) { + arrayType.setFlags(arrayType.getFlags() | ASTNode.MALFORMED); + } + ArrayType subarrayType = arrayType; + int index = noOfDimensions - 1; + int arrayEnd = retrieveProperRightBracketPosition(noOfDimensions, sourceStart); + while (index > 0) { + subarrayType = (ArrayType) componentType(subarrayType); + int end = retrieveProperRightBracketPosition(index, sourceStart); + subarrayType.setSourceRange(sourceStart, end - sourceStart + 1); + index--; + } + if (length <= 0) arrayType.setSourceRange(sourceStart, arrayEnd - sourceStart + 1); + return arrayType; + } + + setArrayTypeAnnotationsAndSource(arrayType, annotationsOnDims); + return arrayType; + } + private EnumDeclaration convertToEnumDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration typeDeclaration) { checkCanceled(); // enum declaration cannot be built if the source is not >= 1.5, since enum is then seen as an identifier @@ -3351,6 +3393,37 @@ class ASTConverter { } } + private void setArrayTypeAnnotations(ExtraDimension currentDimension, + org.eclipse.jdt.internal.compiler.ast.Annotation[][] annotationsOnDims, int index) { + if (annotationsOnDims == null) return; + org.eclipse.jdt.internal.compiler.ast.Annotation[] annotation = annotationsOnDims[index]; + if (annotation != null) { + for (int j = 0; j < annotation.length; j++) { + Annotation annot = convert(annotation[j]); + currentDimension.annotations().add(annot); + } + } + } + + private void setArrayTypeAnnotationsAndSource(ArrayType arrayType, + org.eclipse.jdt.internal.compiler.ast.Annotation[][] annotationsOnDims) { + List dimensions = arrayType.dimensions(); + Type elemType = arrayType.getElementType(); + int start = elemType.getStartPosition(); + int endElement = start + elemType.getLength(); + int length = arrayType.getLength(); + int end = (length <= 0) ? retrieveProperRightBracketPosition(dimensions.size(), endElement) : start + length - 1; + arrayType.setSourceRange(start, end - start + 1); + + start = endElement; + for (int i = 0; i < dimensions.size(); i++) { + ExtraDimension currentDimension = (ExtraDimension) dimensions.get(i); + setArrayTypeAnnotations(currentDimension, annotationsOnDims, i); + retrieveDimensionAndSetPositions(start, end, currentDimension); + start = currentDimension.getStartPosition() + currentDimension.getLength(); + } + } + protected VariableDeclarationStatement convertToVariableDeclarationStatement(org.eclipse.jdt.internal.compiler.ast.LocalDeclaration localDeclaration) { final VariableDeclarationFragment variableDeclarationFragment = convertToVariableDeclarationFragment(localDeclaration); final VariableDeclarationStatement variableDeclarationStatement = new VariableDeclarationStatement(this.ast); @@ -3375,6 +3448,9 @@ class ASTConverter { type.setFlags(type.getFlags() | ASTNode.MALFORMED); break; default: + if (annotations == null) break; + int start = type.getStartPosition(); + int length = type.getLength(); int annotationsLength = annotations.length; for (int i = 0; i < annotationsLength; i++) { org.eclipse.jdt.internal.compiler.ast.Annotation typeAnnotation = annotations[i]; @@ -3383,20 +3459,14 @@ class ASTConverter { type.annotations().add(annotation); } } + int annotationsStart; + if (annotations[0] != null && (annotationsStart = annotations[0].sourceStart) < start && annotationsStart > 0) { + length += start - annotationsStart; + start = annotationsStart; + } + type.setSourceRange(start, length); } } - private void annotateType(Type type, org.eclipse.jdt.internal.compiler.ast.Annotation[][] annotations) { - int level = annotations.length - 1; - while(type.isArrayType()) { - ArrayType arrayType = (ArrayType) type; - org.eclipse.jdt.internal.compiler.ast.Annotation[] typeAnnotations = annotations[level--]; - if (typeAnnotations != null) { - annotateType(arrayType, typeAnnotations); - } - type = arrayType.getComponentType(); - } - } - private void annotateTypeParameter(TypeParameter typeParameter, org.eclipse.jdt.internal.compiler.ast.Annotation[] annotations) { switch(this.ast.apiLevel) { case AST.JLS2_INTERNAL : @@ -3544,23 +3614,7 @@ class ASTConverter { } } if (dimensions != 0) { - org.eclipse.jdt.internal.compiler.ast.Annotation[][] annotationsOnDimensions = typeReference.getAnnotationsOnDimensions(); - type = this.ast.newArrayType(type, dimensions); - type.setSourceRange(sourceStart, length); - ArrayType subarrayType = (ArrayType) type; - int index = dimensions - 1; - while (index > 0) { - if (annotationsOnDimensions != null && (annotations = annotationsOnDimensions[index]) != null) { - annotateType(subarrayType, annotations); - } - subarrayType = (ArrayType) subarrayType.getComponentType(); - int end = retrieveProperRightBracketPosition(index, sourceStart); - subarrayType.setSourceRange(sourceStart, end - sourceStart + 1); - index--; - } - if (annotationsOnDimensions != null && (annotations = annotationsOnDimensions[0]) != null) { - annotateType(subarrayType, annotations); - } + type = convertToArray(type, sourceStart, length, dimensions, typeReference.getAnnotationsOnDimensions()); if (this.resolveBindings) { // store keys for inner types completeRecord((ArrayType) type, typeReference); @@ -3812,31 +3866,10 @@ class ASTConverter { length = typeReference.sourceEnd - sourceStart + 1; if (dimensions != 0) { - org.eclipse.jdt.internal.compiler.ast.Annotation[][] annotationsOnDimensions = typeReference.getAnnotationsOnDimensions(); - type = this.ast.newArrayType(type, dimensions); + type = convertToArray(type, sourceStart, -1, dimensions, typeReference.getAnnotationsOnDimensions()); if (this.resolveBindings) { completeRecord((ArrayType) type, typeReference); } - int end = retrieveEndOfDimensionsPosition(sourceStart+length, this.compilationUnitSourceLength); - if (end != -1) { - type.setSourceRange(sourceStart, end - sourceStart + 1); - } else { - type.setSourceRange(sourceStart, length); - } - ArrayType subarrayType = (ArrayType) type; - int index = dimensions - 1; - while (index > 0) { - if (annotationsOnDimensions != null && (annotations = annotationsOnDimensions[index]) != null) { - annotateType(subarrayType, annotations); - } - subarrayType = (ArrayType) subarrayType.getComponentType(); - end = retrieveProperRightBracketPosition(index, sourceStart); - subarrayType.setSourceRange(sourceStart, end - sourceStart + 1); - index--; - } - if (annotationsOnDimensions != null && (annotations = annotationsOnDimensions[0]) != null) { - annotateType(subarrayType, annotations); - } } } if (this.resolveBindings) { @@ -3896,16 +3929,11 @@ class ASTConverter { private void setSourceRangeAnnotationsAndRecordNodes(TypeReference typeReference, AnnotatableType annotatableType, org.eclipse.jdt.internal.compiler.ast.Annotation[][] typeAnnotations, int index, int start, int end) { org.eclipse.jdt.internal.compiler.ast.Annotation[] annotations; - int annotationsStart = start; int length = end - start + 1; + annotatableType.setSourceRange(start, length); if (typeAnnotations != null && (annotations = typeAnnotations[index]) != null) { annotateType(annotatableType, annotations); - if (annotations[0] != null && (annotationsStart = annotations[0].sourceStart) < start) { - length += annotationsStart > 0 ? start - annotationsStart : 0; - start = annotationsStart; - } } - annotatableType.setSourceRange(start, length); if (this.resolveBindings) { recordNodes(annotatableType, typeReference); } @@ -4603,38 +4631,6 @@ class ASTConverter { } /** - * This method is used to retrieve the ending position for a type declaration when the dimension is right after the type - * name. - * For example: - * int[] i; => return 5, but int i[] => return -1; - * @return int the dimension found - */ - protected int retrieveEndOfDimensionsPosition(int start, int end) { - this.scanner.resetTo(start, end); - int foundPosition = -1; - try { - int token; - while ((token = this.scanner.getNextToken()) != TerminalTokens.TokenNameEOF) { - switch(token) { - case TerminalTokens.TokenNameLBRACKET: - case TerminalTokens.TokenNameCOMMENT_BLOCK: - case TerminalTokens.TokenNameCOMMENT_JAVADOC: - case TerminalTokens.TokenNameCOMMENT_LINE: - break; - case TerminalTokens.TokenNameRBRACKET://166 - foundPosition = this.scanner.currentPosition - 1; - break; - default: - return foundPosition; - } - } - } catch(InvalidInputException e) { - // ignore - } - return foundPosition; - } - - /** * This method is used to retrieve the start and end position of a name or primitive type token. * * @return int[] a single dimensional array, with two elements, for the start and end positions of the name respectively @@ -4644,8 +4640,15 @@ class ASTConverter { boolean isAnnotation = false; try { int token; + int count = 0; while ((token = this.scanner.getNextToken()) != TerminalTokens.TokenNameEOF) { switch(token) { + case TerminalTokens.TokenNameLPAREN: + ++count; + break; + case TerminalTokens.TokenNameRPAREN: + --count; + break; case TerminalTokens.TokenNameAT: isAnnotation = true; break; @@ -4663,6 +4666,7 @@ class ASTConverter { case TerminalTokens.TokenNamelong: case TerminalTokens.TokenNameshort: case TerminalTokens.TokenNameboolean: + if (count > 0) break; return new int[]{this.scanner.startPosition, this.scanner.currentPosition - 1}; } } @@ -4712,9 +4716,17 @@ class ASTConverter { this.scanner.resetTo(start, end); int dimensions = 0; try { - int token; - boolean isAnnotation = false; + int token, lParenCount = 0; + boolean isAnnotation = false, foundAnnotation = false; while ((token = this.scanner.getNextToken()) != TerminalTokens.TokenNameEOF) { + if (foundAnnotation) { + if (token == TerminalTokens.TokenNameLPAREN) ++lParenCount; + else if (token == TerminalTokens.TokenNameRPAREN) { + --lParenCount; + continue; + } + if (lParenCount > 0) continue; + } switch(token) { case TerminalTokens.TokenNameLBRACKET: case TerminalTokens.TokenNameCOMMENT_BLOCK: @@ -4724,6 +4736,7 @@ class ASTConverter { break; case TerminalTokens.TokenNameAT: isAnnotation = true; + foundAnnotation = true; /* check for params */ break; case TerminalTokens.TokenNameIdentifier: if (!isAnnotation) { @@ -4748,22 +4761,36 @@ class ASTConverter { protected void retrieveDimensionAndSetPositions(int start, int end, ExtraDimension dim) { this.scanner.resetTo(start, end); int token; + int count = 0, lParenCount = 0; boolean startSet = false; try { while((token = this.scanner.getNextToken()) != TerminalTokens.TokenNameEOF) { - switch(token) { - case TerminalTokens.TokenNameWHITESPACE: - break; - case TerminalTokens.TokenNameRBRACKET: - int endDim = this.scanner.currentPosition - 1; - dim.setSourceRange(start, endDim - start + 1); - return; - default: - if (!startSet) { - start = this.scanner.startPosition; - startSet = true; - } - break; + if (token != TerminalTokens.TokenNameWHITESPACE) { + if (!startSet) { + start = this.scanner.startPosition; + startSet = true; + } + switch(token) { + case TerminalTokens.TokenNameRBRACKET: + if (lParenCount > 0) break; + --count; + if (count > 0) break; + int endDim = this.scanner.currentPosition - 1; + dim.setSourceRange(start, endDim - start + 1); + return; + case TerminalTokens.TokenNameLBRACKET: + if (lParenCount > 0) break; + count++; + break; + case TerminalTokens.TokenNameLPAREN: + lParenCount++; + break; + case TerminalTokens.TokenNameRPAREN: + --lParenCount; + break; + default: + break; + } } } } catch(InvalidInputException e) { @@ -4841,10 +4868,22 @@ class ASTConverter { int balance = 0; int pos = initializerEnd > nameEnd ? initializerEnd - 1 : nameEnd; try { - int token; + int token, lParenCount = 0; + boolean hasAnnotations = false; while ((token = this.scanner.getNextToken()) != TerminalTokens.TokenNameEOF) { hasTokens = true; + if (hasAnnotations) { + if (token == TerminalTokens.TokenNameLPAREN) ++lParenCount; + else if (token == TerminalTokens.TokenNameRPAREN) { + --lParenCount; + continue; + } + if (lParenCount > 0) continue; + } switch(token) { + case TerminalTokens.TokenNameAT: + hasAnnotations = true; + break; case TerminalTokens.TokenNameLBRACE : case TerminalTokens.TokenNameLBRACKET : balance++; @@ -4874,10 +4913,22 @@ class ASTConverter { protected int retrieveProperRightBracketPosition(int bracketNumber, int start) { this.scanner.resetTo(start, this.compilationUnitSourceLength); try { - int token, count = 0; + int token, count = 0, lParentCount = 0, balance = 0; while ((token = this.scanner.getNextToken()) != TerminalTokens.TokenNameEOF) { switch(token) { + case TerminalTokens.TokenNameLPAREN: + ++lParentCount; + break; + case TerminalTokens.TokenNameRPAREN: + --lParentCount; + break; + case TerminalTokens.TokenNameLBRACKET: + ++balance; + break; case TerminalTokens.TokenNameRBRACKET: + --balance; + if (lParentCount > 0) break; + if (balance > 0) break; count++; if (count == bracketNumber) { return this.scanner.currentPosition - 1; @@ -4933,32 +4984,6 @@ class ASTConverter { } /** - * This method is used to retrieve the position of the right bracket. - * @return int the dimension found, -1 if none - */ - protected int retrieveRightBracketPosition(int start, int end) { - this.scanner.resetTo(start, end); - try { - int token; - int balance = 0; - while ((token = this.scanner.getNextToken()) != TerminalTokens.TokenNameEOF) { - switch(token) { - case TerminalTokens.TokenNameLBRACKET : - balance++; - break; - case TerminalTokens.TokenNameRBRACKET : - balance--; - if (balance == 0) return this.scanner.currentPosition - 1; - break; - } - } - } catch(InvalidInputException e) { - // ignore - } - return -1; - } - - /** * This method is used to retrieve the start position of the block. * @return int the dimension found, -1 if none */ @@ -5643,26 +5668,16 @@ class ASTConverter { this.ast.getBindingResolver().updateKey(type, elementType); fieldDeclaration.setType(elementType); } else { - int start = type.getStartPosition(); - ArrayType subarrayType = arrayType; - int index = extraDimension; - while (index > 0) { - subarrayType = (ArrayType) subarrayType.getComponentType(); - index--; - } - int end = retrieveProperRightBracketPosition(remainingDimensions, start); - subarrayType.setSourceRange(start, end - start + 1); - // cut the child loose from its parent (without creating garbage) - subarrayType.setParent(null, null); + ArrayType subarrayType = extractSubArrayType(arrayType, remainingDimensions, extraDimension); fieldDeclaration.setType(subarrayType); - updateInnerPositions(subarrayType, remainingDimensions); this.ast.getBindingResolver().updateKey(type, subarrayType); } + checkAndSetMalformed(type, fieldDeclaration); } else { fieldDeclaration.setType(type); } } else { - if (type.isArrayType()) { + if (type.isArrayType() && (this.ast.apiLevel() < AST.JLS8)) { // update positions of the component types of the array type int dimensions = ((ArrayType) type).getDimensions(); updateInnerPositions(type, dimensions); @@ -5671,6 +5686,35 @@ class ASTConverter { } } + /** extracts the subArrayType for a given declaration for AST levels less + * @param arrayType parent type + * @param remainingDimensions + * @param extraDimensions + * @return an ArrayType + */ + private ArrayType extractSubArrayType(ArrayType arrayType, int remainingDimensions, int extraDimensions) { + ArrayType subArrayType = arrayType; + int start = subArrayType.getStartPosition(); + if (this.ast.apiLevel() < AST.JLS8) { + while (extraDimensions > 0 ) { + subArrayType = (ArrayType) componentType(subArrayType); + extraDimensions--; + } + updateInnerPositions(subArrayType, remainingDimensions); + } else { + List dimensions = subArrayType.dimensions(); + while (extraDimensions > 0 ) { + dimensions.remove(dimensions.size() - 1); + extraDimensions--; + } + } + int end = retrieveProperRightBracketPosition(remainingDimensions, start); + subArrayType.setSourceRange(start, end - start + 1); + // cut the child loose from its parent (without creating garbage) + subArrayType.setParent(null, null); + return subArrayType; + } + protected void setTypeForMethodDeclaration(MethodDeclaration methodDeclaration, Type type, int extraDimension) { if (extraDimension != 0) { if (type.isArrayType()) { @@ -5691,18 +5735,7 @@ class ASTConverter { break; } } else { - int start = type.getStartPosition(); - ArrayType subarrayType = arrayType; - int index = extraDimension; - while (index > 0) { - subarrayType = (ArrayType) subarrayType.getComponentType(); - index--; - } - int end = retrieveProperRightBracketPosition(remainingDimensions, start); - subarrayType.setSourceRange(start, end - start + 1); - // cut the child loose from its parent (without creating garbage) - subarrayType.setParent(null, null); - updateInnerPositions(subarrayType, remainingDimensions); + ArrayType subarrayType = extractSubArrayType(arrayType, remainingDimensions, extraDimension); switch(this.ast.apiLevel) { case AST.JLS2_INTERNAL : methodDeclaration.internalSetReturnType(subarrayType); @@ -5713,6 +5746,7 @@ class ASTConverter { } this.ast.getBindingResolver().updateKey(type, subarrayType); } + checkAndSetMalformed(type, methodDeclaration); } else { switch(this.ast.apiLevel) { case AST.JLS2_INTERNAL : @@ -5752,21 +5786,12 @@ class ASTConverter { this.ast.getBindingResolver().updateKey(type, elementType); singleVariableDeclaration.setType(elementType); } else { - int start = type.getStartPosition(); - ArrayType subarrayType = arrayType; - int index = extraDimension; - while (index > 0) { - subarrayType = (ArrayType) subarrayType.getComponentType(); - index--; - } - int end = retrieveProperRightBracketPosition(remainingDimensions, start); - subarrayType.setSourceRange(start, end - start + 1); - // cut the child loose from its parent (without creating garbage) - subarrayType.setParent(null, null); - updateInnerPositions(subarrayType, remainingDimensions); - singleVariableDeclaration.setType(subarrayType); + ArrayType subarrayType = extractSubArrayType(arrayType, remainingDimensions, extraDimension); this.ast.getBindingResolver().updateKey(type, subarrayType); + singleVariableDeclaration.setType(subarrayType); } + checkAndSetMalformed(type, singleVariableDeclaration); + } else { singleVariableDeclaration.setType(type); } @@ -5788,21 +5813,11 @@ class ASTConverter { this.ast.getBindingResolver().updateKey(type, elementType); variableDeclarationExpression.setType(elementType); } else { - int start = type.getStartPosition(); - ArrayType subarrayType = arrayType; - int index = extraDimension; - while (index > 0) { - subarrayType = (ArrayType) subarrayType.getComponentType(); - index--; - } - int end = retrieveProperRightBracketPosition(remainingDimensions, start); - subarrayType.setSourceRange(start, end - start + 1); - // cut the child loose from its parent (without creating garbage) - subarrayType.setParent(null, null); - updateInnerPositions(subarrayType, remainingDimensions); + ArrayType subarrayType = extractSubArrayType(arrayType, remainingDimensions, extraDimension); variableDeclarationExpression.setType(subarrayType); this.ast.getBindingResolver().updateKey(type, subarrayType); } + checkAndSetMalformed(type, variableDeclarationExpression); } else { variableDeclarationExpression.setType(type); } @@ -5824,21 +5839,11 @@ class ASTConverter { this.ast.getBindingResolver().updateKey(type, elementType); variableDeclarationStatement.setType(elementType); } else { - int start = type.getStartPosition(); - ArrayType subarrayType = arrayType; - int index = extraDimension; - while (index > 0) { - subarrayType = (ArrayType) subarrayType.getComponentType(); - index--; - } - int end = retrieveProperRightBracketPosition(remainingDimensions, start); - subarrayType.setSourceRange(start, end - start + 1); - // cut the child loose from its parent (without creating garbage) - subarrayType.setParent(null, null); - updateInnerPositions(subarrayType, remainingDimensions); + ArrayType subarrayType = extractSubArrayType(arrayType, remainingDimensions, extraDimension); variableDeclarationStatement.setType(subarrayType); this.ast.getBindingResolver().updateKey(type, subarrayType); } + checkAndSetMalformed(type, variableDeclarationStatement); } else { variableDeclarationStatement.setType(type); } @@ -5851,13 +5856,13 @@ class ASTConverter { if (dimensions > 1) { // need to set positions for intermediate array type see 42839 int start = type.getStartPosition(); - Type currentComponentType = ((ArrayType) type).getComponentType(); + Type currentComponentType = componentType(((ArrayType) type)); int searchedDimension = dimensions - 1; int rightBracketEndPosition = start; while (currentComponentType.isArrayType()) { rightBracketEndPosition = retrieveProperRightBracketPosition(searchedDimension, start); currentComponentType.setSourceRange(start, rightBracketEndPosition - start + 1); - currentComponentType = ((ArrayType) currentComponentType).getComponentType(); + currentComponentType = componentType(((ArrayType) currentComponentType)); searchedDimension--; } } diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTMatcher.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTMatcher.java index f33c5e2b92..b043365147 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTMatcher.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTMatcher.java @@ -164,6 +164,13 @@ public class ASTMatcher { } /** + * @deprecated + */ + private Type componentType(ArrayType array) { + return array.getComponentType(); + } + + /** * Returns whether the given node and the other object match. * <p> * The default implementation provided by this class tests whether the @@ -331,8 +338,11 @@ public class ASTMatcher { } ArrayType o = (ArrayType) other; int level = node.getAST().apiLevel; - return safeSubtreeMatch(node.getComponentType(), o.getComponentType()) - && (level >= AST.JLS8 ? safeSubtreeListMatch(node.annotations(), o.annotations()) : true); + if (level < AST.JLS8) { + return safeSubtreeMatch(componentType(node), componentType(o)); + } + return safeSubtreeMatch(node.getElementType(), o.getElementType()) + && safeSubtreeListMatch(node.dimensions(), o.dimensions()); } /** diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ArrayType.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ArrayType.java index cd1dd839d0..c3e3f7543d 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ArrayType.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ArrayType.java @@ -21,33 +21,42 @@ import java.util.List; /** * Type node for an array type. * <p> - * Array types are expressed in a recursive manner, one dimension at a time. + * Array types are expressed in a recursive manner, one dimension at a time. From JLS8 onwards, + * instead of this recursive manner, arrays are represented by a base element type (which cannot + * be an Array type) and a list of dimensions each of which may have a list of annotations. * </p> * <pre> - * ArrayType: - * Type { Annotation } <b>'['</b> <b>']'</b> + * ArrayType: + * Type ExtraDimension { ExtraDimension } * </pre> * * @since 2.0 * @noinstantiate This class is not intended to be instantiated by clients. */ -public class ArrayType extends AnnotatableType { +public class ArrayType extends Type { /** * The "componentType" structural property of this node type (child type: {@link Type}). + * Not supported from JLS8 onwards. * @since 3.0 */ public static final ChildPropertyDescriptor COMPONENT_TYPE_PROPERTY = new ChildPropertyDescriptor(ArrayType.class, "componentType", Type.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$ /** - * The "annotations" structural property of this node type (element type: {@link Annotation}). + * The "elementType" structural property of this node type (child type: {@link Type}) (added in JLS8 API). * @since 3.9 BETA_JAVA8 */ - public static final ChildListPropertyDescriptor ANNOTATIONS_PROPERTY = - internalAnnotationsPropertyFactory(ArrayType.class); + public static final ChildPropertyDescriptor ELEMENT_TYPE_PROPERTY = + new ChildPropertyDescriptor(ArrayType.class, "elementType", Type.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$ /** + * The "dimensions" structural property of this node type (element type: {@link ExtraDimension}) (added in JLS8 API). + * @since 3.9 BETA_JAVA8 + */ + public static final ChildListPropertyDescriptor DIMENSIONS_PROPERTY = + new ChildListPropertyDescriptor(ArrayType.class, "dimensions", ExtraDimension.class, CYCLE_RISK); //$NON-NLS-1$ + /** * A list of property descriptors (element type: * {@link StructuralPropertyDescriptor}), * or null if uninitialized. @@ -69,8 +78,8 @@ public class ArrayType extends AnnotatableType { propertyList = new ArrayList(3); createPropertyList(ArrayType.class, propertyList); - addProperty(COMPONENT_TYPE_PROPERTY, propertyList); - addProperty(ANNOTATIONS_PROPERTY, propertyList); + addProperty(ELEMENT_TYPE_PROPERTY, propertyList); + addProperty(DIMENSIONS_PROPERTY, propertyList); PROPERTY_DESCRIPTORS_8_0 = reapPropertyList(propertyList); } @@ -98,11 +107,21 @@ public class ArrayType extends AnnotatableType { /** * The component type; lazily initialized; defaults to a simple type with - * an unspecified, but legal, name. + * an unspecified, but legal, name. reused for element type from JLS8 onwards. */ private Type componentType = null; /** + * List of extra dimensions this node has with optional annotations + * (element type: {@link ExtraDimension}). + * Null before JLS8. Added in JLS8; defaults to a list with one element + * (see constructor). + * + * @since 3.9 BETA_JAVA8 + */ + private ASTNode.NodeList dimensions = null; + + /** * Creates a new unparented node for an array type owned by the given AST. * By default, a 1-dimensional array of an unspecified simple type. * <p> @@ -113,14 +132,31 @@ public class ArrayType extends AnnotatableType { */ ArrayType(AST ast) { super(ast); + if (ast.apiLevel >= AST.JLS8) { + this.dimensions = new ASTNode.NodeList(DIMENSIONS_PROPERTY); + // single dimension array is the default + this.dimensions().add(this.ast.newExtraDimension()); + } } - /* (omit javadoc for this method) - * Method declared on AnnotatableType. - * @since 3.9 BETA_JAVA8 - */ - final ChildListPropertyDescriptor internalAnnotationsProperty() { - return ANNOTATIONS_PROPERTY; + /** + * Creates a new unparented node for an array type owned by the given AST. + * <p> + * N.B. This constructor is private. + * </p> + * + * @param ast the AST that is to own this node + * @param dimensions no of dimensions - can be zero + * + * @since 3.9 BETA_JAVA8 + */ + private ArrayType(AST ast, int dimensions) { + super(ast); + unsupportedIn2_3_4(); + this.dimensions = new ASTNode.NodeList(DIMENSIONS_PROPERTY); + for (int i = 0; i < dimensions; ++i) { + this.dimensions().add(this.ast.newExtraDimension()); + } } /* (omit javadoc for this method) @@ -134,8 +170,8 @@ public class ArrayType extends AnnotatableType { * Method declared on ASTNode. */ final List internalGetChildListProperty(ChildListPropertyDescriptor property) { - if (property == ANNOTATIONS_PROPERTY) { - return annotations(); + if (property == DIMENSIONS_PROPERTY) { + return dimensions(); } // allow default implementation to flag the error return super.internalGetChildListProperty(property); @@ -152,6 +188,13 @@ public class ArrayType extends AnnotatableType { setComponentType((Type) child); return null; } + } else if (property == ELEMENT_TYPE_PROPERTY) { + if (get) { + return getElementType(); + } else { + setElementType((Type) child); + return null; + } } // allow default implementation to flag the error return super.internalGetSetChildProperty(property, get, child); @@ -168,13 +211,17 @@ public class ArrayType extends AnnotatableType { * Method declared on ASTNode. */ ASTNode clone0(AST target) { - ArrayType result = new ArrayType(target); - result.setSourceRange(getStartPosition(), getLength()); - result.setComponentType((Type) getComponentType().clone(target)); - if (this.ast.apiLevel >= AST.JLS8) { - result.annotations().addAll( - ASTNode.copySubtrees(target, annotations())); + ArrayType result; + if (this.ast.apiLevel < AST.JLS8) { + result = new ArrayType(target); + result.setComponentType((Type) getComponentType().clone(target)); + } else { + result = new ArrayType(target, 0); + result.setElementType((Type) getElementType().clone(target)); + result.dimensions().addAll( + ASTNode.copySubtrees(target, dimensions())); } + result.setSourceRange(getStartPosition(), getLength()); return result; } @@ -193,9 +240,11 @@ public class ArrayType extends AnnotatableType { boolean visitChildren = visitor.visit(this); if (visitChildren) { // visit children in normal left to right reading order - acceptChild(visitor, getComponentType()); - if (this.ast.apiLevel >= AST.JLS8) { - acceptChildren(visitor, this.annotations); + if (this.ast.apiLevel < AST.JLS8) { + acceptChild(visitor, getComponentType()); + } else { + acceptChild(visitor, getElementType()); + acceptChildren(visitor, this.dimensions); } } visitor.endVisit(this); @@ -206,15 +255,21 @@ public class ArrayType extends AnnotatableType { * may be another array type. * * @return the component type node + * @deprecated from JLS8 and later, the recursive structure is not valid */ public Type getComponentType() { + supportedOnlyIn2_3_4(); + return internalGetComponentType(COMPONENT_TYPE_PROPERTY); + } + + private Type internalGetComponentType(ChildPropertyDescriptor property) { if (this.componentType == null) { // lazy init must be thread-safe for readers synchronized (this) { if (this.componentType == null) { preLazyInit(); this.componentType = new SimpleType(this.ast); - postLazyInit(this.componentType, COMPONENT_TYPE_PROPERTY); + postLazyInit(this.componentType, property); } } } @@ -232,15 +287,21 @@ public class ArrayType extends AnnotatableType { * <li>the node already has a parent</li> * <li>a cycle in would be created</li> * </ul> + * @deprecated from JLS8 and later, the recursive structure is not valid */ public void setComponentType(Type componentType) { + supportedOnlyIn2_3_4(); if (componentType == null) { throw new IllegalArgumentException(); } + internalSetComponentType(componentType, COMPONENT_TYPE_PROPERTY); + } + + private void internalSetComponentType(Type type, ChildPropertyDescriptor property) { ASTNode oldChild = this.componentType; - preReplaceChild(oldChild, componentType, COMPONENT_TYPE_PROPERTY); - this.componentType = componentType; - postReplaceChild(oldChild, componentType, COMPONENT_TYPE_PROPERTY); + preReplaceChild(oldChild, type, property); + this.componentType = type; + postReplaceChild(oldChild, type, property); } /** @@ -248,36 +309,98 @@ public class ArrayType extends AnnotatableType { * never an array type. * <p> * This is a convenience method that descends a chain of nested array types - * until it reaches a non-array type. + * until it reaches a non-array type until JLS4. From JLS8 and later, this returns the + * element type directly. * </p> * - * @return the component type node + * @return the element type node */ public Type getElementType() { - Type t = getComponentType(); - while (t.isArrayType()) { - t = ((ArrayType) t).getComponentType(); + if (this.ast.apiLevel() < AST.JLS8) { + Type t = getComponentType(); + while (t.isArrayType()) { + t = ((ArrayType) t).getComponentType(); + } + return t; } - return t; + return internalGetComponentType(ELEMENT_TYPE_PROPERTY); + } + + /** + * Sets the element type of the array. + * + * @param type the new type + * @exception IllegalArgumentException if: + * <ul> + * <li>the node belongs to a different AST</li> + * <li>the node already has a parent</li> + * <li>the node is an array type</li> + * </ul> + * @since 3.9 BETA_JAVA8 + */ + public void setElementType(Type type) { + if (type == null || type instanceof ArrayType) { + throw new IllegalArgumentException(); + } + internalSetComponentType(type, ELEMENT_TYPE_PROPERTY); } /** * Returns the number of dimensions in this array type. * <p> * This is a convenience method that descends a chain of nested array types - * until it reaches a non-array type. + * until it reaches a non-array type (until JLS4). From JLS8 onwards, + * this returns the size of the dimensions list. * </p> * * @return the number of dimensions (always positive) */ public int getDimensions() { + if (this.ast.apiLevel() >= AST.JLS8) { + return dimensions().size(); + } Type t = getComponentType(); - int dimensions = 1; // always include this array type + int dimension = 1; // always include this array type while (t.isArrayType()) { - dimensions++; + dimension++; t = ((ArrayType) t).getComponentType(); } - return dimensions; + return dimension; + } + + /** + * Returns the dimension d in this array type. + * <p> + * This is a convenience method that returns the dimension at the given number d. + * </p> + * @param d dimension no + * @return Dimension at number d, null if d out of range + * + * @since 3.9 BETA_JAVA8 + */ + public ExtraDimension getDimensionAt(int d) { + unsupportedIn2_3_4(); + ExtraDimension extraDimension = null; + int n = getDimensions() - 1; + if (d >= 0 && d <= n) { + extraDimension = (ExtraDimension) dimensions().get(n - d); + } + return extraDimension; + } + + /** + * Returns the live ordered list of dimensions with optional annotations (added in JLS8 API). + * + * @return the live list of dimensions with optional annotations (element type: {@link ExtraDimension}) + * @exception UnsupportedOperationException if this operation is used below JLS8 + * @since 3.9 BETA_JAVA8 + */ + public List dimensions() { + // more efficient than just calling unsupportedIn2_3_4() to check + if (this.dimensions == null) { + unsupportedIn2_3_4(); + } + return this.dimensions; } /* (omit javadoc for this method) @@ -293,8 +416,8 @@ public class ArrayType extends AnnotatableType { int treeSize() { return memSize() - + (this.componentType == null ? 0 : getComponentType().treeSize()) - + (this.annotations == null ? 0 : this.annotations.listSize()); + + (this.componentType == null ? 0 : (this.ast.apiLevel() < AST.JLS8 ? getComponentType().treeSize() : getElementType().treeSize()) + + (this.dimensions == null ? 0 : this.dimensions.listSize())); } } diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java index b735dea761..6d556c3520 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java @@ -370,9 +370,17 @@ public class NaiveASTFlattener extends ASTVisitor { * @see ASTVisitor#visit(ArrayType) */ public boolean visit(ArrayType node) { - node.getComponentType().accept(this); - visitTypeAnnotations(node); - this.buffer.append("[]");//$NON-NLS-1$ + if (node.getAST().apiLevel() < AST.JLS8) { + visitComponentType(node); + this.buffer.append("[]");//$NON-NLS-1$ + } else { + node.getElementType().accept(this); + List dimensions = node.dimensions(); + for (int i = 0; i < dimensions.size() ; i++) { + ExtraDimension aDimension = (ExtraDimension) dimensions.get(i); + aDimension.accept(this); + } + } return false; } @@ -1944,4 +1952,11 @@ public class NaiveASTFlattener extends ASTVisitor { return false; } + /** + * @deprecated + */ + private void visitComponentType(ArrayType node) { + node.getComponentType().accept(this); + } + } diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java index 6cf44a92e9..ae1ee68f64 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java @@ -2077,15 +2077,15 @@ public final class ASTRewriteAnalyzer extends ASTVisitor { RewriteEvent dimEvent= getEvent(node, ArrayCreation.DIMENSIONS_PROPERTY); boolean hasDimensionChanges= (dimEvent != null && dimEvent.getChangeKind() != RewriteEvent.UNCHANGED); RewriteEvent[] events= hasDimensionChanges ? dimEvent.getChildren() : null; - ArrayType currentLevel= (ArrayType) replacingType.getElementType().getParent(); + boolean astLevelGTE8 = node.getAST().apiLevel() >= AST.JLS8 ? true : false; + ArrayType currentLevel = astLevelGTE8 ? null : (ArrayType) replacingType.getElementType().getParent(); + int replacingTypeDimensions = replacingType.getDimensions(); int i=0, dimSize= (events == null) ? 0 : events.length; Type elementType= arrayType.getElementType(); int offset= elementType.getStartPosition() + elementType.getLength(); - while(currentLevel != null) { - if (node.getAST().apiLevel() >= AST.JLS8) { - rewriteTypeAnnotations(currentLevel, ArrayType.ANNOTATIONS_PROPERTY, offset); - } + while(currentLevel != null || astLevelGTE8) { if (i < dimSize) { + if (astLevelGTE8) internalExtraDimensionRewrite(replacingType, i, offset); offset= getScanner().getTokenEndOffset(TerminalTokens.TokenNameLBRACKET, offset); if (hasDimensionChanges) { RewriteEvent event= events[i]; @@ -2120,13 +2120,19 @@ public final class ASTRewriteAnalyzer extends ASTVisitor { offset= retrieveRightBracketEndPosition(offset, 1, true); } } else if (i < nOldBrackets) { + if (astLevelGTE8) internalExtraDimensionRewrite(replacingType, i, offset); offset= retrieveRightBracketEndPosition(offset, 1, false); } else { + internalExtraDimensionAddition(replacingType, i, offset, editGroup, astLevelGTE8); doTextInsert(offset, "[]", editGroup); //$NON-NLS-1$ } i++; - if (currentLevel == replacingType) break; - currentLevel= (ArrayType) currentLevel.getParent(); + if (astLevelGTE8) { + if (i == replacingTypeDimensions) break; + } else { + if (currentLevel == replacingType) break; + currentLevel= (ArrayType) currentLevel.getParent(); + } } if (i < nOldBrackets) { int endPos= retrieveRightBracketEndPosition(offset, nOldBrackets - i, false); @@ -2146,6 +2152,26 @@ public final class ASTRewriteAnalyzer extends ASTVisitor { return false; } + private void internalExtraDimensionAddition(ArrayType replacingType, int index, int pos, TextEditGroup editGroup, + boolean astLevelGTE8) { + if (astLevelGTE8) { + ExtraDimension dim = (ExtraDimension) replacingType.dimensions().get(index); + List annotations = dim.annotations(); + if (annotations != null) { + for (int j = 0; j < annotations.size(); j++) { + Annotation annotation = (Annotation) annotations.get(j); + doTextInsert(pos, annotation.toString() + " ", editGroup); //$NON-NLS-1$ + } + + } + } + } + + private void internalExtraDimensionRewrite(ArrayType replacingType, int index, int pos) { + ExtraDimension dim = (ExtraDimension) replacingType.dimensions().get(index); + rewriteTypeAnnotations(dim, ExtraDimension.ANNOTATIONS_PROPERTY, pos); + } + /** * This method is used to retrieve the position of the right bracket. * @return int the dimension found, -1 if none @@ -2175,6 +2201,9 @@ public final class ASTRewriteAnalyzer extends ASTVisitor { } private Type getElementType(ArrayType parent) { + if (parent.getAST().apiLevel() >= AST.JLS8) { + return (Type) getOriginalValue(parent, ArrayType.ELEMENT_TYPE_PROPERTY); + } Type t = (Type) getOriginalValue(parent, ArrayType.COMPONENT_TYPE_PROPERTY); while (t.isArrayType()) { t = (Type) getOriginalValue(t, ArrayType.COMPONENT_TYPE_PROPERTY); @@ -2183,6 +2212,9 @@ public final class ASTRewriteAnalyzer extends ASTVisitor { } private int getDimensions(ArrayType parent) { + if (parent.getAST().apiLevel() >= AST.JLS8) { + return ((List) getOriginalValue(parent, ArrayType.DIMENSIONS_PROPERTY)).size(); + } Type t = (Type) getOriginalValue(parent, ArrayType.COMPONENT_TYPE_PROPERTY); int dimensions = 1; // always include this array type while (t.isArrayType()) { @@ -2214,9 +2246,11 @@ public final class ASTRewriteAnalyzer extends ASTVisitor { if (!hasChildrenChanges(node)) { return doVisitUnchangedChildren(node); } - int pos = rewriteRequiredNode(node, ArrayType.COMPONENT_TYPE_PROPERTY); - if (node.getAST().apiLevel() >= AST.JLS8) { - rewriteTypeAnnotations(node, ArrayType.ANNOTATIONS_PROPERTY, pos); + if (node.getAST().apiLevel() < AST.JLS8) { + rewriteRequiredNode(node, ArrayType.COMPONENT_TYPE_PROPERTY); + } else { + int pos = rewriteRequiredNode(node, ArrayType.ELEMENT_TYPE_PROPERTY); + rewriteNodeList(node, ArrayType.DIMENSIONS_PROPERTY, pos, Util.EMPTY_STRING, " "); //$NON-NLS-1$ } return false; } diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java index 20f66e3874..033b496766 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java @@ -236,35 +236,35 @@ public class ASTRewriteFlattener extends ASTVisitor { ArrayType arrayType= (ArrayType) getChildNode(node, ArrayCreation.TYPE_PROPERTY); // get the element type and count dimensions - Type elementType= (Type) getChildNode(arrayType, ArrayType.COMPONENT_TYPE_PROPERTY); - int dimensions= 1; // always include this array type - while (elementType.isArrayType()) { - dimensions++; - elementType = (Type) getChildNode(elementType, ArrayType.COMPONENT_TYPE_PROPERTY); + Type elementType; + int dimensions; + boolean astLevelGTE8 = node.getAST().apiLevel() >= AST.JLS8 ? true : false; + if (astLevelGTE8) { + elementType = (Type) getChildNode(arrayType, ArrayType.ELEMENT_TYPE_PROPERTY); + dimensions = getChildList(arrayType, ArrayType.DIMENSIONS_PROPERTY).size(); + } else { + elementType = (Type) getChildNode(arrayType, ArrayType.COMPONENT_TYPE_PROPERTY); + dimensions = 1; // always include this array type + while (elementType.isArrayType()) { + dimensions++; + elementType = (Type) getChildNode(elementType, ArrayType.COMPONENT_TYPE_PROPERTY); + } } elementType.accept(this); // add "<annotations> [ <dimension> ]" for each dimension expression - Type type= arrayType; List list= getChildList(node, ArrayCreation.DIMENSIONS_PROPERTY); for (int i= 0; i < list.size(); i++) { - if (node.getAST().apiLevel() >= AST.JLS8 && type instanceof ArrayType) { - visitList(type, ArrayType.ANNOTATIONS_PROPERTY, String.valueOf(' '), Util.EMPTY_STRING, String.valueOf(' ')); - type = (Type) getChildNode(type, ArrayType.COMPONENT_TYPE_PROPERTY); - } + internalVisitExtraDimensionAnnotations(arrayType, i, astLevelGTE8); this.result.append('['); ((ASTNode) list.get(i)).accept(this); this.result.append(']'); - dimensions--; } // add "<annotations> []" for each extra array dimension - for (int i= 0; i < dimensions; i++) { - if (node.getAST().apiLevel() >= AST.JLS8 && type instanceof ArrayType) { - visitList(type, ArrayType.ANNOTATIONS_PROPERTY, String.valueOf(' '), Util.EMPTY_STRING, String.valueOf(' ')); - type = (Type) getChildNode(type, ArrayType.COMPONENT_TYPE_PROPERTY); - } + for (int i= list.size(); i < dimensions; i++) { + internalVisitExtraDimensionAnnotations(arrayType, i, astLevelGTE8); this.result.append("[]"); //$NON-NLS-1$ } @@ -275,6 +275,13 @@ public class ASTRewriteFlattener extends ASTVisitor { return false; } + private void internalVisitExtraDimensionAnnotations(ArrayType arrayType, int index, boolean astLevelGTE8) { + if (astLevelGTE8) { + ExtraDimension extraDimension = (ExtraDimension) arrayType.dimensions().get(index); + visitList(extraDimension, ExtraDimension.ANNOTATIONS_PROPERTY, String.valueOf(' '), Util.EMPTY_STRING, String.valueOf(' ')); + } + } + /* * @see ASTVisitor#visit(ArrayInitializer) */ @@ -289,11 +296,13 @@ public class ASTRewriteFlattener extends ASTVisitor { * @see ASTVisitor#visit(ArrayType) */ public boolean visit(ArrayType node) { - getChildNode(node, ArrayType.COMPONENT_TYPE_PROPERTY).accept(this); - if (node.getAST().apiLevel() >= AST.JLS8) { - visitList(node, ArrayType.ANNOTATIONS_PROPERTY, String.valueOf(' '), Util.EMPTY_STRING, String.valueOf(' ')); + if (node.getAST().apiLevel() < AST.JLS8) { + getChildNode(node, ArrayType.COMPONENT_TYPE_PROPERTY).accept(this); + this.result.append("[]"); //$NON-NLS-1$ + } else { + getChildNode(node, ArrayType.ELEMENT_TYPE_PROPERTY).accept(this); + visitList(node, ArrayType.DIMENSIONS_PROPERTY, String.valueOf(' '), Util.EMPTY_STRING, String.valueOf(' ')); } - this.result.append("[]"); //$NON-NLS-1$ return false; } |