langtools/test/tools/javac/6341866/T6341866.java
author akulyakh
Thu, 21 May 2015 11:41:04 -0700
changeset 30730 d3ce7619db2c
parent 27319 030080f03e4f
permissions -rw-r--r--
8076543: Add @modules as needed to the langtools tests Reviewed-by: jjg, shurailine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
     2
 * Copyright (c) 2006, 2015, 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
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
    28
 * @modules java.compiler
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
    29
 *          jdk.compiler
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
 * @build Anno T6341866
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
 * @run main T6341866
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import javax.annotation.processing.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import javax.tools.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * For each of a number of implicit compilation scenarios,
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * and for each of a set of annotation processing scenarios,
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * verify that a class file is generated, or not, for an
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * implicitly compiled source file and that the correct
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 * warning message is given for implicitly compiled files
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 * when annotation processing.
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
public class T6341866 {
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    static final String testSrc = System.getProperty("test.src", ".");
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    static final String testClasses = System.getProperty("test.classes", ".");
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    static final File a_java = new File(testSrc, "A.java");
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    static final File a_class = new File("A.class");
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    static final File b_java = new File(testSrc, "B.java");
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    static final File b_class = new File("B.class");
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    static final File processorServices = services(Processor.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    enum ImplicitType {
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        NONE(null),                     // don't use implicit compilation
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        OPT_UNSET(null),                // implicit compilation, but no -implicit option
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        OPT_NONE("-implicit:none"),     // implicit compilation wiith -implicit:none
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        OPT_CLASS("-implicit:class");   // implicit compilation wiith -implicit:class
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
        ImplicitType(String opt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
            this.opt = opt;
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
        final String opt;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    enum AnnoType {
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        NONE,           // no annotation processing
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
        SERVICE,        // implicit annotation processing, via ServiceLoader
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        SPECIFY         // explicit annotation processing
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    public static void main(String ... args) throws Exception {
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        boolean ok = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        // iterate over all combinations
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        for (ImplicitType implicitType: EnumSet.allOf(ImplicitType.class)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
            for (AnnoType annoType: EnumSet.allOf(AnnoType.class)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
                ok &= test(implicitType, annoType);
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        if (!ok)
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
            throw new AssertionError("test failed");
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     * Verify that a class file is generated, or not, for an implicitly compiled source file,
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     * and that the correct warning message is given for implicitly compiled files when annotation processing.
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    static boolean test(ImplicitType implicitType, AnnoType annoType) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        System.err.println("test  implicit=" + implicitType + "  anno=" + annoType);
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        // ensure clean start
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        a_class.delete();
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        b_class.delete();
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        processorServices.delete();
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        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
   102
        opts.addAll(Arrays.asList("-d", ".", "-sourcepath", testSrc, "-classpath", testClasses, "-Xlint:-options"));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        if (implicitType.opt != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
            opts.add(implicitType.opt);
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        switch (annoType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        case SERVICE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
            createProcessorServices(Anno.class.getName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        case SPECIFY:
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
            opts.addAll(Arrays.asList("-processor", Anno.class.getName()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        MyDiagListener dl = new MyDiagListener();
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   118
        try (StandardJavaFileManager fm = javac.getStandardFileManager(dl, null, null)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   120
            // 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
   121
            File file =  (implicitType != ImplicitType.NONE) ? a_java : b_java;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   122
            Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
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
            //System.err.println("compile: " + opts + " " + files);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   126
            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
   127
            if (!ok) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   128
                error("compilation failed");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
            }
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   131
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   132
            // check implicit compilation results if necessary
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   133
            if (implicitType != ImplicitType.NONE) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   134
                boolean expectClass = (implicitType != ImplicitType.OPT_NONE);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   135
                if (b_class.exists() != expectClass) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   136
                    if (b_class.exists())
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   137
                        error("B implicitly compiled unexpectedly");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   138
                    else
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   139
                        error("B not impliictly compiled");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   140
                    return false;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   141
                }
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   142
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   144
            // check message key results
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   145
            String expectKey = null;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   146
            if (implicitType == ImplicitType.OPT_UNSET) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   147
                switch (annoType) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   148
                case SERVICE:
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   149
                    expectKey = "compiler.warn.proc.use.proc.or.implicit";
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   150
                    break;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   151
                case SPECIFY:
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   152
                    expectKey = "compiler.warn.proc.use.implicit";
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   153
                    break;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   154
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
            }
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   156
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   157
            if (expectKey == null) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   158
                if (dl.diagCodes.size() != 0) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   159
                    error("no diagnostics expected");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   160
                    return false;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   161
                }
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   162
            } else {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   163
                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
   164
                    error("unexpected diagnostics generated");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   165
                    return false;
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
            }
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   168
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   169
            return true;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    static void createProcessorServices(String name) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        processorServices.getParentFile().mkdirs();
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        BufferedWriter out = new BufferedWriter(new FileWriter(processorServices));
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        out.write(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
        out.newLine();
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        out.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
    static class MyDiagListener implements DiagnosticListener<JavaFileObject> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        public void report(Diagnostic d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
            diagCodes.add(d.getCode());
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
            System.err.println(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        List<String> diagCodes = new ArrayList<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    static void error(String msg) {
168
25697c18650b 6307187: clean up code for -Xlint:options
jjg
parents: 10
diff changeset
   192
        System.err.println("ERROR: " + msg);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
    static File services(Class<?> service) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        String[] dirs = { testClasses, "META-INF", "services" };
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
        File dir = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
        for (String d: dirs)
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
            dir = (dir == null ? new File(d) : new File(dir, d));
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
        return new File(dir, service.getName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
}