langtools/test/tools/javac/6341866/T6341866.java
author jjg
Wed, 29 Oct 2014 17:25:23 -0700
changeset 27319 030080f03e4f
parent 22444 c79ee1e6742f
child 30730 d3ce7619db2c
permissions -rw-r--r--
8062348: langtools tests should close file manager (group 1) Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
     2
 * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
    21
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
 * @test
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
 * @bug 6341866
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
 * @summary Source files loaded from source path are not subject to annotation processing
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
 * @build Anno T6341866
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
 * @run main T6341866
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import javax.annotation.processing.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import javax.tools.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * For each of a number of implicit compilation scenarios,
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 * and for each of a set of annotation processing scenarios,
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * verify that a class file is generated, or not, for an
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * implicitly compiled source file and that the correct
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * warning message is given for implicitly compiled files
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * when annotation processing.
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
public class T6341866 {
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    static final String testSrc = System.getProperty("test.src", ".");
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    static final String testClasses = System.getProperty("test.classes", ".");
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    static final File a_java = new File(testSrc, "A.java");
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    static final File a_class = new File("A.class");
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    static final File b_java = new File(testSrc, "B.java");
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    static final File b_class = new File("B.class");
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    static final File processorServices = services(Processor.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    enum ImplicitType {
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
        NONE(null),                     // don't use implicit compilation
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
        OPT_UNSET(null),                // implicit compilation, but no -implicit option
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        OPT_NONE("-implicit:none"),     // implicit compilation wiith -implicit:none
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        OPT_CLASS("-implicit:class");   // implicit compilation wiith -implicit:class
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        ImplicitType(String opt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
            this.opt = opt;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        final String opt;
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    enum AnnoType {
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        NONE,           // no annotation processing
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        SERVICE,        // implicit annotation processing, via ServiceLoader
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        SPECIFY         // explicit annotation processing
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    public static void main(String ... args) throws Exception {
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        boolean ok = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        // iterate over all combinations
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        for (ImplicitType implicitType: EnumSet.allOf(ImplicitType.class)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
            for (AnnoType annoType: EnumSet.allOf(AnnoType.class)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
                ok &= test(implicitType, annoType);
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        if (!ok)
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
            throw new AssertionError("test failed");
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
     * Verify that a class file is generated, or not, for an implicitly compiled source file,
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
     * and that the correct warning message is given for implicitly compiled files when annotation processing.
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    static boolean test(ImplicitType implicitType, AnnoType annoType) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        System.err.println("test  implicit=" + implicitType + "  anno=" + annoType);
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        // ensure clean start
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        a_class.delete();
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        b_class.delete();
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        processorServices.delete();
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        List<String> opts = new ArrayList<String>();
22444
c79ee1e6742f 8031745: Remove unneeded/obsolete -source/-target options in javac tests, part 1
darcy
parents: 7681
diff changeset
   100
        opts.addAll(Arrays.asList("-d", ".", "-sourcepath", testSrc, "-classpath", testClasses, "-Xlint:-options"));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        if (implicitType.opt != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            opts.add(implicitType.opt);
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        switch (annoType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        case SERVICE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
            createProcessorServices(Anno.class.getName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        case SPECIFY:
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
            opts.addAll(Arrays.asList("-processor", Anno.class.getName()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        MyDiagListener dl = new MyDiagListener();
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   116
        try (StandardJavaFileManager fm = javac.getStandardFileManager(dl, null, null)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   118
            // Note: class A references class B, so compile A if we want implicit compilation
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   119
            File file =  (implicitType != ImplicitType.NONE) ? a_java : b_java;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   120
            Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   122
            //System.err.println("compile: " + opts + " " + files);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   124
            boolean ok = javac.getTask(null, fm, dl, opts, null, files).call();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   125
            if (!ok) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   126
                error("compilation failed");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
            }
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   129
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   130
            // check implicit compilation results if necessary
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   131
            if (implicitType != ImplicitType.NONE) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   132
                boolean expectClass = (implicitType != ImplicitType.OPT_NONE);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   133
                if (b_class.exists() != expectClass) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   134
                    if (b_class.exists())
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   135
                        error("B implicitly compiled unexpectedly");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   136
                    else
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   137
                        error("B not impliictly compiled");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   138
                    return false;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   139
                }
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   140
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   142
            // check message key results
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   143
            String expectKey = null;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   144
            if (implicitType == ImplicitType.OPT_UNSET) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   145
                switch (annoType) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   146
                case SERVICE:
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   147
                    expectKey = "compiler.warn.proc.use.proc.or.implicit";
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   148
                    break;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   149
                case SPECIFY:
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   150
                    expectKey = "compiler.warn.proc.use.implicit";
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   151
                    break;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   152
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
            }
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   154
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   155
            if (expectKey == null) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   156
                if (dl.diagCodes.size() != 0) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   157
                    error("no diagnostics expected");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   158
                    return false;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   159
                }
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   160
            } else {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   161
                if (!(dl.diagCodes.size() == 1 && dl.diagCodes.get(0).equals(expectKey))) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   162
                    error("unexpected diagnostics generated");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   163
                    return false;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   164
                }
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   165
            }
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   166
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   167
            return true;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
    static void createProcessorServices(String name) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        processorServices.getParentFile().mkdirs();
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        BufferedWriter out = new BufferedWriter(new FileWriter(processorServices));
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        out.write(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        out.newLine();
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        out.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
    static class MyDiagListener implements DiagnosticListener<JavaFileObject> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        public void report(Diagnostic d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
            diagCodes.add(d.getCode());
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
            System.err.println(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
        List<String> diagCodes = new ArrayList<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
    static void error(String msg) {
168
25697c18650b 6307187: clean up code for -Xlint:options
jjg
parents: 10
diff changeset
   190
        System.err.println("ERROR: " + msg);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
    static File services(Class<?> service) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        String[] dirs = { testClasses, "META-INF", "services" };
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
        File dir = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        for (String d: dirs)
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
            dir = (dir == null ? new File(d) : new File(dir, d));
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
        return new File(dir, service.getName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
}