Search in sources :

Example 76 with ClassWriter

use of org.objectweb.asm.ClassWriter in project MineCamera by PorPit.

the class CamTransformer method transform.

public byte[] transform(String name, byte[] basicClass) {
    int i = 0;
    while (i < transformers.size()) {
        if (transformers.get(i).is(name)) {
            ClassNode classNode = new ClassNode();
            ClassReader classReader = new ClassReader(basicClass);
            classReader.accept(classNode, 0);
            transformers.get(i).transform(classNode);
            ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
            classNode.accept(writer);
            basicClass = writer.toByteArray();
            System.out.println("[" + MineCamera.MODID + "] Patched " + transformers.get(i).className + " ...");
            transformers.get(i).done();
            i++;
        // transformers.remove(i);
        } else
            i++;
    }
    return basicClass;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ClassReader(org.objectweb.asm.ClassReader) ClassWriter(org.objectweb.asm.ClassWriter)

Example 77 with ClassWriter

use of org.objectweb.asm.ClassWriter in project teiid by teiid.

the class RestASMBasedWebArchiveBuilder method getApplicationClass.

public byte[] getApplicationClass(ArrayList<String> models) {
    ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;
    MethodVisitor mv;
    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, "org/teiid/jboss/rest/TeiidRestApplication", null, "javax/ws/rs/core/Application", null);
    {
        fv = cw.visitField(ACC_PRIVATE, "singletons", "Ljava/util/Set;", "Ljava/util/Set<Ljava/lang/Object;>;", null);
        fv.visitEnd();
    }
    {
        fv = cw.visitField(ACC_PRIVATE, "empty", "Ljava/util/Set;", "Ljava/util/Set<Ljava/lang/Class<*>;>;", null);
        fv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "javax/ws/rs/core/Application", "<init>", "()V");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(NEW, "java/util/HashSet");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "java/util/HashSet", "<init>", "()V");
        mv.visitFieldInsn(PUTFIELD, "org/teiid/jboss/rest/TeiidRestApplication", "singletons", "Ljava/util/Set;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(NEW, "java/util/HashSet");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "java/util/HashSet", "<init>", "()V");
        mv.visitFieldInsn(PUTFIELD, "org/teiid/jboss/rest/TeiidRestApplication", "empty", "Ljava/util/Set;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, "org/teiid/jboss/rest/TeiidRestApplication", "singletons", "Ljava/util/Set;");
        mv.visitTypeInsn(NEW, "org/teiid/jboss/rest/CustomApiListingResource");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "org/teiid/jboss/rest/CustomApiListingResource", "<init>", "()V");
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Set", "add", "(Ljava/lang/Object;)Z");
        mv.visitInsn(POP);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, "org/teiid/jboss/rest/TeiidRestApplication", "singletons", "Ljava/util/Set;");
        mv.visitTypeInsn(NEW, "io/swagger/jaxrs/listing/SwaggerSerializers");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "io/swagger/jaxrs/listing/SwaggerSerializers", "<init>", "()V");
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Set", "add", "(Ljava/lang/Object;)Z");
        mv.visitInsn(POP);
        mv.visitVarInsn(ALOAD, 0);
        for (int i = 0; i < models.size(); i++) {
            mv.visitFieldInsn(GETFIELD, "org/teiid/jboss/rest/TeiidRestApplication", "singletons", "Ljava/util/Set;");
            mv.visitTypeInsn(NEW, "org/teiid/jboss/rest/" + models.get(i));
            mv.visitInsn(DUP);
            mv.visitMethodInsn(INVOKESPECIAL, "org/teiid/jboss/rest/" + models.get(i), "<init>", "()V");
            mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Set", "add", "(Ljava/lang/Object;)Z");
            mv.visitInsn(POP);
            if (i < models.size() - 1) {
                mv.visitVarInsn(ALOAD, 0);
            }
        }
        mv.visitInsn(RETURN);
        mv.visitMaxs(3, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "getClasses", "()Ljava/util/Set;", "()Ljava/util/Set<Ljava/lang/Class<*>;>;", null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, "org/teiid/jboss/rest/TeiidRestApplication", "empty", "Ljava/util/Set;");
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "getSingletons", "()Ljava/util/Set;", "()Ljava/util/Set<Ljava/lang/Object;>;", null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, "org/teiid/jboss/rest/TeiidRestApplication", "singletons", "Ljava/util/Set;");
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : FieldVisitor(org.objectweb.asm.FieldVisitor) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 78 with ClassWriter

use of org.objectweb.asm.ClassWriter in project teiid by teiid.

the class RestASMBasedWebArchiveBuilder method getViewClass.

