Example usage for org.objectweb.asm.tree FieldNode accept

List of usage examples for org.objectweb.asm.tree FieldNode accept

Introduction

In this page you can find the example usage for org.objectweb.asm.tree FieldNode accept.

Prototype

public void accept(final ClassVisitor classVisitor) 

Source Link

Document

Makes the given class visitor visit this field.

Usage

From source file:blue.origami.asm.OClassWriter.java

License:Apache License

public final void addField(OAnno anno, String name, OType ty, String signature, Object value) {
    // String signature = ty.desc();
    FieldNode fn = new FieldNode(anno.acc(), name, ty.typeDesc(0), signature, value);
    if (ty.isNullable()) {
        anno.setAnnotation(ONullable.class);
    }//from w w w. j ava  2  s .  c o  m
    if (ty.isMutable()) {
        anno.setAnnotation(OMutable.class);
    }
    anno.asm(fn);
    fn.accept(this);
}

From source file:com.alibaba.hotswap.processor.field.holder.FieldAheadVisitor.java

License:Open Source License

@Override
public void visit(int version, int access, String name, String signature, String superName,
        String[] interfaces) {/*from  www. j a v a 2 s .c  om*/
    super.visit(version, access, name, signature, superName, interfaces);

    classMeta = HotswapRuntime.getClassMeta(className);

    if (!classMeta.isLoaded()) {
        // First load
        for (String key : classMeta.primaryFieldKeyList) {
            classMeta.primaryFieldNodes.get(key).accept(cv);
        }
    } else {
        // Reload
        Map<String, FieldNode> loadedFieldNodes = new HashMap<String, FieldNode>();
        loadedFieldNodes.putAll(classMeta.loadedFieldNodes);

        // 1. Visit the primary fields.
        for (String key : classMeta.primaryFieldKeyList) {
            FieldNode primaryFN = classMeta.primaryFieldNodes.get(key);
            FieldNode loadedFN = loadedFieldNodes.get(key);
            if (loadedFN != null) {
                if (loadedFN.access == primaryFN.access) {
                    // Primary field(may change annotation/signature) or change from other field
                    loadedFN.accept(cv);
                    loadedFieldNodes.remove(key);
                    // update loadedIndex
                    classMeta.putFieldMeta(classMeta.getFieldMeta(key));
                } else {
                    primaryFN.accept(cv);
                }
            } else {
                // This primary field is removed, so do not change loadedIndex
                primaryFN.accept(cv);
            }
        }

        // 2. Add and remove modified field.
        for (FieldNode fn : loadedFieldNodes.values()) {
            // All these fields are the reloaded class's fields

            String fieldKey = HotswapFieldUtil.getFieldKey(fn.name, fn.desc);
            FieldMeta fm2 = classMeta.getFieldMeta(fieldKey);

            if (fm2 == null) {
                // This is a new field
                classMeta.addFieldMeta(fn.access, fn.name, fn.desc, fn.signature, fn.value);
            } else {
                if (classMeta.primaryFieldKeyList.contains(fieldKey)) {
                    // It's a primary field
                    if (fn.access == fm2.access) {
                        // An exist field
                        classMeta.putFieldMeta(fn.access, fn.name, fn.desc, fn.signature, fn.value);
                    } else {
                        // Modified field, alias it
                        fn.name = HotswapConstants.PREFIX_FIELD_ALIAS + fn.name;
                        classMeta.addFieldMeta(fn.access, fn.name, fn.desc, fn.signature, fn.value);
                    }
                } else {
                    classMeta.putFieldMeta(fn.access, fn.name, fn.desc, fn.signature, fn.value);
                }
            }
        }
    }
}

From source file:com.alibaba.hotswap.processor.v.VClassGenerateVisitor.java

License:Open Source License

