use of org.objectweb.asm.ClassWriter in project storm by apache.
the class DefaultShader method addRemappedClass.
private void addRemappedClass(RelocatorRemapper remapper, JarOutputStream jos, String name, InputStream is) throws IOException {
LOG.debug("Remapping class... " + name);
if (!remapper.hasRelocators()) {
try {
LOG.debug("Just copy class...");
jos.putNextEntry(new JarEntry(name));
IOUtil.copy(is, jos);
} catch (ZipException e) {
LOG.info("zip exception ", e);
}
return;
}
ClassReader cr = new ClassReader(is);
// We don't pass the ClassReader here. This forces the ClassWriter to rebuild the constant pool.
// Copying the original constant pool should be avoided because it would keep references
// to the original class names. This is not a problem at runtime (because these entries in the
// constant pool are never used), but confuses some tools such as Felix' maven-bundle-plugin
// that use the constant pool to determine the dependencies of a class.
ClassWriter cw = new ClassWriter(0);
final String pkg = name.substring(0, name.lastIndexOf('/') + 1);
ClassVisitor cv = new RemappingClassAdapter(cw, remapper) {
@Override
public void visitSource(final String source, final String debug) {
LOG.debug("visitSource " + source);
if (source == null) {
super.visitSource(source, debug);
} else {
final String fqSource = pkg + source;
final String mappedSource = remapper.map(fqSource);
final String filename = mappedSource.substring(mappedSource.lastIndexOf('/') + 1);
LOG.debug("Remapped to " + filename);
super.visitSource(filename, debug);
}
}
};
try {
cr.accept(cv, ClassReader.EXPAND_FRAMES);
} catch (Throwable ise) {
throw new IOException("Error in ASM processing class " + name, ise);
}
byte[] renamedClass = cw.toByteArray();
// Need to take the .class off for remapping evaluation
String mappedName = remapper.map(name.substring(0, name.indexOf('.')));
LOG.debug("Remapped class name to " + mappedName);
try {
// Now we put it back on so the class file is written out with the right extension.
jos.putNextEntry(new JarEntry(mappedName + ".class"));
jos.write(renamedClass);
} catch (ZipException e) {
LOG.info("zip exception ", e);
}
}
use of org.objectweb.asm.ClassWriter in project cglib by cglib.
the class DebuggingClassWriter method toByteArray.
public byte[] toByteArray() {
return (byte[]) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
public Object run() {
byte[] b = ((ClassWriter) DebuggingClassWriter.super.cv).toByteArray();
if (debugLocation != null) {
String dirs = className.replace('.', File.separatorChar);
try {
new File(debugLocation + File.separatorChar + dirs).getParentFile().mkdirs();
File file = new File(new File(debugLocation), dirs + ".class");
OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
try {
out.write(b);
} finally {
out.close();
}
if (traceCtor != null) {
file = new File(new File(debugLocation), dirs + ".asm");
out = new BufferedOutputStream(new FileOutputStream(file));
try {
ClassReader cr = new ClassReader(b);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
ClassVisitor tcv = (ClassVisitor) traceCtor.newInstance(new Object[] { null, pw });
cr.accept(tcv, 0);
pw.flush();
} finally {
out.close();
}
}
} catch (Exception e) {
throw new CodeGenerationException(e);
}
}
return b;
}
});
}
use of org.objectweb.asm.ClassWriter in project buck by facebook.
the class FilesystemStubJarWriter method getStubClassBytes.
private static ByteSource getStubClassBytes(ClassNode node) {
ClassWriter writer = new ClassWriter(0);
node.accept(new AbiFilteringClassVisitor(writer));
return ByteSource.wrap(writer.toByteArray());
}
use of org.objectweb.asm.ClassWriter in project MinecraftForge by MinecraftForge.
the class DeobfuscationTransformer method transform.
// COMPUTE_FRAMES causes classes to be loaded, which could cause issues if the classes do not exist.
// However in testing this has not happened. {As we run post SideTransformer}
// If reported we need to add a custom implementation of ClassWriter.getCommonSuperClass
// that does not cause class loading.
@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
if (bytes == null) {
return null;
}
ClassReader classReader = new ClassReader(bytes);
ClassWriter classWriter = new ClassWriter(WRITER_FLAGS);
RemappingClassAdapter remapAdapter = new FMLRemappingAdapter(classWriter);
classReader.accept(remapAdapter, READER_FLAGS);
return classWriter.toByteArray();
}
use of org.objectweb.asm.ClassWriter in project MinecraftForge by MinecraftForge.
the class EventSubscriptionTransformer method transform.
@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
if (bytes == null || name.equals("net.minecraftforge.fml.common.eventhandler.Event") || name.startsWith("net.minecraft.") || name.indexOf('.') == -1) {
return bytes;
}
ClassReader cr = new ClassReader(bytes);
ClassNode classNode = new ClassNode();
cr.accept(classNode, 0);
try {
if (buildEvents(classNode)) {
ClassWriter cw = new ClassWriter(COMPUTE_FRAMES);
classNode.accept(cw);
return cw.toByteArray();
}
return bytes;
} catch (ClassNotFoundException ex) {
// Discard silently- it's just noise
} catch (Exception e) {
e.printStackTrace();
}
return bytes;
}
Aggregations