226 Lookup lookup = lookup().dropLookupMode(PACKAGE);
227 lookup.defineClass(generateClass(THIS_PACKAGE + ".C"));
228 }
229
230 @Test(expectedExceptions = { ClassFormatError.class })
231 public void testTruncatedClassFile() throws Exception {
232 lookup().defineClass(new byte[0]);
233 }
234
235 @Test(expectedExceptions = { NullPointerException.class })
236 public void testNull() throws Exception {
237 lookup().defineClass(null);
238 }
239
240 /**
241 * Generates a class file with the given class name
242 */
243 byte[] generateClass(String className) {
244 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
245 + ClassWriter.COMPUTE_FRAMES);
246 cw.visit(V1_9,
247 ACC_PUBLIC + ACC_SUPER,
248 className.replace(".", "/"),
249 null,
250 "java/lang/Object",
251 null);
252
253 // <init>
254 MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
255 mv.visitVarInsn(ALOAD, 0);
256 mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
257 mv.visitInsn(RETURN);
258 mv.visitMaxs(0, 0);
259 mv.visitEnd();
260
261 cw.visitEnd();
262 return cw.toByteArray();
263 }
264
265 /**
266 * Generate a class file with the given class name. The class implements Runnable
267 * with a run method to invokestatic the given targetClass/targetMethod.
268 */
269 byte[] generateRunner(String className,
270 String targetClass,
271 String targetMethod) throws Exception {
272
273 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
274 + ClassWriter.COMPUTE_FRAMES);
275 cw.visit(V1_9,
276 ACC_PUBLIC + ACC_SUPER,
277 className.replace(".", "/"),
278 null,
279 "java/lang/Object",
280 new String[] { "java/lang/Runnable" });
281
282 // <init>
283 MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
284 mv.visitVarInsn(ALOAD, 0);
285 mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
286 mv.visitInsn(RETURN);
287 mv.visitMaxs(0, 0);
288 mv.visitEnd();
289
290 // run()
291 String tc = targetClass.replace(".", "/");
292 mv = cw.visitMethod(ACC_PUBLIC, "run", "()V", null, null);
293 mv.visitMethodInsn(INVOKESTATIC, tc, targetMethod, "()V", false);
294 mv.visitInsn(RETURN);
295 mv.visitMaxs(0, 0);
296 mv.visitEnd();
297
298 cw.visitEnd();
299 return cw.toByteArray();
300 }
301
302 /**
303 * Generate a class file with the given class name. The class will initializer
304 * to invokestatic the given targetClass/targetMethod.
305 */
306 byte[] generateClassWithInitializer(String className,
307 String targetClass,
308 String targetMethod) throws Exception {
309
310 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
311 + ClassWriter.COMPUTE_FRAMES);
312 cw.visit(V1_9,
313 ACC_PUBLIC + ACC_SUPER,
314 className.replace(".", "/"),
315 null,
316 "java/lang/Object",
317 null);
318
319 // <init>
320 MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
321 mv.visitVarInsn(ALOAD, 0);
322 mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
323 mv.visitInsn(RETURN);
324 mv.visitMaxs(0, 0);
325 mv.visitEnd();
326
327 // <clinit>
328 String tc = targetClass.replace(".", "/");
329 mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
330 mv.visitMethodInsn(INVOKESTATIC, tc, targetMethod, "()V", false);
331 mv.visitInsn(RETURN);
332 mv.visitMaxs(0, 0);
|
226 Lookup lookup = lookup().dropLookupMode(PACKAGE);
227 lookup.defineClass(generateClass(THIS_PACKAGE + ".C"));
228 }
229
230 @Test(expectedExceptions = { ClassFormatError.class })
231 public void testTruncatedClassFile() throws Exception {
232 lookup().defineClass(new byte[0]);
233 }
234
235 @Test(expectedExceptions = { NullPointerException.class })
236 public void testNull() throws Exception {
237 lookup().defineClass(null);
238 }
239
240 /**
241 * Generates a class file with the given class name
242 */
243 byte[] generateClass(String className) {
244 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
245 + ClassWriter.COMPUTE_FRAMES);
246 cw.visit(V9,
247 ACC_PUBLIC + ACC_SUPER,
248 className.replace(".", "/"),
249 null,
250 "java/lang/Object",
251 null);
252
253 // <init>
254 MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
255 mv.visitVarInsn(ALOAD, 0);
256 mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
257 mv.visitInsn(RETURN);
258 mv.visitMaxs(0, 0);
259 mv.visitEnd();
260
261 cw.visitEnd();
262 return cw.toByteArray();
263 }
264
265 /**
266 * Generate a class file with the given class name. The class implements Runnable
267 * with a run method to invokestatic the given targetClass/targetMethod.
268 */
269 byte[] generateRunner(String className,
270 String targetClass,
271 String targetMethod) throws Exception {
272
273 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
274 + ClassWriter.COMPUTE_FRAMES);
275 cw.visit(V9,
276 ACC_PUBLIC + ACC_SUPER,
277 className.replace(".", "/"),
278 null,
279 "java/lang/Object",
280 new String[] { "java/lang/Runnable" });
281
282 // <init>
283 MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
284 mv.visitVarInsn(ALOAD, 0);
285 mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
286 mv.visitInsn(RETURN);
287 mv.visitMaxs(0, 0);
288 mv.visitEnd();
289
290 // run()
291 String tc = targetClass.replace(".", "/");
292 mv = cw.visitMethod(ACC_PUBLIC, "run", "()V", null, null);
293 mv.visitMethodInsn(INVOKESTATIC, tc, targetMethod, "()V", false);
294 mv.visitInsn(RETURN);
295 mv.visitMaxs(0, 0);
296 mv.visitEnd();
297
298 cw.visitEnd();
299 return cw.toByteArray();
300 }
301
302 /**
303 * Generate a class file with the given class name. The class will initializer
304 * to invokestatic the given targetClass/targetMethod.
305 */
306 byte[] generateClassWithInitializer(String className,
307 String targetClass,
308 String targetMethod) throws Exception {
309
310 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
311 + ClassWriter.COMPUTE_FRAMES);
312 cw.visit(V9,
313 ACC_PUBLIC + ACC_SUPER,
314 className.replace(".", "/"),
315 null,
316 "java/lang/Object",
317 null);
318
319 // <init>
320 MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
321 mv.visitVarInsn(ALOAD, 0);
322 mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
323 mv.visitInsn(RETURN);
324 mv.visitMaxs(0, 0);
325 mv.visitEnd();
326
327 // <clinit>
328 String tc = targetClass.replace(".", "/");
329 mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
330 mv.visitMethodInsn(INVOKESTATIC, tc, targetMethod, "()V", false);
331 mv.visitInsn(RETURN);
332 mv.visitMaxs(0, 0);
|