@Override
public void visit(int version, int access, String name, String signature, String superName,
        String[] interfaces) {/* w  w w. j a v  a2  s. c om*/
    HotswapThreadLocalUtil.setClassName(name);
    super.visit(version, access, name, signature, superName, interfaces);
    boolean isInterface = ((access & Opcodes.ACC_INTERFACE) == Opcodes.ACC_INTERFACE);

    if (isInterface && (access & Opcodes.ACC_ABSTRACT) == Opcodes.ACC_ABSTRACT) {
        // If it is a interface, then transformer it to class
        access = access - Opcodes.ACC_INTERFACE;
    }

    ClassMeta classMeta = HotswapRuntime.getClassMeta(className);
    classMeta.isInterface = isInterface;
    if (!classMeta.isLoaded()) {
        // First load
        for (String key : classMeta.primaryFieldKeyList) {
            classMeta.primaryFieldNodes.get(key).accept(cv);
        }
    } else {
        // Reload
        Map<String, FieldNode> loadedFieldNodes = new HashMap<String, FieldNode>();
        loadedFieldNodes.putAll(classMeta.loadedFieldNodes);

        // 1. Visit the primary fields.
        for (String key : classMeta.primaryFieldKeyList) {
            FieldNode primaryFN = classMeta.primaryFieldNodes.get(key);
            FieldNode loadedFN = loadedFieldNodes.get(key);
            if (loadedFN != null) {
                if (loadedFN.access == primaryFN.access) {
                    // Primary field(may change annotation/signature) or change from other field
                    loadedFN.accept(cv);
                    loadedFieldNodes.remove(key);
                } else {
                    primaryFN.accept(cv);
                }
            } else {
                primaryFN.accept(cv);
            }
        }

        // 2. Add and remove modified field.
        for (FieldNode fn : loadedFieldNodes.values()) {
            // All these fields are the reloaded class's fields

            String fieldKey = HotswapFieldUtil.getFieldKey(fn.name, fn.desc);
            FieldMeta fm2 = classMeta.getFieldMeta(fieldKey);

            if (fm2 == null) {
                // This is a new field
                fn.accept(cv);
            } else {
                if (classMeta.primaryFieldKeyList.contains(fieldKey)) {
                    // It's a primary field
                    if (fn.access == fm2.access) {

                    } else {
                        // Modified field, alias it
                        fn.name = HotswapConstants.PREFIX_FIELD_ALIAS + fn.name;
                        fn.accept(cv);
                    }
                } else {
                    fn.accept(cv);
                }
            }
        }
    }
}

From source file:com.github.veithen.cosmos.osgi.runtime.MemberInjector.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if (!fieldsInjected) {
        for (FieldNode field : classNode.fields) {
            field.accept(cv);
        }//from w w w . j  a va 2 s. com
        fieldsInjected = true;
    }
    for (MethodNode method : classNode.methods) {
        if (name.equals(method.name) && desc.equals(method.desc)) {
            return null;
        }
    }
    return super.visitMethod(access, name, desc, signature, exceptions);
}

From source file:com.googlecode.ddom.weaver.mixin.MergeAdapter.java

License:Apache License

@Override
public void visitEnd() {
    for (MixinInfo mixin : mixins) {
        for (FieldNode field : mixin.getFields()) {
            if (log.isTraceEnabled()) {
                log.trace("Merging field " + field.name + " from mixin " + mixin.getName());
            }/*from   w ww .j a v  a2 s .c  o  m*/
            if (!seenFields.add(field.name)) {
                errorHandler.handleError("Duplicate field " + field.name);
            }
            field.accept(this);
        }
        MethodNode initMethod = mixin.getInitMethod();
        if (initMethod != null) {
            if (log.isTraceEnabled()) {
                log.trace("Merging constructor code from mixin " + mixin.getName());
            }
            mergeMixinMethod(mixin, mixin.getInitMethod());
        }
        for (MethodNode mn : mixin.getMethods()) {
            if (log.isTraceEnabled()) {
                log.trace("Merging method " + mn.name + mn.desc + " from mixin " + mixin.getName());
            }
            if (!seenMethods.add(mn.name + mn.desc)) {
                errorHandler.handleError("Method " + mn.name + mn.desc + " of mixin " + mixin.getName()
                        + " collides with a method declared in the base class or another mixin");
            }
            mergeMixinMethod(mixin, mn);
        }
    }
    super.visitEnd();
}

From source file:org.anon.smart.base.stt.asm.ASMSTTWriter.java

License:Open Source License

public Object createField(STTVisitor sttvisit, STTDescriptor stt, Object field) {
    ASMClazzContext cctx = (ASMClazzContext) sttvisit.clazzContext();
    ClassVisitor visit = cctx.visitor();
    FieldNode sfn = (FieldNode) field;/*ww w  .  ja  v a  2s.  c  o m*/
    FieldNode fn = new FieldNode(sfn.access, sfn.name, sfn.desc, sfn.signature, sfn.value);
    List<AnnotationNode> annons = sfn.visibleAnnotations;
    if (annons != null) {
        for (AnnotationNode annon : annons)
            fn.visitAnnotation(annon.desc, true);
    }

    sttvisit.addFieldAnnotations(this, stt, sfn, sfn.name);
    fn.accept(visit);
    return fn;
}

