Search in sources :

Example 1 with ByteCodeVisitor

use of act.util.ByteCodeVisitor in project actframework by actframework.

the class MetricContextScanner method byteCodeVisitor.

@Override
public ByteCodeVisitor byteCodeVisitor() {
    return new ByteCodeVisitor() {

        private String className;

        @Override
        public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
            super.visit(version, access, name, signature, superName, interfaces);
            className = Type.getObjectType(name).getClassName();
        }

        @Override
        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
            AnnotationVisitor av = super.visitAnnotation(desc, visible);
            if (DESC_METRIC_CONTEXT.equals(desc)) {
                return new AnnotationVisitor(ASM5, av) {

                    @Override
                    public void visit(String name, Object value) {
                        super.visit(name, value);
                        if ("value".equals(name)) {
                            repo.registerMetricContext(className, value.toString());
                        }
                    }
                };
            }
            return av;
        }
    };
}
Also used : ByteCodeVisitor(act.util.ByteCodeVisitor) AnnotationVisitor(act.asm.AnnotationVisitor)

Example 2 with ByteCodeVisitor

use of act.util.ByteCodeVisitor in project actframework by actframework.

the class ConfigurationByteCodeScanner method byteCodeVisitor.

@Override
public ByteCodeVisitor byteCodeVisitor() {
    return new ByteCodeVisitor() {

        @Override
        public FieldVisitor visitField(int access, final String name, String desc, String signature, Object value) {
            FieldVisitor fv = super.visitField(access, name, desc, signature, value);
            boolean isStatic = AsmTypes.isStatic(access);
            if (!isStatic) {
                return fv;
            }
            final String fieldName = name;
            return new FieldVisitor(ASM5, fv) {

                @Override
                public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                    AnnotationVisitor av = super.visitAnnotation(desc, visible);
                    if (S.eq(CONF_DESC, desc)) {
                        return new AnnotationVisitor(ASM5, av) {

                            @Override
                            public void visit(String name, Object value) {
                                staticConfigurationFields.put(fieldName, S.string(value));
                                super.visit(name, value);
                            }
                        };
                    }
                    return av;
                }
            };
        }
    };
}
Also used : ByteCodeVisitor(act.util.ByteCodeVisitor) AnnotationVisitor(act.asm.AnnotationVisitor) FieldVisitor(act.asm.FieldVisitor)

Example 3 with ByteCodeVisitor

use of act.util.ByteCodeVisitor in project actframework by actframework.

the class GenieFactoryFinder method byteCodeVisitor.

@Override
public ByteCodeVisitor byteCodeVisitor() {
    return new ByteCodeVisitor() {

        private boolean isPublic;

        @Override
        public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
            super.visit(version, access, name, signature, superName, interfaces);
            isPublic = AsmTypes.isPublic(access);
            isModule = Module.class.getName().equals(Type.getObjectType(superName).getClassName());
        }

        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
            MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
            return !isPublic || isModule || isFactory ? mv : new MethodVisitor(ASM5, mv) {

                @Override
                public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                    Type annoType = Type.getType(desc);
                    if (AsmTypes.PROVIDES.asmType().equals(annoType)) {
                        isFactory = true;
                    }
                    return super.visitAnnotation(desc, visible);
                }
            };
        }
    };
}
Also used : Type(act.asm.Type) ByteCodeVisitor(act.util.ByteCodeVisitor) AnnotationVisitor(act.asm.AnnotationVisitor) MethodVisitor(act.asm.MethodVisitor)

Example 4 with ByteCodeVisitor

use of act.util.ByteCodeVisitor in project actframework by actframework.

the class ServerBootstrapClassLoader method loadActClass.

protected Class<?> loadActClass(String name, boolean resolve, boolean pluginOnly) {
    boolean fromPlugin = false;
    byte[] ba = pluginBC.remove(name);
    if (null == ba) {
        if (!pluginOnly) {
            ba = libBC.remove(name);
        }
    } else {
        fromPlugin = true;
    }
    if (null == ba) {
        ba = tryLoadResource(name);
    }
    if (null == ba) {
        if (pluginOnly) {
            return findLoadedClass(name);
        }
        return null;
    }
    Class<?> c = null;
    if (!name.startsWith(ACT_PKG) || name.startsWith(ASM_PKG)) {
        // skip bytecode enhancement for asm classes or non Act classes
        c = super.defineClass(name, ba, 0, ba.length, DOMAIN);
    }
    if (null == c) {
        $.Var<ClassWriter> cw = $.val(null);
        ByteCodeVisitor enhancer = Act.enhancerManager().generalEnhancer(name, cw);
        if (null == enhancer) {
            c = super.defineClass(name, ba, 0, ba.length, DOMAIN);
        } else {
            ClassWriter w = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
            cw.set(w);
            enhancer.commitDownstream();
            ClassReader r;
            r = new ClassReader(ba);
            try {
                r.accept(enhancer, 0);
                byte[] baNew = w.toByteArray();
                c = super.defineClass(name, baNew, 0, baNew.length, DOMAIN);
            } catch (RuntimeException e) {
                throw e;
            } catch (Error e) {
                throw e;
            } catch (Exception e) {
                throw E.unexpected("Error processing class " + name);
            }
        }
    }
    if (resolve) {
        super.resolveClass(c);
    }
    return c;
}
Also used : ByteCodeVisitor(act.util.ByteCodeVisitor) org.osgl.$(org.osgl.$) ClassReader(act.asm.ClassReader) ClassWriter(act.asm.ClassWriter)

Example 5 with ByteCodeVisitor

use of act.util.ByteCodeVisitor in project actframework by actframework.

the class BootstrapClassLoader method defineClass.

protected Class<?> defineClass(String name, byte[] ba) {
    Class<?> c = null;
    $.Var<ClassWriter> cw = $.val(null);
    ByteCodeVisitor enhancer = enhancerManager.generalEnhancer(name, cw);
    if (null == enhancer) {
        c = defineClassX(name, ba, 0, ba.length, DOMAIN);
    } else {
        Exception exception = null;
        ClassWriter w = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
        cw.set(w);
        enhancer.commitDownstream();
        ClassReader r;
        r = new ClassReader(ba);
        try {
            r.accept(enhancer, 0);
            byte[] baNew = w.toByteArray();
            c = defineClassX(name, baNew, 0, baNew.length, DOMAIN);
        } catch (Error e) {
            throw e;
        } catch (Exception e) {
            exception = e;
        }
        if (null != exception) {
            logger.error(exception, "Error enhancing class %s", name);
            throw E.unexpected(exception);
        }
    }
    return c;
}
Also used : ByteCodeVisitor(act.util.ByteCodeVisitor) org.osgl.$(org.osgl.$) ClassReader(act.asm.ClassReader) ClassWriter(act.asm.ClassWriter) IOException(java.io.IOException)

Aggregations

ByteCodeVisitor (act.util.ByteCodeVisitor)5 AnnotationVisitor (act.asm.AnnotationVisitor)3 ClassReader (act.asm.ClassReader)2 ClassWriter (act.asm.ClassWriter)2 org.osgl.$ (org.osgl.$)2 FieldVisitor (act.asm.FieldVisitor)1 MethodVisitor (act.asm.MethodVisitor)1 Type (act.asm.Type)1 IOException (java.io.IOException)1