Wed, 27 Apr 2016 01:34:52 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | /**@test |
aoqi@0 | 25 | * @bug 8035890 |
aoqi@0 | 26 | * @summary Verify that the parser correctly checks for source level 8 on the new places where |
aoqi@0 | 27 | * annotations can appear in 8. |
aoqi@0 | 28 | * @run main CheckErrorsForSource7 CheckErrorsForSource7.java |
aoqi@0 | 29 | */ |
aoqi@0 | 30 | import java.io.File; |
aoqi@0 | 31 | import java.io.IOException; |
aoqi@0 | 32 | import java.lang.annotation.ElementType; |
aoqi@0 | 33 | import java.lang.annotation.Target; |
aoqi@0 | 34 | import java.net.URI; |
aoqi@0 | 35 | import java.net.URISyntaxException; |
aoqi@0 | 36 | import java.util.ArrayList; |
aoqi@0 | 37 | import java.util.Arrays; |
aoqi@0 | 38 | import java.util.Collections; |
aoqi@0 | 39 | import java.util.Comparator; |
aoqi@0 | 40 | import java.util.HashSet; |
aoqi@0 | 41 | import java.util.List; |
aoqi@0 | 42 | import java.util.Set; |
aoqi@0 | 43 | import javax.tools.Diagnostic; |
aoqi@0 | 44 | import javax.tools.DiagnosticCollector; |
aoqi@0 | 45 | import javax.tools.JavaFileObject; |
aoqi@0 | 46 | import javax.tools.SimpleJavaFileObject; |
aoqi@0 | 47 | import com.sun.source.tree.AnnotationTree; |
aoqi@0 | 48 | import com.sun.source.tree.CompilationUnitTree; |
aoqi@0 | 49 | import com.sun.source.tree.IdentifierTree; |
aoqi@0 | 50 | import com.sun.source.tree.Tree.Kind; |
aoqi@0 | 51 | import com.sun.source.util.JavacTask; |
aoqi@0 | 52 | import com.sun.source.util.TreePathScanner; |
aoqi@0 | 53 | import com.sun.source.util.Trees; |
aoqi@0 | 54 | import com.sun.tools.javac.api.JavacTool; |
aoqi@0 | 55 | import com.sun.tools.javac.file.JavacFileManager; |
aoqi@0 | 56 | |
aoqi@0 | 57 | /**For each place where an annotation can syntactically appear with -source 8, but not with |
aoqi@0 | 58 | * -source 7, this test verifies that an error is correctly emitted from the parser for |
aoqi@0 | 59 | * the annotation for -source 7. This test first gathers the occurrences of @TA from |
aoqi@0 | 60 | * the CheckErrorsForSource7Data class below, and then repeatedly removes all these annotations |
aoqi@0 | 61 | * except one and checks the parser reports an expected error. This is needed as as the parser |
aoqi@0 | 62 | * typically produces only one 'insufficient source level' error for each new feature used. |
aoqi@0 | 63 | */ |
aoqi@0 | 64 | public class CheckErrorsForSource7 { |
aoqi@0 | 65 | public static void main(String... args) throws IOException, URISyntaxException { |
aoqi@0 | 66 | new CheckErrorsForSource7().run(args); |
aoqi@0 | 67 | } |
aoqi@0 | 68 | |
aoqi@0 | 69 | private void run(String... args) throws IOException, URISyntaxException { |
aoqi@0 | 70 | //the first and only parameter must be the name of the file to be analyzed: |
aoqi@0 | 71 | if (args.length != 1) throw new IllegalStateException("Must provide source file!"); |
aoqi@0 | 72 | File testSrc = new File(System.getProperty("test.src")); |
aoqi@0 | 73 | File testFile = new File(testSrc, args[0]); |
aoqi@0 | 74 | if (!testFile.canRead()) throw new IllegalStateException("Cannot read the test source"); |
aoqi@0 | 75 | JavacFileManager fm = JavacTool.create().getStandardFileManager(null, null, null); |
aoqi@0 | 76 | |
aoqi@0 | 77 | //gather spans of the @TA annotations into typeAnnotationSpans: |
aoqi@0 | 78 | JavacTask task = JavacTool.create().getTask(null, |
aoqi@0 | 79 | fm, |
aoqi@0 | 80 | null, |
aoqi@0 | 81 | Collections.<String>emptyList(), |
aoqi@0 | 82 | null, |
aoqi@0 | 83 | fm.getJavaFileObjects(testFile)); |
aoqi@0 | 84 | final Trees trees = Trees.instance(task); |
aoqi@0 | 85 | final CompilationUnitTree cut = task.parse().iterator().next(); |
aoqi@0 | 86 | final List<int[]> typeAnnotationSpans = new ArrayList<>(); |
aoqi@0 | 87 | |
aoqi@0 | 88 | new TreePathScanner<Void, Void>() { |
aoqi@0 | 89 | @Override |
aoqi@0 | 90 | public Void visitAnnotation(AnnotationTree node, Void p) { |
aoqi@0 | 91 | if (node.getAnnotationType().getKind() == Kind.IDENTIFIER && |
aoqi@0 | 92 | ((IdentifierTree) node.getAnnotationType()).getName().contentEquals("TA")) { |
aoqi@0 | 93 | int start = (int) trees.getSourcePositions().getStartPosition(cut, node); |
aoqi@0 | 94 | int end = (int) trees.getSourcePositions().getEndPosition(cut, node); |
aoqi@0 | 95 | typeAnnotationSpans.add(new int[] {start, end}); |
aoqi@0 | 96 | } |
aoqi@0 | 97 | return null; |
aoqi@0 | 98 | } |
aoqi@0 | 99 | }.scan(cut, null); |
aoqi@0 | 100 | |
aoqi@0 | 101 | //sort the spans in the reverse order, to simplify removing them from the source: |
aoqi@0 | 102 | Collections.sort(typeAnnotationSpans, new Comparator<int[]>() { |
aoqi@0 | 103 | @Override |
aoqi@0 | 104 | public int compare(int[] o1, int[] o2) { |
aoqi@0 | 105 | return o2[0] - o1[0]; |
aoqi@0 | 106 | } |
aoqi@0 | 107 | }); |
aoqi@0 | 108 | |
aoqi@0 | 109 | //verify the errors are produce correctly: |
aoqi@0 | 110 | String originalSource = cut.getSourceFile().getCharContent(false).toString(); |
aoqi@0 | 111 | |
aoqi@0 | 112 | for (int[] toKeep : typeAnnotationSpans) { |
aoqi@0 | 113 | //prepare updated source code by removing all the annotations except the toKeep one: |
aoqi@0 | 114 | String updated = originalSource; |
aoqi@0 | 115 | |
aoqi@0 | 116 | for (int[] span : typeAnnotationSpans) { |
aoqi@0 | 117 | if (span == toKeep) continue; |
aoqi@0 | 118 | |
aoqi@0 | 119 | updated = updated.substring(0, span[0]) + updated.substring(span[1]); |
aoqi@0 | 120 | } |
aoqi@0 | 121 | |
aoqi@0 | 122 | //parse and verify: |
aoqi@0 | 123 | JavaFileObject updatedFile = new TestFO(cut.getSourceFile().toUri(), updated); |
aoqi@0 | 124 | DiagnosticCollector<JavaFileObject> errors = new DiagnosticCollector<>(); |
aoqi@0 | 125 | JavacTask task2 = JavacTool.create().getTask(null, |
aoqi@0 | 126 | fm, |
aoqi@0 | 127 | errors, |
aoqi@0 | 128 | Arrays.asList("-source", "7"), |
aoqi@0 | 129 | null, |
aoqi@0 | 130 | Arrays.asList(updatedFile)); |
aoqi@0 | 131 | task2.parse(); |
aoqi@0 | 132 | |
aoqi@0 | 133 | boolean found = false; |
aoqi@0 | 134 | |
aoqi@0 | 135 | for (Diagnostic<? extends JavaFileObject> d : errors.getDiagnostics()) { |
aoqi@0 | 136 | if (d.getKind() == Diagnostic.Kind.ERROR && EXPECTED_ERRORS.contains(d.getCode())) { |
aoqi@0 | 137 | if (found) { |
aoqi@0 | 138 | throw new IllegalStateException("More than one expected error found."); |
aoqi@0 | 139 | } |
aoqi@0 | 140 | found = true; |
aoqi@0 | 141 | } |
aoqi@0 | 142 | } |
aoqi@0 | 143 | |
aoqi@0 | 144 | if (!found) |
aoqi@0 | 145 | throw new IllegalStateException("Did not produce proper errors for: " + updated); |
aoqi@0 | 146 | } |
aoqi@0 | 147 | } |
aoqi@0 | 148 | |
aoqi@0 | 149 | static final Set<String> EXPECTED_ERRORS = new HashSet<>(Arrays.asList( |
aoqi@0 | 150 | "compiler.err.type.annotations.not.supported.in.source", |
aoqi@0 | 151 | "compiler.err.annotations.after.type.params.not.supported.in.source" |
aoqi@0 | 152 | )); |
aoqi@0 | 153 | |
aoqi@0 | 154 | class TestFO extends SimpleJavaFileObject { |
aoqi@0 | 155 | private final String content; |
aoqi@0 | 156 | public TestFO(URI uri, String content) { |
aoqi@0 | 157 | super(uri, Kind.SOURCE); |
aoqi@0 | 158 | this.content = content; |
aoqi@0 | 159 | } |
aoqi@0 | 160 | |
aoqi@0 | 161 | @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { |
aoqi@0 | 162 | return content; |
aoqi@0 | 163 | } |
aoqi@0 | 164 | |
aoqi@0 | 165 | @Override public boolean isNameCompatible(String simpleName, Kind kind) { |
aoqi@0 | 166 | return true; |
aoqi@0 | 167 | } |
aoqi@0 | 168 | } |
aoqi@0 | 169 | } |
aoqi@0 | 170 | |
aoqi@0 | 171 | //data on which the source level check is verified: |
aoqi@0 | 172 | class CheckErrorsForSource7Data { |
aoqi@0 | 173 | @Target(ElementType.TYPE_USE) |
aoqi@0 | 174 | @interface TA { } |
aoqi@0 | 175 | |
aoqi@0 | 176 | Object n1 = new @TA ArrayList<@TA String>(); |
aoqi@0 | 177 | Object n2 = new @TA Object() {}; |
aoqi@0 | 178 | Object [] @TA [] arr @TA[]; |
aoqi@0 | 179 | <T> @TA int @TA[] ret(Object obj) @TA[] throws @TA Exception { |
aoqi@0 | 180 | this.<@TA String>ret(null); |
aoqi@0 | 181 | Object c1 = new @TA String[1]; |
aoqi@0 | 182 | |
aoqi@0 | 183 | int val = obj instanceof @TA String ? ((@TA String) obj).length() : 0; |
aoqi@0 | 184 | List<@TA ?> l; |
aoqi@0 | 185 | return null; |
aoqi@0 | 186 | } |
aoqi@0 | 187 | void vararg(String @TA ... args) { } |
aoqi@0 | 188 | |
aoqi@0 | 189 | abstract class C<@TA T extends @TA Number & @TA Runnable> |
aoqi@0 | 190 | extends @TA ArrayList<@TA String> |
aoqi@0 | 191 | implements java.util. @TA Comparator<@TA T> { } |
aoqi@0 | 192 | |
aoqi@0 | 193 | interface I extends java.util. @TA Comparator<@TA String> { } |
aoqi@0 | 194 | |
aoqi@0 | 195 | } |