Package javassist.bytecode

Examples of javassist.bytecode.CodeIterator


            for (MethodInfo m : methods) {
                try {
                    if (m.getCodeAttribute() == null) {
                        continue;
                    }
                    CodeIterator it = m.getCodeAttribute().iterator();
                    while (it.hasNext()) {
                        int index = it.next();
                        int op = it.byteAt(index);
                        if (op == CodeIterator.GETSTATIC || op == CodeIterator.PUTSTATIC) {
                            int val = it.s16bitAt(index + 1);
                            if (fieldAccessLocations.containsKey(val)) {
                                StaticFieldAccessRewriteData data = fieldAccessLocations.get(val);
                                it.write16bit(newFieldAccessLocations.get(data), index + 1);
                            }
                        }
                    }
                    m.getCodeAttribute().computeMaxStack();
                } catch (Exception e) {
View Full Code Here


                try {
                    // ignore abstract methods
                    if (m.getCodeAttribute() == null) {
                        continue;
                    }
                    CodeIterator it = m.getCodeAttribute().iterator();
                    while (it.hasNext()) {
                        // loop through the bytecode
                        int index = it.next();
                        int op = it.byteAt(index);
                        // if the bytecode is a method invocation
                        if (op == CodeIterator.INVOKEVIRTUAL) {
                            int val = it.s16bitAt(index + 1);
                            // if the method call is one of the methods we are
                            // replacing
                            if (methodCallLocations.contains(val)) {
                                Bytecode b = new Bytecode(file.getConstPool());
                                // our stack looks like Method, instance,params
                                // we need Method, instance, params , Method
                                b.add(Opcode.DUP_X2);
                                b.add(Opcode.POP);
                                b.add(Opcode.DUP_X2);
                                b.add(Opcode.POP);
                                b.add(Opcode.DUP_X2);
                                b.addInvokestatic(methodReflectionLocation, "fakeCallRequired", "(Ljava/lang/reflect/Method;)Z");
                                b.add(Opcode.IFEQ);
                                JumpMarker performRealCall = JumpUtils.addJumpInstruction(b);
                                // now perform the fake call
                                b.addInvokestatic(methodReflectionLocation, "invoke", REPLACED_METHOD_DESCRIPTOR);
                                b.add(Opcode.GOTO);
                                JumpMarker finish = JumpUtils.addJumpInstruction(b);
                                performRealCall.mark();
                                b.addInvokevirtual(Method.class.getName(), METHOD_NAME, METHOD_DESCRIPTOR);
                                finish.mark();
                                it.writeByte(CodeIterator.NOP, index);
                                it.writeByte(CodeIterator.NOP, index + 1);
                                it.writeByte(CodeIterator.NOP, index + 2);
                                it.insertEx(b.get());
                            }
                        }

                    }
                    m.getCodeAttribute().computeMaxStack();
View Full Code Here

                try {
                    // ignore abstract methods
                    if (m.getCodeAttribute() == null) {
                        continue;
                    }
                    CodeIterator it = m.getCodeAttribute().iterator();
                    while (it.hasNext()) {
                        // loop through the bytecode
                        int index = it.next();
                        int op = it.byteAt(index);
                        // if the bytecode is a method invocation
                        if (op == CodeIterator.INVOKEVIRTUAL || op == CodeIterator.INVOKESTATIC || op == CodeIterator.INVOKEINTERFACE || op == CodeIterator.INVOKESPECIAL) {
                            int val = it.s16bitAt(index + 1);
                            // if the method call is one of the methods we are
                            // replacing
                            if (methodCallLocations.containsKey(val)) {
                                VirtualToStaticData data = methodCallLocations.get(val);
                                // change the call to an invokestatic
                                it.writeByte(CodeIterator.INVOKESTATIC, index);
                                // change the method that is being called
                                it.write16bit(newCallLocations.get(data), index + 1);
                                if (op == CodeIterator.INVOKEINTERFACE) {
                                    // INVOKEINTERFACE has some extra parameters
                                    it.writeByte(CodeIterator.NOP, index + 3);
                                    it.writeByte(CodeIterator.NOP, index + 4);
                                }
                                modifiedMethods.add(m);
                            }
                        }
View Full Code Here

                try {
                    // ignore abstract methods
                    if (m.getCodeAttribute() == null) {
                        continue;
                    }
                    CodeIterator it = m.getCodeAttribute().iterator();
                    while (it.hasNext()) {
                        // loop through the bytecode
                        int index = it.next();
                        int op = it.byteAt(index);
                        // if the bytecode is a field access
                        if (op == Opcode.PUTFIELD || op == Opcode.GETFIELD) {
                            int val = it.s16bitAt(index + 1);
                            // if the field access is for an added field
                            if (fieldAccessLocations.containsKey(val)) {
                                AddedFieldData data = fieldAccessLocations.get(val);
                                int arrayPos = file.getConstPool().addIntegerInfo(data.getArrayIndex());
                                // write over the field access with nop
                                it.writeByte(Opcode.NOP, index);
                                it.writeByte(Opcode.NOP, index + 1);
                                it.writeByte(Opcode.NOP, index + 2);

                                if (op == Opcode.PUTFIELD) {
                                    Bytecode b = new Bytecode(file.getConstPool());
                                    if (data.getDescriptor().charAt(0) != 'L' && data.getDescriptor().charAt(0) != '[') {
                                        Boxing.box(b, data.getDescriptor().charAt(0));
                                    }
                                    b.addLdc(arrayPos);
                                    b.addInvokestatic(FIELD_DATA_STORE_CLASS, "setValue", "(Ljava/lang/Object;Ljava/lang/Object;I)V");
                                    it.insertEx(b.get());
                                } else if (op == Opcode.GETFIELD) {
                                    Bytecode b = new Bytecode(file.getConstPool());
                                    b.addLdc(arrayPos);
                                    b.addInvokestatic(FIELD_DATA_STORE_CLASS, "getValue", "(Ljava/lang/Object;I)Ljava/lang/Object;");

                                    if (DescriptorUtils.isPrimitive(data.getDescriptor())) {
                                        Boxing.unbox(b, data.getDescriptor().charAt(0));
                                    } else {
                                        b.addCheckcast(DescriptorUtils.getTypeStringFromDescriptorFormat(data.getDescriptor()));
                                    }
                                    it.insertEx(b.get());
                                }

                            }
                        }
                    }
View Full Code Here

        // take it past the end of the code
        ManipulationUtils.add16bit(bc, offset + 3); // add the branch offset

        // now we need to insert our generated conditional at the start of the
        // new method
        CodeIterator newInfo = ca.iterator();
        newInfo.insert(bc.get());
        // now insert the new method code at the beginning of the static method
        // code attribute
        addedMethod.iterator().insert(ca.getCode());

        // update the exception table
View Full Code Here

        // would indicate an abstract method, continue to next method
        continue;
      }

      try {
        final CodeIterator itr = codeAttr.iterator();
        while ( itr.hasNext() ) {
          final int index = itr.next();
          final int op = itr.byteAt( index );
          if ( op != Opcode.PUTFIELD && op != Opcode.GETFIELD ) {
            continue;
          }

          final int constIndex = itr.u16bitAt( index+1 );

          final String fieldName = constPool.getFieldrefName( constIndex );
          final PersistentAttributeDescriptor attributeDescriptor = attributeDescriptorMap.get( fieldName );

          if ( attributeDescriptor == null ) {
            // its not a field we have enhanced for interception, so skip it
            continue;
          }

          log.tracef(
              "Transforming access to field [%s] from method [%s]",
              fieldName,
              methodName
          );

          if ( op == Opcode.GETFIELD ) {
            final int readMethodIndex = constPool.addMethodrefInfo(
                constPool.getThisClassInfo(),
                attributeDescriptor.getReader().getName(),
                attributeDescriptor.getReader().getSignature()
            );
            itr.writeByte( Opcode.INVOKESPECIAL, index );
            itr.write16bit( readMethodIndex, index+1 );
          }
          else {
            final int writeMethodIndex = constPool.addMethodrefInfo(
                constPool.getThisClassInfo(),
                attributeDescriptor.getWriter().getName(),
                attributeDescriptor.getWriter().getSignature()
            );
            itr.writeByte( Opcode.INVOKESPECIAL, index );
            itr.write16bit( writeMethodIndex, index+1 );
          }
        }

        final StackMapTable smt = MapMaker.make( classPool, methodInfo );
        methodInfo.getCodeAttribute().setAttribute( smt );
View Full Code Here

                    // The instruction at which this local variable has been created
                    Integer pc = localVariableAttribute.startPc(i);

                    // Move to the next instruction (insertionPc)
                    CodeIterator iterator = codeAttribute.iterator();
                    iterator.move(pc);
                    Integer insertionPc = iterator.next();

                    Javac jv = new Javac(ctClass);

                    // Compile the code snippet
                    jv.recordLocalVariables(codeAttribute, insertionPc);
                    jv.recordParams(method.getParameterTypes(), Modifier.isStatic(method.getModifiers()));
                    jv.setMaxLocals(codeAttribute.getMaxLocals());
                    jv.compileStmnt("play.classloading.enhancers.LocalvariablesNamesEnhancer.LocalVariablesNamesTracer.addVariable(\"" + aliasedName + "\", " + name + ");");

                    Bytecode b = jv.getBytecode();
                    int locals = b.getMaxLocals();
                    int stack = b.getMaxStack();
                    codeAttribute.setMaxLocals(locals);
                    if (stack > codeAttribute.getMaxStack()) {
                        codeAttribute.setMaxStack(stack);
                    }
                    iterator.insert(insertionPc, b.get());
                    iterator.insert(b.getExceptionTable(), insertionPc);


                    // Then we need to trace each affectation to the variable
                    CodeIterator codeIterator = codeAttribute.iterator();

                    // Bon chaque instruction de cette méthode
                    while (codeIterator.hasNext()) {
                        int index = codeIterator.next();
                        int op = codeIterator.byteAt(index);

                        // DEBUG
                        // printOp(op);

                        int varNumber = -1;
                        // The variable changes
                        if (storeByCode.containsKey(op)) {
                            varNumber = storeByCode.get(op);
                            if (varNumber == -2) {
                                varNumber = codeIterator.byteAt(index + 1);
                            }
                        }

                        // Si c'est un store de la variable en cours d'examination
                        // et que c'est dans la frame d'utilisation de cette variable on trace l'affectation.
                        // (en fait la frame commence à localVariableAttribute.startPc(i)-1 qui est la première affectation
                        //  mais aussi l'initialisation de la variable qui est deja tracé plus haut, donc on commence à localVariableAttribute.startPc(i))
                        if (varNumber == localVariableAttribute.index(i) && index >= localVariableAttribute.startPc(i) && index < localVariableAttribute.startPc(i) + localVariableAttribute.codeLength(i)) {

                            jv.compileStmnt("play.classloading.enhancers.LocalvariablesNamesEnhancer.LocalVariablesNamesTracer.addVariable(\"" + aliasedName + "\", " + name + ");");

                            b = jv.getBytecode();
                            locals = b.getMaxLocals();
                            stack = b.getMaxStack();
                            codeAttribute.setMaxLocals(locals);

                            if (stack > codeAttribute.getMaxStack()) {
                                codeAttribute.setMaxStack(stack);
                            }
                            codeIterator.insert(b.get());

                        }

                    }
View Full Code Here

        // would indicate an abstract method, continue to next method
        continue;
      }

      try {
        CodeIterator itr = codeAttr.iterator();
        while ( itr.hasNext() ) {
          int index = itr.next();
          int op = itr.byteAt( index );
          if ( op != Opcode.PUTFIELD && op != Opcode.GETFIELD ) {
            continue;
          }

          int constIndex = itr.u16bitAt( index+1 );

          final String fieldName = constPool.getFieldrefName( constIndex );
          final PersistentAttributeDescriptor attributeDescriptor = attributeDescriptorMap.get( fieldName );

          if ( attributeDescriptor == null ) {
            // its not a field we have enhanced for interception, so skip it
            continue;
          }

          log.tracef(
              "Transforming access to field [%s] from method [%s]",
              fieldName,
              methodName
          );

          if ( op == Opcode.GETFIELD ) {
            int read_method_index = constPool.addMethodrefInfo(
                constPool.getThisClassInfo(),
                attributeDescriptor.getReader().getName(),
                attributeDescriptor.getReader().getSignature()
            );
            itr.writeByte( Opcode.INVOKESPECIAL, index );
            itr.write16bit( read_method_index, index+1 );
          }
          else {
            int write_method_index = constPool.addMethodrefInfo(
                constPool.getThisClassInfo(),
                attributeDescriptor.getWriter().getName(),
                attributeDescriptor.getWriter().getSignature()
            );
            itr.writeByte( Opcode.INVOKESPECIAL, index );
            itr.write16bit( write_method_index, index+1 );
          }
        }

        StackMapTable smt = MapMaker.make( classPool, methodInfo );
        methodInfo.getCodeAttribute().setAttribute(smt);
View Full Code Here

      }
      CodeAttribute codeAttr = minfo.getCodeAttribute();
      if (codeAttr == null) {
        return;
      }
      CodeIterator iter = codeAttr.iterator();
      while (iter.hasNext()) {
        try {
          int pos = iter.next();
          pos = transformInvokevirtualsIntoGetfields(classfile, iter,
                                                     pos);
          pos = transformInvokevirtualsIntoPutfields(classfile, iter,
                                                     pos);
View Full Code Here

            CodeAttribute codeAttr = m.getCodeAttribute();
            if (codeAttr == null)
                throw new CannotCompileException("empty <clinit>");

            try {
                CodeIterator it = codeAttr.iterator();
                int pos = it.insertEx(code.get());
                it.insert(code.getExceptionTable(), pos);
                int maxstack = codeAttr.getMaxStack();
                if (maxstack < stacksize)
                    codeAttr.setMaxStack(stacksize);

                int maxlocals = codeAttr.getMaxLocals();
View Full Code Here

TOP

Related Classes of javassist.bytecode.CodeIterator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.