langtools/test/tools/javac/6341866/T6341866.java
author jjg
Mon, 29 Nov 2010 14:15:36 -0800
changeset 7335 8b390fd27190
parent 5520 86e4b9a9da40
child 7681 1f0819a3341f
permissions -rw-r--r--
6900037: javac should warn if earlier -source is used and bootclasspath not set Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
     2
 * Copyright (c) 2006, 2008, 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>();
7335
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 5520
diff changeset
   100
        opts.addAll(Arrays.asList("-d", ".", "-sourcepath", testSrc, "-classpath", testClasses, "-source", "1.6", "-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();
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        StandardJavaFileManager fm = javac.getStandardFileManager(dl, null, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        // Note: class A references class B, so compile A if we want implicit compilation
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        File file =  (implicitType != ImplicitType.NONE) ? a_java : b_java;
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        //System.err.println("compile: " + opts + " " + files);
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        boolean ok = javac.getTask(null, fm, dl, opts, null, files).call();
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        if (!ok) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
            error("compilation failed");
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        // check implicit compilation results if necessary
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        if (implicitType != ImplicitType.NONE) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
            boolean expectClass = (implicitType != ImplicitType.OPT_NONE);
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
            if (b_class.exists() != expectClass) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
                if (b_class.exists())
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
                    error("B implicitly compiled unexpectedly");
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
                    error("B not impliictly compiled");
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        // check message key results
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        String expectKey = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        if (implicitType == ImplicitType.OPT_UNSET) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            switch (annoType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            case SERVICE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
                expectKey = "compiler.warn.proc.use.proc.or.implicit";
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            case SPECIFY:
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
                expectKey = "compiler.warn.proc.use.implicit";
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        if (expectKey == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
            if (dl.diagCodes.size() != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
                error("no diagnostics expected");
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
            if (!(dl.diagCodes.size() == 1 && dl.diagCodes.get(0).equals(expectKey))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
                error("unexpected diagnostics generated");
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
    static void createProcessorServices(String name) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        processorServices.getParentFile().mkdirs();
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        BufferedWriter out = new BufferedWriter(new FileWriter(processorServices));
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        out.write(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        out.newLine();
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        out.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
    static class MyDiagListener implements DiagnosticListener<JavaFileObject> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        public void report(Diagnostic d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
            diagCodes.add(d.getCode());
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
            System.err.println(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        List<String> diagCodes = new ArrayList<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
    static void error(String msg) {
168
25697c18650b 6307187: clean up code for -Xlint:options
jjg
parents: 10
diff changeset
   189
        System.err.println("ERROR: " + msg);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
    static File services(Class<?> service) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
        String[] dirs = { testClasses, "META-INF", "services" };
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        File dir = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
        for (String d: dirs)
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
            dir = (dir == null ? new File(d) : new File(dir, d));
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
        return new File(dir, service.getName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
}