0N/A/*
698N/A * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6380018 6453386 6457283
0N/A * @summary Test that the constraints guaranteed by the Filer and maintained
0N/A * @author Joseph D. Darcy
698N/A * @library ../../lib
0N/A * @build TestFilerConstraints
0N/A * @compile -encoding iso-8859-1 -processor TestFilerConstraints -proc:only TestFilerConstraints.java
0N/A */
0N/A
0N/Aimport java.util.Set;
0N/Aimport javax.annotation.processing.*;
0N/Aimport javax.lang.model.SourceVersion;
0N/Aimport static javax.lang.model.SourceVersion.*;
0N/Aimport javax.lang.model.element.*;
0N/Aimport javax.lang.model.util.*;
0N/Aimport static javax.lang.model.util.ElementFilter.*;
0N/Aimport static javax.tools.Diagnostic.Kind.*;
0N/Aimport static javax.tools.StandardLocation.*;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.nio.charset.Charset;
0N/A
0N/A/**
0N/A * A processor that verifies the explicit and implicit constraints in
0N/A * the Filer contract are maintained:
0N/A *
0N/A * <blockquote>
0N/A *
0N/A * During each run of an annotation processing tool, a file with a
0N/A * given pathname may be created only once. If that file already
0N/A * exists before the first attempt to create it, the old contents
0N/A * will be deleted. Any subsequent attempt to create the same file
0N/A * during a run will throw a FilerException, as will attempting to
0N/A * open both a class file and source file for the same type name.
0N/A *
0N/A * </blockquote>
0N/A *
0N/A * Specific checks will include:
0N/A *
0N/A * <ul>
0N/A *
0N/A * <li> Source and class files can be written to from either a Writer or an OutputStream.
0N/A *
0N/A * <li> Calling close multiple times does not re-register the file for
0N/A * processing.
0N/A *
0N/A * </ul>
0N/A */
698N/Apublic class TestFilerConstraints extends JavacTestingAbstractProcessor {
0N/A private int round = 0;
0N/A
0N/A private PrintWriter pw_src1 = null;
0N/A private PrintWriter pw_src2 = null;
0N/A private OutputStream os_classFile1 = null;
0N/A private Writer pw_classFile2 = null;
0N/A
0N/A public boolean process(Set<? extends TypeElement> annotations,
0N/A RoundEnvironment roundEnv) {
0N/A round++;
0N/A
0N/A try {
0N/A switch(round) {
0N/A // Open two source files
0N/A case 1:
0N/A pw_src1 = new PrintWriter(filer.createSourceFile("Src1").openWriter());
0N/A pw_src1.println("class Src1 {}");
0N/A pw_src1.close();
0N/A
0N/A // Hold open across rounds
0N/A pw_src2 = new PrintWriter(new OutputStreamWriter(filer.createSourceFile("Src2").openOutputStream()));
0N/A break;
0N/A
0N/A case 2:
0N/A testExpectedType(roundEnv, "Src1");
0N/A
0N/A // Close Src1 a second time
0N/A pw_src1.close();
0N/A
0N/A pw_src2.println("class Src2 {}");
0N/A pw_src2.close();
0N/A
0N/A break;
0N/A
0N/A case 3:
0N/A testExpectedType(roundEnv, "Src2");
0N/A
0N/A // Close Src2 a second time
0N/A pw_src2.close();
0N/A
0N/A os_classFile1 = filer.createClassFile("ClassFile1").openOutputStream();
0N/A for (int value : classFile1Bytes)
0N/A os_classFile1.write((byte)value);
0N/A os_classFile1.close();
0N/A
0N/A break;
0N/A
0N/A case 4:
0N/A testExpectedType(roundEnv, "ClassFile1");
0N/A
0N/A // Close a second time
0N/A os_classFile1.close();
0N/A
0N/A testReopening();
0N/A
0N/A pw_classFile2 = new PrintWriter(filer.createClassFile("ClassFile2",
0N/A (Element[])null).openWriter());
0N/A
0N/A for(int byteVal : classFile2Bytes) {
0N/A // int value = (0xff00 & (classFile2Bytes[i]<<8)) | classFile2Bytes[i+1];
0N/A // System.out.print(Integer.toHexString(value));
0N/A //if ((i % 4) == 0)
0N/A // System.out.println();
0N/A pw_classFile2.write((char) (0xff & byteVal));
0N/A }
0N/A pw_classFile2.close();
0N/A
0N/A break;
0N/A
0N/A
0N/A
0N/A case 5:
0N/A testExpectedType(roundEnv, "ClassFile2");
0N/A // Close a second time
0N/A pw_classFile2.close();
0N/A
0N/A
0N/A break;
0N/A
0N/A case 6:
0N/A if (!roundEnv.processingOver() && !roundEnv.errorRaised())
0N/A throw new RuntimeException("Bad round state: " + roundEnv);
0N/A break;
0N/A
0N/A default:
0N/A throw new RuntimeException("Unexpected round number!");
0N/A }
0N/A } catch (IOException ioe) {
0N/A throw new RuntimeException(ioe);
0N/A }
0N/A
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Test that the single expected expected type, name, is the root
0N/A * element.
0N/A */
0N/A private void testExpectedType(RoundEnvironment roundEnv, String name) {
0N/A if (!roundEnv.getRootElements().isEmpty()) {
0N/A for(TypeElement type : typesIn(roundEnv.getRootElements())) {
0N/A if (!name.contentEquals(type.getSimpleName()))
0N/A throw new RuntimeException("Unexpected type " + type.getSimpleName());
0N/A }
0N/A } else
0N/A throw new RuntimeException("Unexpected empty root elements.");
0N/A }
0N/A
0N/A private void testReopening() throws IOException {
0N/A String[] names = {"Src1", "Src2", "ClassFile1"};
0N/A for (String name : names) {
0N/A try {
0N/A filer.createSourceFile(name);
0N/A throw new RuntimeException("Opened a source file for type " + name);
0N/A } catch (FilerException fe) {;}
0N/A
0N/A try {
0N/A filer.createClassFile(name);
0N/A throw new RuntimeException("Opened a class file for type " + name);
0N/A } catch (FilerException fe) {;}
0N/A }
0N/A
0N/A // Try to open a resource over a source file
0N/A try {
0N/A filer.createResource(SOURCE_OUTPUT, "", "Src1.java");
0N/A throw new RuntimeException("Opened a text file over Src1.java!");
0N/A } catch (FilerException fe) {;}
0N/A
0N/A // Try to open a resource over a class file
0N/A try {
0N/A filer.createResource(CLASS_OUTPUT, "", "ClassFile1.class");
0N/A throw new RuntimeException("Opened a text file over Src1.java!");
0N/A } catch (FilerException fe) {;}
0N/A
0N/A }
0N/A
0N/A private int[] classFile1Bytes =
0N/A {202, 254, 186, 190, 0, 0, 0, 50,
0N/A 0, 13, 10, 0, 3, 0, 10, 7,
0N/A 0, 11, 7, 0, 12, 1, 0, 6,
0N/A 60, 105, 110, 105, 116, 62, 1, 0,
0N/A 3, 40, 41, 86, 1, 0, 4, 67,
0N/A 111, 100, 101, 1, 0, 15, 76, 105,
0N/A 110, 101, 78, 117, 109, 98, 101, 114,
0N/A 84, 97, 98, 108, 101, 1, 0, 10,
0N/A 83, 111, 117, 114, 99, 101, 70, 105,
0N/A 108, 101, 1, 0, 15, 67, 108, 97,
0N/A 115, 115, 70, 105, 108, 101, 49, 46,
0N/A 106, 97, 118, 97, 12, 0, 4, 0,
0N/A 5, 1, 0, 10, 67, 108, 97, 115,
0N/A 115, 70, 105, 108, 101, 49, 1, 0,
0N/A 16, 106, 97, 118, 97, 47, 108, 97,
0N/A 110, 103, 47, 79, 98, 106, 101, 99,
0N/A 116, 0, 33, 0, 2, 0, 3, 0,
0N/A 0, 0, 0, 0, 1, 0, 1, 0,
0N/A 4, 0, 5, 0, 1, 0, 6, 0,
0N/A 0, 0, 29, 0, 1, 0, 1, 0,
0N/A 0, 0, 5, 42, 183, 0, 1, 177,
0N/A 0, 0, 0, 1, 0, 7, 0, 0,
0N/A 0, 6, 0, 1, 0, 0, 0, 1,
0N/A 0, 1, 0, 8, 0, 0, 0, 2,
0N/A 0, 9,};
0N/A
0N/A private int[] classFile2Bytes =
0N/A {202, 254, 186, 190, 0, 0, 0, 50,
0N/A 0, 13, 10, 0, 3, 0, 10, 7,
0N/A 0, 11, 7, 0, 12, 1, 0, 6,
0N/A 60, 105, 110, 105, 116, 62, 1, 0,
0N/A 3, 40, 41, 86, 1, 0, 4, 67,
0N/A 111, 100, 101, 1, 0, 15, 76, 105,
0N/A 110, 101, 78, 117, 109, 98, 101, 114,
0N/A 84, 97, 98, 108, 101, 1, 0, 10,
0N/A 83, 111, 117, 114, 99, 101, 70, 105,
0N/A 108, 101, 1, 0, 15, 67, 108, 97,
0N/A 115, 115, 70, 105, 108, 101, 50, 46,
0N/A 106, 97, 118, 97, 12, 0, 4, 0,
0N/A 5, 1, 0, 10, 67, 108, 97, 115,
0N/A 115, 70, 105, 108, 101, 50, 1, 0,
0N/A 16, 106, 97, 118, 97, 47, 108, 97,
0N/A 110, 103, 47, 79, 98, 106, 101, 99,
0N/A 116, 0, 33, 0, 2, 0, 3, 0,
0N/A 0, 0, 0, 0, 1, 0, 1, 0,
0N/A 4, 0, 5, 0, 1, 0, 6, 0,
0N/A 0, 0, 29, 0, 1, 0, 1, 0,
0N/A 0, 0, 5, 42, 183, 0, 1, 177,
0N/A 0, 0, 0, 1, 0, 7, 0, 0,
0N/A 0, 6, 0, 1, 0, 0, 0, 1,
0N/A 0, 1, 0, 8, 0, 0, 0, 2,
0N/A 0, 9,};
0N/A}