Thu, 31 Aug 2017 15:17:03 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2006, 2010, 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 | /* |
aoqi@0 | 25 | * @test |
aoqi@0 | 26 | * @bug 6380018 6453386 6457283 |
aoqi@0 | 27 | * @summary Test that the constraints guaranteed by the Filer and maintained |
aoqi@0 | 28 | * @author Joseph D. Darcy |
aoqi@0 | 29 | * @library /tools/javac/lib |
aoqi@0 | 30 | * @build TestFilerConstraints |
aoqi@0 | 31 | * @compile -encoding iso-8859-1 -processor TestFilerConstraints -proc:only TestFilerConstraints.java |
aoqi@0 | 32 | */ |
aoqi@0 | 33 | |
aoqi@0 | 34 | import java.util.Set; |
aoqi@0 | 35 | import javax.annotation.processing.*; |
aoqi@0 | 36 | import javax.lang.model.SourceVersion; |
aoqi@0 | 37 | import static javax.lang.model.SourceVersion.*; |
aoqi@0 | 38 | import javax.lang.model.element.*; |
aoqi@0 | 39 | import javax.lang.model.util.*; |
aoqi@0 | 40 | import static javax.lang.model.util.ElementFilter.*; |
aoqi@0 | 41 | import static javax.tools.Diagnostic.Kind.*; |
aoqi@0 | 42 | import static javax.tools.StandardLocation.*; |
aoqi@0 | 43 | |
aoqi@0 | 44 | import java.io.*; |
aoqi@0 | 45 | import java.nio.charset.Charset; |
aoqi@0 | 46 | |
aoqi@0 | 47 | /** |
aoqi@0 | 48 | * A processor that verifies the explicit and implicit constraints in |
aoqi@0 | 49 | * the Filer contract are maintained: |
aoqi@0 | 50 | * |
aoqi@0 | 51 | * <blockquote> |
aoqi@0 | 52 | * |
aoqi@0 | 53 | * During each run of an annotation processing tool, a file with a |
aoqi@0 | 54 | * given pathname may be created only once. If that file already |
aoqi@0 | 55 | * exists before the first attempt to create it, the old contents |
aoqi@0 | 56 | * will be deleted. Any subsequent attempt to create the same file |
aoqi@0 | 57 | * during a run will throw a FilerException, as will attempting to |
aoqi@0 | 58 | * open both a class file and source file for the same type name. |
aoqi@0 | 59 | * |
aoqi@0 | 60 | * </blockquote> |
aoqi@0 | 61 | * |
aoqi@0 | 62 | * Specific checks will include: |
aoqi@0 | 63 | * |
aoqi@0 | 64 | * <ul> |
aoqi@0 | 65 | * |
aoqi@0 | 66 | * <li> Source and class files can be written to from either a Writer or an OutputStream. |
aoqi@0 | 67 | * |
aoqi@0 | 68 | * <li> Calling close multiple times does not re-register the file for |
aoqi@0 | 69 | * processing. |
aoqi@0 | 70 | * |
aoqi@0 | 71 | * </ul> |
aoqi@0 | 72 | */ |
aoqi@0 | 73 | public class TestFilerConstraints extends JavacTestingAbstractProcessor { |
aoqi@0 | 74 | private int round = 0; |
aoqi@0 | 75 | |
aoqi@0 | 76 | private PrintWriter pw_src1 = null; |
aoqi@0 | 77 | private PrintWriter pw_src2 = null; |
aoqi@0 | 78 | private OutputStream os_classFile1 = null; |
aoqi@0 | 79 | private Writer pw_classFile2 = null; |
aoqi@0 | 80 | |
aoqi@0 | 81 | public boolean process(Set<? extends TypeElement> annotations, |
aoqi@0 | 82 | RoundEnvironment roundEnv) { |
aoqi@0 | 83 | round++; |
aoqi@0 | 84 | |
aoqi@0 | 85 | try { |
aoqi@0 | 86 | switch(round) { |
aoqi@0 | 87 | // Open two source files |
aoqi@0 | 88 | case 1: |
aoqi@0 | 89 | pw_src1 = new PrintWriter(filer.createSourceFile("Src1").openWriter()); |
aoqi@0 | 90 | pw_src1.println("class Src1 {}"); |
aoqi@0 | 91 | pw_src1.close(); |
aoqi@0 | 92 | |
aoqi@0 | 93 | // Hold open across rounds |
aoqi@0 | 94 | pw_src2 = new PrintWriter(new OutputStreamWriter(filer.createSourceFile("Src2").openOutputStream())); |
aoqi@0 | 95 | break; |
aoqi@0 | 96 | |
aoqi@0 | 97 | case 2: |
aoqi@0 | 98 | testExpectedType(roundEnv, "Src1"); |
aoqi@0 | 99 | |
aoqi@0 | 100 | // Close Src1 a second time |
aoqi@0 | 101 | pw_src1.close(); |
aoqi@0 | 102 | |
aoqi@0 | 103 | pw_src2.println("class Src2 {}"); |
aoqi@0 | 104 | pw_src2.close(); |
aoqi@0 | 105 | |
aoqi@0 | 106 | break; |
aoqi@0 | 107 | |
aoqi@0 | 108 | case 3: |
aoqi@0 | 109 | testExpectedType(roundEnv, "Src2"); |
aoqi@0 | 110 | |
aoqi@0 | 111 | // Close Src2 a second time |
aoqi@0 | 112 | pw_src2.close(); |
aoqi@0 | 113 | |
aoqi@0 | 114 | os_classFile1 = filer.createClassFile("ClassFile1").openOutputStream(); |
aoqi@0 | 115 | for (int value : classFile1Bytes) |
aoqi@0 | 116 | os_classFile1.write((byte)value); |
aoqi@0 | 117 | os_classFile1.close(); |
aoqi@0 | 118 | |
aoqi@0 | 119 | break; |
aoqi@0 | 120 | |
aoqi@0 | 121 | case 4: |
aoqi@0 | 122 | testExpectedType(roundEnv, "ClassFile1"); |
aoqi@0 | 123 | |
aoqi@0 | 124 | // Close a second time |
aoqi@0 | 125 | os_classFile1.close(); |
aoqi@0 | 126 | |
aoqi@0 | 127 | testReopening(); |
aoqi@0 | 128 | |
aoqi@0 | 129 | pw_classFile2 = new PrintWriter(filer.createClassFile("ClassFile2", |
aoqi@0 | 130 | (Element[])null).openWriter()); |
aoqi@0 | 131 | |
aoqi@0 | 132 | for(int byteVal : classFile2Bytes) { |
aoqi@0 | 133 | // int value = (0xff00 & (classFile2Bytes[i]<<8)) | classFile2Bytes[i+1]; |
aoqi@0 | 134 | // System.out.print(Integer.toHexString(value)); |
aoqi@0 | 135 | //if ((i % 4) == 0) |
aoqi@0 | 136 | // System.out.println(); |
aoqi@0 | 137 | pw_classFile2.write((char) (0xff & byteVal)); |
aoqi@0 | 138 | } |
aoqi@0 | 139 | pw_classFile2.close(); |
aoqi@0 | 140 | |
aoqi@0 | 141 | break; |
aoqi@0 | 142 | |
aoqi@0 | 143 | |
aoqi@0 | 144 | |
aoqi@0 | 145 | case 5: |
aoqi@0 | 146 | testExpectedType(roundEnv, "ClassFile2"); |
aoqi@0 | 147 | // Close a second time |
aoqi@0 | 148 | pw_classFile2.close(); |
aoqi@0 | 149 | |
aoqi@0 | 150 | |
aoqi@0 | 151 | break; |
aoqi@0 | 152 | |
aoqi@0 | 153 | case 6: |
aoqi@0 | 154 | if (!roundEnv.processingOver() && !roundEnv.errorRaised()) |
aoqi@0 | 155 | throw new RuntimeException("Bad round state: " + roundEnv); |
aoqi@0 | 156 | break; |
aoqi@0 | 157 | |
aoqi@0 | 158 | default: |
aoqi@0 | 159 | throw new RuntimeException("Unexpected round number!"); |
aoqi@0 | 160 | } |
aoqi@0 | 161 | } catch (IOException ioe) { |
aoqi@0 | 162 | throw new RuntimeException(ioe); |
aoqi@0 | 163 | } |
aoqi@0 | 164 | |
aoqi@0 | 165 | return true; |
aoqi@0 | 166 | } |
aoqi@0 | 167 | |
aoqi@0 | 168 | /** |
aoqi@0 | 169 | * Test that the single expected expected type, name, is the root |
aoqi@0 | 170 | * element. |
aoqi@0 | 171 | */ |
aoqi@0 | 172 | private void testExpectedType(RoundEnvironment roundEnv, String name) { |
aoqi@0 | 173 | if (!roundEnv.getRootElements().isEmpty()) { |
aoqi@0 | 174 | for(TypeElement type : typesIn(roundEnv.getRootElements())) { |
aoqi@0 | 175 | if (!name.contentEquals(type.getSimpleName())) |
aoqi@0 | 176 | throw new RuntimeException("Unexpected type " + type.getSimpleName()); |
aoqi@0 | 177 | } |
aoqi@0 | 178 | } else |
aoqi@0 | 179 | throw new RuntimeException("Unexpected empty root elements."); |
aoqi@0 | 180 | } |
aoqi@0 | 181 | |
aoqi@0 | 182 | private void testReopening() throws IOException { |
aoqi@0 | 183 | String[] names = {"Src1", "Src2", "ClassFile1"}; |
aoqi@0 | 184 | for (String name : names) { |
aoqi@0 | 185 | try { |
aoqi@0 | 186 | filer.createSourceFile(name); |
aoqi@0 | 187 | throw new RuntimeException("Opened a source file for type " + name); |
aoqi@0 | 188 | } catch (FilerException fe) {;} |
aoqi@0 | 189 | |
aoqi@0 | 190 | try { |
aoqi@0 | 191 | filer.createClassFile(name); |
aoqi@0 | 192 | throw new RuntimeException("Opened a class file for type " + name); |
aoqi@0 | 193 | } catch (FilerException fe) {;} |
aoqi@0 | 194 | } |
aoqi@0 | 195 | |
aoqi@0 | 196 | // Try to open a resource over a source file |
aoqi@0 | 197 | try { |
aoqi@0 | 198 | filer.createResource(SOURCE_OUTPUT, "", "Src1.java"); |
aoqi@0 | 199 | throw new RuntimeException("Opened a text file over Src1.java!"); |
aoqi@0 | 200 | } catch (FilerException fe) {;} |
aoqi@0 | 201 | |
aoqi@0 | 202 | // Try to open a resource over a class file |
aoqi@0 | 203 | try { |
aoqi@0 | 204 | filer.createResource(CLASS_OUTPUT, "", "ClassFile1.class"); |
aoqi@0 | 205 | throw new RuntimeException("Opened a text file over Src1.java!"); |
aoqi@0 | 206 | } catch (FilerException fe) {;} |
aoqi@0 | 207 | |
aoqi@0 | 208 | } |
aoqi@0 | 209 | |
aoqi@0 | 210 | private int[] classFile1Bytes = |
aoqi@0 | 211 | {202, 254, 186, 190, 0, 0, 0, 50, |
aoqi@0 | 212 | 0, 13, 10, 0, 3, 0, 10, 7, |
aoqi@0 | 213 | 0, 11, 7, 0, 12, 1, 0, 6, |
aoqi@0 | 214 | 60, 105, 110, 105, 116, 62, 1, 0, |
aoqi@0 | 215 | 3, 40, 41, 86, 1, 0, 4, 67, |
aoqi@0 | 216 | 111, 100, 101, 1, 0, 15, 76, 105, |
aoqi@0 | 217 | 110, 101, 78, 117, 109, 98, 101, 114, |
aoqi@0 | 218 | 84, 97, 98, 108, 101, 1, 0, 10, |
aoqi@0 | 219 | 83, 111, 117, 114, 99, 101, 70, 105, |
aoqi@0 | 220 | 108, 101, 1, 0, 15, 67, 108, 97, |
aoqi@0 | 221 | 115, 115, 70, 105, 108, 101, 49, 46, |
aoqi@0 | 222 | 106, 97, 118, 97, 12, 0, 4, 0, |
aoqi@0 | 223 | 5, 1, 0, 10, 67, 108, 97, 115, |
aoqi@0 | 224 | 115, 70, 105, 108, 101, 49, 1, 0, |
aoqi@0 | 225 | 16, 106, 97, 118, 97, 47, 108, 97, |
aoqi@0 | 226 | 110, 103, 47, 79, 98, 106, 101, 99, |
aoqi@0 | 227 | 116, 0, 33, 0, 2, 0, 3, 0, |
aoqi@0 | 228 | 0, 0, 0, 0, 1, 0, 1, 0, |
aoqi@0 | 229 | 4, 0, 5, 0, 1, 0, 6, 0, |
aoqi@0 | 230 | 0, 0, 29, 0, 1, 0, 1, 0, |
aoqi@0 | 231 | 0, 0, 5, 42, 183, 0, 1, 177, |
aoqi@0 | 232 | 0, 0, 0, 1, 0, 7, 0, 0, |
aoqi@0 | 233 | 0, 6, 0, 1, 0, 0, 0, 1, |
aoqi@0 | 234 | 0, 1, 0, 8, 0, 0, 0, 2, |
aoqi@0 | 235 | 0, 9,}; |
aoqi@0 | 236 | |
aoqi@0 | 237 | private int[] classFile2Bytes = |
aoqi@0 | 238 | {202, 254, 186, 190, 0, 0, 0, 50, |
aoqi@0 | 239 | 0, 13, 10, 0, 3, 0, 10, 7, |
aoqi@0 | 240 | 0, 11, 7, 0, 12, 1, 0, 6, |
aoqi@0 | 241 | 60, 105, 110, 105, 116, 62, 1, 0, |
aoqi@0 | 242 | 3, 40, 41, 86, 1, 0, 4, 67, |
aoqi@0 | 243 | 111, 100, 101, 1, 0, 15, 76, 105, |
aoqi@0 | 244 | 110, 101, 78, 117, 109, 98, 101, 114, |
aoqi@0 | 245 | 84, 97, 98, 108, 101, 1, 0, 10, |
aoqi@0 | 246 | 83, 111, 117, 114, 99, 101, 70, 105, |
aoqi@0 | 247 | 108, 101, 1, 0, 15, 67, 108, 97, |
aoqi@0 | 248 | 115, 115, 70, 105, 108, 101, 50, 46, |
aoqi@0 | 249 | 106, 97, 118, 97, 12, 0, 4, 0, |
aoqi@0 | 250 | 5, 1, 0, 10, 67, 108, 97, 115, |
aoqi@0 | 251 | 115, 70, 105, 108, 101, 50, 1, 0, |
aoqi@0 | 252 | 16, 106, 97, 118, 97, 47, 108, 97, |
aoqi@0 | 253 | 110, 103, 47, 79, 98, 106, 101, 99, |
aoqi@0 | 254 | 116, 0, 33, 0, 2, 0, 3, 0, |
aoqi@0 | 255 | 0, 0, 0, 0, 1, 0, 1, 0, |
aoqi@0 | 256 | 4, 0, 5, 0, 1, 0, 6, 0, |
aoqi@0 | 257 | 0, 0, 29, 0, 1, 0, 1, 0, |
aoqi@0 | 258 | 0, 0, 5, 42, 183, 0, 1, 177, |
aoqi@0 | 259 | 0, 0, 0, 1, 0, 7, 0, 0, |
aoqi@0 | 260 | 0, 6, 0, 1, 0, 0, 0, 1, |
aoqi@0 | 261 | 0, 1, 0, 8, 0, 0, 0, 2, |
aoqi@0 | 262 | 0, 9,}; |
aoqi@0 | 263 | } |