List of usage examples for org.objectweb.asm MethodVisitor MethodVisitor
public MethodVisitor(final int api)
From source file:org.coldswap.asm.field.ProtectedStaticFieldReplacer.java
License:Open Source License
/** * Replaces any GETSTATIC/PUTSTATIC call of the field in the old class with the field * introduced in the new class.//from w w w.j a v a2 s.c om * * @param classNode containing the old class. * @param fieldNode containing the old field. */ private void replaceReferences(ClassNode classNode, FieldNode fieldNode) { List<MethodNode> methodNodes = classNode.methods; String contClass = classNode.name.substring(classNode.name.lastIndexOf("/") + 1); final String className = classPackage + TransformerNameGenerator.getProtectedStaticFieldClassName(contClass, fieldNode.name); for (MethodNode method : methodNodes) { InsnList inst = method.instructions; Iterator iter = inst.iterator(); while (iter.hasNext()) { AbstractInsnNode absIns = (AbstractInsnNode) iter.next(); int opcode = absIns.getOpcode(); // check if instruction is GETSTATIC or PUTSTATIC if (opcode == Opcodes.GETSTATIC) { // get type if (absIns.getType() == AbstractInsnNode.FIELD_INSN) { final Boolean[] foundField = { false }; final ClassNode cNode = classNode; final FieldNode fNode = fieldNode; absIns.accept(new MethodVisitor(Opcodes.ASM5) { @Override public void visitFieldInsn(int i, String s, String s2, String s3) { if (cNode.name.equals(s) && fNode.name.equals(s2)) { foundField[0] = true; } super.visitFieldInsn(i, s, s2, s3); } }); if (foundField[0]) { inst.set(absIns, new FieldInsnNode(Opcodes.GETSTATIC, className, fieldNode.name, fieldNode.desc)); } } } else if (opcode == Opcodes.PUTSTATIC) { if (absIns.getType() == AbstractInsnNode.FIELD_INSN) { final Boolean[] foundField = { false }; final ClassNode cNode = classNode; final FieldNode fNode = fieldNode; absIns.accept(new MethodVisitor(Opcodes.ASM5) { @Override public void visitFieldInsn(int i, String s, String s2, String s3) { if (cNode.name.equals(s) && fNode.name.equals(s2)) { foundField[0] = true; } super.visitFieldInsn(i, s, s2, s3); } }); if (foundField[0]) { inst.set(absIns, new FieldInsnNode(Opcodes.PUTSTATIC, className, fieldNode.name, fieldNode.desc)); } } } } } }
From source file:org.coldswap.asm.field.PublicStaticFieldReferenceReplacer.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//from w w w. j a v a 2 s.co m public int findAndReplace(ClassNode classNode) { int counter = 0; List<MethodNode> methodNodes = classNode.methods; for (MethodNode method : methodNodes) { InsnList inst = method.instructions; Iterator iter = inst.iterator(); while (iter.hasNext()) { AbstractInsnNode absIns = (AbstractInsnNode) iter.next(); int opcode = absIns.getOpcode(); // check if instruction is GETSTATIC or PUTSTATIC if (opcode == Opcodes.GETSTATIC) { // get type if (absIns.getType() == AbstractInsnNode.FIELD_INSN) { final Boolean[] foundField = { false }; absIns.accept(new MethodVisitor(Opcodes.ASM5) { @Override public void visitFieldInsn(int i, String s, String s2, String s3) { if (oldClass.equals(s) && fieldToReplace.name.equals(s2)) { foundField[0] = true; } super.visitFieldInsn(i, s, s2, s3); } }); if (foundField[0]) { inst.set(absIns, new FieldInsnNode(Opcodes.GETSTATIC, newClass, fieldToReplace.name, fieldToReplace.desc)); counter++; } } } else if (opcode == Opcodes.PUTSTATIC) { if (absIns.getType() == AbstractInsnNode.FIELD_INSN) { final Boolean[] foundField = { false }; absIns.accept(new MethodVisitor(Opcodes.ASM5) { @Override public void visitFieldInsn(int i, String s, String s2, String s3) { if (oldClass.equals(s) && fieldToReplace.name.equals(s2)) { foundField[0] = true; } super.visitFieldInsn(i, s, s2, s3); } }); if (foundField[0]) { inst.set(absIns, new FieldInsnNode(Opcodes.PUTSTATIC, newClass, fieldToReplace.name, fieldToReplace.desc)); counter++; } } } } } return counter; }
From source file:org.coldswap.asm.field.PublicStaticFieldReplacer.java
License:Open Source License
/** * Replaces any GETSTATIC/PUTSTATIC call of the field in the old class with the field * introduced in the new class.//www. j a v a 2s.c om * * @param classNode containing the old class. * @param fieldNode containing the old field. */ private void replaceReferences(ClassNode classNode, FieldNode fieldNode) { List<MethodNode> methodNodes = classNode.methods; String contClass = classNode.name.substring(classNode.name.lastIndexOf("/") + 1); final String className = classPackage + TransformerNameGenerator.getPublicStaticFieldClassName(contClass, fieldNode.name); for (MethodNode method : methodNodes) { InsnList inst = method.instructions; Iterator iter = inst.iterator(); while (iter.hasNext()) { AbstractInsnNode absIns = (AbstractInsnNode) iter.next(); int opcode = absIns.getOpcode(); // check if instruction is GETSTATIC or PUTSTATIC if (opcode == Opcodes.GETSTATIC) { // get type if (absIns.getType() == AbstractInsnNode.FIELD_INSN) { final Boolean[] foundField = { false }; final ClassNode cNode = classNode; final FieldNode fNode = fieldNode; absIns.accept(new MethodVisitor(Opcodes.ASM5) { @Override public void visitFieldInsn(int i, String s, String s2, String s3) { if (cNode.name.equals(s) && fNode.name.equals(s2)) { foundField[0] = true; } super.visitFieldInsn(i, s, s2, s3); } }); if (foundField[0]) { inst.set(absIns, new FieldInsnNode(Opcodes.GETSTATIC, className, fieldNode.name, fieldNode.desc)); } } } else if (opcode == Opcodes.PUTSTATIC) { if (absIns.getType() == AbstractInsnNode.FIELD_INSN) { final Boolean[] foundField = { false }; final ClassNode cNode = classNode; final FieldNode fNode = fieldNode; absIns.accept(new MethodVisitor(Opcodes.ASM5) { @Override public void visitFieldInsn(int i, String s, String s2, String s3) { if (cNode.name.equals(s) && fNode.name.equals(s2)) { foundField[0] = true; } super.visitFieldInsn(i, s, s2, s3); } }); if (foundField[0]) { inst.set(absIns, new FieldInsnNode(Opcodes.PUTSTATIC, className, fieldNode.name, fieldNode.desc)); } } } } } }
From source file:org.coldswap.asm.method.PublicFloatMethodReplacer.java
License:Open Source License
private InsnList replaceReturn(InsnList insnList, Type retType) { final Type rretType = retType; int retOpcode = MethodUtil.getRetOpcodeToReplace(retType); for (int i = 0; i < insnList.size(); i++) { AbstractInsnNode absIns = insnList.get(i); int opcode = absIns.getOpcode(); if (opcode == retOpcode) { // if tries to return a Reference type into a primitive then // remove the unbox( we return an Object). If a primitive is returned // into a primitive then we must try to box from primitive to Object/Integer, etc.. // check if an unbox takes place before return final boolean[] isBoxUnbox = { false, false }; AbstractInsnNode valueOf = null; AbstractInsnNode primitiveValue = null; if (i > 1) { valueOf = insnList.get(i - 1); primitiveValue = insnList.get(i - 2); if (valueOf.getOpcode() == Opcodes.INVOKESTATIC) { valueOf.accept(new MethodVisitor(Opcodes.ASM5) { @Override public void visitMethodInsn(int i, String s, String s2, String s3) { if (AutoBoxing.isPrimitive(rretType.getDescriptor())) { if ((AutoBoxing.getBoxClassName(rretType) + ".valueOf").equals(s + s2)) { isBoxUnbox[0] = true; }/*from w w w. j a v a2 s. com*/ } super.visitMethodInsn(i, s, s2, s3); } }); } if (isBoxUnbox[0] && primitiveValue.getOpcode() == Opcodes.INVOKEVIRTUAL) { primitiveValue.accept(new MethodVisitor(Opcodes.ASM5) { @Override public void visitMethodInsn(int i, String s, String s2, String s3) { if ((s + s2).equals(AutoBoxing.getUnBoxInvoke(rretType))) { isBoxUnbox[1] = true; } super.visitMethodInsn(i, s, s2, s3); } }); } } if (isBoxUnbox[0] && isBoxUnbox[1]) { // remove indexes insnList.remove(valueOf); insnList.remove(primitiveValue); } else { InsnList iList = new InsnList(); iList.add(AutoBoxing.box(retType)); iList.add(new InsnNode(Opcodes.ARETURN)); insnList.insertBefore(absIns, iList); insnList.remove(absIns); } } } return insnList; }
From source file:org.coldswap.asm.method.PublicObjectMethodReplacer.java
License:Open Source License
private InsnList replaceReturn(InsnList insnList, Type retType) { final Type rretType = retType; int retOpcode = MethodUtil.getRetOpcodeToReplace(retType); for (int i = 0; i < insnList.size(); i++) { AbstractInsnNode absIns = insnList.get(i); int opcode = absIns.getOpcode(); if (opcode == retOpcode) { // if tries to return a Reference type into a primitive then // remove the unbox( we return an Object). If a primitive is returned // into a primitive then we must try to box from primitive to Object/Integer, etc.. // check if an unbox takes place before return final boolean[] isBoxUnbox = { false, false }; AbstractInsnNode valueOf = null; AbstractInsnNode primitiveValue = null; if (i > 1) { valueOf = insnList.get(i - 1); primitiveValue = insnList.get(i - 2); if (valueOf.getOpcode() == Opcodes.INVOKESTATIC) { valueOf.accept(new MethodVisitor(Opcodes.ASM5) { @Override public void visitMethodInsn(int i, String s, String s2, String s3) { if (AutoBoxing.isPrimitive(rretType.getDescriptor())) { if ((AutoBoxing.getBoxClassName(rretType) + ".valueOf").equals(s + s2)) { isBoxUnbox[0] = true; }/* ww w . j a va 2 s. com*/ } super.visitMethodInsn(i, s, s2, s3); } }); } if (isBoxUnbox[0] && primitiveValue.getOpcode() == Opcodes.INVOKEVIRTUAL) { primitiveValue.accept(new MethodVisitor(Opcodes.ASM5) { @Override public void visitMethodInsn(int i, String s, String s2, String s3) { if ((s + s2).equals(AutoBoxing.getUnBoxInvoke(rretType))) { isBoxUnbox[1] = true; } super.visitMethodInsn(i, s, s2, s3); } }); } } if (isBoxUnbox[0] && isBoxUnbox[1]) { // remove indexes insnList.remove(valueOf); insnList.remove(primitiveValue); } else { InsnList iList = new InsnList(); iList.add(AutoBoxing.box(retType)); iList.add(new InsnNode(Opcodes.ARETURN)); insnList.insertBefore(absIns, iList); insnList.remove(absIns); } } } return insnList; }
From source file:org.coldswap.asm.VirtualMethodReplacer.java
License:Open Source License
@Override public MethodNode replaceInvoke(MethodNode methodNode) { InsnList instructions = methodNode.instructions; Iterator it = instructions.iterator(); while (it.hasNext()) { AbstractInsnNode code = (AbstractInsnNode) it.next(); if (code.getOpcode() == Opcodes.INVOKEVIRTUAL) { // check if methodToReplace is called final boolean[] callFounded = new boolean[] { false }; code.accept(new MethodVisitor(Opcodes.ASM5) { @Override//from w w w . j ava 2s . c o m public void visitMethodInsn(int i, String s, String s2, String s3) { if (s.equals(classContainer) && s2.equals(methodName)) { callFounded[0] = true; } super.visitMethodInsn(i, s, s2, s3); } }); if (callFounded[0]) { // if the return type is primitive and the value is not discarded, unbox if (AutoBoxing.isPrimitive(retType.getDescriptor())) { AbstractInsnNode codeNext = code.getNext(); boolean discarded = false; // if returning primitive double or long and it is discarded with a pop2 than discard with // simple pop, because we use an Object as return value. if (codeNext.getOpcode() == Opcodes.POP2 && (retType.getDescriptor().equals("D") || retType.getDescriptor().equals("J"))) { instructions.set(codeNext, new InsnNode(Opcodes.POP)); } if (codeNext.getOpcode() == Opcodes.POP || codeNext.getOpcode() == Opcodes.POP2) { discarded = true; } if (!discarded) { instructions.insert(code, AutoBoxing.unbox(retType)); } } // replace call with a custom call String newMethodName; AbstractInsnNode newInvoke = null; if (Constants.VAROBJECT.equals(methodType)) { newMethodName = TransformerNameGenerator.getObjectMethodNameWithCounter(classContainer, methodNumber); newInvoke = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classContainer, newMethodName, "([Ljava/lang/Object;)Ljava/lang/Object;"); } else if (Constants.INT.equals(methodType)) { newMethodName = TransformerNameGenerator.getIntMethodNameWithCounter(classContainer, methodNumber); newInvoke = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classContainer, newMethodName, "(I)Ljava/lang/Object;"); } else if (Constants.FLOAT.equals(methodType)) { newMethodName = TransformerNameGenerator.getFloatMethodNameWithCounter(classContainer, methodNumber); newInvoke = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classContainer, newMethodName, "(F)Ljava/lang/Object;"); } else if (Constants.STRING.equals(methodType)) { newMethodName = TransformerNameGenerator.getStringMethodNameWithCounter(classContainer, methodNumber); newInvoke = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classContainer, newMethodName, "(Ljava/lang/String;)Ljava/lang/Object;"); } else if (Constants.LONG.equals(methodType)) { newMethodName = TransformerNameGenerator.getLongMethodNameWithCounter(classContainer, methodNumber); newInvoke = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classContainer, newMethodName, "(J)Ljava/lang/Object;"); } if (newInvoke != null) { instructions.set(code, newInvoke); } } } } return methodNode; }
From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AsmClassVisitor.java
License:Open Source License
/** * Parses the methods of the class/* w ww. jav a 2 s. c om*/ */ @Override public MethodVisitor visitMethod(int access, final String name, final String desc, String signature, String[] exceptions) { clazz.addMethod(name, desc, (access & Opcodes.ACC_STATIC) != 0, (access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED | Opcodes.ACC_PRIVATE))); if (clazz.isTeam() || clazz.isRole()) // check for method annotation ImplicitTeamActivation: return new MethodVisitor(this.api) { @Override public AnnotationVisitor visitAnnotation(String annDesc, boolean visible) { if (annDesc.equals(AddImplicitActivationAdapter.ANNOTATION_IMPLICIT_ACTIVATION)) clazz.registerMethodForImplicitActivation(name + desc); return super.visitAnnotation(annDesc, visible); } }; return super.visitMethod(access, name, desc, signature, exceptions); }
From source file:org.eclipse.ocl.examples.codegen.asm5.ASM5JavaAnnotationReader.java
License:Open Source License
public @Nullable Boolean getIsNonNull(@NonNull Method method) { final String className = method.getDeclaringClass().getName(); final String requiredDesc = getMethodKey(className, method.getName(), Type.getMethodDescriptor(method)); Map<@NonNull Integer, @Nullable Boolean> typeref2state = desc2typerefValue2state.get(requiredDesc); Integer returnTypeReference = TypeReference.newTypeReference(TypeReference.METHOD_RETURN).getValue(); if (typeref2state != null) { return typeref2state.get(returnTypeReference); }//from w w w. jav a 2 s . c o m if (!readClasses.add(className)) { return null; } // System.out.println("getIsNonNull: " + requiredDesc + " " + Integer.toHexString(returnTypeReference)); InputStream classStream = null; try { final int flags = ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE; ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); String classFileName = className.replace('.', '/') + ".class"; classStream = contextClassLoader.getResourceAsStream(classFileName); final ClassReader cr = new ClassReader(classStream) { @Override public void accept(ClassVisitor classVisitor, int flags) { super.accept(classVisitor, flags); } @Override public void accept(ClassVisitor classVisitor, Attribute[] attrs, int flags) { super.accept(classVisitor, attrs, flags); } }; ClassVisitor cv = new ClassVisitor(Opcodes.ASM5) { @Override public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { } @Override public AnnotationVisitor visitAnnotation(String desc, boolean visible) { return null; } @Override public void visitAttribute(Attribute attr) { } @Override public void visitEnd() { } @Override public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { return null; } @Override public void visitInnerClass(String name, String outerName, String innerName, int access) { } @Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { final String methodDesc = getMethodKey(className, name, desc);// + " " + signature; // System.out.println(" ClassVisitor.visitMethod: " + methodDesc); final HashMap<@NonNull Integer, @Nullable Boolean> typerefValue2state = new HashMap<@NonNull Integer, @Nullable Boolean>(); desc2typerefValue2state.put(methodDesc, typerefValue2state); return new MethodVisitor(Opcodes.ASM5) { @Override public AnnotationVisitor visitAnnotation(String annotationDesc, boolean visible) { return null; } @Override public AnnotationVisitor visitAnnotationDefault() { return null; } @Override public void visitAttribute(Attribute attr) { } @Override public void visitCode() { } @Override public void visitEnd() { } @Override public void visitFieldInsn(int opcode, String owner, String name, String desc) { } @Override public void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack) { } @Override public void visitIincInsn(int var, int increment) { } @Override public void visitInsn(int opcode) { } @Override public AnnotationVisitor visitInsnAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) { return null; } @Override public void visitIntInsn(int opcode, int operand) { } @Override public void visitInvokeDynamicInsn(String name, String desc, Handle bsm, Object... bsmArgs) { } @Override public void visitJumpInsn(int opcode, Label label) { } @Override public void visitLabel(Label label) { } @Override public void visitLdcInsn(Object cst) { } @Override public void visitLineNumber(int line, Label start) { } @Override public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) { } @Override public AnnotationVisitor visitLocalVariableAnnotation(int typeRef, TypePath typePath, Label[] start, Label[] end, int[] index, String desc, boolean visible) { return null; } @Override public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) { } @Override public void visitMaxs(int maxStack, int maxLocals) { } @Override public void visitMethodInsn(int opcode, String owner, String name, String desc) { } @Override public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) { } @Override public void visitMultiANewArrayInsn(String desc, int dims) { } @Override public void visitParameter(String name, int access) { } @Override public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) { return null; } @Override public void visitTableSwitchInsn(int min, int max, Label dflt, Label... labels) { } @Override public AnnotationVisitor visitTryCatchAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) { return null; } @Override public void visitTryCatchBlock(Label start, Label end, Label handler, String type) { } @Override public AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) { // System.out.println(" MethodVisitor-TypeAnnotation:" + Integer.toHexString(typeRef) + " " + typePath + " " + desc + " " + visible); // TypeReference typeReference = new TypeReference(typeRef); // System.out.println(" : " + Integer.toHexString(typeReference.getValue()) + ":" + typeReference.getSort() + ":" + typeReference.getTypeParameterIndex()); if (desc.equals(nonNullDesc)) { // System.out.println(" MethodVisitor-TypeAnnotation:" + Integer.toHexString(typeRef) + " " + typePath + " " + desc); typerefValue2state.put(typeRef, true); } else if (desc.equals(nullableDesc)) { // System.out.println(" MethodVisitor-TypeAnnotation:" + Integer.toHexString(typeRef) + " " + typePath + " " + desc); typerefValue2state.put(typeRef, false); } return null; } @Override public void visitTypeInsn(int opcode, String type) { } @Override public void visitVarInsn(int opcode, int var) { } }; } @Override public void visitOuterClass(String owner, String name, String desc) { } @Override public void visitSource(String source, String debug) { } @Override public AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) { // System.out.println(" ClassVisitor-TypeAnnotation:" + typeRef + " " + typePath + " " + desc + " " + visible); return null; } }; cr.accept(cv, flags); } catch (IOException e) { logger.error("Failed to read '" + className + "'", e); } finally { if (classStream != null) { try { classStream.close(); } catch (IOException e) { } } } typeref2state = desc2typerefValue2state.get(requiredDesc); if (typeref2state == null) { return null; } Boolean state = typeref2state.get(returnTypeReference); // System.out.println(" => " + state); return state; }
From source file:org.ehcache.impl.internal.store.offheap.AssertingOffHeapValueHolder.java
License:Apache License
private static boolean isLockedInFrame(StackTraceElement ste) { try {/*from w w w . j a v a 2 s .c o m*/ ClassReader reader = new ClassReader(ste.getClassName()); NavigableMap<Integer, Integer> lockLevels = new TreeMap<>(); reader.accept(new ClassVisitor(ASM6) { @Override public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) { if (ste.getMethodName().equals(name)) { return new InstructionAdapter(ASM6, new MethodVisitor(ASM6) { }) { private final Map<Label, Integer> levels = new HashMap<>(); private int lockLevel; @Override public void invokeinterface(String owner, String name, String descriptor) { if (LOCK_CLASS.equals(getObjectType(owner))) { if (LOCK_METHOD.equals(new Method(name, descriptor))) { lockLevel++; } else if (UNLOCK_METHOD.equals(new Method(name, descriptor))) { lockLevel--; } } } @Override public void visitJumpInsn(int opcode, Label label) { levels.merge(label, lockLevel, Integer::max); } @Override public void visitLabel(Label label) { lockLevel = levels.merge(label, lockLevel, Integer::max); } @Override public void visitLineNumber(int line, Label start) { lockLevels.merge(line, levels.get(start), Integer::max); } }; } else { return null; } } }, 0); Map.Entry<Integer, Integer> entry = lockLevels.floorEntry(ste.getLineNumber()); return entry.getValue() > 0; } catch (IOException e) { throw new AssertionError(e); } }
From source file:org.evosuite.instrumentation.EmptyVisitor.java
License:Open Source License
/** {@inheritDoc} */ @Override/*from ww w. j av a 2 s. co m*/ public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { return new MethodVisitor(Opcodes.ASM5) { @Override public AnnotationVisitor visitAnnotationDefault() { return av; } @Override public AnnotationVisitor visitAnnotation(String desc, boolean visible) { return av; } @Override public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) { return av; } }; }