use of org.objectweb.asm.ClassVisitor in project drill by apache.
the class ReplaceMethodInvoke method check.
private static final void check(final byte[] b) {
final ClassReader cr = new ClassReader(b);
final ClassWriter cw = writer();
final ClassVisitor cv = new DrillCheckClassAdapter(cw);
cr.accept(cv, 0);
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
DrillCheckClassAdapter.verify(new ClassReader(cw.toByteArray()), false, pw);
final String checkString = sw.toString();
if (!checkString.isEmpty()) {
throw new IllegalStateException(checkString);
}
}
use of org.objectweb.asm.ClassVisitor in project maven-plugins by apache.
the class DefaultShader method addRemappedClass.
private void addRemappedClass(RelocatorRemapper remapper, JarOutputStream jos, File jar, String name, InputStream is) throws IOException, MojoExecutionException {
if (!remapper.hasRelocators()) {
try {
jos.putNextEntry(new JarEntry(name));
IOUtil.copy(is, jos);
} catch (ZipException e) {
getLogger().debug("We have a duplicate " + name + " in " + jar);
}
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) {
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);
super.visitSource(filename, debug);
}
}
};
try {
cr.accept(cv, ClassReader.EXPAND_FRAMES);
} catch (Throwable ise) {
throw new MojoExecutionException("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('.')));
try {
// Now we put it back on so the class file is written out with the right extension.
jos.putNextEntry(new JarEntry(mappedName + ".class"));
IOUtil.copy(renamedClass, jos);
} catch (ZipException e) {
getLogger().debug("We have a duplicate " + mappedName + " in " + jar);
}
}
use of org.objectweb.asm.ClassVisitor in project maven-plugins by apache.
the class DefaultShaderTest method testShaderWithRelocatedClassname.
public void testShaderWithRelocatedClassname() throws Exception {
DefaultShader s = newShader();
Set<File> set = new LinkedHashSet<File>();
set.add(new File("src/test/jars/test-project-1.0-SNAPSHOT.jar"));
set.add(new File("src/test/jars/plexus-utils-1.4.1.jar"));
List<Relocator> relocators = new ArrayList<Relocator>();
relocators.add(new SimpleRelocator("org/codehaus/plexus/util/", "_plexus/util/__", null, Arrays.<String>asList()));
List<ResourceTransformer> resourceTransformers = new ArrayList<ResourceTransformer>();
resourceTransformers.add(new ComponentsXmlResourceTransformer());
List<Filter> filters = new ArrayList<Filter>();
File file = new File("target/foo-relocate-class.jar");
ShadeRequest shadeRequest = new ShadeRequest();
shadeRequest.setJars(set);
shadeRequest.setUberJar(file);
shadeRequest.setFilters(filters);
shadeRequest.setRelocators(relocators);
shadeRequest.setResourceTransformers(resourceTransformers);
s.shade(shadeRequest);
URLClassLoader cl = new URLClassLoader(new URL[] { file.toURI().toURL() });
Class<?> c = cl.loadClass("_plexus.util.__StringUtils");
// first, ensure it works:
Object o = c.newInstance();
assertEquals("", c.getMethod("clean", String.class).invoke(o, (String) null));
// now, check that its source file was rewritten:
final String[] source = { null };
final ClassReader classReader = new ClassReader(cl.getResourceAsStream("_plexus/util/__StringUtils.class"));
classReader.accept(new ClassVisitor(Opcodes.ASM4) {
@Override
public void visitSource(String arg0, String arg1) {
super.visitSource(arg0, arg1);
source[0] = arg0;
}
}, ClassReader.SKIP_CODE);
assertEquals("__StringUtils.java", source[0]);
}
use of org.objectweb.asm.ClassVisitor in project maven-plugins by apache.
the class AsmModuleInfoParser method getModuleDescriptor.
@Override
public JavaModuleDescriptor getModuleDescriptor(File modulePath) throws IOException {
final JavaModuleDescriptorWrapper wrapper = new JavaModuleDescriptorWrapper();
InputStream in = getModuleInfoClass(modulePath);
if (in != null) {
ClassReader reader = new ClassReader(in);
reader.accept(new ClassVisitor(Opcodes.ASM6) {
}, 0);
in.close();
} else {
wrapper.builder = JavaModuleDescriptor.newAutomaticModule(null);
}
return wrapper.builder.build();
}
use of org.objectweb.asm.ClassVisitor in project cdap by caskdata.
the class SparkClassRewriter method rewriteDStreamGraph.
/**
* Rewrites the DStreamGraph class for calls to parallel array with a call to
* SparkRuntimeUtils#setTaskSupport(ParArray).
*/
private byte[] rewriteDStreamGraph(InputStream byteCodeStream) throws IOException {
ClassReader cr = new ClassReader(byteCodeStream);
ClassWriter cw = new ClassWriter(0);
cr.accept(new ClassVisitor(Opcodes.ASM5, cw) {
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
return new MethodVisitor(Opcodes.ASM5, mv) {
@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
super.visitMethodInsn(opcode, owner, name, desc, itf);
// If detected call to ArrayBuffer.par(), set the TaskSupport to avoid thread leak.
//INVOKEVIRTUAL scala/collection/mutable/ ArrayBuffer.par ()Lscala/collection/parallel/mutable/ParArray;
Type returnType = Type.getReturnType(desc);
if (opcode == Opcodes.INVOKEVIRTUAL && name.equals("par") && owner.equals("scala/collection/mutable/ArrayBuffer") && returnType.getClassName().equals("scala.collection.parallel.mutable.ParArray")) {
super.visitMethodInsn(Opcodes.INVOKESTATIC, SPARK_RUNTIME_UTILS_TYPE.getInternalName(), "setTaskSupport", Type.getMethodDescriptor(returnType, returnType), false);
}
}
};
}
}, ClassReader.EXPAND_FRAMES);
return cw.toByteArray();
}
Aggregations