Java Code Examples for jdk.internal.org.objectweb.asm.ClassWriter#toByteArray()
The following examples show how to use
jdk.internal.org.objectweb.asm.ClassWriter#toByteArray() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestInstrumentation.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
public byte[] transform( ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain pd, byte[] bytes) throws IllegalClassFormatException { // Check if this class should be instrumented. if (!instrClassesTarget.contains(className)) { return null; } boolean isRedefinition = classBeingRedefined != null; log("instrument class(" + className + ") " + (isRedefinition ? "redef" : "load")); ClassReader reader = new ClassReader(bytes); ClassWriter writer = new ClassWriter( reader, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); CallbackClassVisitor classVisitor = new CallbackClassVisitor(writer); reader.accept(classVisitor, 0); instrClassesDone.add(className); return writer.toByteArray(); }
Example 2
Source File: LambdaAsm.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
static void verifyASM() throws Exception { ClassWriter cw = new ClassWriter(0); cw.visit(V1_8, ACC_PUBLIC, "X", null, "java/lang/Object", null); MethodVisitor mv = cw.visitMethod(ACC_STATIC, "foo", "()V", null, null); mv.visitMaxs(2, 1); mv.visitMethodInsn(INVOKESTATIC, "java/util/function/Function.class", "identity", "()Ljava/util/function/Function;", true); mv.visitInsn(RETURN); cw.visitEnd(); byte[] carray = cw.toByteArray(); // for debugging // write((new File("X.class")).toPath(), carray, CREATE, TRUNCATE_EXISTING); // verify using javap/classfile reader ClassFile cf = ClassFile.read(new ByteArrayInputStream(carray)); int mcount = checkMethod(cf, "foo"); if (mcount < 1) { throw new RuntimeException("unexpected method count, expected 1" + "but got " + mcount); } }
Example 3
Source File: RedefineRunningMethodsWithResolutionErrors.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private static byte[] loadC(boolean redefine) { ClassWriter cw = new ClassWriter(0); cw.visit(52, ACC_SUPER | ACC_PUBLIC, "C", null, "java/lang/Object", null); { MethodVisitor mv; mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "m", "()V", null, null); mv.visitCode(); // First time we run we will: // 1) Cache resolution errors // 2) Redefine the class / method // 3) Try to read the resolution errors that were cached // // The redefined method will never run, throw error to be sure if (redefine) { createThrowRuntimeExceptionCode(mv, "The redefined method was called"); } else { createMethodBody(mv); } mv.visitMaxs(3, 0); mv.visitEnd(); } cw.visitEnd(); return cw.toByteArray(); }
Example 4
Source File: UnloadClass.java From LearningOfThinkInJava with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws NoSuchMethodException,SecurityException, IllegalAccessException,IllegalArgumentException,InvocationTargetException{ ClassWriter cw=new ClassWriter(ClassWriter.COMPUTE_MAXS|ClassWriter.COMPUTE_FRAMES); cw.visit(V1_7,ACC_PUBLIC,"Example",null,"java/lang/Object",null); MethodVisitor mw=cw.visitMethod(ACC_PUBLIC,"<init>","()V",null,null); mw.visitVarInsn(ALOAD,0); mw.visitMethodInsn(INVOKESPECIAL,"java/lang/Object","<init>","()V"); mw.visitInsn(RETURN); mw.visitMaxs(0,0); mw.visitEnd(); mw=cw.visitMethod(ACC_PUBLIC+ACC_STATIC,"main","([Ljava/lang/String;)V",null,null); mw.visitFieldInsn(GETSTATIC,"java/lang/System","out","Ljava/io/PrintStream;"); mw.visitLdcInsn("Hello world!"); mw.visitMethodInsn(INVOKEVIRTUAL,"java/io/PrintStream","println","(Ljava/lang/String;)V"); mw.visitInsn(RETURN); mw.visitMaxs(0,0); mw.visitEnd(); byte[] code=cw.toByteArray(); for(int i=0;i<1;i++){ ClassLoader loader=UnloadClass.class.getClassLoader(); Method m=ClassLoader.class.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class); m.setAccessible(true); m.invoke(loader,"Example",code,0,code.length); m.setAccessible(false); System.gc(); } }
Example 5
Source File: TestConcreteClassWithAbstractMethod.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
public static byte[] dumpT1() { ClassWriter cw = new ClassWriter(0); MethodVisitor mv; cw.visit(52, ACC_PUBLIC | ACC_SUPER, "p1/T1", null, "java/lang/Object", null); { mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitInsn(RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "m", "()I", null, null); mv.visitCode(); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("p1/T1.m()"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false); mv.visitIntInsn(BIPUSH, 3); mv.visitInsn(IRETURN); mv.visitMaxs(2, 1); mv.visitEnd(); } cw.visitEnd(); return cw.toByteArray(); }
Example 6
Source File: TestPrivateInterfaceMethodReflect.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
private byte[] loadClassData(String name) throws Exception { ClassWriter cw = new ClassWriter(0); MethodVisitor mv; switch (name) { case INTERFACE_NAME: cw.visit(V1_8, ACC_ABSTRACT | ACC_INTERFACE | ACC_PUBLIC, INTERFACE_NAME, null, "java/lang/Object", null); { mv = cw.visitMethod(ACC_PRIVATE, "privInstance", "()I", null, null); mv.visitCode(); mv.visitLdcInsn(EXPECTED); mv.visitInsn(IRETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } break; case CLASS_NAME: cw.visit(52, ACC_SUPER | ACC_PUBLIC, CLASS_NAME, null, "java/lang/Object", new String[]{INTERFACE_NAME}); { mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitInsn(RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } break; default: break; } cw.visitEnd(); return cw.toByteArray(); }
Example 7
Source File: RedefineAnnotations.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
public byte[] asm(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { ClassWriter cw = new ClassWriter(0); ClassVisitor cv = new ReAddDummyFieldsClassVisitor(ASM5, cw) { }; ClassReader cr = new ClassReader(classfileBuffer); cr.accept(cv, 0); return cw.toByteArray(); }
Example 8
Source File: EventInstrumentation.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
private byte[] toByteArray() { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); classNode.accept(cw); cw.visitEnd(); byte[] result = cw.toByteArray(); Utils.writeGeneratedASM(classNode.name, result); return result; }
Example 9
Source File: JIClassInstrumentation.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
private byte[] makeBytecode() throws IOException, ClassNotFoundException { // Find the methods to instrument and inline final List<Method> instrumentationMethods = new ArrayList<>(); for (final Method m : instrumentor.getDeclaredMethods()) { JIInstrumentationMethod im = m.getAnnotation(JIInstrumentationMethod.class); if (im != null) { instrumentationMethods.add(m); } } // We begin by inlining the target's methods into the instrumentor ClassNode temporary = new ClassNode(); ClassVisitor inliner = new JIInliner( Opcodes.ASM5, temporary, targetName, instrumentorName, targetClassReader, instrumentationMethods); instrClassReader.accept(inliner, ClassReader.EXPAND_FRAMES); // Now we have the target's methods inlined into the instrumentation code (in 'temporary'). // We now need to replace the target's method with the code in the // instrumentation method. ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); JIMethodMergeAdapter ma = new JIMethodMergeAdapter( cw, temporary, instrumentationMethods, instrumentor.getAnnotationsByType(JITypeMapping.class)); targetClassReader.accept(ma, ClassReader.EXPAND_FRAMES); return cw.toByteArray(); }
Example 10
Source File: BootstrapMethodErrorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private byte[] loadClassData(String name) throws Exception { ClassWriter cw = new ClassWriter( ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); if (name.equals(BOOTSTRAP_METHOD_CLASS_NAME)) { defineIndyBootstrapMethodClass(cw); return cw.toByteArray(); } else if (name.equals("Exec")) { defineIndyCallingClass(cw); return cw.toByteArray(); } return null; }
Example 11
Source File: ModuleInfoExtender.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Returns the bytes of the modified module-info.class. * Once this method has been called then the Extender object should * be discarded. */ public byte[] toByteArray() throws IOException { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES); AttributeAddingClassVisitor cv = new AttributeAddingClassVisitor(Opcodes.ASM5, cw); ClassReader cr = new ClassReader(in); if (packages != null) cv.addAttribute(new ModulePackagesAttribute(packages)); if (mainClass != null) cv.addAttribute(new ModuleMainClassAttribute(mainClass)); if (targetPlatform != null) cv.addAttribute(new ModuleTargetAttribute(targetPlatform)); if (hashes != null) cv.addAttribute(new ModuleHashesAttribute(hashes)); if (moduleResolution != null) cv.addAttribute(new ModuleResolutionAttribute(moduleResolution.value())); List<Attribute> attrs = new ArrayList<>(); // prototypes of attributes that should be parsed attrs.add(new ModuleAttribute(version)); attrs.add(new ModulePackagesAttribute()); attrs.add(new ModuleMainClassAttribute()); attrs.add(new ModuleTargetAttribute()); attrs.add(new ModuleHashesAttribute()); cr.accept(cv, attrs.toArray(new Attribute[0]), 0); // add any attributes that didn't replace previous attributes cv.finish(); return cw.toByteArray(); }
Example 12
Source File: LambdaStackTrace.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
private static byte[] generateStringMaker() { // interface StringMaker extends Maker { // String make(); // } ClassWriter cw = new ClassWriter(0); cw.visit(V1_7, ACC_INTERFACE | ACC_ABSTRACT, "StringMaker", null, "java/lang/Object", new String[]{"Maker"}); cw.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, "make", "()Ljava/lang/String;", null, null); cw.visitEnd(); return cw.toByteArray(); }
Example 13
Source File: ModuleInfoWriter.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Writes the given module descriptor to a module-info.class file, * returning it in a byte array. */ private static byte[] toModuleInfo(ModuleDescriptor md, ModuleTarget target) { ClassWriter cw = new ClassWriter(0); cw.visit(Opcodes.V1_9, ACC_MODULE, "module-info", null, null, null); cw.visitAttribute(new ModuleAttribute(md)); // for tests: write the ModulePackages attribute when there are packages // that aren't exported or open Stream<String> exported = md.exports().stream() .map(ModuleDescriptor.Exports::source); Stream<String> open = md.opens().stream() .map(ModuleDescriptor.Opens::source); long exportedOrOpen = Stream.concat(exported, open).distinct().count(); if (md.packages().size() > exportedOrOpen) cw.visitAttribute(new ModulePackagesAttribute(md.packages())); // write ModuleMainClass if the module has a main class md.mainClass().ifPresent(mc -> cw.visitAttribute(new ModuleMainClassAttribute(mc))); // write ModuleTarget if there is a target platform if (target != null) { cw.visitAttribute(new ModuleTargetAttribute(target.targetPlatform())); } cw.visitEnd(); return cw.toByteArray(); }
Example 14
Source File: LambdaStackTrace.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
private static byte[] generateStringMaker() { // interface StringMaker extends Maker { // String make(); // } ClassWriter cw = new ClassWriter(0); cw.visit(V1_7, ACC_INTERFACE | ACC_ABSTRACT, "StringMaker", null, "java/lang/Object", new String[]{"Maker"}); cw.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, "make", "()Ljava/lang/String;", null, null); cw.visitEnd(); return cw.toByteArray(); }
Example 15
Source File: TestConcreteClassWithAbstractMethod.java From hottub with GNU General Public License v2.0 | 5 votes |
public static byte[] dumpT1() { ClassWriter cw = new ClassWriter(0); MethodVisitor mv; cw.visit(52, ACC_PUBLIC | ACC_SUPER, "p1/T1", null, "java/lang/Object", null); { mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitInsn(RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "m", "()I", null, null); mv.visitCode(); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("p1/T1.m()"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false); mv.visitIntInsn(BIPUSH, 3); mv.visitInsn(IRETURN); mv.visitMaxs(2, 1); mv.visitEnd(); } cw.visitEnd(); return cw.toByteArray(); }
Example 16
Source File: ConstructorTracerWriter.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
static byte[] generateBytes(Class<?> clz, byte[] oldBytes) throws IOException { InputStream in = new ByteArrayInputStream(oldBytes); ClassReader cr = new ClassReader(in); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); ConstructorTracerWriter ctw = new ConstructorTracerWriter(cw, clz); cr.accept(ctw, 0); return cw.toByteArray(); }
Example 17
Source File: GenerateJLIClassesHelper.java From Bytecoder with Apache License 2.0 | 5 votes |
private static byte[] generateCodeBytesForLFs(String className, String[] names, LambdaForm[] forms) { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES); cw.visit(Opcodes.V1_8, Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER, className, null, InvokerBytecodeGenerator.INVOKER_SUPER_NAME, null); cw.visitSource(className.substring(className.lastIndexOf('/') + 1), null); for (int i = 0; i < forms.length; i++) { addMethod(className, names[i], forms[i], forms[i].methodType(), cw); } return cw.toByteArray(); }
Example 18
Source File: TestAMEnotNPE.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
/** * Returns the bytes for a class with name d_name (presumably "D" in some * package), extending some class with name sub_what, implementing p.I, * and defining two methods m() and m(11args) with access method_acc. * * @param d_name Name of class that is defined * @param sub_what Name of class that it extends * @param method_acc Accessibility of method(s) m in defined class. * @return * @throws Exception */ public static byte[] bytesForSomeDsubSomethingSomeAccess (String d_name, String sub_what, int method_acc) throws Exception { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); MethodVisitor mv; String[] interfaces = {"p/I"}; cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, d_name, null, sub_what, interfaces); { mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, sub_what, "<init>", "()V"); mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } // int m() {return 3;} { mv = cw.visitMethod(method_acc, "m", "()I", null, null); mv.visitCode(); mv.visitLdcInsn(new Integer(3)); mv.visitInsn(IRETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } // int m(11args) {return 3;} { mv = cw.visitMethod(method_acc, "m", "(BCSIJ" + "Ljava/lang/Object;" + "Ljava/lang/Object;" + "Ljava/lang/Object;" + "Ljava/lang/Object;" + "Ljava/lang/Object;" + "Ljava/lang/Object;" + ")I", null, null); mv.visitCode(); mv.visitLdcInsn(new Integer(3)); mv.visitInsn(IRETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } cw.visitEnd(); return cw.toByteArray(); }
Example 19
Source File: TestAMEnotNPE.java From TencentKona-8 with GNU General Public License v2.0 | 4 votes |
/** * Returns the bytes for a class with name d_name (presumably "D" in some * package), extending some class with name sub_what, implementing p.I, * and defining two methods m() and m(11args) with access method_acc. * * @param d_name Name of class that is defined * @param sub_what Name of class that it extends * @param method_acc Accessibility of method(s) m in defined class. * @return * @throws Exception */ public static byte[] bytesForSomeDsubSomethingSomeAccess (String d_name, String sub_what, int method_acc) throws Exception { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); MethodVisitor mv; String[] interfaces = {"p/I"}; cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, d_name, null, sub_what, interfaces); { mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, sub_what, "<init>", "()V"); mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } // int m() {return 3;} { mv = cw.visitMethod(method_acc, "m", "()I", null, null); mv.visitCode(); mv.visitLdcInsn(new Integer(3)); mv.visitInsn(IRETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } // int m(11args) {return 3;} { mv = cw.visitMethod(method_acc, "m", "(BCSIJ" + "Ljava/lang/Object;" + "Ljava/lang/Object;" + "Ljava/lang/Object;" + "Ljava/lang/Object;" + "Ljava/lang/Object;" + "Ljava/lang/Object;" + ")I", null, null); mv.visitCode(); mv.visitLdcInsn(new Integer(3)); mv.visitInsn(IRETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } cw.visitEnd(); return cw.toByteArray(); }
Example 20
Source File: TestAMEnotNPE.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
/** * Returns the bytes for a class with name d_name (presumably "D" in some * package), extending some class with name sub_what, implementing p.I, * and defining two methods m() and m(11args) with access method_acc. * * @param d_name Name of class that is defined * @param sub_what Name of class that it extends * @param method_acc Accessibility of method(s) m in defined class. * @return * @throws Exception */ public static byte[] bytesForSomeDsubSomethingSomeAccess (String d_name, String sub_what, int method_acc) throws Exception { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); MethodVisitor mv; String[] interfaces = {"p/I"}; cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, d_name, null, sub_what, interfaces); { mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, sub_what, "<init>", "()V"); mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } // int m() {return 3;} { mv = cw.visitMethod(method_acc, "m", "()I", null, null); mv.visitCode(); mv.visitLdcInsn(new Integer(3)); mv.visitInsn(IRETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } // int m(11args) {return 3;} { mv = cw.visitMethod(method_acc, "m", "(BCSIJ" + "Ljava/lang/Object;" + "Ljava/lang/Object;" + "Ljava/lang/Object;" + "Ljava/lang/Object;" + "Ljava/lang/Object;" + "Ljava/lang/Object;" + ")I", null, null); mv.visitCode(); mv.visitLdcInsn(new Integer(3)); mv.visitInsn(IRETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } cw.visitEnd(); return cw.toByteArray(); }