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