Example usage for org.objectweb.asm Opcodes ASM5

List of usage examples for org.objectweb.asm Opcodes ASM5

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes ASM5.

Prototype

int ASM5

To view the source code for org.objectweb.asm Opcodes ASM5.

Click Source Link

Usage

From source file:com.github.megatronking.stringfog.plugin.StringFogClassVisitor.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
    if (mv != null && !mIgnoreClass) {
        if ("<clinit>".equals(name)) {
            isClInitExists = true;//from w ww. ja va 2  s  . c om
            // If clinit exists meaning the static fields (not final) would have be inited here.
            mv = new MethodVisitor(Opcodes.ASM5, mv) {

                private String lastStashCst;

                @Override
                public void visitCode() {
                    super.visitCode();
                    // Here init static final fields.
                    for (ClassStringField field : mStaticFinalFields) {
                        if (!canEncrypted(field.value)) {
                            continue;
                        }
                        String originValue = field.value;
                        String encryptValue = mStringFogImpl.encrypt(originValue, mKey);
                        mMappingPrinter.output(getJavaClassName(), originValue, encryptValue);
                        super.visitLdcInsn(encryptValue);
                        super.visitMethodInsn(Opcodes.INVOKESTATIC, mFogClassName, "decrypt",
                                "(Ljava/lang/String;)Ljava/lang/String;", false);
                        super.visitFieldInsn(Opcodes.PUTSTATIC, mClassName, field.name,
                                ClassStringField.STRING_DESC);
                    }
                }

                @Override
                public void visitLdcInsn(Object cst) {
                    // Here init static or static final fields, but we must check field name int 'visitFieldInsn'
                    if (cst != null && cst instanceof String && canEncrypted((String) cst)) {
                        lastStashCst = (String) cst;
                        String originValue = lastStashCst;
                        String encryptValue = mStringFogImpl.encrypt(originValue, mKey);
                        mMappingPrinter.output(getJavaClassName(), originValue, encryptValue);
                        super.visitLdcInsn(encryptValue);
                        super.visitMethodInsn(Opcodes.INVOKESTATIC, mFogClassName, "decrypt",
                                "(Ljava/lang/String;)Ljava/lang/String;", false);
                    } else {
                        lastStashCst = null;
                        super.visitLdcInsn(cst);
                    }
                }

                @Override
                public void visitFieldInsn(int opcode, String owner, String name, String desc) {
                    if (mClassName.equals(owner) && lastStashCst != null) {
                        boolean isContain = false;
                        for (ClassStringField field : mStaticFields) {
                            if (field.name.equals(name)) {
                                isContain = true;
                                break;
                            }
                        }
                        if (!isContain) {
                            for (ClassStringField field : mStaticFinalFields) {
                                if (field.name.equals(name) && field.value == null) {
                                    field.value = lastStashCst;
                                    break;
                                }
                            }
                        }
                    }
                    lastStashCst = null;
                    super.visitFieldInsn(opcode, owner, name, desc);
                }
            };

        } else if ("<init>".equals(name)) {
            // Here init final(not static) and normal fields
            mv = new MethodVisitor(Opcodes.ASM5, mv) {
                @Override
                public void visitLdcInsn(Object cst) {
                    // We don't care about whether the field is final or normal
                    if (cst != null && cst instanceof String && canEncrypted((String) cst)) {
                        String originValue = (String) cst;
                        String encryptValue = mStringFogImpl.encrypt(originValue, mKey);
                        mMappingPrinter.output(getJavaClassName(), originValue, encryptValue);
                        super.visitLdcInsn(encryptValue);
                        super.visitMethodInsn(Opcodes.INVOKESTATIC, mFogClassName, "decrypt",
                                "(Ljava/lang/String;)Ljava/lang/String;", false);
                    } else {
                        super.visitLdcInsn(cst);
                    }
                }
            };
        } else {
            mv = new MethodVisitor(Opcodes.ASM5, mv) {

                @Override
                public void visitLdcInsn(Object cst) {
                    if (cst != null && cst instanceof String && canEncrypted((String) cst)) {
                        // If the value is a static final field
                        for (ClassStringField field : mStaticFinalFields) {
                            if (cst.equals(field.value)) {
                                super.visitFieldInsn(Opcodes.GETSTATIC, mClassName, field.name,
                                        ClassStringField.STRING_DESC);
                                return;
                            }
                        }
                        // If the value is a final field (not static)
                        for (ClassStringField field : mFinalFields) {
                            // if the value of a final field is null, we ignore it
                            if (cst.equals(field.value)) {
                                super.visitVarInsn(Opcodes.ALOAD, 0);
                                super.visitFieldInsn(Opcodes.GETFIELD, mClassName, field.name,
                                        "Ljava/lang/String;");
                                return;
                            }
                        }
                        // local variables
                        String originValue = (String) cst;
                        String encryptValue = mStringFogImpl.encrypt(originValue, mKey);
                        mMappingPrinter.output(getJavaClassName(), originValue, encryptValue);
                        super.visitLdcInsn(encryptValue);
                        super.visitMethodInsn(Opcodes.INVOKESTATIC, mFogClassName, "decrypt",
                                "(Ljava/lang/String;)Ljava/lang/String;", false);
                        return;
                    }
                    super.visitLdcInsn(cst);
                }

            };
        }
    }
    return mv;
}

