test/tools/javac/processing/options/TestImplicitNone.java

changeset 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/processing/options/TestImplicitNone.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,109 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 6935638
    1.30 + * @summary -implicit:none prevents compilation with annotation processing
    1.31 + */
    1.32 +
    1.33 +import java.io.*;
    1.34 +import java.util.*;
    1.35 +import javax.annotation.processing.*;
    1.36 +import javax.lang.model.*;
    1.37 +import javax.lang.model.element.*;
    1.38 +
    1.39 +
    1.40 +@SupportedAnnotationTypes("*")
    1.41 +public class TestImplicitNone extends AbstractProcessor {
    1.42 +    public static void main(String... args) throws Exception {
    1.43 +        new TestImplicitNone().run();
    1.44 +    }
    1.45 +
    1.46 +    void run() throws Exception {
    1.47 +        File classesDir = new File("tmp", "classes");
    1.48 +        classesDir.mkdirs();
    1.49 +        File test_java = new File(new File("tmp", "src"), "Test.java");
    1.50 +        writeFile(test_java, "class Test { }");
    1.51 +
    1.52 +        // build up list of options and files to be compiled
    1.53 +        List<String> opts = new ArrayList<String>();
    1.54 +        List<File> files = new ArrayList<File>();
    1.55 +
    1.56 +        opts.add("-d");
    1.57 +        opts.add(classesDir.getPath());
    1.58 +        opts.add("-processorpath");
    1.59 +        opts.add(System.getProperty("test.classes"));
    1.60 +        opts.add("-implicit:none");
    1.61 +        opts.add("-processor");
    1.62 +        opts.add(TestImplicitNone.class.getName());
    1.63 +        files.add(test_java);
    1.64 +
    1.65 +        compile(opts, files);
    1.66 +
    1.67 +        File test_class = new File(classesDir, "Test.class");
    1.68 +        if (!test_class.exists())
    1.69 +            throw new Exception("Test.class not generated");
    1.70 +    }
    1.71 +
    1.72 +    /** Compile files with options provided. */
    1.73 +    void compile(List<String> opts, List<File> files) throws Exception {
    1.74 +        System.err.println("javac: " + opts + " " + files);
    1.75 +        List<String> args = new ArrayList<String>();
    1.76 +        args.addAll(opts);
    1.77 +        for (File f: files)
    1.78 +            args.add(f.getPath());
    1.79 +        StringWriter sw = new StringWriter();
    1.80 +        PrintWriter pw = new PrintWriter(sw);
    1.81 +        int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw);
    1.82 +        pw.flush();
    1.83 +        if (sw.getBuffer().length() > 0)
    1.84 +            System.err.println(sw.toString());
    1.85 +        if (rc != 0)
    1.86 +            throw new Exception("compilation failed: rc=" + rc);
    1.87 +    }
    1.88 +
    1.89 +    /** Write a file with a given body. */
    1.90 +    void writeFile(File f, String body) throws Exception {
    1.91 +        if (f.getParentFile() != null)
    1.92 +            f.getParentFile().mkdirs();
    1.93 +        Writer out = new FileWriter(f);
    1.94 +        try {
    1.95 +            out.write(body);
    1.96 +        } finally {
    1.97 +            out.close();
    1.98 +        }
    1.99 +    }
   1.100 +
   1.101 +    //----- annotation processor methods -----
   1.102 +
   1.103 +    public boolean process(Set<? extends TypeElement> annotations,
   1.104 +                         RoundEnvironment roundEnv) {
   1.105 +        return true;
   1.106 +    }
   1.107 +
   1.108 +    @Override
   1.109 +    public SourceVersion getSupportedSourceVersion() {
   1.110 +        return SourceVersion.latest();
   1.111 +    }
   1.112 +}

mercurial