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 import java.foreign.annotations.NativeAddressof;
  25 import java.foreign.annotations.NativeGetter;
  26 import java.foreign.annotations.NativeHeader;
  27 import java.foreign.annotations.NativeSetter;
  28 import java.foreign.annotations.NativeStruct;
  29 import java.foreign.memory.Pointer;
  30 import java.io.File;
  31 import java.io.IOException;
  32 import java.lang.reflect.Method;
  33 import java.lang.reflect.Modifier;
  34 import java.nio.file.Files;
  35 import java.nio.file.Path;
  36 import java.nio.file.Paths;
  37 import java.util.Arrays;
  38 import java.util.ArrayList;
  39 import java.util.spi.ToolProvider;
  40 import org.testng.annotations.Test;
  41 import static org.testng.Assert.assertTrue;
  42 
  43 /*
  44  * @test
  45  * @bug 8221154 8221228 8221336 8221419 8221443
  46  * @summary jextract should generate java source files
  47  * @library ..
  48  * @run testng SrcGenTest
  49  */
  50 public class SrcGenTest extends JextractToolRunner {
  51     private static final ToolProvider JEXTRACT = ToolProvider.findFirst("jextract")
  52             .orElseThrow(() ->
  53                     new RuntimeException("jextract tool not found")
  54             );
  55 
  56     private static final ToolProvider JAVAC = ToolProvider.findFirst("javac")
  57             .orElseThrow(() ->
  58                     new RuntimeException("javac tool not found")
  59             );
  60 
  61     @Test
  62     public void test() throws IOException {
  63         Path inputDir = Paths.get(System.getProperty("test.src", "."));
  64         Path outputDir = Paths.get(System.getProperty("test.classes", "."));
  65         inputDir = inputDir.toAbsolutePath();
  66         outputDir = outputDir.toAbsolutePath();
  67         String pkgName = "test8221154";
  68         Path jarPath = outputDir.resolve(pkgName + ".jar");
  69 
  70         // run jextract with --src-dump-dir option
  71         ArrayList<String> jextrOpts = new ArrayList<>();
  72         jextrOpts.add("-C-nostdinc");
  73         jextrOpts.add("-I");
  74         jextrOpts.add(inputDir.toString());
  75         jextrOpts.add("-o");
  76         jextrOpts.add(jarPath.toString());
  77         jextrOpts.add("--src-dump-dir");
  78         jextrOpts.add(outputDir.toString());
  79         jextrOpts.add("-t");
  80         jextrOpts.add(pkgName);
  81         jextrOpts.add("-l");
  82         jextrOpts.add("srcgentest");
  83         jextrOpts.add(inputDir + File.separator + "srcgentest.h");
  84 
  85         int result = JEXTRACT.run(System.out, System.err, jextrOpts.toArray(String[]::new));
  86         if (result != 0) {
  87             throw new RuntimeException(JEXTRACT.name() + " returns non-zero value");
  88         }
  89 
  90         // delete .jar file generated by jextract
  91         Files.delete(jarPath);
  92 
  93         Path pkgDir = outputDir.resolve(pkgName);
  94 
  95         String srcgentestIfaceName = headerInterfaceName("srcgentest.h");
  96         String srcgentestForwarderName = staticForwarderName("srcgentest.h");
  97         String dupnameIfaceName = headerInterfaceName("dupname.h");
  98         String dupnameDupnameNotName = structInterfaceName("dupname.h", "dupnameNot");
  99         String dupnameForwarderName = staticForwarderName("dupname.h");
 100         String srcgentestPointName = structInterfaceName("srcgentest.h", "Point");
 101         String srcgentestColorName = enumInterfaceName("srcgentest.h", "Color");
 102         String srcgentestForwarderEnumName = enumForwarderInterfaceName("srcgentest.h", "Color");
 103 
 104         // compile jextract generated java sources
 105         ArrayList<String> javacOpts = new ArrayList<>();
 106         javacOpts.add("-d");
 107         javacOpts.add(outputDir.toString());
 108         javacOpts.add(pkgDir.resolve(srcgentestIfaceName + ".java").toString());
 109         javacOpts.add(pkgDir.resolve(srcgentestForwarderName + ".java").toString());
 110         javacOpts.add(pkgDir.resolve("sub").resolve(dupnameIfaceName + ".java").toString());
 111         javacOpts.add(pkgDir.resolve("sub").resolve(dupnameForwarderName + ".java").toString());
 112         result = JAVAC.run(System.out, System.err, javacOpts.toArray(String[]::new));
 113         if (result != 0) {
 114             throw new RuntimeException(JAVAC.name() + " returns non-zero value");
 115         }
 116 
 117         // sanity checks for .class file existence
 118         assertTrue(Files.isRegularFile(pkgDir.resolve(srcgentestIfaceName + ".class")));
 119         assertTrue(Files.isRegularFile(pkgDir.resolve(srcgentestPointName + ".class")));
 120         assertTrue(Files.isRegularFile(pkgDir.resolve(srcgentestColorName + ".class")));
 121         assertTrue(Files.isRegularFile(pkgDir.resolve(srcgentestForwarderName + ".class")));
 122         assertTrue(Files.isRegularFile(pkgDir.resolve(srcgentestForwarderEnumName + ".class")));
 123         assertTrue(Files.isRegularFile(pkgDir.resolve("sub").resolve(dupnameForwarderName + ".class")));
 124         assertTrue(Files.isRegularFile(pkgDir.resolve("sub").resolve(dupnameIfaceName + ".class")));
 125         assertTrue(Files.isRegularFile(pkgDir.resolve("sub").resolve(dupnameDupnameNotName + ".class")));
 126 
 127         checkClasses(outputDir, pkgName);
 128     }
 129 
 130     private void checkClasses(Path outputDir, String pkgName) {
 131         Loader loader = classLoader(outputDir);
 132         checkSubdirClass(loader, pkgName);
 133         checkHeaderClass(loader, pkgName);
 134         checkForwarderClass(loader, pkgName);
 135     }
 136 
 137     private void checkSubdirClass(Loader loader, String pkgName) {
 138         Class<?> dupname = loader.loadClass(pkgName + ".sub." + headerInterfaceName("dupname.h"));
 139         assertTrue(dupname != null);
 140     }
 141 
 142     private void checkHeaderClass(Loader loader, String pkgName) {
 143         Class<?> headerCls = loader.loadClass(pkgName + "." + headerInterfaceName("srcgentest.h"));
 144         assertTrue(headerCls != null);
 145         assertTrue(headerCls.getAnnotation(NativeHeader.class) != null);
 146 
 147         // global 'num' getter, pointer getter
 148         Method numGetter = findGlobalVariableGet(headerCls, "num");
 149         assertTrue(numGetter != null);
 150         assertTrue(numGetter.getAnnotation(NativeGetter.class) != null);
 151 
 152         Method numPtrGetter = findGlobalVariablePointerGet(headerCls, "num");
 153         assertTrue(numPtrGetter != null);
 154         assertTrue(numPtrGetter.getAnnotation(NativeAddressof.class) != null);
 155 
 156         // global 'num' setter
 157         Method numSetter = findGlobalVariableSet(headerCls, "num", int.class);
 158         assertTrue(numSetter != null);
 159         assertTrue(numSetter.getAnnotation(NativeSetter.class) != null);
 160 
 161         // struct Point
 162         Class<?> pointCls = Arrays.stream(headerCls.getClasses())
 163             .filter(c -> c.getSimpleName().equals("Point")).findFirst().get();
 164         assertTrue(Modifier.isInterface(pointCls.getModifiers()));
 165         assertTrue(pointCls.getAnnotation(NativeStruct.class) != null);
 166 
 167         // Point extends Struct
 168         Class<?> pointSuper = pointCls.getInterfaces()[0];
 169         assertTrue(pointSuper.getName().equals("java.foreign.memory.Struct"));
 170 
 171         // x, y getters, pointer getters
 172         Method xGetter = findStructFieldGet(pointCls, "x");
 173         assertTrue(xGetter != null);
 174         assertTrue(xGetter.getAnnotation(NativeGetter.class) != null);
 175 
 176         Method yGetter = findStructFieldGet(pointCls, "y");
 177         assertTrue(yGetter != null);
 178         assertTrue(yGetter.getAnnotation(NativeGetter.class) != null);
 179 
 180         Method xPtrGetter = findStructFieldPointerGet(pointCls, "x");
 181         assertTrue(xPtrGetter != null);
 182         assertTrue(xPtrGetter.getAnnotation(NativeAddressof.class) != null);
 183 
 184         Method yPtrGetter = findStructFieldPointerGet(pointCls, "y");
 185         assertTrue(yPtrGetter != null);
 186         assertTrue(yPtrGetter.getAnnotation(NativeAddressof.class) != null);
 187 
 188         // x, y setters
 189         Method xSetter = findStructFieldSet(pointCls, "x", int.class);
 190         assertTrue(xSetter != null);
 191         assertTrue(xSetter.getAnnotation(NativeSetter.class) != null);
 192 
 193         Method ySetter = findStructFieldSet(pointCls, "y", int.class);
 194         assertTrue(ySetter != null);
 195         assertTrue(ySetter.getAnnotation(NativeSetter.class) != null);
 196     }
 197 
 198     private void checkForwarderClass(Loader loader, String pkgName) {
 199         Class<?> forwarderCls = loader.loadClass(pkgName + "." + staticForwarderName("srcgentest.h"));
 200         assertTrue(forwarderCls != null);
 201 
 202         // check "sum" method
 203         Method sumMethod = findFirstMethod(forwarderCls, "sum");
 204         assertTrue(sumMethod.getReturnType() == int.class);
 205         Class<?>[] sumParamTypes = sumMethod.getParameterTypes();
 206         assertTrue(sumParamTypes.length == 2);
 207         assertTrue(sumParamTypes[0] == int.class);
 208         assertTrue(sumParamTypes[1] == Object[].class);
 209         assertTrue(Modifier.isStatic(sumMethod.getModifiers()));
 210 
 211         // check "x_coord" method
 212         Method xCoordMethod = findFirstMethod(forwarderCls, "x_coord");
 213         assertTrue(xCoordMethod.getReturnType() == int.class);
 214         Class<?>[] xCoordParamTypes = xCoordMethod.getParameterTypes();
 215         assertTrue(xCoordParamTypes.length == 1);
 216         assertTrue(xCoordParamTypes[0] == Pointer.class);
 217 
 218         // check "y_coord" method
 219         Method yCoordMethod = findFirstMethod(forwarderCls, "y_coord");
 220         assertTrue(yCoordMethod.getReturnType() == int.class);
 221         Class<?>[] yCoordParamTypes = yCoordMethod.getParameterTypes();
 222         assertTrue(yCoordParamTypes.length == 1);
 223         assertTrue(yCoordParamTypes[0] == Pointer.class);
 224 
 225         // global variable "num" getter
 226         Method numGet = findGlobalVariableGet(forwarderCls, "num");
 227         assertTrue(numGet != null);
 228 
 229         // anonymous enum fields
 230         assertTrue(findField(forwarderCls, "R") != null);
 231         assertTrue(findField(forwarderCls, "G") != null);
 232         assertTrue(findField(forwarderCls, "B") != null);
 233 
 234         // enum interface class
 235         Class<?> colorCls = Arrays.stream(forwarderCls.getClasses())
 236             .filter(c -> c.getSimpleName().equals("Color")).findFirst().get();
 237         assertTrue(Modifier.isInterface(colorCls.getModifiers()));
 238         checkIntField(colorCls, "RED", 0);
 239         checkIntField(colorCls, "GREEN", 1);
 240         checkIntField(colorCls, "BLUE", 2);
 241     }
 242 }