Search in sources :

Example 26 with ClassVisitor

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);
    }
}
Also used : StringWriter(java.io.StringWriter) ClassReader(org.objectweb.asm.ClassReader) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) ClassVisitor(org.objectweb.asm.ClassVisitor) DrillCheckClassAdapter(org.apache.drill.exec.compile.DrillCheckClassAdapter) ClassWriter(org.objectweb.asm.ClassWriter) PrintWriter(java.io.PrintWriter)

Example 27 with ClassVisitor

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);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) RemappingClassAdapter(org.objectweb.asm.commons.RemappingClassAdapter) ClassReader(org.objectweb.asm.ClassReader) ZipException(java.util.zip.ZipException) ClassVisitor(org.objectweb.asm.ClassVisitor) JarEntry(java.util.jar.JarEntry) ClassWriter(org.objectweb.asm.ClassWriter)

Example 28 with ClassVisitor

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]);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) ClassVisitor(org.objectweb.asm.ClassVisitor) Relocator(org.apache.maven.plugins.shade.relocation.Relocator) SimpleRelocator(org.apache.maven.plugins.shade.relocation.SimpleRelocator) SimpleRelocator(org.apache.maven.plugins.shade.relocation.SimpleRelocator) Filter(org.apache.maven.plugins.shade.filter.Filter) URLClassLoader(java.net.URLClassLoader) ComponentsXmlResourceTransformer(org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer) ClassReader(org.objectweb.asm.ClassReader) ResourceTransformer(org.apache.maven.plugins.shade.resource.ResourceTransformer) ComponentsXmlResourceTransformer(org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer) File(java.io.File)

Example 29 with ClassVisitor

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();
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor)

Example 30 with ClassVisitor

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();
}
Also used : Type(org.objectweb.asm.Type) ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Aggregations

ClassVisitor (org.objectweb.asm.ClassVisitor)70 ClassReader (org.objectweb.asm.ClassReader)47 ClassWriter (org.objectweb.asm.ClassWriter)41 MethodVisitor (org.objectweb.asm.MethodVisitor)23 InputStream (java.io.InputStream)12 IOException (java.io.IOException)10 Type (org.objectweb.asm.Type)9 Test (org.junit.Test)7 File (java.io.File)6 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)6 FieldList (net.bytebuddy.description.field.FieldList)5 MethodList (net.bytebuddy.description.method.MethodList)5 TypeDescription (net.bytebuddy.description.type.TypeDescription)5 TypePool (net.bytebuddy.pool.TypePool)5 TraceClassVisitor (org.objectweb.asm.util.TraceClassVisitor)5 URL (java.net.URL)4 MethodDescription (net.bytebuddy.description.method.MethodDescription)4 FileOutputStream (java.io.FileOutputStream)3 Path (java.nio.file.Path)3 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)3