Search in sources :

Example 81 with ClassWriter

use of org.objectweb.asm.ClassWriter in project evosuite by EvoSuite.

the class BytecodeInstrumentation method handleCarving.

private byte[] handleCarving(String className, ClassWriter writer) {
    ClassReader cr = new ClassReader(writer.toByteArray());
    ClassNode cn2 = new ClassNode();
    cr.accept(cn2, ClassReader.EXPAND_FRAMES);
    this.testCarvingInstrumenter.transformClassNode(cn2, className);
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cn2.accept(cw);
    if (logger.isDebugEnabled()) {
        final StringWriter sw = new StringWriter();
        cn2.accept(new TraceClassVisitor(new PrintWriter(sw)));
        logger.debug("test carving instrumentation result:\n{}", sw);
    }
    return cw.toByteArray();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) StringWriter(java.io.StringWriter) ClassReader(org.objectweb.asm.ClassReader) ClassWriter(org.objectweb.asm.ClassWriter) ComputeClassWriter(org.evosuite.runtime.util.ComputeClassWriter) PrintWriter(java.io.PrintWriter)

Example 82 with ClassWriter

use of org.objectweb.asm.ClassWriter in project evosuite by EvoSuite.

the class CarvingClassLoader method instrumentClass.

private Class<?> instrumentClass(String fullyQualifiedTargetClass) throws ClassNotFoundException {
    logger.warn("Instrumenting class '" + fullyQualifiedTargetClass + "'.");
    try {
        String className = fullyQualifiedTargetClass.replace('.', '/');
        InputStream is = ResourceList.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getClassAsStream(className);
        if (is == null) {
            throw new ClassNotFoundException("Class '" + className + ".class" + "' should be in target project, but could not be found!");
        }
        ClassReader reader = new ClassReader(is);
        ClassNode classNode = new ClassNode();
        reader.accept(classNode, ClassReader.SKIP_FRAMES);
        instrumenter.transformClassNode(classNode, className);
        ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
        classNode.accept(new JSRInlinerClassVisitor(writer));
        // classNode.accept(writer);
        byte[] byteBuffer = writer.toByteArray();
        Class<?> result = defineClass(fullyQualifiedTargetClass, byteBuffer, 0, byteBuffer.length);
        if (Modifier.isPrivate(result.getModifiers())) {
            logger.info("REPLACING PRIVATE CLASS " + fullyQualifiedTargetClass);
            result = super.loadClass(fullyQualifiedTargetClass);
        }
        classes.put(fullyQualifiedTargetClass, result);
        logger.info("Keeping class: " + fullyQualifiedTargetClass);
        return result;
    } catch (Throwable t) {
        logger.info("Error: " + t);
        for (StackTraceElement e : t.getStackTrace()) {
            logger.info(e.toString());
        }
        throw new ClassNotFoundException(t.getMessage(), t);
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) JSRInlinerClassVisitor(org.evosuite.runtime.instrumentation.JSRInlinerClassVisitor) InputStream(java.io.InputStream) ClassReader(org.objectweb.asm.ClassReader) ClassWriter(org.objectweb.asm.ClassWriter)

Example 83 with ClassWriter

use of org.objectweb.asm.ClassWriter in project whole by wholeplatform.

the class BytecodeGeneratorOperation method getClassVisitor.

public ClassVisitor getClassVisitor() {
    if (classVisitor == null) {
        classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        // FIXME print nothing
        // ClassVisitor cc = new CheckClassAdapter(classWriter);
        // ClassVisitor tv = new TraceClassVisitor(cc, new PrintWriter(System.out));
        // classVisitor = tv;
        classVisitor = classWriter;
        classVisitor.visit(V1_4, ACC_PUBLIC, "Example", null, "java/lang/Object", null);
        // default constructor
        MethodVisitor mv = classVisitor.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();
    }
    return classVisitor;
}
Also used : ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 84 with ClassWriter

use of org.objectweb.asm.ClassWriter in project ecf by eclipse.

the class ProxyGenerator method generateProxyClass.

/**
 * @param interfaceName
 *            interface name
 * @param interfaceClass
 *            interface class
 * @return bytes of the generated proxy class
 * @throws IOException
 *             in case of generation error
 */
private byte[] generateProxyClass(final String[] interfaceNames, final byte[] interfaceClass) throws IOException {
    serviceInterfaceNames = interfaceNames;
    implName = // $NON-NLS-1$ //$NON-NLS-2$
    "proxy/" + sourceID + "/" + interfaceNames[0].replace('.', '/') + // $NON-NLS-1$
    "Impl";
    final ClassReader reader = new ClassReader(interfaceClass);
    writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    reader.accept(this, ClassReader.SKIP_DEBUG);
    visitedInterfaces.add(interfaceNames[0].replace('.', '/'));
    recurseInterfaceHierarchy();
    serviceInterfaceNames = null;
    final byte[] bytes = writer.toByteArray();
    return bytes;
}
Also used : ClassReader(org.objectweb.asm.ClassReader) ClassWriter(org.objectweb.asm.ClassWriter)

Example 85 with ClassWriter

use of org.objectweb.asm.ClassWriter in project ecf by eclipse.

the class ProxyGenerator method generateProxyBundle.

/**
 * @param service
 *            ServiceURL
 * @param deliv
 *            DeliverServiceMessage
 * @return bundle location
 * @throws IOException
 *             in case of proxy generation error
 */
protected InputStream generateProxyBundle(final URI service, final DeliverServiceMessage deliv) throws IOException {
    uri = service.toString();
    sourceID = generateSourceID(uri);
    implemented = new HashSet();
    injections = deliv.getInjections();
    final byte[] bytes = deliv.getSmartProxyName() == null ? generateProxyClass(deliv.getInterfaceNames(), deliv.getInterfaceClass()) : generateProxyClass(deliv.getInterfaceNames(), deliv.getInterfaceClass(), deliv.getSmartProxyName(), deliv.getProxyClass());
    final String className = implName.replace('/', '.');
    JarEntry jarEntry;
    // generate Jar
    final Manifest mf = new Manifest();
    final Attributes attr = mf.getMainAttributes();
    // $NON-NLS-1$ //$NON-NLS-2$
    attr.putValue("Manifest-Version", "1.0");
    // $NON-NLS-1$ //$NON-NLS-2$
    attr.putValue("Created-By", "R-OSGi Proxy Generator");
    // $NON-NLS-1$
    attr.putValue("Bundle-Activator", className);
    // $NON-NLS-1$ //$NON-NLS-2$
    attr.putValue("Bundle-Classpath", ".");
    attr.putValue("Bundle-SymbolicName", // $NON-NLS-1$ //$NON-NLS-2$
    RemoteOSGiService.R_OSGi_PROXY_PREFIX + service.getHost() + "." + service.getPort() + "." + service.getFragment());
    attr.putValue(// $NON-NLS-1$
    "Import-Package", // $NON-NLS-1$
    "org.osgi.framework, ch.ethz.iks.r_osgi, ch.ethz.iks.r_osgi.types, ch.ethz.iks.r_osgi.channels" + ("".equals(deliv.getOptionalImports()) ? "" : // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    ", " + deliv.getOptionalImports()) + // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    ("".equals(deliv.getImports()) ? "" : ", ") + deliv.getImports());
    if (!"".equals(deliv.getExports())) {
        // $NON-NLS-1$
        // $NON-NLS-1$
        attr.putValue("Export-Package", deliv.getExports());
    }
    final ByteArrayOutputStream bout = new ByteArrayOutputStream();
    final JarOutputStream out = new JarOutputStream(bout, mf);
    CRC32 crc = new CRC32();
    crc.update(bytes, 0, bytes.length);
    // $NON-NLS-1$
    jarEntry = new JarEntry(implName + ".class");
    jarEntry.setSize(bytes.length);
    jarEntry.setCrc(crc.getValue());
    out.putNextEntry(jarEntry);
    out.write(bytes, 0, bytes.length);
    out.flush();
    out.closeEntry();
    final String[] injectionNames = (String[]) injections.keySet().toArray(new String[injections.size()]);
    // write the class injections
    for (int i = 0; i < injectionNames.length; i++) {
        String name = injectionNames[i];
        // the original smart proxy class is omitted
        if (name.equals(smartProxyClassNameDashed + ".class")) {
            // $NON-NLS-1$
            continue;
        }
        final byte[] data = (byte[]) injections.get(name);
        final byte[] rewritten;
        // point to the generated proxy class
        if (smartProxyClassNameDashed != null && name.startsWith(smartProxyClassNameDashed)) {
            final String rest = name.substring(smartProxyClassNameDashed.length());
            name = implName + rest;
            final ClassReader rewriterReader = new ClassReader(data);
            final ClassWriter rewriterWriter = new ClassWriter(0);
            rewriterReader.accept(new ClassRewriter(rewriterWriter), ClassReader.SKIP_DEBUG);
            rewritten = rewriterWriter.toByteArray();
        } else {
            rewritten = data;
        }
        crc = new CRC32();
        crc.update(rewritten, 0, rewritten.length);
        jarEntry = new JarEntry(name);
        jarEntry.setSize(rewritten.length);
        jarEntry.setCrc(crc.getValue());
        out.putNextEntry(jarEntry);
        out.write(rewritten, 0, rewritten.length);
        out.flush();
        out.closeEntry();
    }
    out.flush();
    out.finish();
    out.close();
    if (RemoteOSGiServiceImpl.PROXY_DEBUG) {
    // final File file =
    // RemoteOSGiActivator.context.getDataFile(fileName
    // + "_" + sourceID + ".jar");
    // RemoteOSGiServiceImpl.log.log(LogService.LOG_DEBUG,
    // "Created Proxy Bundle " + file);
    }
    return new ByteArrayInputStream(bout.toByteArray());
}
Also used : CRC32(java.util.zip.CRC32) Attributes(java.util.jar.Attributes) JarOutputStream(java.util.jar.JarOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JarEntry(java.util.jar.JarEntry) Manifest(java.util.jar.Manifest) ChannelEndpoint(ch.ethz.iks.r_osgi.channels.ChannelEndpoint) ClassWriter(org.objectweb.asm.ClassWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) ClassReader(org.objectweb.asm.ClassReader) HashSet(java.util.HashSet)

Aggregations

ClassWriter (org.objectweb.asm.ClassWriter)502 ClassReader (org.objectweb.asm.ClassReader)285 MethodVisitor (org.objectweb.asm.MethodVisitor)127 Test (org.junit.Test)99 ClassVisitor (org.objectweb.asm.ClassVisitor)88 ClassNode (org.objectweb.asm.tree.ClassNode)70 IOException (java.io.IOException)62 Label (org.objectweb.asm.Label)55 SemanticVersioningClassVisitor (org.apache.aries.versioning.utils.SemanticVersioningClassVisitor)52 HashSet (java.util.HashSet)37 Method (java.lang.reflect.Method)36 Type (org.objectweb.asm.Type)33 BinaryCompatibilityStatus (org.apache.aries.versioning.utils.BinaryCompatibilityStatus)32 File (java.io.File)30 InvocationTargetException (java.lang.reflect.InvocationTargetException)28 FieldVisitor (org.objectweb.asm.FieldVisitor)28 FileOutputStream (java.io.FileOutputStream)27 InputStream (java.io.InputStream)26 MethodNode (org.objectweb.asm.tree.MethodNode)25 OuterClass (com.android.tools.layoutlib.create.dataclass.OuterClass)23