langtools/test/tools/javac/processing/T6920317.java
author akulyakh
Thu, 21 May 2015 11:41:04 -0700
changeset 30730 d3ce7619db2c
parent 14963 974d4423c999
permissions -rw-r--r--
8076543: Add @modules as needed to the langtools tests Reviewed-by: jjg, shurailine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 14963
diff changeset
     2
 * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
     4
 *
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
     8
 *
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    13
 * accompanied this code).
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    14
 *
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4871
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4871
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4871
diff changeset
    21
 * questions.
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    22
 */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    23
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    24
/*
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    25
 * @test
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    26
 * @bug 6920317
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    27
 * @summary package-info.java file has to be specified on the javac cmdline, else it will not be avail
14963
974d4423c999 8005282: Use @library tag with non-relative path for javac tests
darcy
parents: 13634
diff changeset
    28
 * @library /tools/javac/lib
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 14963
diff changeset
    29
 * @modules jdk.compiler
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    30
 */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    31
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    32
import java.io.*;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    33
import java.util.*;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    34
import javax.annotation.processing.*;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    35
import javax.lang.model.*;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    36
import javax.lang.model.element.*;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    37
import javax.lang.model.util.*;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    38
import javax.tools.*;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    39
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    40
/**
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    41
 * The test exercises different ways of providing annotations for a package.
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    42
 * Each way provides an annotation with a unique argument. For each test
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    43
 * case, the test verifies that the annotation with the correct argument is
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    44
 * found by the compiler.
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    45
 */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    46
