net.bytebuddy.jar.asm.ClassWriter Java Examples

The following examples show how to use net.bytebuddy.jar.asm.ClassWriter. 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: InstrumentationUtils.java    From BlockHound with Apache License 2.0 6 votes vote down vote up
static void injectBootstrapClasses(Instrumentation instrumentation, String... classNames) throws IOException {
    File tempJarFile = File.createTempFile("BlockHound", ".jar");
    tempJarFile.deleteOnExit();

    ClassLoader classLoader = BlockHound.class.getClassLoader();
    try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(tempJarFile))) {
        for (String className : classNames) {
            String classFile = className.replace(".", "/") + ".class";
            try (InputStream inputStream = classLoader.getResourceAsStream(classFile)) {
                ZipEntry entry = new ZipEntry(classFile);
                zipOutputStream.putNextEntry(entry);

                ClassReader cr = new ClassReader(inputStream);
                ClassWriter cw = new ClassWriter(cr, 0);

                cr.accept(new MakePublicClassVisitor(cw), 0);

                zipOutputStream.write(cw.toByteArray());
            }

            zipOutputStream.closeEntry();
        }
    }
    instrumentation.appendToBootstrapClassLoaderSearch(new JarFile(tempJarFile));
}
 
Example #2
Source File: ClassFileExtraction.java    From garmadon with Apache License 2.0 5 votes vote down vote up
public static byte[] extract(Class<?> type, AsmVisitorWrapper asmVisitorWrapper) throws IOException {
    ClassReader classReader = new ClassReader(type.getName());
    ClassWriter classWriter = new ClassWriter(classReader, AsmVisitorWrapper.NO_FLAGS);
    classReader.accept(asmVisitorWrapper.wrap(new TypeDescription.ForLoadedType(type),
            classWriter,
            new IllegalContext(),
            TypePool.Empty.INSTANCE,
            new FieldList.Empty<FieldDescription.InDefinedShape>(),
            new MethodList.Empty<MethodDescription>(),
            AsmVisitorWrapper.NO_FLAGS,
            AsmVisitorWrapper.NO_FLAGS), AsmVisitorWrapper.NO_FLAGS);
    return classWriter.toByteArray();
}
 
Example #3
Source File: InstrumentationUtils.java    From BlockHound with Apache License 2.0 4 votes vote down vote up
MakePublicClassVisitor(ClassWriter cw) {
    super(Opcodes.ASM7, cw);
}
 
Example #4
Source File: ReactorDebugAgent.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
private static void instrument(Instrumentation instrumentation) {
	ClassFileTransformer transformer = new ClassFileTransformer() {
		@Override
		public byte[] transform(
				ClassLoader loader,
				String className,
				Class<?> clazz,
				ProtectionDomain protectionDomain,
				byte[] bytes
		) {
			if (loader == null) {
				return null;
			}

			if (
					className == null ||
							className.startsWith("java/") ||
							className.startsWith("jdk/") ||
							className.startsWith("sun/") ||
							className.startsWith("com/sun/") ||
							className.startsWith("reactor/core/")
			) {
				return null;
			}

			if (
					clazz != null && (
							clazz.isPrimitive() ||
									clazz.isArray() ||
									clazz.isAnnotation() ||
									clazz.isSynthetic()
					)
			) {
				return null;
			}

			ClassReader cr = new ClassReader(bytes);
			ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);

			AtomicBoolean changed = new AtomicBoolean();
			ClassVisitor classVisitor = new ReactorDebugClassVisitor(cw, changed);

			try {
				cr.accept(classVisitor, 0);
			}
			catch (Throwable e) {
				e.printStackTrace();
				throw e;
			}

			if (!changed.get()) {
				return null;
			}

			return cw.toByteArray();
		}
	};

	instrumentation.addTransformer(transformer, true);
}
 
Example #5
Source File: ClassWriterFlags.java    From kanela with Apache License 2.0 2 votes vote down vote up
/**
 * In versions of the JVM  &gt; 1.5 the classes bytecode contain a stack map along with the method code. This map describes the layout of the stack at key points (jump targets) during the method's execution.
 *
 * In previous versions(JVM &lt; 1.5 bytecode), the JVM would have to compute this information, which is computationally expensive.
 *
 * By requiring this information, the JVM can just verify that the frames work, which is significantly easier than recalculating everything.
 *
 * So if JVM &lt; 1.5 ClassWriter.COMPUTE_MAXS otherwise ClassWriter.COMPUTE_FRAME(computeFrames implies computeMaxs).
 */
public static int resolve(TypeDescription typeDescription, ClassLoader classLoader) {
    if (classFileVersionIsGreaterThan(ClassFileVersion.JAVA_V5, typeDescription, classLoader)) return ClassWriter.COMPUTE_FRAMES;
    return ClassWriter.COMPUTE_MAXS;
}