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

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 0
959103a6100f
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 /*
    25  * @test
    26  * @bug 6935638
    27  * @summary -implicit:none prevents compilation with annotation processing
    28  */
    30 import java.io.*;
    31 import java.util.*;
    32 import javax.annotation.processing.*;
    33 import javax.lang.model.*;
    34 import javax.lang.model.element.*;
    37 @SupportedAnnotationTypes("*")
    38 public class TestImplicitNone extends AbstractProcessor {
    39     public static void main(String... args) throws Exception {
    40         new TestImplicitNone().run();
    41     }
    43     void run() throws Exception {
    44         File classesDir = new File("tmp", "classes");
    45         classesDir.mkdirs();
    46         File test_java = new File(new File("tmp", "src"), "Test.java");
    47         writeFile(test_java, "class Test { }");
    49         // build up list of options and files to be compiled
    50         List<String> opts = new ArrayList<String>();
    51         List<File> files = new ArrayList<File>();
    53         opts.add("-d");
    54         opts.add(classesDir.getPath());
    55         opts.add("-processorpath");
    56         opts.add(System.getProperty("test.classes"));
    57         opts.add("-implicit:none");
    58         opts.add("-processor");
    59         opts.add(TestImplicitNone.class.getName());
    60         files.add(test_java);
    62         compile(opts, files);
    64         File test_class = new File(classesDir, "Test.class");
    65         if (!test_class.exists())
    66             throw new Exception("Test.class not generated");
    67     }
    69     /** Compile files with options provided. */
    70     void compile(List<String> opts, List<File> files) throws Exception {
    71         System.err.println("javac: " + opts + " " + files);
    72         List<String> args = new ArrayList<String>();
    73         args.addAll(opts);
    74         for (File f: files)
    75             args.add(f.getPath());
    76         StringWriter sw = new StringWriter();
    77         PrintWriter pw = new PrintWriter(sw);
    78         int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw);
    79         pw.flush();
    80         if (sw.getBuffer().length() > 0)
    81             System.err.println(sw.toString());
    82         if (rc != 0)
    83             throw new Exception("compilation failed: rc=" + rc);
    84     }
    86     /** Write a file with a given body. */
    87     void writeFile(File f, String body) throws Exception {
    88         if (f.getParentFile() != null)
    89             f.getParentFile().mkdirs();
    90         Writer out = new FileWriter(f);
    91         try {
    92             out.write(body);
    93         } finally {
    94             out.close();
    95         }
    96     }
    98     //----- annotation processor methods -----
   100     public boolean process(Set<? extends TypeElement> annotations,
   101                          RoundEnvironment roundEnv) {
   102         return true;
   103     }
   105     @Override
   106     public SourceVersion getSupportedSourceVersion() {
   107         return SourceVersion.latest();
   108     }
   109 }

mercurial