protected byte[] getViewClass(String vdbName, String vdbVersion, String modelName, Schema schema, boolean passthroughAuth) {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;
    AnnotationVisitor av0;
    boolean hasValidProcedures = false;
    cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "org/teiid/jboss/rest/" + modelName, null, "org/teiid/jboss/rest/TeiidRSProvider", null);
    {
        av0 = cw.visitAnnotation("Ljavax/ws/rs/Path;", true);
        av0.visit("value", "/" + modelName);
        av0.visitEnd();
    }
    {
        av0 = cw.visitAnnotation("Lio/swagger/annotations/Api;", true);
        av0.visit("value", "/" + modelName);
        av0.visitEnd();
    }
    cw.visitInnerClass("javax/ws/rs/core/Response$Status", "javax/ws/rs/core/Response", "Status", ACC_PUBLIC + ACC_FINAL + ACC_STATIC + ACC_ENUM);
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "org/teiid/jboss/rest/TeiidRSProvider", "<init>", "()V");
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    Collection<Procedure> procedures = schema.getProcedures().values();
    for (Procedure procedure : procedures) {
        String uri = procedure.getProperty(REST_NAMESPACE + "URI", false);
        String method = procedure.getProperty(REST_NAMESPACE + "METHOD", false);
        String contentType = procedure.getProperty(REST_NAMESPACE + "PRODUCES", false);
        String charSet = procedure.getProperty(REST_NAMESPACE + "CHARSET", false);
        if (uri != null && method != null) {
            if (contentType == null) {
                contentType = findContentType(procedure);
            }
            if (contentType != null) {
                contentType = contentType.toLowerCase();
                if (contentType.equals("xml")) {
                    contentType = "application/xml";
                } else if (contentType.equals("json")) {
                    contentType = "application/json";
                } else if (contentType.equals("plain")) {
                    contentType = "text/plain";
                }
                buildRestService(vdbName, vdbVersion, modelName, procedure, method, uri, cw, contentType, charSet, passthroughAuth);
                hasValidProcedures = true;
            }
        }
    }
    buildQueryProcedure(vdbName, vdbVersion, modelName, "xml", cw, passthroughAuth);
    buildQueryProcedure(vdbName, vdbVersion, modelName, "json", cw, passthroughAuth);
    cw.visitEnd();
    if (!hasValidProcedures) {
        return null;
    }
    return cw.toByteArray();
}
Also used : AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) Procedure(org.teiid.metadata.Procedure) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 79 with ClassWriter

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

the class TracingTestRunner method loadClass.

/**
 * {@inheritDoc}
 */
@Override
public Class<?> loadClass(String fullyQualifiedTargetClass) throws ClassNotFoundException {
    if (isSystemClass(fullyQualifiedTargetClass)) {
        logger.info("Not instrumenting class '{}'.", fullyQualifiedTargetClass);
        return super.loadClass(fullyQualifiedTargetClass);
    }
    logger.info("Instrumenting class '{}'.", fullyQualifiedTargetClass);
    String className = fullyQualifiedTargetClass.replace('.', '/');
    InputStream is = java.lang.ClassLoader.getSystemResourceAsStream(className + ".class");
    if (is == null) {
        throw new ClassNotFoundException("Class " + fullyQualifiedTargetClass + "could not be found!");
    }
    ClassReader reader = null;
    try {
        reader = new ClassReader(is);
    } catch (IOException exc) {
        throw new ClassNotFoundException();
    }
    ClassWriter writer = new ClassWriter(reader, ClassWriter.COMPUTE_MAXS);
    ClassVisitor cv = new StatementTracingVisitor(fullyQualifiedTargetClass, writer);
    CheckClassAdapter checkClassAdapter = new CheckClassAdapter(cv);
    reader.accept(checkClassAdapter, ClassReader.SKIP_FRAMES);
    byte[] byteBuffer = writer.toByteArray();
    Class<?> result = defineClass(fullyQualifiedTargetClass, byteBuffer, 0, byteBuffer.length);
    return result;
}
Also used : InputStream(java.io.InputStream) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) ClassReader(org.objectweb.asm.ClassReader) IOException(java.io.IOException) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter)

Example 80 with ClassWriter

use of org.objectweb.asm.ClassWriter in project kalang by kasonyang.

the class Ast2Class method annotation.

protected void annotation(Object obj, AnnotationNode... annotations) {
    for (AnnotationNode an : annotations) {
        AnnotationVisitor av;
        String desc = getTypeDescriptor(Types.getClassType(an.getAnnotationType()));
        // TODO set annotation visible
        boolean isVisible = true;
        if (obj instanceof ClassWriter) {
            av = ((ClassWriter) obj).visitAnnotation(desc, isVisible);
        } else if (obj instanceof MethodVisitor) {
            av = ((MethodVisitor) obj).visitAnnotation(desc, isVisible);
        } else {
            throw Exceptions.unsupportedTypeException(obj);
        }
        for (String v : an.values.keySet()) {
            // TODO handle enum value
            Object javaConst = getJavaConst(an.values.get(v));
            av.visit(v, javaConst);
        }
    }
}
Also used : AnnotationNode(kalang.ast.AnnotationNode) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) VarObject(kalang.ast.VarObject) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor)

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