1 /*
   2  * Copyright (c) 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  * @summary test Origin of record members
  27  * @modules jdk.compiler/com.sun.tools.javac.util
  28  * @run main OriginTest
  29  */
  30 
  31 import java.io.IOException;
  32 import java.nio.file.Files;
  33 import java.util.List;
  34 import java.nio.file.Path;
  35 import java.util.regex.Matcher;
  36 import java.util.regex.Pattern;
  37 import javax.lang.model.element.ExecutableElement;
  38 import javax.lang.model.util.ElementScanner9;
  39 import javax.lang.model.util.Elements;
  40 import javax.tools.JavaCompiler;
  41 import javax.tools.JavaFileObject;
  42 import javax.tools.StandardJavaFileManager;
  43 import javax.tools.ToolProvider;
  44 
  45 import com.sun.source.util.JavacTask;
  46 import com.sun.source.util.TaskEvent;
  47 import com.sun.source.util.TaskListener;
  48 
  49 public class OriginTest {
  50 
  51     public static void main(String... args) throws Exception {
  52         new OriginTest().run();
  53     }
  54 
  55     private String ALL_MANDATED =
  56             "record AllMandated(int x, int y) { }";
  57 
  58     private String ALL_EXPLICIT =
  59             "record AllExplicit(int x, int y) {\n" +
  60             "    public AllExplicit { }\n" +
  61             "    public int x() { return x; }\n" +
  62             "    public int y() { return y; }\n" +
  63             "    public boolean equals(Object other) { return false; }\n" +
  64             "    public int hashCode() { return 0; }\n" +
  65             "    public String toString() { return null; }\n" +
  66             "}";
  67 
  68     void run() throws Exception {
  69         test(ALL_MANDATED, Elements.Origin.MANDATED);
  70         test(ALL_EXPLICIT, Elements.Origin.EXPLICIT);
  71     }
  72 
  73     void test(String src, Elements.Origin origin) throws Exception {
  74         Matcher m = Pattern.compile("record ([^(]+)").matcher(src);
  75         if (!m.find()) throw new IllegalArgumentException();
  76         Path file = Path.of(m.group(1) + ".java");
  77         Files.writeString(file, src);
  78         JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
  79         StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
  80         List<String> options = List.of();
  81         List<String> classes = List.of();
  82         Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
  83         JavacTask task = (JavacTask) tool.getTask(null, null, null,
  84                 options, classes, files);
  85         task.addTaskListener(new Checker(task.getElements(), origin));
  86         boolean ok = task.call();
  87         if (!ok) {
  88             error("Task failed");
  89         }
  90 
  91         if (errors > 0) {
  92             System.err.println(errors + " occurred");
  93             throw new Exception(errors + " occurred");
  94         }
  95     }
  96 
  97     class Checker implements TaskListener {
  98         final Elements elements;
  99         final Elements.Origin expectedOrigin;
 100         Checker(Elements elements, Elements.Origin expectedOrigin) {
 101             this.elements = elements;
 102             this.expectedOrigin = expectedOrigin;
 103         }
 104 
 105         @Override
 106         public void started(TaskEvent e) {
 107             System.err.println("Started " + e);
 108             if (e.getKind() == TaskEvent.Kind.ANALYZE) {
 109                 ElementScanner9<Void, Void> scanner = new ElementScanner9<>() {
 110                     public Void visitExecutable(ExecutableElement ee, Void p) {
 111                         Elements.Origin o = elements.getOrigin(ee);
 112                         System.err.println("  Executable " + o + " " + ee);
 113                         if (o != expectedOrigin) {
 114                             error("Unexpected origin " + o + " for " + ee);
 115                         }
 116                         return null;
 117                     }
 118                 };
 119                 scanner.scan(e.getTypeElement());
 120             }
 121         }
 122     }
 123 
 124     void error(String message) {
 125         System.err.println("Error: " + message);
 126         errors++;
 127     }
 128 
 129     int errors;
 130 }
 131 
 132 
 133 
 134 
 135 
 136 
 137