From source file:org.glowroot.agent.weaving.WeavingClassVisitor.java

License:Apache License

@RequiresNonNull("type")
private void addMixin(MixinType mixinType) {
    ClassReader cr = new ClassReader(mixinType.implementationBytes());
    ClassNode cn = new ClassNode();
    cr.accept(cn, ClassReader.EXPAND_FRAMES);
    // SuppressWarnings because generics are explicitly removed from asm binaries
    // see http://forge.ow2.org/tracker/?group_id=23&atid=100023&func=detail&aid=316377
    @SuppressWarnings("unchecked")
    List<FieldNode> fieldNodes = cn.fields;
    for (FieldNode fieldNode : fieldNodes) {
        fieldNode.accept(this);
    }/*  w  w  w  .jav a 2  s  . c  om*/
    // SuppressWarnings because generics are explicitly removed from asm binaries
    @SuppressWarnings("unchecked")
    List<MethodNode> methodNodes = cn.methods;
    for (MethodNode mn : methodNodes) {
        if (mn.name.equals("<init>")) {
            continue;
        }
        // SuppressWarnings because generics are explicitly removed from asm binaries
        @SuppressWarnings("unchecked")
        String[] exceptions = Iterables.toArray(mn.exceptions, String.class);
        MethodVisitor mv = cw.visitMethod(mn.access, mn.name, mn.desc, mn.signature, exceptions);
        mn.accept(new RemappingMethodAdapter(mn.access, mn.desc, mv,
                new SimpleRemapper(cn.name, type.getInternalName())));
    }
}

From source file:org.glowroot.weaving.WeavingClassVisitor.java

License:Apache License

@RequiresNonNull("type")
private void addMixin(MixinType mixinType) {
    ClassReader cr = new ClassReader(mixinType.implementationBytes());
    ClassNode cn = new ClassNode();
    cr.accept(cn, ClassReader.EXPAND_FRAMES);
    // SuppressWarnings because generics are explicitly removed from asm binaries
    // see http://forge.ow2.org/tracker/?group_id=23&atid=100023&func=detail&aid=316377
    @SuppressWarnings("unchecked")
    List<FieldNode> fieldNodes = cn.fields;
    for (FieldNode fieldNode : fieldNodes) {
        fieldNode.accept(this);
    }/*from  www .  j a v a2 s. c o m*/
    // SuppressWarnings because generics are explicitly removed from asm binaries
    @SuppressWarnings("unchecked")
    List<MethodNode> methodNodes = cn.methods;
    for (MethodNode mn : methodNodes) {
        if (mn.name.equals("<init>")) {
            continue;
        }
        // SuppressWarnings because generics are explicitly removed from asm binaries
        @SuppressWarnings("unchecked")
        String[] exceptions = Iterables.toArray(mn.exceptions, String.class);
        MethodVisitor mv = cv.visitMethod(mn.access, mn.name, mn.desc, mn.signature, exceptions);
        checkNotNull(mv);
        mn.accept(new RemappingMethodAdapter(mn.access, mn.desc, mv,
                new SimpleRemapper(cn.name, type.getInternalName())));
    }
}

From source file:org.summer.aop.ltw.AspectWeaver.java

License:Open Source License