From source file:com.github.projectsandstone.asmmeta.asm.ASMMetaClassVisitor.java

License:Open Source License

@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {

    FieldElement fieldElement = new FieldElement(this.current, access, name, desc, new MetaData(),
            this.resolvers.getFieldResolver());

    this.addToEnclosing(fieldElement);

    return new FieldVisitor(Opcodes.ASM5, super.visitField(access, name, desc, signature, value)) {
        @Override// w w w  . j a v  a  2 s .  com
        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
            AnnotationElement annotationElement = createAnnotationElement(fieldElement, desc);

            fieldElement.getMetaData().getAnnotationList().add(annotationElement);

            return new ASMMetaAnnotationVisitor(this.api, super.visitAnnotation(desc, visible),
                    annotationElement, annotationElement.getValues());
        }

        @Override
        public void visitEnd() {
            fieldElement.immutate();
            super.visitEnd();
        }
    };
}

From source file:com.github.projectsandstone.asmmeta.asm.ASMMetaClassVisitor.java

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    MetaElement<?> mElement;//  w  w  w  . j a va  2  s.co  m

    if (name.equals("<init>") || name.equals("<clinit>")) {
        mElement = new ConstructorElement(this.current, access, name, signature, new MetaData(),
                this.resolvers.getConstructorResolver());
    } else {
        mElement = new MethodElement(this.current, access, name, signature, new MetaData(),
                this.resolvers.getMethodResolver());
    }

    this.addToEnclosing(mElement);

    return new MethodVisitor(Opcodes.ASM5, super.visitMethod(access, name, desc, signature, exceptions)) {
        @Override
        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
            AnnotationElement annotationElement = createAnnotationElement(mElement, desc);

            mElement.getMetaData().getAnnotationList().add(annotationElement);

            return new ASMMetaAnnotationVisitor(this.api, super.visitAnnotation(desc, visible),
                    annotationElement, annotationElement.getValues());
        }

        @Override
        public void visitEnd() {
            mElement.immutate();
            super.visitEnd();
        }
    };
}

From source file:com.github.projectsandstone.asmmeta.ASMMeta.java

License:Open Source License

public IClassElement read(byte[] bytes) {
    ASMMetaClassVisitor asmMetaClassVisitor = VisitorFactory.createVisitor(Opcodes.ASM5);

    ClassReader cr = new ClassReader(bytes);

    cr.accept(asmMetaClassVisitor, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);

    MetaElement<?> current = asmMetaClassVisitor.getCurrent();

    if (!(current instanceof IClassElement)) {
        throw new IllegalStateException("Byte array is not a class!");
    }//from  w w  w.  ja  v  a2  s. co  m

    return (IClassElement) current;
}

From source file:com.github.trohovsky.just.visitor.AnnotationDependenciesVisitor.java

License:Apache License

public AnnotationDependenciesVisitor(final Dependencies dependencies) {
    super(Opcodes.ASM5);
    this.dependencies = dependencies;
}

From source file:com.github.trohovsky.just.visitor.ClassDependenciesVisitor.java

License:Apache License

public ClassDependenciesVisitor(final Dependencies dependencies, final AnnotationVisitor annotationVisitor,
        final SignatureVisitor signatureVisitor, final FieldVisitor fieldVisitor,
        final MethodVisitor methodVisitor) {
    super(Opcodes.ASM5);
    this.dependencies = dependencies;
    this.annotationVisitor = annotationVisitor;
    this.signatureVisitor = signatureVisitor;
    this.fieldVisitor = fieldVisitor;
    this.methodVisitor = methodVisitor;
}

From source file:com.github.trohovsky.just.visitor.FieldDependenciesVisitor.java

License:Apache License

public FieldDependenciesVisitor(final Dependencies dependencies, final AnnotationVisitor annotationVisitor) {
    super(Opcodes.ASM5);
    this.dependencies = dependencies;
    this.annotationVisitor = annotationVisitor;
}

From source file:com.github.trohovsky.just.visitor.MethodDependenciesVisitor.java

License:Apache License

public MethodDependenciesVisitor(final Dependencies dependencies, final AnnotationVisitor annotationVisitor,
        SignatureVisitor signatureVisitor) {
    super(Opcodes.ASM5);
    this.dependencies = dependencies;
    this.annotationVisitor = annotationVisitor;
    this.signatureVisitor = signatureVisitor;
}

From source file:com.github.trohovsky.just.visitor.SignatureDependenciesVisitor.java

License:Apache License

public SignatureDependenciesVisitor(final Dependencies dependencies) {
    super(Opcodes.ASM5);
    this.dependencies = dependencies;
}

From source file:com.github.veithen.phos.enforcer.ClassProcessor.java

License:Apache License

ClassProcessor(ReferenceCollector referenceCollector) {
    super(Opcodes.ASM5);
    this.referenceCollector = referenceCollector;
}