public class T6920317 {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    47
    public static void main(String... args) throws Exception {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    48
        new T6920317().run(args);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    49
    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    50
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    51
    // Used to describe properties of files to be put on command line, source path, class path
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    52
    enum Kind {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    53
        /** File is not used. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    54
        NONE,
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    55
        /** File is used. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    56
        OLD,
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    57
        /** Only applies to files on classpath/sourcepath, when there is another file on the
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    58
         *  other path of type OLD, in which case, this file must be newer than the other one. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    59
        NEW,
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    60
        /** Only applies to files on classpath/sourcepath, when there is no file in any other
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    61
         *  location, in which case, this file will be generated by the annotation processor. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    62
        GEN
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    63
    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    64
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    65
    void run(String... args) throws Exception {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    66
        // if no args given, all test cases are run
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    67
        // if args given, they indicate the test cases to be run
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    68
        for (int i = 0; i < args.length; i++) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    69
            tests.add(Integer.valueOf(args[i]));
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    70
        }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    71
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    72
        setup();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    73
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    74
        // Run tests for all combinations of files on command line, source path and class path.
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    75
        // Invalid combinations are skipped in the test method
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    76
        for (Kind cmdLine: EnumSet.of(Kind.NONE, Kind.OLD)) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    77
            for (Kind srcPath: Kind.values()) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    78
                for (Kind clsPath: Kind.values()) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    79
                    try {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    80
                        test(cmdLine, srcPath, clsPath);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    81
                    } catch (Exception e) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    82
                        e.printStackTrace();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    83
                        error("Exception " + e);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    84
                        // uncomment to stop on first failed test case
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    85
                        // throw e;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    86
                    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    87
                }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    88
            }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    89
        }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    90
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    91
        if (errors > 0)
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    92
            throw new Exception(errors + " errors occurred");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    93
    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    94
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    95
    /** One time setup for files and directories to be used in the various test cases. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    96
    void setup() throws Exception {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    97
        // Annotation used in test cases to annotate package. This file is
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    98
        // given on the command line in test cases.
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    99
        test_java = writeFile("Test.java", "package p; @interface Test { String value(); }");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   100
        // Compile the annotation for use later in setup
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   101
        File tmpClasses = new File("tmp.classes");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   102
        compile(tmpClasses, new String[] { }, test_java);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   103
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   104
        // package-info file to use on the command line when requied
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   105
        cl_pkgInfo_java = writeFile("cl/p/package-info.java", "@Test(\"CL\") package p;");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   106
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   107
        // source path containing package-info
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   108
        sp_old = new File("src.old");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   109
        writeFile("src.old/p/package-info.java", "@Test(\"SP_OLD\") package p;");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   110
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   111
        // class path containing package-info
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   112
        cp_old = new File("classes.old");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   113
        compile(cp_old, new String[] { "-classpath", tmpClasses.getPath() },
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   114
                writeFile("tmp.old/p/package-info.java", "@Test(\"CP_OLD\") package p;"));
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   115
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   116
        // source path containing package-info which is newer than the one in cp-old
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   117
        sp_new = new File("src.new");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   118
        File old_class = new File(cp_old, "p/package-info.class");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   119
        writeFile("src.new/p/package-info.java", "@Test(\"SP_NEW\") package p;", old_class);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   120
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   121
        // class path containing package-info which is newer than the one in sp-old
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   122
        cp_new = new File("classes.new");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   123
        File old_java = new File(sp_old, "p/package-info.java");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   124
        compile(cp_new, new String[] { "-classpath", tmpClasses.getPath() },
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   125
                writeFile("tmp.new/p/package-info.java", "@Test(\"CP_NEW\") package p;", old_java));
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   126
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   127
        // directory containing package-info.java to be "generated" later by annotation processor
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   128
        sp_gen = new File("src.gen");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   129
        writeFile("src.gen/p/package-info.java", "@Test(\"SP_GEN\") package p;");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   130
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   131
        // directory containing package-info.class to be "generated" later by annotation processor
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   132
        cp_gen = new File("classes.gen");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   133
        compile(cp_gen, new String[] { "-classpath", tmpClasses.getPath() },
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   134
                writeFile("tmp.gen/p/package-info.java", "@Test(\"CP_GEN\") package p;"));
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   135
    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   136
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   137
    void test(Kind cl, Kind sp, Kind cp) throws Exception {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   138
        if (skip(cl, sp, cp))
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   139
            return;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   140
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   141
        ++count;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   142
        // if test cases specified, skip this test case if not selected
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   143
        if (tests.size() > 0 && !tests.contains(count))
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   144
            return;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   145
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   146
        System.err.println("Test " + count + " cl:" + cl + " sp:" + sp + " cp:" + cp);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   147
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   148
        // test specific tmp directory
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   149
        File test_tmp = new File("tmp.test" + count);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   150
        test_tmp.mkdirs();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   151
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   152
        // build up list of options and files to be compiled
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   153
        List<String> opts = new ArrayList<String>();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   154
        List<File> files = new ArrayList<File>();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   155
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   156
        // expected value for annotation
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   157
        String expect = null;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   158
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   159
        opts.add("-processorpath");
13634
91f0b2d4d816 7192744: fix up tests to accommodate jtreg spec change
jjg
parents: 6720
diff changeset
   160
        String testClasses = System.getProperty("test.classes");
91f0b2d4d816 7192744: fix up tests to accommodate jtreg spec change
jjg
parents: 6720
diff changeset
   161
        String testClassPath = System.getProperty("test.class.path", testClasses);
91f0b2d4d816 7192744: fix up tests to accommodate jtreg spec change
jjg
parents: 6720
diff changeset
   162
        opts.add(testClassPath);
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   163
        opts.add("-processor");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   164
        opts.add(Processor.class.getName());
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   165
        opts.add("-proc:only");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   166
        opts.add("-d");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   167
        opts.add(test_tmp.getPath());
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   168
        //opts.add("-verbose");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   169
        files.add(test_java);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   170
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   171
        /*
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   172
         * Analyze each of cl, cp, sp, building up the options and files to
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   173
         * be compiled, and determining the expected outcome fo the test case.
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   174
         */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   175
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   176
        // command line file: either omitted or given
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   177
        if (cl == Kind.OLD) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   178
            files.add(cl_pkgInfo_java);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   179
            // command line files always supercede files on paths
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   180
            expect = "CL";
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   181
        }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   182
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   183
        // source path:
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   184
        switch (sp) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   185
        case NONE:
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   186
            break;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   187
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   188
        case OLD:
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   189
            opts.add("-sourcepath");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   190
            opts.add(sp_old.getPath());
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   191
            if (expect == null && cp == Kind.NONE) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   192
                assert cl == Kind.NONE && cp == Kind.NONE;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   193
                expect = "SP_OLD";
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   194
            }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   195
            break;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   196
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   197
        case NEW:
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   198
            opts.add("-sourcepath");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   199
            opts.add(sp_new.getPath());
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   200
            if (expect == null) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   201
                assert cl == Kind.NONE && cp == Kind.OLD;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   202
                expect = "SP_NEW";
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   203
            }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   204
            break;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   205
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   206
        case GEN:
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   207
            opts.add("-Agen=" + new File(sp_gen, "p/package-info.java"));
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   208
            assert cl == Kind.NONE && cp == Kind.NONE;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   209
            expect = "SP_GEN";
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   210
            break;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   211
        }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   212
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   213
        // class path:
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   214
        switch (cp) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   215
        case NONE:
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   216
            break;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   217
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   218
        case OLD:
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   219
            opts.add("-classpath");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   220
            opts.add(cp_old.getPath());
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   221
            if (expect == null && sp == Kind.NONE) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   222
                assert cl == Kind.NONE && sp == Kind.NONE;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   223
                expect = "CP_OLD";
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   224
            }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   225
            break;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   226
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   227
        case NEW:
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   228
            opts.add("-classpath");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   229
            opts.add(cp_new.getPath());
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   230
            if (expect == null) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   231
                assert cl == Kind.NONE && sp == Kind.OLD;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   232
                expect = "CP_NEW";
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   233
            }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   234
            break;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   235
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   236
        case GEN:
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   237
            opts.add("-Agen=" + new File(cp_gen, "p/package-info.class"));
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   238
            assert cl == Kind.NONE && sp == Kind.NONE;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   239
            expect = "CP_GEN";
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   240
            break;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   241
        }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   242
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   243
        // pass expected value to annotation processor
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   244
        assert expect != null;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   245
        opts.add("-Aexpect=" + expect);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   246
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   247
        // compile the files with the options that have been built up
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   248
        compile(opts, files);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   249
    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   250
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   251
    /**
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   252
     * Return true if this combination of parameters does not identify a useful test case.
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   253
     */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   254
    boolean skip(Kind cl, Kind sp, Kind cp) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   255
        // skip if no package files required
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   256
        if (cl == Kind.NONE && sp == Kind.NONE && cp == Kind.NONE)
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   257
            return true;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   258
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   259
        // skip if both sp and sp are OLD, since results may be indeterminate
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   260
        if (sp == Kind.OLD && cp == Kind.OLD)
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   261
            return true;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   262
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   263
        // skip if sp or cp is NEW but the other is not OLD
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   264
        if ((sp == Kind.NEW && cp != Kind.OLD) || (cp == Kind.NEW && sp != Kind.OLD))
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   265
            return true;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   266
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   267
        // only use GEN if no other package-info files present
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   268
        if (sp == Kind.GEN && !(cl == Kind.NONE && cp == Kind.NONE) ||
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   269
            cp == Kind.GEN && !(cl == Kind.NONE && sp == Kind.NONE)) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   270
            return true;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   271
        }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   272
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   273
        // remaining combinations are valid
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   274
        return false;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   275
    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   276
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   277
    /** Write a file with a given body. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   278
    File writeFile(String path, String body) throws Exception {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   279
        File f = new File(path);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   280
        if (f.getParentFile() != null)
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   281
            f.getParentFile().mkdirs();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   282
        Writer out = new FileWriter(path);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   283
        try {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   284
            out.write(body);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   285
        } finally {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   286
            out.close();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   287
        }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   288
        return f;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   289
    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   290
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   291
    /** Write a file with a given body, ensuring that the file is newer than a reference file. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   292
    File writeFile(String path, String body, File ref) throws Exception {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   293
        for (int i = 0; i < 5; i++) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   294
            File f = writeFile(path, body);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   295
            if (f.lastModified() > ref.lastModified())
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   296
                return f;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   297
            Thread.sleep(2000);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   298
        }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   299
        throw new Exception("cannot create file " + path + " newer than " + ref);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   300
    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   301
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   302
    /** Compile a file to a given directory, with options provided. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   303
    void compile(File dir, String[] opts, File src) throws Exception {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   304
        dir.mkdirs();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   305
        List<String> opts2 = new ArrayList<String>();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   306
        opts2.addAll(Arrays.asList("-d", dir.getPath()));
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   307
        opts2.addAll(Arrays.asList(opts));
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   308
        compile(opts2, Collections.singletonList(src));
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   309
    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   310
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   311
    /** Compile files with options provided. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   312
    void compile(List<String> opts, List<File> files) throws Exception {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   313
        System.err.println("javac: " + opts + " " + files);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   314
        List<String> args = new ArrayList<String>();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   315
        args.addAll(opts);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   316
        for (File f: files)
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   317
            args.add(f.getPath());
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   318
        StringWriter sw = new StringWriter();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   319
        PrintWriter pw = new PrintWriter(sw);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   320
        int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   321
        pw.flush();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   322
        if (sw.getBuffer().length() > 0)
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   323
            System.err.println(sw.toString());
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   324
        if (rc != 0)
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   325
            throw new Exception("compilation failed: rc=" + rc);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   326
    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   327
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   328
    /** Report an error. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   329
    void error(String msg) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   330
        System.err.println("Error: " + msg);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   331
        errors++;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   332
    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   333
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   334
    /** Test case counter. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   335
    int count;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   336
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   337
    /** Number of errors found. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   338
    int errors;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   339
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   340
    /** Optional set of test cases to be run; empty implies all test cases. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   341
    Set<Integer> tests = new HashSet<Integer>();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   342
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   343
    /*  Files created by setup. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   344
    File test_java;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   345
    File sp_old;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   346
    File sp_new;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   347
    File sp_gen;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   348
    File cp_old;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   349
    File cp_new;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   350
    File cp_gen;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   351
    File cl_pkgInfo_java;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   352
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   353
    /** Annotation processor used to verify the expected value for the
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   354
        package annotations found by javac. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   355
    @SupportedOptions({ "gen", "expect" })
6720
f16f91662ad8 6983738: Use a JavacTestingAbstractProcessor
darcy
parents: 5520
diff changeset
   356
    public static class Processor extends JavacTestingAbstractProcessor {
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   357
        public boolean process(Set<? extends TypeElement> annots, RoundEnvironment renv) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   358
            round++;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   359
            System.err.println("Round " + round + " annots:" + annots + " rootElems:" + renv.getRootElements());
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   360
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   361
            // if this is the first round and the gen option is given, use the filer to create
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   362
            // a copy of the file specified by the gen option.
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   363
            String gen = getOption("gen");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   364
            if (round == 1 && gen != null) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   365
                try {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   366
                    Filer filer = processingEnv.getFiler();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   367
                    JavaFileObject f;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   368
                    if (gen.endsWith(".java"))
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   369
                        f = filer.createSourceFile("p.package-info");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   370
                    else
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   371
                        f = filer.createClassFile("p.package-info");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   372
                    System.err.println("copy " + gen + " to " + f.getName());
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   373
                    write(f, read(new File(gen)));
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   374
                } catch (IOException e) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   375
                    error("Cannot create package-info file: " + e);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   376
                }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   377
            }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   378
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   379
            // if annotation processing is complete, verify the package annotation
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   380
            // found by the compiler.
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   381
            if (renv.processingOver()) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   382
                System.err.println("final round");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   383
                Elements eu = processingEnv.getElementUtils();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   384
                TypeElement te = eu.getTypeElement("p.Test");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   385
                PackageElement pe = eu.getPackageOf(te);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   386
                System.err.println("final: te:" + te + " pe:" + pe);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   387
                List<? extends AnnotationMirror> annos = pe.getAnnotationMirrors();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   388
                System.err.println("final: annos:" + annos);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   389
                if (annos.size() == 1) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   390
                    String expect = "@" + te + "(\"" + getOption("expect") + "\")";
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   391
                    String actual = annos.get(0).toString();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   392
                    checkEqual("package annotations", actual, expect);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   393
                } else {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   394
                    error("Wrong number of annotations found: (" + annos.size() + ") " + annos);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   395
                }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   396
            }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   397
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   398
            return true;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   399
        }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   400
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   401
        /** Get an option given to the annotation processor. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   402
        String getOption(String name) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   403
            return processingEnv.getOptions().get(name);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   404
        }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   405
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   406
        /** Read a file. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   407
        byte[] read(File file) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   408
            byte[] bytes = new byte[(int) file.length()];
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   409
            DataInputStream in = null;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   410
            try {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   411
                in = new DataInputStream(new FileInputStream(file));
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   412
                in.readFully(bytes);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   413
            } catch (IOException e) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   414
                error("Error reading file: " + e);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   415
            } finally {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   416
                if (in != null) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   417
                    try {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   418
                        in.close();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   419
                    } catch (IOException e) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   420
                        error("Error closing file: " + e);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   421
                    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   422
                }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   423
            }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   424
            return  bytes;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   425
        }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   426
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   427
        /** Write a file. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   428
        void write(JavaFileObject file, byte[] bytes) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   429
            OutputStream out = null;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   430
            try {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   431
                out = file.openOutputStream();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   432
                out.write(bytes, 0, bytes.length);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   433
            } catch (IOException e) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   434
                error("Error writing file: " + e);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   435
            } finally {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   436
                if (out != null) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   437
                    try {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   438
                        out.close();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   439
                    } catch (IOException e) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   440
                        error("Error closing file: " + e);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   441
                    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   442
                }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   443
            }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   444
        }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   445
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   446
        /** Check two strings are equal, and report an error if they are not. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   447
        private void checkEqual(String label, String actual, String expect) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   448
            if (!actual.equals(expect)) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   449
                error("Unexpected value for " + label + "; actual=" + actual + ", expected=" + expect);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   450
            }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   451
        }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   452
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   453
        /** Report an error to the annotation processing system. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   454
        void error(String msg) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   455
            Messager messager = processingEnv.getMessager();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   456
            messager.printMessage(Diagnostic.Kind.ERROR, msg);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   457
        }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   458
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   459
        int round;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   460
    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   461
}