@Override
public void visitEnd() {
    isVisitEndReached = true;// w w w. java  2 s  .  com
    // If this class should be merged...
    if (mergingClassNode != null) {
        // Add all remaining non matching merging fields
        for (Object fieldNode : mergingClassNode.fields) {
            FieldNode fn = (FieldNode) fieldNode;
            if (!visitedMergingClassFields.contains(fn.name))
                fn.accept(cv);
        }
        // Add all remaining non matching merging methods
        for (Object methodNode : mergingClassNode.methods) {
            shouldInsertRedirectionCondition = false;
            MethodNode mn = (MethodNode) methodNode;
            if (!visitedMergingClassMethods.contains(mn.name + mn.desc))
                mn.accept(this);
        }
    } else if (replacingClassNode != null) // If this class should be replaced...
    {
        // Add all replacing fields
        for (Object fieldNode : replacingClassNode.fields) {
            ((FieldNode) fieldNode).accept(cv);
        }
        // Add all remaining replacing non matching replacing methods
        for (Object methodNode : replacingClassNode.methods) {
            MethodNode mn = (MethodNode) methodNode;
            if (!mn.name.equals("<clinit>"))
                mn.accept(this);
        }
    }
    MethodVisitor mv;
    // Split the advised methods
    for (AspectWeaver.MethodInfo info : interceptedMethodInfos) {
        if (!info.methodName.equals("<clinit>")) {
            int privateAccess = toPrivateAccess(info.access);
            // For non constructor methods replace the original method body
            if (!info.methodName.equals("<init>")) {
                mv = cv.visitMethod(info.access, info.methodName, info.desc, info.signature, info.exceptions);
                mv = new MethodReplacer(className, mv, privateAccess, info.methodName, info.desc,
                        info.signature, info.exceptions, info.matchingAspects);
                mv.visitCode();
                mv.visitMaxs(-1, -1);
                mv.visitEnd();
            }
            // Add a corresponding proceeding method
            mv = cv.visitMethod(privateAccess,
                    AOPContext.toProceedingMethodName(info.methodName, info.constructorNumber), info.desc,
                    info.signature, info.exceptions);
            mv = new ProceedingMethodGenerator(className, mv, privateAccess, info.methodName,
                    info.constructorNumber, info.desc, info.signature, info.exceptions, info.matchingAspects);
            mv.visitCode();
            mv.visitMaxs(-1, -1);
            mv.visitEnd();
        }
    }
    // Add the STATIC_ASPECTS_FIELD
    cv.visitField(ACC_PRIVATE + ACC_STATIC, AOPContext.STATIC_ASPECTS_FIELD, "Ljava/util/Map;",
            "Ljava/util/Map<Ljava/lang/String;Ljava/util/List<Ljava/lang/String;>;>;", null).visitEnd();
    if (mergingClassNode != null) {
        // Add the STATIC_IS_MERGING_REVERTED_FIELD
        cv.visitField(ACC_PRIVATE + ACC_STATIC, AOPContext.STATIC_IS_MERGING_REVERTED_FIELD, "Z", null, null)
                .visitEnd();
    }
    // Declare the static aspects field within the class' static block if not done yet
    if (!isStaticBlockVisited) {
        mv = cv.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
        if (!tryToAppendReplacingClassStaticBlock(mv)) {
            mv.visitCode();
            initializeStaticAspectsField(mv);
            int maxStack = 2;
            if (mergingClassNode != null) {
                initializeStaticIsMergingActiveField(mv);
                maxStack++;
            }
            mv.visitInsn(RETURN);
            mv.visitMaxs(maxStack, 0);
            mv.visitEnd();
        }
        isStaticBlockVisited = true;
    }
    cv.visitInnerClass(anonymousInnerClassName, null, null, 0);
    cv.visitEnd();
    // Instantiate the static aspects field via a dynamic anonymous inner class and populate it with
    // all advised methods and their matching aspects
    ClassWriter cw = new ClassWriter(0);
    //    cv = new org.objectweb.asm.util.CheckClassAdapter(cw); // ASM Tree API (asm-tree.jar) is needed for this
    cw.visit(V1_5, ACC_PUBLIC, anonymousInnerClassName,
            "Ljava/util/HashMap<Ljava/lang/String;Ljava/util/List<Ljava/lang/String;>;>;", "java/util/HashMap",
            null);
    cw.visitOuterClass(className, null, null);
    cw.visitInnerClass(anonymousInnerClassName, null, null, 0);
    mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/util/HashMap", "<init>", "()V");
    for (AspectWeaver.MethodInfo info : interceptedMethodInfos) {
        mv.visitTypeInsn(NEW, "java/util/LinkedList");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "java/util/LinkedList", "<init>", "()V");
        mv.visitVarInsn(ASTORE, 1);
        for (Aspect aspect : info.matchingAspects) {
            mv.visitVarInsn(ALOAD, 1);
            mv.visitLdcInsn(aspect.getId());
            mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z");
            mv.visitInsn(POP);
        }
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(info.methodName + info.desc);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEVIRTUAL, anonymousInnerClassName, "put",
                "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
        mv.visitInsn(POP);
    }
    mv.visitInsn(RETURN);
    mv.visitMaxs(3, 2);
    mv.visitEnd();
    // Finish up class writer
    cw.visitEnd();
    // Register the dynamic anonymous inner class within the top level class loader for further class resolving issues
    TopLevelClassLoader.registerAnonymousInnerClass(className, anonymousInnerClassCounter, cw.toByteArray());
}