langtools/test/tools/javac/processing/T6920317.java
author sogoel
Thu, 05 Jun 2014 10:57:10 -0700
changeset 24797 850ebd4d80a7
parent 14963 974d4423c999
child 30730 d3ce7619db2c
permissions -rw-r--r--
8044072: Group 2: create .out files for OverrideChecks tests in tools/javac dir Reviewed-by: jjg
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
/*
13634
91f0b2d4d816 7192744: fix up tests to accommodate jtreg spec change
jjg
parents: 6720
diff changeset
     2
 * Copyright (c) 2010, 2012, 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
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    29
 */
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
import java.io.*;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    32
import java.util.*;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    33
import javax.annotation.processing.*;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    34
import javax.lang.model.*;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    35
import javax.lang.model.element.*;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    36
import javax.lang.model.util.*;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    37
import javax.tools.*;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    38
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
 * The test exercises different ways of providing annotations for a package.
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    41
 * 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
    42
 * 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
    43
 * found by the compiler.
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    44
 */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    45
public class T6920317 {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    46
    public static void main(String... args) throws Exception {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    47
        new T6920317().run(args);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    48
    }
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
    // 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
    51
    enum Kind {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    52
        /** File is not used. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    53
        NONE,
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    54
        /** File is used. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    55
        OLD,
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    56
        /** 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
    57
         *  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
    58
        NEW,
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    59
        /** 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
    60
         *  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
    61
        GEN
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    62
    }
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
    void run(String... args) throws Exception {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    65
        // if no args given, all test cases are run
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    66
        // if args given, they indicate the test cases to be run
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    67
        for (int i = 0; i < args.length; i++) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    68
            tests.add(Integer.valueOf(args[i]));
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    69
        }
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
        setup();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    72
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    73
        // 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
    74
        // Invalid combinations are skipped in the test method
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    75
        for (Kind cmdLine: EnumSet.of(Kind.NONE, Kind.OLD)) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    76
            for (Kind srcPath: Kind.values()) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    77
                for (Kind clsPath: Kind.values()) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    78
                    try {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    79
                        test(cmdLine, srcPath, clsPath);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    80
                    } catch (Exception e) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    81
                        e.printStackTrace();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    82
                        error("Exception " + e);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    83
                        // uncomment to stop on first failed test case
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    84
                        // throw e;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    85
                    }
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
        if (errors > 0)
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    91
            throw new Exception(errors + " errors occurred");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    92
    }
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
    /** 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
    95
    void setup() throws Exception {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    96
        // Annotation used in test cases to annotate package. This file is
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    97
        // given on the command line in test cases.
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    98
        test_java = writeFile("Test.java", "package p; @interface Test { String value(); }");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    99
        // Compile the annotation for use later in setup
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   100
        File tmpClasses = new File("tmp.classes");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   101
        compile(tmpClasses, new String[] { }, test_java);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   102
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   103
        // package-info file to use on the command line when requied
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   104
        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
   105
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   106
        // source path containing package-info
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   107
        sp_old = new File("src.old");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   108
        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
   109
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   110
        // class path containing package-info
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   111
        cp_old = new File("classes.old");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   112
        compile(cp_old, new String[] { "-classpath", tmpClasses.getPath() },
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   113
                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
   114
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   115
        // 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
   116
        sp_new = new File("src.new");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   117
        File old_class = new File(cp_old, "p/package-info.class");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   118
        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
   119
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   120
        // 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
   121
        cp_new = new File("classes.new");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   122
        File old_java = new File(sp_old, "p/package-info.java");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   123
        compile(cp_new, new String[] { "-classpath", tmpClasses.getPath() },
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   124
                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
   125
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   126
        // 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
   127
        sp_gen = new File("src.gen");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   128
        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
   129
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   130
        // 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
   131
        cp_gen = new File("classes.gen");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   132
        compile(cp_gen, new String[] { "-classpath", tmpClasses.getPath() },
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   133
                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
   134
    }
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
    void test(Kind cl, Kind sp, Kind cp) throws Exception {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   137
        if (skip(cl, sp, cp))
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   138
            return;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   139
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   140
        ++count;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   141
        // if test cases specified, skip this test case if not selected
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   142
        if (tests.size() > 0 && !tests.contains(count))
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   143
            return;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   144
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   145
        System.err.println("Test " + count + " cl:" + cl + " sp:" + sp + " cp:" + cp);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   146
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   147
        // test specific tmp directory
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   148
        File test_tmp = new File("tmp.test" + count);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   149
        test_tmp.mkdirs();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   150
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   151
        // build up list of options and files to be compiled
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   152
        List<String> opts = new ArrayList<String>();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   153
        List<File> files = new ArrayList<File>();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   154
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   155
        // expected value for annotation
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   156
        String expect = null;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   157
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   158
        opts.add("-processorpath");
13634
91f0b2d4d816 7192744: fix up tests to accommodate jtreg spec change
jjg
parents: 6720
diff changeset
   159
        String testClasses = System.getProperty("test.classes");
91f0b2d4d816 7192744: fix up tests to accommodate jtreg spec change
jjg
parents: 6720
diff changeset
   160
        String testClassPath = System.getProperty("test.class.path", testClasses);
91f0b2d4d816 7192744: fix up tests to accommodate jtreg spec change
jjg
parents: 6720
diff changeset
   161
        opts.add(testClassPath);
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   162
        opts.add("-processor");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   163
        opts.add(Processor.class.getName());
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   164
        opts.add("-proc:only");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   165
        opts.add("-d");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   166
        opts.add(test_tmp.getPath());
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   167
        //opts.add("-verbose");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   168
        files.add(test_java);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   169
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
         * 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
   172
         * be compiled, and determining the expected outcome fo the test case.
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   173
         */
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
        // command line file: either omitted or given
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   176
        if (cl == Kind.OLD) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   177
            files.add(cl_pkgInfo_java);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   178
            // command line files always supercede files on paths
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   179
            expect = "CL";
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   180
        }
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
        // source path:
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   183
        switch (sp) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   184
        case NONE:
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   185
            break;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   186
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   187
        case OLD:
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   188
            opts.add("-sourcepath");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   189
            opts.add(sp_old.getPath());
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   190
            if (expect == null && cp == Kind.NONE) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   191
                assert cl == Kind.NONE && cp == Kind.NONE;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   192
                expect = "SP_OLD";
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   193
            }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   194
            break;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   195
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   196
        case NEW:
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   197
            opts.add("-sourcepath");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   198
            opts.add(sp_new.getPath());
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   199
            if (expect == null) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   200
                assert cl == Kind.NONE && cp == Kind.OLD;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   201
                expect = "SP_NEW";
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   202
            }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   203
            break;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   204
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   205
        case GEN:
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   206
            opts.add("-Agen=" + new File(sp_gen, "p/package-info.java"));
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   207
            assert cl == Kind.NONE && cp == Kind.NONE;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   208
            expect = "SP_GEN";
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   209
            break;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   210
        }
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
        // class path:
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   213
        switch (cp) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   214
        case NONE:
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   215
            break;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   216
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   217
        case OLD:
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   218
            opts.add("-classpath");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   219
            opts.add(cp_old.getPath());
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   220
            if (expect == null && sp == Kind.NONE) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   221
                assert cl == Kind.NONE && sp == Kind.NONE;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   222
                expect = "CP_OLD";
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   223
            }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   224
            break;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   225
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   226
        case NEW:
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   227
            opts.add("-classpath");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   228
            opts.add(cp_new.getPath());
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   229
            if (expect == null) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   230
                assert cl == Kind.NONE && sp == Kind.OLD;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   231
                expect = "CP_NEW";
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   232
            }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   233
            break;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   234
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   235
        case GEN:
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   236
            opts.add("-Agen=" + new File(cp_gen, "p/package-info.class"));
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   237
            assert cl == Kind.NONE && sp == Kind.NONE;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   238
            expect = "CP_GEN";
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   239
            break;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   240
        }
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
        // pass expected value to annotation processor
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   243
        assert expect != null;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   244
        opts.add("-Aexpect=" + expect);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   245
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   246
        // compile the files with the options that have been built up
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   247
        compile(opts, files);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   248
    }
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
     * 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
   252
     */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   253
    boolean skip(Kind cl, Kind sp, Kind cp) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   254
        // skip if no package files required
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   255
        if (cl == Kind.NONE && sp == Kind.NONE && cp == Kind.NONE)
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   256
            return true;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   257
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   258
        // 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
   259
        if (sp == Kind.OLD && cp == Kind.OLD)
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   260
            return true;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   261
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   262
        // 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
   263
        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
   264
            return true;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   265
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   266
        // only use GEN if no other package-info files present
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   267
        if (sp == Kind.GEN && !(cl == Kind.NONE && cp == Kind.NONE) ||
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   268
            cp == Kind.GEN && !(cl == Kind.NONE && sp == Kind.NONE)) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   269
            return true;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   270
        }
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
        // remaining combinations are valid
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   273
        return false;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   274
    }
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
    /** Write a file with a given body. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   277
    File writeFile(String path, String body) throws Exception {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   278
        File f = new File(path);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   279
        if (f.getParentFile() != null)
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   280
            f.getParentFile().mkdirs();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   281
        Writer out = new FileWriter(path);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   282
        try {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   283
            out.write(body);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   284
        } finally {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   285
            out.close();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   286
        }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   287
        return f;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   288
    }
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
    /** 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
   291
    File writeFile(String path, String body, File ref) throws Exception {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   292
        for (int i = 0; i < 5; i++) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   293
            File f = writeFile(path, body);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   294
            if (f.lastModified() > ref.lastModified())
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   295
                return f;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   296
            Thread.sleep(2000);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   297
        }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   298
        throw new Exception("cannot create file " + path + " newer than " + ref);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   299
    }
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
    /** Compile a file to a given directory, with options provided. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   302
    void compile(File dir, String[] opts, File src) throws Exception {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   303
        dir.mkdirs();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   304
        List<String> opts2 = new ArrayList<String>();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   305
        opts2.addAll(Arrays.asList("-d", dir.getPath()));
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   306
        opts2.addAll(Arrays.asList(opts));
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   307
        compile(opts2, Collections.singletonList(src));
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   308
    }
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
    /** Compile files with options provided. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   311
    void compile(List<String> opts, List<File> files) throws Exception {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   312
        System.err.println("javac: " + opts + " " + files);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   313
        List<String> args = new ArrayList<String>();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   314
        args.addAll(opts);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   315
        for (File f: files)
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   316
            args.add(f.getPath());
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   317
        StringWriter sw = new StringWriter();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   318
        PrintWriter pw = new PrintWriter(sw);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   319
        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
   320
        pw.flush();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   321
        if (sw.getBuffer().length() > 0)
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   322
            System.err.println(sw.toString());
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   323
        if (rc != 0)
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   324
            throw new Exception("compilation failed: rc=" + rc);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   325
    }
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
    /** Report an error. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   328
    void error(String msg) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   329
        System.err.println("Error: " + msg);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   330
        errors++;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   331
    }
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
    /** Test case counter. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   334
    int count;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   335
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   336
    /** Number of errors found. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   337
    int errors;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   338
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   339
    /** 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
   340
    Set<Integer> tests = new HashSet<Integer>();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   341
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   342
    /*  Files created by setup. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   343
    File test_java;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   344
    File sp_old;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   345
    File sp_new;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   346
    File sp_gen;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   347
    File cp_old;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   348
    File cp_new;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   349
    File cp_gen;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   350
    File cl_pkgInfo_java;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   351
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   352
    /** Annotation processor used to verify the expected value for the
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   353
        package annotations found by javac. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   354
    @SupportedOptions({ "gen", "expect" })
6720
f16f91662ad8 6983738: Use a JavacTestingAbstractProcessor
darcy
parents: 5520
diff changeset
   355
    public static class Processor extends JavacTestingAbstractProcessor {
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   356
        public boolean process(Set<? extends TypeElement> annots, RoundEnvironment renv) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   357
            round++;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   358
            System.err.println("Round " + round + " annots:" + annots + " rootElems:" + renv.getRootElements());
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   359
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   360
            // 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
   361
            // a copy of the file specified by the gen option.
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   362
            String gen = getOption("gen");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   363
            if (round == 1 && gen != null) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   364
                try {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   365
                    Filer filer = processingEnv.getFiler();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   366
                    JavaFileObject f;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   367
                    if (gen.endsWith(".java"))
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   368
                        f = filer.createSourceFile("p.package-info");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   369
                    else
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   370
                        f = filer.createClassFile("p.package-info");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   371
                    System.err.println("copy " + gen + " to " + f.getName());
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   372
                    write(f, read(new File(gen)));
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   373
                } catch (IOException e) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   374
                    error("Cannot create package-info file: " + e);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   375
                }
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
            // if annotation processing is complete, verify the package annotation
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   379
            // found by the compiler.
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   380
            if (renv.processingOver()) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   381
                System.err.println("final round");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   382
                Elements eu = processingEnv.getElementUtils();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   383
                TypeElement te = eu.getTypeElement("p.Test");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   384
                PackageElement pe = eu.getPackageOf(te);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   385
                System.err.println("final: te:" + te + " pe:" + pe);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   386
                List<? extends AnnotationMirror> annos = pe.getAnnotationMirrors();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   387
                System.err.println("final: annos:" + annos);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   388
                if (annos.size() == 1) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   389
                    String expect = "@" + te + "(\"" + getOption("expect") + "\")";
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   390
                    String actual = annos.get(0).toString();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   391
                    checkEqual("package annotations", actual, expect);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   392
                } else {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   393
                    error("Wrong number of annotations found: (" + annos.size() + ") " + annos);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   394
                }
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
            return true;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   398
        }
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
        /** Get an option given to the annotation processor. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   401
        String getOption(String name) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   402
            return processingEnv.getOptions().get(name);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   403
        }
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
        /** Read a file. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   406
        byte[] read(File file) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   407
            byte[] bytes = new byte[(int) file.length()];
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   408
            DataInputStream in = null;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   409
            try {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   410
                in = new DataInputStream(new FileInputStream(file));
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   411
                in.readFully(bytes);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   412
            } catch (IOException e) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   413
                error("Error reading file: " + e);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   414
            } finally {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   415
                if (in != null) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   416
                    try {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   417
                        in.close();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   418
                    } catch (IOException e) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   419
                        error("Error closing file: " + e);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   420
                    }
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
            return  bytes;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   424
        }
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
        /** Write a file. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   427
        void write(JavaFileObject file, byte[] bytes) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   428
            OutputStream out = null;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   429
            try {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   430
                out = file.openOutputStream();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   431
                out.write(bytes, 0, bytes.length);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   432
            } catch (IOException e) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   433
                error("Error writing file: " + e);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   434
            } finally {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   435
                if (out != null) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   436
                    try {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   437
                        out.close();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   438
                    } catch (IOException e) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   439
                        error("Error closing file: " + e);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   440
                    }
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
        /** 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
   446
        private void checkEqual(String label, String actual, String expect) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   447
            if (!actual.equals(expect)) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   448
                error("Unexpected value for " + label + "; actual=" + actual + ", expected=" + expect);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   449
            }
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
        /** Report an error to the annotation processing system. */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   453
        void error(String msg) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   454
            Messager messager = processingEnv.getMessager();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   455
            messager.printMessage(Diagnostic.Kind.ERROR, msg);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   456
        }
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
        int round;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   459
    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   460
}