author | akulyakh |
Thu, 21 May 2015 11:41:04 -0700 | |
changeset 30730 | d3ce7619db2c |
parent 27319 | 030080f03e4f |
permissions | -rw-r--r-- |
15385 | 1 |
/* |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27319
diff
changeset
|
2 |
* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved. |
15385 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @summary Checks that the interaction between annotated and unannotated |
|
27 |
* array levels in array creation trees |
|
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27319
diff
changeset
|
28 |
* @modules jdk.compiler/com.sun.tools.javac.api |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27319
diff
changeset
|
29 |
* jdk.compiler/com.sun.tools.javac.file |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27319
diff
changeset
|
30 |
* jdk.compiler/com.sun.tools.javac.tree |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27319
diff
changeset
|
31 |
* jdk.compiler/com.sun.tools.javac.util |
15385 | 32 |
*/ |
33 |
||
34 |
import com.sun.tools.javac.api.JavacTool; |
|
35 |
import com.sun.tools.javac.tree.JCTree.JCNewArray; |
|
36 |
import java.lang.annotation.*; |
|
37 |
import java.io.File; |
|
38 |
import java.io.PrintWriter; |
|
39 |
import java.util.Arrays; |
|
40 |
import java.util.List; |
|
41 |
import java.util.Map; |
|
42 |
import java.util.HashMap; |
|
43 |
import javax.tools.JavaFileManager; |
|
44 |
import javax.tools.JavaFileObject; |
|
45 |
import com.sun.source.tree.*; |
|
46 |
import com.sun.source.util.JavacTask; |
|
47 |
import com.sun.source.util.TreeScanner; |
|
48 |
import javax.tools.StandardJavaFileManager; |
|
49 |
||
50 |
||
51 |
public class ArrayCreationTree { |
|
52 |
public static void main(String[] args) throws Exception { |
|
53 |
PrintWriter out = new PrintWriter(System.out, true); |
|
54 |
JavacTool tool = JavacTool.create(); |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22448
diff
changeset
|
55 |
try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22448
diff
changeset
|
56 |
File testSrc = new File(System.getProperty("test.src")); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22448
diff
changeset
|
57 |
Iterable<? extends JavaFileObject> f = |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22448
diff
changeset
|
58 |
fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "ArrayCreationTree.java"))); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22448
diff
changeset
|
59 |
JavacTask task = tool.getTask(out, fm, null, null, null, f); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22448
diff
changeset
|
60 |
Iterable<? extends CompilationUnitTree> trees = task.parse(); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22448
diff
changeset
|
61 |
out.flush(); |
15385 | 62 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22448
diff
changeset
|
63 |
Scanner s = new Scanner(); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22448
diff
changeset
|
64 |
for (CompilationUnitTree t: trees) |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22448
diff
changeset
|
65 |
s.scan(t, null); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22448
diff
changeset
|
66 |
} |
15385 | 67 |
} |
68 |
||
69 |
private static class Scanner extends TreeScanner<Void,Void> { |
|
70 |
int foundAnnotations = 0; |
|
71 |
public Void visitCompilationUnit(CompilationUnitTree node, Void ignore) { |
|
72 |
super.visitCompilationUnit(node, ignore); |
|
73 |
if (foundAnnotations != expectedAnnotations) { |
|
74 |
throw new AssertionError("Expected " + expectedAnnotations + |
|
75 |
" annotations but found: " + foundAnnotations); |
|
76 |
} |
|
77 |
return null; |
|
78 |
} |
|
79 |
||
80 |
private void testAnnotations(List<? extends AnnotationTree> annos, int found) { |
|
81 |
if (annos.isEmpty()) return; |
|
82 |
||
83 |
String annotation = annos.get(0).toString(); |
|
84 |
foundAnnotations++; |
|
85 |
||
86 |
int expected = -1; |
|
87 |
if (annotation.equals("@A()")) |
|
88 |
expected = 0; |
|
89 |
else if (annotation.equals("@B()")) |
|
90 |
expected = 1; |
|
91 |
else if (annotation.equals("@C()")) |
|
92 |
expected = 2; |
|
93 |
else |
|
94 |
throw new AssertionError("found an unexpected annotation: " + annotation); |
|
95 |
if (found != expected) { |
|
96 |
throw new AssertionError("Unexpected found length" + |
|
97 |
", found " + found + " but expected " + expected); |
|
98 |
} |
|
99 |
} |
|
100 |
||
101 |
public Void visitAnnotatedType(AnnotatedTypeTree node, Void ignore) { |
|
102 |
testAnnotations(node.getAnnotations(), arrayLength(node)); |
|
103 |
return super.visitAnnotatedType(node, ignore); |
|
104 |
} |
|
105 |
||
106 |
public Void visitNewArray(NewArrayTree node, Void ignore) { |
|
107 |
// the Tree API hasn't been updated to expose annotations yet |
|
108 |
JCNewArray newArray = (JCNewArray)node; |
|
109 |
int totalLength = node.getDimensions().size() |
|
110 |
+ arrayLength(node.getType()) |
|
111 |
+ ((newArray.getInitializers() != null) ? 1 : 0); |
|
112 |
testAnnotations(newArray.annotations, totalLength); |
|
113 |
int count = 0; |
|
114 |
for (List<? extends AnnotationTree> annos : newArray.dimAnnotations) { |
|
115 |
testAnnotations(annos, totalLength - count); |
|
116 |
count++; |
|
117 |
} |
|
118 |
return super.visitNewArray(node, ignore); |
|
119 |
} |
|
120 |
||
121 |
private int arrayLength(Tree tree) { |
|
122 |
// TODO: the tree is null when called with node.getType(). Why? |
|
123 |
if (tree==null) return -1; |
|
124 |
switch (tree.getKind()) { |
|
125 |
case ARRAY_TYPE: |
|
126 |
return 1 + arrayLength(((ArrayTypeTree)tree).getType()); |
|
127 |
case ANNOTATED_TYPE: |
|
128 |
return arrayLength(((AnnotatedTypeTree)tree).getUnderlyingType()); |
|
129 |
default: |
|
130 |
return 0; |
|
131 |
} |
|
132 |
} |
|
133 |
} |
|
134 |
||
135 |
static int expectedAnnotations = 21; |
|
136 |
||
137 |
Object a1 = new @A Object @C [2] @B [1]; |
|
138 |
Object b1 = new @A Object @C [2] @B [ ]; |
|
139 |
Object c1 = new @A Object @C [ ] @B [ ] { }; |
|
140 |
||
141 |
Object a2 = new @A Object @C [2] [1]; |
|
142 |
Object b2 = new @A Object @C [2] [ ]; |
|
143 |
Object c2 = new @A Object @C [ ] [ ] { }; |
|
144 |
||
145 |
Object a3 = new @A Object [2] @B [1]; |
|
146 |
Object b3 = new @A Object [2] @B [ ]; |
|
147 |
Object c3 = new @A Object [ ] @B [ ] { }; |
|
148 |
||
149 |
@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) |
|
150 |
@interface A {} |
|
151 |
@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) |
|
152 |
@interface B {} |
|
153 |
@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) |
|
154 |
@interface C {} |
|
155 |
||
156 |
} |