< prev index next >

test/langtools/tools/javac/modules/AnnotationsOnModules.java

Print this page




  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  */
  23 
  24 /*
  25  * @test
  26  * @bug 8159602 8170549 8171255 8171322
  27  * @summary Test annotations on module declaration.
  28  * @library /tools/lib
  29  * @modules jdk.compiler/com.sun.tools.javac.api
  30  *          jdk.compiler/com.sun.tools.javac.main
  31  *          jdk.jdeps/com.sun.tools.classfile
  32  * @build toolbox.ToolBox toolbox.JavacTask ModuleTestBase
  33  * @run main AnnotationsOnModules
  34  */
  35 
  36 import java.io.File;
  37 import java.nio.file.Files;
  38 import java.nio.file.Path;
  39 import java.util.Arrays;
  40 import java.util.HashSet;
  41 import java.util.List;
  42 import java.util.Objects;
  43 import java.util.Set;
  44 import java.util.stream.Collectors;
  45 
  46 import javax.annotation.processing.AbstractProcessor;
  47 import javax.annotation.processing.RoundEnvironment;
  48 import javax.annotation.processing.SupportedAnnotationTypes;
  49 import javax.annotation.processing.SupportedOptions;
  50 import javax.lang.model.element.AnnotationMirror;
  51 import javax.lang.model.element.ModuleElement;
  52 import javax.lang.model.element.TypeElement;
  53 
  54 import com.sun.tools.classfile.Attribute;
  55 import com.sun.tools.classfile.ClassFile;
  56 import com.sun.tools.classfile.RuntimeInvisibleAnnotations_attribute;
  57 import com.sun.tools.classfile.RuntimeVisibleAnnotations_attribute;
  58 import toolbox.JavacTask;
  59 import toolbox.Task;
  60 import toolbox.Task.OutputKind;
  61 
  62 public class AnnotationsOnModules extends ModuleTestBase {
  63 
  64     public static void main(String... args) throws Exception {


 662 
 663             Path modulePath = testBase.resolve("module-path");
 664 
 665             Files.createDirectories(modulePath);
 666 
 667             new JavacTask(tb)
 668                 .options("--module-source-path", moduleSrc.toString())
 669                 .outdir(modulePath)
 670                 .files(findJavaFiles(moduleSrc))
 671                 .run()
 672                 .writeAll();
 673 
 674             Path classes = testBase.resolve("classes");
 675 
 676             Files.createDirectories(classes);
 677 
 678             new JavacTask(tb)
 679                 .options("--module-path", modulePath.toString(),
 680                          "--add-modules", "m",
 681                          "-processorpath", System.getProperty("test.classes"),
 682                          "-processor", ProxyTypeValidator.class.getName(),
 683                          "-A" + OPT_EXPECTED_ANNOTATIONS + "=" + tc.expectedAnnotations)
 684                 .outdir(classes)
 685                 .files(findJavaFiles(extraSrc))
 686                 .run()
 687                 .writeAll();
 688         }
 689     }
 690 
 691     private static final String OPT_EXPECTED_ANNOTATIONS = "expectedAnnotations";
 692 
 693     @SupportedAnnotationTypes("*")
 694     @SupportedOptions(OPT_EXPECTED_ANNOTATIONS)
 695     public static final class ProxyTypeValidator extends AbstractProcessor {
 696 
 697         @Override
 698         public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
 699             ModuleElement m = processingEnv.getElementUtils().getModuleElement("m");
 700             String actualTypes = m.getAnnotationMirrors()
 701                                   .stream()
 702                                   .map(am -> am.toString())
 703                                   .collect(Collectors.joining(", "));
 704             if (!Objects.equals(actualTypes, processingEnv.getOptions().get(OPT_EXPECTED_ANNOTATIONS))) {
 705                 throw new IllegalStateException("Expected annotations not found, actual: " + actualTypes);
 706             }
 707             return false;
 708         }
 709 
 710     }
 711 
 712 }


  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  */
  23 
  24 /*
  25  * @test
  26  * @bug 8159602 8170549 8171255 8171322
  27  * @summary Test annotations on module declaration.
  28  * @library /tools/lib
  29  * @modules jdk.compiler/com.sun.tools.javac.api
  30  *          jdk.compiler/com.sun.tools.javac.main
  31  *          jdk.jdeps/com.sun.tools.classfile
  32  * @build toolbox.ToolBox toolbox.JavacTask ModuleTestBase ProxyTypeValidator
  33  * @run main AnnotationsOnModules
  34  */
  35 
  36 import java.io.File;
  37 import java.nio.file.Files;
  38 import java.nio.file.Path;
  39 import java.util.Arrays;
  40 import java.util.HashSet;
  41 import java.util.List;

  42 import java.util.Set;

  43 
  44 import javax.annotation.processing.AbstractProcessor;
  45 import javax.annotation.processing.RoundEnvironment;
  46 import javax.annotation.processing.SupportedAnnotationTypes;
  47 import javax.annotation.processing.SupportedOptions;
  48 import javax.lang.model.element.AnnotationMirror;
  49 import javax.lang.model.element.ModuleElement;
  50 import javax.lang.model.element.TypeElement;
  51 
  52 import com.sun.tools.classfile.Attribute;
  53 import com.sun.tools.classfile.ClassFile;
  54 import com.sun.tools.classfile.RuntimeInvisibleAnnotations_attribute;
  55 import com.sun.tools.classfile.RuntimeVisibleAnnotations_attribute;
  56 import toolbox.JavacTask;
  57 import toolbox.Task;
  58 import toolbox.Task.OutputKind;
  59 
  60 public class AnnotationsOnModules extends ModuleTestBase {
  61 
  62     public static void main(String... args) throws Exception {


 660 
 661             Path modulePath = testBase.resolve("module-path");
 662 
 663             Files.createDirectories(modulePath);
 664 
 665             new JavacTask(tb)
 666                 .options("--module-source-path", moduleSrc.toString())
 667                 .outdir(modulePath)
 668                 .files(findJavaFiles(moduleSrc))
 669                 .run()
 670                 .writeAll();
 671 
 672             Path classes = testBase.resolve("classes");
 673 
 674             Files.createDirectories(classes);
 675 
 676             new JavacTask(tb)
 677                 .options("--module-path", modulePath.toString(),
 678                          "--add-modules", "m",
 679                          "-processorpath", System.getProperty("test.classes"),
 680                          "-processor", "ProxyTypeValidator",
 681                          "-A" + OPT_EXPECTED_ANNOTATIONS + "=" + tc.expectedAnnotations)
 682                 .outdir(classes)
 683                 .files(findJavaFiles(extraSrc))
 684                 .run()
 685                 .writeAll();
 686         }
 687     }
 688 
 689     private static final String OPT_EXPECTED_ANNOTATIONS = "expectedAnnotations";




















 690 }
< prev index next >