List of usage examples for org.objectweb.asm Opcodes ASM5
int ASM5
To view the source code for org.objectweb.asm Opcodes ASM5.
Click Source Link
From source file:org.coldswap.asm.field.PrivateStaticFieldReplacer.java
License:Open Source License
public byte[] replace() { final ClassNode cn = new ClassNode(Opcodes.ASM5); ClassReader cr = new ClassReader(bytes); ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES); cr.accept(cn, 0);//ww w . j a v a 2 s.c o m // get a list of public/protected/package/private fields but exclude // inherited fields. Field[] refFields = aClass.getDeclaredFields(); List asmFields = cn.fields; Iterator iterator = asmFields.iterator(); while (iterator.hasNext()) { final FieldNode fNode = (FieldNode) iterator.next(); int privateStatic = Opcodes.ACC_STATIC | Opcodes.ACC_PRIVATE; // search only for private static fields if ((fNode.access == privateStatic)) { // check if this field exist in the old loaded class // and if not proceed with the trick. boolean foundIt = false; for (Field refField : refFields) { if (fNode.name.equals(refField.getName())) { if (fNode.desc.equals(Type.getType(refField.getType()).getDescriptor())) { foundIt = true; break; } } } if (!foundIt) { String contClass = cn.name.substring(cn.name.lastIndexOf("/") + 1); String className = TransformerNameGenerator.getPrivateStaticFieldClassName(contClass, fNode.name); // remove the static reference from <clinit> InsnList insnList = cleanClInit(cn, fNode); // create a new class that contains the field // before anything change the field access to public static so that it can be referenced // from other classes. fNode.access = Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC; byte[] newBClass = ByteCodeGenerator.newFieldClass(cn, fNode, insnList, classPackage + className); try { String cp = ClassUtil.getClassPath(aClass); ByteCodeClassWriter.setClassPath(cp); ByteCodeClassWriter.writeClass(className, newBClass); } catch (IOException e) { logger.warning(e.toString()); } iterator.remove(); // replace every reference replaceReferences(cn, fNode); } } } cn.accept(cw); return cw.toByteArray(); }
From source file:org.coldswap.asm.field.PrivateStaticFieldReplacer.java
License:Open Source License
/** * Removes any initializing reference of the field. * * @param classNode containing the old class. * @param fieldNode containing the old field. * @return the initializing list of instructions. *//*from www . java 2 s . c o m*/ @SuppressWarnings("unchecked") private InsnList cleanClInit(ClassNode classNode, FieldNode fieldNode) { List<MethodNode> methodNodes = classNode.methods; AbstractInsnNode firstInst = null; int counter = 0; for (MethodNode methodNode : methodNodes) { if (methodNode.name.equals("<clinit>")) { // search for PUTSTATIC InsnList insnList = methodNode.instructions; Iterator iterator1 = insnList.iterator(); while (iterator1.hasNext()) { AbstractInsnNode ins2 = (AbstractInsnNode) iterator1.next(); // if a initializing has been found, then copy everything from // the corresponding label to the PUTSTATIC if (ins2.getOpcode() == Opcodes.PUTSTATIC) { final Boolean[] fieldFound = { false }; final FieldNode fNode = fieldNode; ins2.accept(new MethodVisitor(Opcodes.ASM5) { @Override public void visitFieldInsn(int i, String s, String s2, String s3) { if (s2.equals(fNode.name)) { fieldFound[0] = true; } super.visitFieldInsn(i, s, s2, s3); } }); if (fieldFound[0]) { // find the first PUTSTATIC before this one. boolean staticFound = false; while (!staticFound) { AbstractInsnNode tmpInst = ins2.getPrevious(); if (tmpInst != null) { if (tmpInst.getOpcode() != Opcodes.F_NEW) { if (tmpInst.getOpcode() == Opcodes.PUTSTATIC) { staticFound = true; } else { firstInst = tmpInst; counter++; } } } else { staticFound = true; } ins2 = tmpInst; } break; } } } if (firstInst != null) { InsnList iList = new InsnList(); iList.add(firstInst.clone(null)); counter--; while (counter > 0) { AbstractInsnNode ain = firstInst.getNext(); iList.add(ain.clone(null)); counter--; insnList.remove(firstInst); firstInst = ain; } // remove last instruction and the putstatic instruction AbstractInsnNode putStatic = firstInst.getNext(); insnList.remove(firstInst); insnList.remove(putStatic); return iList; } } } return null; }
From source file:org.coldswap.asm.field.PrivateStaticFieldReplacer.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 ww w. j a v a2 s .c o m*/ * * @param classNode containing the old class. * @param fieldNode containing the old field. */ @SuppressWarnings("unchecked") 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.getPrivateStaticFieldClassName(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.ProtectedStaticFieldReferenceReplacer.java
License:Open Source License
@SuppressWarnings("uncheked") @Override//w w w .j a v a 2s. c o m public int findAndReplace(ClassNode classNode) { int counter = 0; if (classNode.superName.equals(supperClass)) { 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.ProtectedStaticFieldReplacer.java
License:Open Source License
public byte[] replace() { final ClassNode cn = new ClassNode(Opcodes.ASM5); ClassReader cr = new ClassReader(bytes); ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES); cr.accept(cn, 0);// w w w. j av a2s .co m // get a list of public/protected fields. Field[] refFields = aClass.getDeclaredFields(); List asmFields = getFields(cn); Iterator iterator = asmFields.iterator(); while (iterator.hasNext()) { final FieldNode fNode = (FieldNode) iterator.next(); int protectedStatic = Opcodes.ACC_STATIC | Opcodes.ACC_PROTECTED; // search only for protected static fields if ((fNode.access == protectedStatic)) { // check if this field exist in the old loaded class // and if not proceed with the trick. boolean foundIt = false; for (Field refField : refFields) { if (fNode.name.equals(refField.getName())) { if (fNode.desc.equals(Type.getType(refField.getType()).getDescriptor())) { foundIt = true; break; } } } if (!foundIt) { // register a public static reference replacer String contClass = cn.name.substring(cn.name.lastIndexOf("/") + 1); String className = TransformerNameGenerator.getProtectedStaticFieldClassName(contClass, fNode.name); // make field access as public static fNode.access = Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC; replacerManager.registerFieldReferenceReplacer(new ProtectedStaticFieldReferenceReplacer( contClass, fNode, classPackage + className, contClass)); // remove the static reference from <clinit> InsnList insnList = cleanClInit(cn, fNode, true); // because the field might be inherited from superclass we need to copy // the initializer from the parent. if ((insnList == null) || (insnList.size() > 0)) { insnList = inheritedInitCode.get(fNode); } // create a new class that contains the field // before anything change the field access to public static so that it can be referenced // from other classes. fNode.access = Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC; byte[] newBClass = ByteCodeGenerator.newFieldClass(cn, fNode, insnList, classPackage + className); try { String cp = ClassUtil.getClassPath(aClass); ByteCodeClassWriter.setClassPath(cp); ByteCodeClassWriter.writeClass(className, newBClass); } catch (IOException e) { logger.warning(e.toString()); } iterator.remove(); // replace every reference replaceReferences(cn, fNode); } } } cn.accept(cw); return cw.toByteArray(); }
From source file:org.coldswap.asm.field.ProtectedStaticFieldReplacer.java
License:Open Source License
/** * Removes any initializing reference of the field. * * @param classNode containing the old class. * @param fieldNode containing the old field. * @param canRemove <code>true</code> if this method should remove the initializing code and return it * or <code>false</code> if you only want to return the init code. * @return the initializing list of instructions. *//* www. j av a 2s . c o m*/ @SuppressWarnings("unchecked") private InsnList cleanClInit(ClassNode classNode, FieldNode fieldNode, boolean canRemove) { List<MethodNode> methodNodes = classNode.methods; AbstractInsnNode firstInst = null; int counter = 0; for (MethodNode methodNode : methodNodes) { if (methodNode.name.equals("<clinit>")) { // search for PUTSTATIC InsnList insnList = methodNode.instructions; Iterator iterator1 = insnList.iterator(); while (iterator1.hasNext()) { AbstractInsnNode ins2 = (AbstractInsnNode) iterator1.next(); // if a initializing has been found, then copy everything from // the coresponding label to the PUTSTATIC if (ins2.getOpcode() == Opcodes.PUTSTATIC) { final Boolean[] fieldFound = { false }; final FieldNode fNode = fieldNode; ins2.accept(new MethodVisitor(Opcodes.ASM5) { @Override public void visitFieldInsn(int i, String s, String s2, String s3) { if (s2.equals(fNode.name)) { fieldFound[0] = true; } super.visitFieldInsn(i, s, s2, s3); } }); if (fieldFound[0]) { // find the first PUTSTATIC before this one. boolean staticFound = false; while (!staticFound) { AbstractInsnNode tmpInst = ins2.getPrevious(); if (tmpInst != null) { if (tmpInst.getOpcode() != Opcodes.F_NEW) { if (tmpInst.getOpcode() == Opcodes.PUTSTATIC) { staticFound = true; } else { firstInst = tmpInst; counter++; } } } else { staticFound = true; } ins2 = tmpInst; } break; } } } if (firstInst != null) { InsnList iList = new InsnList(); iList.add(firstInst.clone(null)); counter--; while (counter > 0) { AbstractInsnNode ain = firstInst.getNext(); iList.add(ain.clone(null)); counter--; if (canRemove) { insnList.remove(firstInst); } firstInst = ain; } if (canRemove) { // remove last instruction and the putstatic instruction AbstractInsnNode putStatic = firstInst.getNext(); insnList.remove(firstInst); insnList.remove(putStatic); } return iList; } } } return null; }
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./* w w w . j av a 2 s.c o m*/ * * @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.ProtectedStaticFieldReplacer.java
License:Open Source License
/** * Returns an array of fields that where declared in this class. In * addition there are also the protected fields that were inherited. * * @param classNode {@link ClassNode} containing the new class version. * @return a list of all fields including the inherited fields. *//*from ww w . ja v a 2 s .c om*/ private List<FieldNode> getFields(ClassNode classNode) { List<FieldNode> toRet = classNode.fields; // get inherited package fields try { String superName = classNode.superName; // filter superclasses for (String pack : ClassUtil.skipTransforming) { if (superName.startsWith(pack)) { return toRet; } } Class<?> c = Class.forName(superName); String path = ClassUtil.getClassPath(c); path = path + ClassUtil.fileSeparator + superName + ".class"; byte[] clazz = ByteCodeClassLoader.loadClassBytes(path); ClassNode cNode = new ClassNode(Opcodes.ASM5); ClassReader cr = new ClassReader(clazz); cr.accept(cNode, 0); List<FieldNode> superFields = cNode.fields; for (FieldNode fieldNode : superFields) { if (fieldNode.access == (Opcodes.ACC_STATIC | Opcodes.ACC_PROTECTED)) { toRet.add(fieldNode); InsnList code = cleanClInit(cNode, fieldNode, false); inheritedInitCode.put(fieldNode, code); } } } catch (ClassNotFoundException e) { logger.warning(e.toString()); } return toRet; }
From source file:org.coldswap.asm.field.PublicStaticFieldReferenceReplacer.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//from w w w. jav 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
public byte[] replace() { final ClassNode cn = new ClassNode(Opcodes.ASM5); ClassReader cr = new ClassReader(bytes); ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES); cr.accept(cn, 0);/*from ww w . j a va2 s .c om*/ // get a list of public/protected/package/private fields but exclude // inherited fields. Field[] refFields = aClass.getDeclaredFields(); List asmFields = cn.fields; Iterator iterator = asmFields.iterator(); while (iterator.hasNext()) { final FieldNode fNode = (FieldNode) iterator.next(); int publicStatic = Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC; //int publicStaticFinal = Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL; // search only for public static fields if ((fNode.access == publicStatic)) { // check if this field exist in the old loaded class // and if not proceed with the trick. boolean foundIt = false; for (Field refField : refFields) { if (fNode.name.equals(refField.getName())) { if (fNode.desc.equals(Type.getType(refField.getType()).getDescriptor())) { foundIt = true; break; } } } if (!foundIt) { // register a public static reference replacer String contClass = cn.name.substring(cn.name.lastIndexOf("/") + 1); String className = TransformerNameGenerator.getPublicStaticFieldClassName(contClass, fNode.name); replacerManager.registerFieldReferenceReplacer( new PublicStaticFieldReferenceReplacer(contClass, fNode, classPackage + className)); // remove the static reference from <clinit> InsnList insnList = cleanClInit(cn, fNode); // create a new class that contains the field byte[] newBClass = ByteCodeGenerator.newFieldClass(cn, fNode, insnList, classPackage + className); try { String cp = ClassUtil.getClassPath(aClass); ByteCodeClassWriter.setClassPath(cp); ByteCodeClassWriter.writeClass(className, newBClass); } catch (IOException e) { logger.warning(e.toString()); } iterator.remove(); // replace every reference replaceReferences(cn, fNode); } } } cn.accept(cw); return cw.toByteArray(); }