< prev index next >

test/jdk/com/sun/tools/jextract/test8221154/SrcGenTest.java

Print this page




   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


 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 }


   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.NativeLocation;
  28 import java.foreign.annotations.NativeSetter;
  29 import java.foreign.annotations.NativeStruct;
  30 import java.foreign.memory.Pointer;
  31 import java.io.File;
  32 import java.io.IOException;
  33 import java.lang.reflect.Method;
  34 import java.lang.reflect.Modifier;
  35 import java.nio.file.Files;
  36 import java.nio.file.Path;
  37 import java.nio.file.Paths;
  38 import java.util.Arrays;
  39 import java.util.ArrayList;
  40 import java.util.spi.ToolProvider;
  41 import org.testng.annotations.Test;
  42 import static org.testng.Assert.assertTrue;
  43 
  44 /*
  45  * @test
  46  * @bug 8221154 8221228 8221336 8221419 8221443
  47  * @summary jextract should generate java source files


 132         Loader loader = classLoader(outputDir);
 133         checkSubdirClass(loader, pkgName);
 134         checkHeaderClass(loader, pkgName);
 135         checkForwarderClass(loader, pkgName);
 136     }
 137 
 138     private void checkSubdirClass(Loader loader, String pkgName) {
 139         Class<?> dupname = loader.loadClass(pkgName + ".sub." + headerInterfaceName("dupname.h"));
 140         assertTrue(dupname != null);
 141     }
 142 
 143     private void checkHeaderClass(Loader loader, String pkgName) {
 144         Class<?> headerCls = loader.loadClass(pkgName + "." + headerInterfaceName("srcgentest.h"));
 145         assertTrue(headerCls != null);
 146         assertTrue(headerCls.getAnnotation(NativeHeader.class) != null);
 147 
 148         // global 'num' getter, pointer getter
 149         Method numGetter = findGlobalVariableGet(headerCls, "num");
 150         assertTrue(numGetter != null);
 151         assertTrue(numGetter.getAnnotation(NativeGetter.class) != null);
 152         assertTrue(numGetter.getAnnotation(NativeLocation.class) != null);
 153 
 154         Method numPtrGetter = findGlobalVariablePointerGet(headerCls, "num");
 155         assertTrue(numPtrGetter != null);
 156         assertTrue(numPtrGetter.getAnnotation(NativeAddressof.class) != null);
 157 
 158         // global 'num' setter
 159         Method numSetter = findGlobalVariableSet(headerCls, "num", int.class);
 160         assertTrue(numSetter != null);
 161         assertTrue(numSetter.getAnnotation(NativeSetter.class) != null);
 162 
 163         // check "x_coord" method
 164         Method xCoordMethod = findFirstMethod(headerCls, "x_coord");
 165         assertTrue(xCoordMethod.getReturnType() == int.class);
 166         Class<?>[] xCoordParamTypes = xCoordMethod.getParameterTypes();
 167         assertTrue(xCoordParamTypes.length == 1);
 168         assertTrue(xCoordParamTypes[0] == Pointer.class);
 169         assertTrue(xCoordMethod.getAnnotation(NativeLocation.class) != null);
 170         assertTrue(!Modifier.isStatic(xCoordMethod.getModifiers()));
 171 
 172         // check "y_coord" method
 173         Method yCoordMethod = findFirstMethod(headerCls, "y_coord");
 174         assertTrue(yCoordMethod.getReturnType() == int.class);
 175         Class<?>[] yCoordParamTypes = yCoordMethod.getParameterTypes();
 176         assertTrue(yCoordParamTypes.length == 1);
 177         assertTrue(yCoordParamTypes[0] == Pointer.class);
 178         assertTrue(yCoordMethod.getAnnotation(NativeLocation.class) != null);
 179         assertTrue(!Modifier.isStatic(yCoordMethod.getModifiers()));
 180 
 181         // check "sum" method
 182         Method sumMethod = findFirstMethod(headerCls, "sum");
 183         assertTrue(sumMethod.getReturnType() == int.class);
 184         Class<?>[] sumParamTypes = sumMethod.getParameterTypes();
 185         assertTrue(sumParamTypes.length == 2);
 186         assertTrue(sumParamTypes[0] == int.class);
 187         assertTrue(sumParamTypes[1] == Object[].class);
 188         assertTrue(sumMethod.getAnnotation(NativeLocation.class) != null);
 189         assertTrue(!Modifier.isStatic(sumMethod.getModifiers()));
 190 
 191         // struct Point
 192         Class<?> pointCls = Arrays.stream(headerCls.getClasses())
 193             .filter(c -> c.getSimpleName().equals("Point")).findFirst().get();
 194         assertTrue(Modifier.isInterface(pointCls.getModifiers()));
 195         assertTrue(pointCls.getAnnotation(NativeStruct.class) != null);
 196         assertTrue(pointCls.getAnnotation(NativeLocation.class) != null);
 197 
 198         // Point extends Struct
 199         Class<?> pointSuper = pointCls.getInterfaces()[0];
 200         assertTrue(pointSuper.getName().equals("java.foreign.memory.Struct"));
 201 
 202         // x, y getters, pointer getters
 203         Method xGetter = findStructFieldGet(pointCls, "x");
 204         assertTrue(xGetter != null);
 205         assertTrue(xGetter.getAnnotation(NativeGetter.class) != null);
 206         assertTrue(xGetter.getAnnotation(NativeLocation.class) != null);
 207 
 208         Method yGetter = findStructFieldGet(pointCls, "y");
 209         assertTrue(yGetter != null);
 210         assertTrue(yGetter.getAnnotation(NativeGetter.class) != null);
 211         assertTrue(yGetter.getAnnotation(NativeLocation.class) != null);
 212 
 213         Method xPtrGetter = findStructFieldPointerGet(pointCls, "x");
 214         assertTrue(xPtrGetter != null);
 215         assertTrue(xPtrGetter.getAnnotation(NativeAddressof.class) != null);
 216 
 217         Method yPtrGetter = findStructFieldPointerGet(pointCls, "y");
 218         assertTrue(yPtrGetter != null);
 219         assertTrue(yPtrGetter.getAnnotation(NativeAddressof.class) != null);
 220 
 221         // x, y setters
 222         Method xSetter = findStructFieldSet(pointCls, "x", int.class);
 223         assertTrue(xSetter != null);
 224         assertTrue(xSetter.getAnnotation(NativeSetter.class) != null);
 225 
 226         Method ySetter = findStructFieldSet(pointCls, "y", int.class);
 227         assertTrue(ySetter != null);
 228         assertTrue(ySetter.getAnnotation(NativeSetter.class) != null);
 229     }
 230 
 231     private void checkForwarderClass(Loader loader, String pkgName) {
 232         Class<?> forwarderCls = loader.loadClass(pkgName + "." + staticForwarderName("srcgentest.h"));
 233         assertTrue(forwarderCls != null);
 234 
 235         // check "sum" method
 236         Method sumMethod = findFirstMethod(forwarderCls, "sum");
 237         assertTrue(sumMethod.getReturnType() == int.class);
 238         Class<?>[] sumParamTypes = sumMethod.getParameterTypes();
 239         assertTrue(sumParamTypes.length == 2);
 240         assertTrue(sumParamTypes[0] == int.class);
 241         assertTrue(sumParamTypes[1] == Object[].class);
 242         assertTrue(Modifier.isStatic(sumMethod.getModifiers()));
 243 
 244         // check "x_coord" method
 245         Method xCoordMethod = findFirstMethod(forwarderCls, "x_coord");
 246         assertTrue(xCoordMethod.getReturnType() == int.class);
 247         Class<?>[] xCoordParamTypes = xCoordMethod.getParameterTypes();
 248         assertTrue(xCoordParamTypes.length == 1);
 249         assertTrue(xCoordParamTypes[0] == Pointer.class);
 250         assertTrue(Modifier.isStatic(xCoordMethod.getModifiers()));
 251 
 252         // check "y_coord" method
 253         Method yCoordMethod = findFirstMethod(forwarderCls, "y_coord");
 254         assertTrue(yCoordMethod.getReturnType() == int.class);
 255         Class<?>[] yCoordParamTypes = yCoordMethod.getParameterTypes();
 256         assertTrue(yCoordParamTypes.length == 1);
 257         assertTrue(yCoordParamTypes[0] == Pointer.class);
 258         assertTrue(Modifier.isStatic(xCoordMethod.getModifiers()));
 259 
 260         // global variable "num" getter
 261         Method numGet = findGlobalVariableGet(forwarderCls, "num");
 262         assertTrue(numGet != null);
 263 
 264         // anonymous enum fields
 265         assertTrue(findField(forwarderCls, "R") != null);
 266         assertTrue(findField(forwarderCls, "G") != null);
 267         assertTrue(findField(forwarderCls, "B") != null);
 268 
 269         // enum interface class
 270         Class<?> colorCls = Arrays.stream(forwarderCls.getClasses())
 271             .filter(c -> c.getSimpleName().equals("Color")).findFirst().get();
 272         assertTrue(Modifier.isInterface(colorCls.getModifiers()));
 273         checkIntField(colorCls, "RED", 0);
 274         checkIntField(colorCls, "GREEN", 1);
 275         checkIntField(colorCls, "BLUE", 2);
 276     }
 277 }
< prev index next >