242 @Test(expectedExceptions = { NullPointerException.class })
243 public void testNull() throws Exception {
244 lookup().defineClass(null);
245 }
246
247 @Test(expectedExceptions = { NoClassDefFoundError.class })
248 public void testLinking() throws Exception {
249 lookup().defineClass(generateNonLinkableClass(THIS_PACKAGE + ".NonLinkableClass"));
250 }
251
252 @Test(expectedExceptions = { IllegalArgumentException.class })
253 public void testModuleInfo() throws Exception {
254 lookup().defineClass(generateModuleInfo());
255 }
256
257 /**
258 * Generates a class file with the given class name
259 */
260 byte[] generateClass(String className) {
261 return ClassFile.of().build(ClassDesc.of(className), clb -> {
262 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.SUPER);
263 clb.withSuperclass(CD_Object);
264 clb.withMethodBody(INIT_NAME, MTD_void, PUBLIC, cob -> {
265 cob.aload(0);
266 cob.invokespecial(CD_Object, INIT_NAME, MTD_void);
267 cob.return_();
268 });
269 });
270 }
271
272 /**
273 * Generate a class file with the given class name. The class implements Runnable
274 * with a run method to invokestatic the given targetClass/targetMethod.
275 */
276 byte[] generateRunner(String className,
277 String targetClass,
278 String targetMethod) throws Exception {
279
280 return ClassFile.of().build(ClassDesc.of(className), clb -> {
281 clb.withSuperclass(CD_Object);
282 clb.withInterfaceSymbols(CD_Runnable);
284 cob.aload(0);
285 cob.invokespecial(CD_Object, INIT_NAME, MTD_void);
286 cob.return_();
287 });
288 clb.withMethodBody("run", MTD_void, PUBLIC, cob -> {
289 cob.invokestatic(ClassDesc.of(targetClass), targetMethod, MTD_void);
290 cob.return_();
291 });
292 });
293 }
294
295 /**
296 * Generate a class file with the given class name. The class will initializer
297 * to invokestatic the given targetClass/targetMethod.
298 */
299 byte[] generateClassWithInitializer(String className,
300 String targetClass,
301 String targetMethod) throws Exception {
302
303 return ClassFile.of().build(ClassDesc.of(className), clb -> {
304 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.SUPER);
305 clb.withSuperclass(CD_Object);
306 clb.withMethodBody(INIT_NAME, MTD_void, ACC_PUBLIC, cob -> {
307 cob.aload(0);
308 cob.invokespecial(CD_Object, INIT_NAME, MTD_void);
309 cob.return_();
310 });
311 clb.withMethodBody(CLASS_INIT_NAME, MTD_void, ACC_STATIC, cob -> {
312 cob.invokestatic(ClassDesc.of(targetClass), targetMethod, MTD_void);
313 cob.return_();
314 });
315 });
316 }
317
318 /**
319 * Generates a non-linkable class file with the given class name
320 */
321 byte[] generateNonLinkableClass(String className) {
322 return ClassFile.of().build(ClassDesc.of(className), clb -> {
323 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.SUPER);
324 clb.withSuperclass(CD_MissingSuperClass);
325 clb.withMethodBody(INIT_NAME, MTD_void, ACC_PUBLIC, cob -> {
326 cob.aload(0);
327 cob.invokespecial(CD_MissingSuperClass, INIT_NAME, MTD_void);
328 cob.return_();
329 });
330 });
331 }
332
333 /**
334 * Generates a class file with the given class name
335 */
336 byte[] generateModuleInfo() {
337 return ClassFile.of().build(ClassDesc.of("module-info"), cb -> cb.withFlags(AccessFlag.MODULE));
338 }
339
340 private int nextNumber() {
341 return ++nextNumber;
342 }
343
|
242 @Test(expectedExceptions = { NullPointerException.class })
243 public void testNull() throws Exception {
244 lookup().defineClass(null);
245 }
246
247 @Test(expectedExceptions = { NoClassDefFoundError.class })
248 public void testLinking() throws Exception {
249 lookup().defineClass(generateNonLinkableClass(THIS_PACKAGE + ".NonLinkableClass"));
250 }
251
252 @Test(expectedExceptions = { IllegalArgumentException.class })
253 public void testModuleInfo() throws Exception {
254 lookup().defineClass(generateModuleInfo());
255 }
256
257 /**
258 * Generates a class file with the given class name
259 */
260 byte[] generateClass(String className) {
261 return ClassFile.of().build(ClassDesc.of(className), clb -> {
262 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.IDENTITY);
263 clb.withSuperclass(CD_Object);
264 clb.withMethodBody(INIT_NAME, MTD_void, PUBLIC, cob -> {
265 cob.aload(0);
266 cob.invokespecial(CD_Object, INIT_NAME, MTD_void);
267 cob.return_();
268 });
269 });
270 }
271
272 /**
273 * Generate a class file with the given class name. The class implements Runnable
274 * with a run method to invokestatic the given targetClass/targetMethod.
275 */
276 byte[] generateRunner(String className,
277 String targetClass,
278 String targetMethod) throws Exception {
279
280 return ClassFile.of().build(ClassDesc.of(className), clb -> {
281 clb.withSuperclass(CD_Object);
282 clb.withInterfaceSymbols(CD_Runnable);
284 cob.aload(0);
285 cob.invokespecial(CD_Object, INIT_NAME, MTD_void);
286 cob.return_();
287 });
288 clb.withMethodBody("run", MTD_void, PUBLIC, cob -> {
289 cob.invokestatic(ClassDesc.of(targetClass), targetMethod, MTD_void);
290 cob.return_();
291 });
292 });
293 }
294
295 /**
296 * Generate a class file with the given class name. The class will initializer
297 * to invokestatic the given targetClass/targetMethod.
298 */
299 byte[] generateClassWithInitializer(String className,
300 String targetClass,
301 String targetMethod) throws Exception {
302
303 return ClassFile.of().build(ClassDesc.of(className), clb -> {
304 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.IDENTITY);
305 clb.withSuperclass(CD_Object);
306 clb.withMethodBody(INIT_NAME, MTD_void, ACC_PUBLIC, cob -> {
307 cob.aload(0);
308 cob.invokespecial(CD_Object, INIT_NAME, MTD_void);
309 cob.return_();
310 });
311 clb.withMethodBody(CLASS_INIT_NAME, MTD_void, ACC_STATIC, cob -> {
312 cob.invokestatic(ClassDesc.of(targetClass), targetMethod, MTD_void);
313 cob.return_();
314 });
315 });
316 }
317
318 /**
319 * Generates a non-linkable class file with the given class name
320 */
321 byte[] generateNonLinkableClass(String className) {
322 return ClassFile.of().build(ClassDesc.of(className), clb -> {
323 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.IDENTITY);
324 clb.withSuperclass(CD_MissingSuperClass);
325 clb.withMethodBody(INIT_NAME, MTD_void, ACC_PUBLIC, cob -> {
326 cob.aload(0);
327 cob.invokespecial(CD_MissingSuperClass, INIT_NAME, MTD_void);
328 cob.return_();
329 });
330 });
331 }
332
333 /**
334 * Generates a class file with the given class name
335 */
336 byte[] generateModuleInfo() {
337 return ClassFile.of().build(ClassDesc.of("module-info"), cb -> cb.withFlags(AccessFlag.MODULE));
338 }
339
340 private int nextNumber() {
341 return ++nextNumber;
342 }
343
|