< prev index next >

test/langtools/tools/javac/processing/model/element/TestOrigin.java

Print this page


   1 /*
   2  * Copyright (c) 2012, 2015, 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  */
  23 
  24 /*
  25  * @test
  26  * @bug 8171355
  27  * @summary Test behavior of javax.lang.model.util.Elements.getOrigin.
  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 toolbox.TestRunner
  33  * @build TestOrigin
  34  * @run main TestOrigin
  35  */
  36 
  37 import java.io.OutputStream;
  38 import java.nio.file.Files;
  39 import java.nio.file.Path;
  40 import java.nio.file.Paths;
  41 import java.util.ArrayList;
  42 import java.util.Arrays;
  43 import java.util.Collections;
  44 import java.util.HashMap;
  45 import java.util.List;
  46 import java.util.Map;
  47 import java.util.Set;
  48 
  49 import javax.annotation.processing.*;
  50 import javax.lang.model.SourceVersion;
  51 import javax.lang.model.element.*;
  52 import javax.lang.model.element.ModuleElement.Directive;
  53 import javax.lang.model.element.ModuleElement.ExportsDirective;
  54 import javax.lang.model.element.ModuleElement.OpensDirective;
  55 import javax.lang.model.element.ModuleElement.RequiresDirective;
  56 import javax.lang.model.util.Elements;
  57 
  58 import com.sun.tools.classfile.Attribute;
  59 import com.sun.tools.classfile.Attributes;
  60 import com.sun.tools.classfile.ClassFile;
  61 import com.sun.tools.classfile.ClassWriter;
  62 import com.sun.tools.classfile.Module_attribute;
  63 import com.sun.tools.classfile.Module_attribute.ExportsEntry;


  78     }
  79 
  80     public static void main(String... args) throws Exception {
  81         new TestOrigin().runTests(m -> new Object[] { Paths.get(m.getName()) });
  82     }
  83 
  84     @Test
  85     public void testGeneratedConstr(Path base) throws Exception {
  86         Path src = base.resolve("src");
  87         tb.writeJavaFiles(src,
  88                           "package test; public class Test { private void test() { } }",
  89                           "class Dummy {}");
  90         Path classes = base.resolve("classes");
  91         tb.createDirectories(classes);
  92 
  93         List<String> log;
  94         List<String> expected;
  95 
  96         //from source:
  97         log = new JavacTask(tb)
  98             .options("-processor", ListMembersAP.class.getName())
  99             .outdir(classes)
 100             .files(tb.findJavaFiles(src))
 101             .run()
 102             .writeAll()
 103             .getOutputLines(Task.OutputKind.STDOUT);
 104 
 105         expected = Arrays.asList(
 106                 "<init>:MANDATED",
 107                 "test:EXPLICIT");
 108 
 109         if (!expected.equals(log))
 110             throw new AssertionError("expected output not found: " + log);
 111 
 112         //from class:
 113         log = new JavacTask(tb)
 114             .options("-classpath", classes.toString(),
 115                      "-processorpath", System.getProperty("test.classes"),
 116                      "-processor", ListMembersAP.class.getName())
 117             .outdir(classes)
 118             .files(src.resolve("Dummy.java"))
 119             .run()
 120             .writeAll()
 121             .getOutputLines(Task.OutputKind.STDOUT);
 122 
 123         expected = Arrays.asList(
 124                 "<init>:EXPLICIT",
 125                 "test:EXPLICIT");
 126 
 127         if (!expected.equals(log))
 128             throw new AssertionError("expected output not found: " + log);
 129     }
 130 
 131     @SupportedAnnotationTypes("*")
 132     public static final class ListMembersAP extends AbstractProcessor {
 133 
 134         @Override
 135         public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
 136             if (!roundEnv.processingOver())
 137                 return false;
 138 
 139             Elements elements = processingEnv.getElementUtils();
 140             TypeElement test = elements.getTypeElement("test.Test");
 141             List<? extends Element> members = new ArrayList<>(test.getEnclosedElements());
 142 
 143             Collections.sort(members,
 144                              (e1, e2) -> e1.getSimpleName().toString().compareTo(e2.getSimpleName().toString()));
 145 
 146             for (Element el : members) {
 147                 System.out.println(el.getSimpleName() + ":" + elements.getOrigin(el));
 148             }
 149 
 150             return false;
 151         }
 152 
 153         @Override
 154         public SourceVersion getSupportedSourceVersion() {
 155             return SourceVersion.latestSupported();
 156         }
 157 
 158     }
 159 
 160     @Test
 161     public void testRepeatableAnnotations(Path base) throws Exception {
 162         Path src = base.resolve("src");
 163         tb.writeJavaFiles(src,
 164                           "package test; @A @A public class Test { }",
 165                           "package test;" +
 166                           "import java.lang.annotation.*;" +
 167                           "@Repeatable(Container.class)" +
 168                           "@Retention(RetentionPolicy.CLASS)" +
 169                           "@interface A {}",
 170                           "package test; @interface Container { A[] value(); }",
 171                           "class Dummy {}");
 172         Path classes = base.resolve("classes");
 173         tb.createDirectories(classes);
 174 
 175         List<String> log;
 176         List<String> expected;
 177 


   1 /*
   2  * Copyright (c) 2012, 2019, 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  */
  23 
  24 /*
  25  * @test
  26  * @bug 8171355
  27  * @summary Test behavior of javax.lang.model.util.Elements.getOrigin.
  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 toolbox.TestRunner
  33  * @build TestOrigin ListMembersAP
  34  * @run main TestOrigin
  35  */
  36 
  37 import java.io.OutputStream;
  38 import java.nio.file.Files;
  39 import java.nio.file.Path;
  40 import java.nio.file.Paths;

  41 import java.util.Arrays;

  42 import java.util.HashMap;
  43 import java.util.List;
  44 import java.util.Map;
  45 import java.util.Set;
  46 
  47 import javax.annotation.processing.*;
  48 import javax.lang.model.SourceVersion;
  49 import javax.lang.model.element.*;
  50 import javax.lang.model.element.ModuleElement.Directive;
  51 import javax.lang.model.element.ModuleElement.ExportsDirective;
  52 import javax.lang.model.element.ModuleElement.OpensDirective;
  53 import javax.lang.model.element.ModuleElement.RequiresDirective;
  54 import javax.lang.model.util.Elements;
  55 
  56 import com.sun.tools.classfile.Attribute;
  57 import com.sun.tools.classfile.Attributes;
  58 import com.sun.tools.classfile.ClassFile;
  59 import com.sun.tools.classfile.ClassWriter;
  60 import com.sun.tools.classfile.Module_attribute;
  61 import com.sun.tools.classfile.Module_attribute.ExportsEntry;


  76     }
  77 
  78     public static void main(String... args) throws Exception {
  79         new TestOrigin().runTests(m -> new Object[] { Paths.get(m.getName()) });
  80     }
  81 
  82     @Test
  83     public void testGeneratedConstr(Path base) throws Exception {
  84         Path src = base.resolve("src");
  85         tb.writeJavaFiles(src,
  86                           "package test; public class Test { private void test() { } }",
  87                           "class Dummy {}");
  88         Path classes = base.resolve("classes");
  89         tb.createDirectories(classes);
  90 
  91         List<String> log;
  92         List<String> expected;
  93 
  94         //from source:
  95         log = new JavacTask(tb)
  96             .options("-processor", "ListMembersAP")
  97             .outdir(classes)
  98             .files(tb.findJavaFiles(src))
  99             .run()
 100             .writeAll()
 101             .getOutputLines(Task.OutputKind.STDOUT);
 102 
 103         expected = Arrays.asList(
 104                 "<init>:MANDATED",
 105                 "test:EXPLICIT");
 106 
 107         if (!expected.equals(log))
 108             throw new AssertionError("expected output not found: " + log);
 109 
 110         //from class:
 111         log = new JavacTask(tb)
 112             .options("-classpath", classes.toString(),
 113                      "-processorpath", System.getProperty("test.classes"),
 114                      "-processor", "ListMembersAP")
 115             .outdir(classes)
 116             .files(src.resolve("Dummy.java"))
 117             .run()
 118             .writeAll()
 119             .getOutputLines(Task.OutputKind.STDOUT);
 120 
 121         expected = Arrays.asList(
 122                 "<init>:EXPLICIT",
 123                 "test:EXPLICIT");
 124 
 125         if (!expected.equals(log))
 126             throw new AssertionError("expected output not found: " + log);





























 127     }
 128 
 129     @Test
 130     public void testRepeatableAnnotations(Path base) throws Exception {
 131         Path src = base.resolve("src");
 132         tb.writeJavaFiles(src,
 133                           "package test; @A @A public class Test { }",
 134                           "package test;" +
 135                           "import java.lang.annotation.*;" +
 136                           "@Repeatable(Container.class)" +
 137                           "@Retention(RetentionPolicy.CLASS)" +
 138                           "@interface A {}",
 139                           "package test; @interface Container { A[] value(); }",
 140                           "class Dummy {}");
 141         Path classes = base.resolve("classes");
 142         tb.createDirectories(classes);
 143 
 144         List<String> log;
 145         List<String> expected;
 146 


< prev index next >