Java Code Examples for org.jf.dexlib2.iface.instruction.ReferenceInstruction

The following examples show how to use org.jf.dexlib2.iface.instruction.ReferenceInstruction. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: ZjDroid   Source File: DexWriter.java    License: Apache License 2.0 6 votes vote down vote up
private void fixInstructions(@Nonnull MutableMethodImplementation methodImplementation) {
    List<? extends Instruction> instructions = methodImplementation.getInstructions();

    for (int i=0; i<instructions.size(); i++) {
        Instruction instruction = instructions.get(i);

        if (instruction.getOpcode() == Opcode.CONST_STRING) {
            if (stringSection.getItemIndex(
                    (StringRef)((ReferenceInstruction)instruction).getReference()) >= 65536) {
                methodImplementation.replaceInstruction(i, new BuilderInstruction31c(Opcode.CONST_STRING_JUMBO,
                        ((OneRegisterInstruction)instruction).getRegisterA(),
                        ((ReferenceInstruction)instruction).getReference()));
            }
        }
    }
}
 
Example 2
Source Project: ZjDroid   Source File: InstructionRewriter.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull @Override public Instruction rewrite(@Nonnull Instruction instruction) {
    if (instruction instanceof ReferenceInstruction) {
        switch (instruction.getOpcode().format) {
            case Format20bc:
                return new RewrittenInstruction20bc((Instruction20bc)instruction);
            case Format21c:
                return new RewrittenInstruction21c((Instruction21c)instruction);
            case Format22c:
                return new RewrittenInstruction22c((Instruction22c)instruction);
            case Format31c:
                return new RewrittenInstruction31c((Instruction31c)instruction);
            case Format35c:
                return new RewrittenInstruction35c((Instruction35c)instruction);
            case Format3rc:
                return new RewrittenInstruction3rc((Instruction3rc)instruction);
            default:
                throw new IllegalArgumentException();
        }
    }
    return instruction;
}
 
Example 3
Source Project: atlas   Source File: AtlasFrameworkPropertiesReader.java    License: Apache License 2.0 6 votes vote down vote up
public LinkedHashMap<String,BundleListing.BundleInfo>read(String className,String memberName) throws Exception {

        if (reader!= null) {
            Method method = (Method) reader.read(className, memberName);
            if (method!= null){
                Iterable<? extends Instruction> instructions = method.getImplementation().getInstructions();
                for (Instruction instruction:instructions){
                    if (instruction instanceof ReferenceInstruction){
                        if (((ReferenceInstruction) instruction).getReferenceType()== 0){
                            StringReference s = (StringReference) ((ReferenceInstruction) instruction).getReference();
                            return BundleListingUtil.parseArray(s.getString(), (LinkedHashMap<String, BundleListing.BundleInfo>) map);
                        }
                    }
                }
            }
        }
        return null;
    }
 
Example 4
Source Project: zjdroid   Source File: DexWriter.java    License: Apache License 2.0 6 votes vote down vote up
private void fixInstructions(@Nonnull MutableMethodImplementation methodImplementation) {
    List<? extends Instruction> instructions = methodImplementation.getInstructions();

    for (int i=0; i<instructions.size(); i++) {
        Instruction instruction = instructions.get(i);

        if (instruction.getOpcode() == Opcode.CONST_STRING) {
            if (stringSection.getItemIndex(
                    (StringRef)((ReferenceInstruction)instruction).getReference()) >= 65536) {
                methodImplementation.replaceInstruction(i, new BuilderInstruction31c(Opcode.CONST_STRING_JUMBO,
                        ((OneRegisterInstruction)instruction).getRegisterA(),
                        ((ReferenceInstruction)instruction).getReference()));
            }
        }
    }
}
 
Example 5
Source Project: zjdroid   Source File: InstructionRewriter.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull @Override public Instruction rewrite(@Nonnull Instruction instruction) {
    if (instruction instanceof ReferenceInstruction) {
        switch (instruction.getOpcode().format) {
            case Format20bc:
                return new RewrittenInstruction20bc((Instruction20bc)instruction);
            case Format21c:
                return new RewrittenInstruction21c((Instruction21c)instruction);
            case Format22c:
                return new RewrittenInstruction22c((Instruction22c)instruction);
            case Format31c:
                return new RewrittenInstruction31c((Instruction31c)instruction);
            case Format35c:
                return new RewrittenInstruction35c((Instruction35c)instruction);
            case Format3rc:
                return new RewrittenInstruction3rc((Instruction3rc)instruction);
            default:
                throw new IllegalArgumentException();
        }
    }
    return instruction;
}
 
Example 6
Source Project: JAADAS   Source File: IgetInstruction.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void jimplify (DexBody body) {
      TwoRegisterInstruction i = (TwoRegisterInstruction)instruction;
      int dest = i.getRegisterA();
      int object = i.getRegisterB();
      FieldReference f = (FieldReference)((ReferenceInstruction)instruction).getReference();
      InstanceFieldRef r = Jimple.v().newInstanceFieldRef(body.getRegisterLocal(object),
                                                          getSootFieldRef(f));
      assign = Jimple.v().newAssignStmt(body.getRegisterLocal(dest), r);
      setUnit(assign);
      addTags(assign);
      body.add(assign);
      
if (IDalvikTyper.ENABLE_DVKTYPER) {
	Debug.printDbg(IDalvikTyper.DEBUG, "constraint: "+ assign);
        int op = (int)instruction.getOpcode().value;
        DalvikTyper.v().setType(assign.getLeftOpBox(), r.getType(), false);
      }
  }
 
Example 7
Source Project: JAADAS   Source File: SputInstruction.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void jimplify (DexBody body) {
      int source = ((OneRegisterInstruction)instruction).getRegisterA();
      FieldReference f = (FieldReference)((ReferenceInstruction)instruction).getReference();
      StaticFieldRef instanceField = Jimple.v().newStaticFieldRef(getStaticSootFieldRef(f));
      Local sourceValue = body.getRegisterLocal(source);
      assign = getAssignStmt(body, sourceValue, instanceField);
      setUnit(assign);
      addTags(assign);
      body.add(assign);

if (IDalvikTyper.ENABLE_DVKTYPER) {
	Debug.printDbg(IDalvikTyper.DEBUG, "constraint: "+ assign);
        int op = (int)instruction.getOpcode().value;
        DalvikTyper.v().setType(assign.getRightOpBox(), instanceField.getType(), true);
      }
  }
 
Example 8
Source Project: JAADAS   Source File: ConstClassInstruction.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void jimplify (DexBody body) {
      if(!(instruction instanceof Instruction21c))
          throw new IllegalArgumentException("Expected Instruction21c but got: "+instruction.getClass());

      ReferenceInstruction constClass = (ReferenceInstruction) this.instruction;

      TypeReference tidi = (TypeReference)(constClass.getReference());
      String type = tidi.getType();
      if (type.startsWith("L") && type.endsWith(";"))
        type = type.replaceAll("^L", "").replaceAll(";$", "");

      int dest = ((OneRegisterInstruction) instruction).getRegisterA();
      Constant cst = ClassConstant.v(type);
      assign = Jimple.v().newAssignStmt(body.getRegisterLocal(dest), cst);
      setUnit(assign);
      addTags(assign);
      body.add(assign);

if (IDalvikTyper.ENABLE_DVKTYPER) {
	Debug.printDbg(IDalvikTyper.DEBUG, "constraint: "+ assign);
        int op = (int)instruction.getOpcode().value;
        //DalvikTyper.v().captureAssign((JAssignStmt)assign, op); //TODO: classtype could be null!
        DalvikTyper.v().setType(assign.getLeftOpBox(), cst.getType(), false);
      }
  }
 
Example 9
Source Project: JAADAS   Source File: IputInstruction.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void jimplify (DexBody body) {
      TwoRegisterInstruction i = (TwoRegisterInstruction)instruction;
      int source = i.getRegisterA();
      int object = i.getRegisterB();
      FieldReference f = (FieldReference)((ReferenceInstruction)instruction).getReference();
      InstanceFieldRef instanceField = Jimple.v().newInstanceFieldRef(body.getRegisterLocal(object),
                           getSootFieldRef(f));
      Local sourceValue = body.getRegisterLocal(source);
      assign = getAssignStmt(body, sourceValue, instanceField);
      setUnit(assign);
      addTags(assign);
      body.add(assign);

if (IDalvikTyper.ENABLE_DVKTYPER) {
	Debug.printDbg(IDalvikTyper.DEBUG, "constraint: "+ assign);
        int op = (int)instruction.getOpcode().value;
        DalvikTyper.v().setType(assign.getRightOpBox(), instanceField.getType(), true);
      }
  }
 
Example 10
/**
 * Determine if register is used as floating point.
 *
 * Abstraction for static and non-static methods. Non-static methods need to ignore the first parameter (this)
 * @param isStatic if this method is static
 */
protected boolean isUsedAsFloatingPoint(DexBody body, int register, boolean isStatic) {
    MethodReference item = (MethodReference) ((ReferenceInstruction) instruction).getReference();
    List<? extends CharSequence> paramTypes = item.getParameterTypes();
    List<Integer> regs = getUsedRegistersNums();
    if (paramTypes == null)
        return false;

    for (int i = 0, j = 0; i < regs.size(); i++, j++) {
        if (!isStatic && i == 0) {
            j--;
            continue;
        }

        if (regs.get(i) == register && isFloatLike(DexType.toSoot(paramTypes.get(j).toString())))
            return true;
        if (DexType.isWide(paramTypes.get(j).toString()))
            i++;
    }
    return false;
}
 
Example 11
/**
 * Determine if register is used as object.
 *
 * Abstraction for static and non-static methods. Non-static methods need to ignore the first parameter (this)
 * @param isStatic if this method is static
 */
protected boolean isUsedAsObject(DexBody body, int register, boolean isStatic) {
    MethodReference item = (MethodReference) ((ReferenceInstruction) instruction).getReference();
    List<? extends CharSequence> paramTypes = item.getParameterTypes();
    List<Integer> regs = getUsedRegistersNums();
    if (paramTypes == null)
        return false;

    // we call a method on the register
    if (!isStatic && regs.get(0) == register)
        return true;

    // we call a method with register as a reftype paramter
    for (int i = 0, j = 0; i < regs.size(); i++, j++) {
        if (!isStatic && i == 0) {
            j--;
            continue;
        }

        if (regs.get(i) == register && (DexType.toSoot(paramTypes.get(j).toString()) instanceof RefType))
            return true;
        if (DexType.isWide(paramTypes.get(j).toString()))
            i++;
    }
    return false;
}
 
Example 12
Source Project: HeyGirl   Source File: DexWriter.java    License: Apache License 2.0 6 votes vote down vote up
private void fixInstructions(@Nonnull MutableMethodImplementation methodImplementation) {
    List<? extends Instruction> instructions = methodImplementation.getInstructions();

    for (int i=0; i<instructions.size(); i++) {
        Instruction instruction = instructions.get(i);

        if (instruction.getOpcode() == Opcode.CONST_STRING) {
            if (stringSection.getItemIndex(
                    (StringRef)((ReferenceInstruction)instruction).getReference()) >= 65536) {
                methodImplementation.replaceInstruction(i, new BuilderInstruction31c(Opcode.CONST_STRING_JUMBO,
                        ((OneRegisterInstruction)instruction).getRegisterA(),
                        ((ReferenceInstruction)instruction).getReference()));
            }
        }
    }
}
 
Example 13
Source Project: HeyGirl   Source File: InstructionRewriter.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull @Override public Instruction rewrite(@Nonnull Instruction instruction) {
    if (instruction instanceof ReferenceInstruction) {
        switch (instruction.getOpcode().format) {
            case Format20bc:
                return new RewrittenInstruction20bc((Instruction20bc)instruction);
            case Format21c:
                return new RewrittenInstruction21c((Instruction21c)instruction);
            case Format22c:
                return new RewrittenInstruction22c((Instruction22c)instruction);
            case Format31c:
                return new RewrittenInstruction31c((Instruction31c)instruction);
            case Format35c:
                return new RewrittenInstruction35c((Instruction35c)instruction);
            case Format3rc:
                return new RewrittenInstruction3rc((Instruction3rc)instruction);
            default:
                throw new IllegalArgumentException();
        }
    }
    return instruction;
}
 
Example 14
Source Project: ZjDroid   Source File: DexWriter.java    License: Apache License 2.0 6 votes vote down vote up
private void fixInstructions(@Nonnull MutableMethodImplementation methodImplementation) {
    List<? extends Instruction> instructions = methodImplementation.getInstructions();

    for (int i=0; i<instructions.size(); i++) {
        Instruction instruction = instructions.get(i);

        if (instruction.getOpcode() == Opcode.CONST_STRING) {
            if (stringSection.getItemIndex(
                    (StringRef)((ReferenceInstruction)instruction).getReference()) >= 65536) {
                methodImplementation.replaceInstruction(i, new BuilderInstruction31c(Opcode.CONST_STRING_JUMBO,
                        ((OneRegisterInstruction)instruction).getRegisterA(),
                        ((ReferenceInstruction)instruction).getReference()));
            }
        }
    }
}
 
Example 15
Source Project: ZjDroid   Source File: InstructionRewriter.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull @Override public Instruction rewrite(@Nonnull Instruction instruction) {
    if (instruction instanceof ReferenceInstruction) {
        switch (instruction.getOpcode().format) {
            case Format20bc:
                return new RewrittenInstruction20bc((Instruction20bc)instruction);
            case Format21c:
                return new RewrittenInstruction21c((Instruction21c)instruction);
            case Format22c:
                return new RewrittenInstruction22c((Instruction22c)instruction);
            case Format31c:
                return new RewrittenInstruction31c((Instruction31c)instruction);
            case Format35c:
                return new RewrittenInstruction35c((Instruction35c)instruction);
            case Format3rc:
                return new RewrittenInstruction3rc((Instruction3rc)instruction);
            default:
                throw new IllegalArgumentException();
        }
    }
    return instruction;
}
 
Example 16
Source Project: ZjDroid   Source File: InstructionWriter.java    License: Apache License 2.0 5 votes vote down vote up
private int getReferenceIndex(ReferenceInstruction referenceInstruction) {
    switch (referenceInstruction.getOpcode().referenceType) {
        case ReferenceType.FIELD:
            return fieldSection.getItemIndex((FieldRefKey)referenceInstruction.getReference());
        case ReferenceType.METHOD:
            return methodSection.getItemIndex((MethodRefKey)referenceInstruction.getReference());
        case ReferenceType.STRING:
            return stringSection.getItemIndex((StringRef)referenceInstruction.getReference());
        case ReferenceType.TYPE:
            return typeSection.getItemIndex((TypeRef)referenceInstruction.getReference());
        default:
            throw new ExceptionWithContext("Unknown reference type: %d",
                    referenceInstruction.getOpcode().referenceType);
    }
}
 
Example 17
Source Project: zjdroid   Source File: InstructionWriter.java    License: Apache License 2.0 5 votes vote down vote up
private int getReferenceIndex(ReferenceInstruction referenceInstruction) {
    switch (referenceInstruction.getOpcode().referenceType) {
        case ReferenceType.FIELD:
            return fieldSection.getItemIndex((FieldRefKey)referenceInstruction.getReference());
        case ReferenceType.METHOD:
            return methodSection.getItemIndex((MethodRefKey)referenceInstruction.getReference());
        case ReferenceType.STRING:
            return stringSection.getItemIndex((StringRef)referenceInstruction.getReference());
        case ReferenceType.TYPE:
            return typeSection.getItemIndex((TypeRef)referenceInstruction.getReference());
        default:
            throw new ExceptionWithContext("Unknown reference type: %d",
                    referenceInstruction.getOpcode().referenceType);
    }
}
 
Example 18
Source Project: JAADAS   Source File: SgetInstruction.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void jimplify (DexBody body) {
      int dest = ((OneRegisterInstruction)instruction).getRegisterA();
      FieldReference f = (FieldReference)((ReferenceInstruction)instruction).getReference();
      StaticFieldRef r = Jimple.v().newStaticFieldRef(getStaticSootFieldRef(f));
      assign = Jimple.v().newAssignStmt(body.getRegisterLocal(dest), r);
      setUnit(assign);
      addTags(assign);
      body.add(assign);
      
if (IDalvikTyper.ENABLE_DVKTYPER) {
	Debug.printDbg(IDalvikTyper.DEBUG, "constraint: "+ assign);
        int op = (int)instruction.getOpcode().value;
        DalvikTyper.v().setType(assign.getLeftOpBox(), r.getType(), false);
      }
  }
 
Example 19
Source Project: JAADAS   Source File: FieldInstruction.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public Set<Type> introducedTypes() {
    Set<Type> types = new HashSet<Type>();
    // Aput instructions don't have references
    if (!(instruction instanceof ReferenceInstruction))
        return types;

    ReferenceInstruction i = (ReferenceInstruction) instruction;

    FieldReference field = (FieldReference) i.getReference();

    types.add(DexType.toSoot(field.getType()));
    types.add(DexType.toSoot(field.getDefiningClass()));
    return types;
}
 
Example 20
Source Project: JAADAS   Source File: InstanceOfInstruction.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public Set<Type> introducedTypes() {
    ReferenceInstruction i = (ReferenceInstruction) instruction;

    Set<Type> types = new HashSet<Type>();
    types.add(DexType.toSoot((TypeReference) i.getReference()));
    return types;
}
 
Example 21
@Override
public Set<Type> introducedTypes() {
    ReferenceInstruction i = (ReferenceInstruction) instruction;

    Set<Type> types = new HashSet<Type>();
    types.add(DexType.toSoot((TypeReference) i.getReference()));
    return types;
}
 
Example 22
@Override
public Set<Type> introducedTypes() {
    ReferenceInstruction i = (ReferenceInstruction) instruction;

    Set<Type> types = new HashSet<Type>();
    types.add(DexType.toSoot((TypeReference) i.getReference()));
    return types;
}
 
Example 23
Source Project: JAADAS   Source File: CheckCastInstruction.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public Set<Type> introducedTypes() {
    ReferenceInstruction i = (ReferenceInstruction) instruction;

    Set<Type> types = new HashSet<Type>();
    types.add(DexType.toSoot((TypeReference) i.getReference()));
    return types;
}
 
Example 24
Source Project: JAADAS   Source File: NewArrayInstruction.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public Set<Type> introducedTypes() {
    ReferenceInstruction i = (ReferenceInstruction) instruction;

    Set<Type> types = new HashSet<Type>();
    types.add(DexType.toSoot((TypeReference) i.getReference()));
    return types;
}
 
Example 25
Source Project: JAADAS   Source File: ConstClassInstruction.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public Set<Type> introducedTypes() {
    ReferenceInstruction i = (ReferenceInstruction) instruction;

    Set<Type> types = new HashSet<Type>();
    types.add(DexType.toSoot((TypeReference) i.getReference()));
    return types;
}
 
Example 26
public Set<Type> introducedTypes() {
    Set<Type> types = new HashSet<Type>();
    MethodReference method = (MethodReference) (((ReferenceInstruction) instruction).getReference());

    types.add(DexType.toSoot(method.getDefiningClass()));
    types.add(DexType.toSoot(method.getReturnType()));
    List<? extends CharSequence> paramTypes = method.getParameterTypes();
    if (paramTypes != null)
        for (CharSequence type : paramTypes)
            types.add(DexType.toSoot(type.toString()));

    return types;
}
 
Example 27
/**
 * Return the SootMethodRef for the invoked method.
 *
 * @param invType The invocation type
 */
private SootMethodRef getSootMethodRef(InvocationType invType) {
	if (methodRef != null)
		return methodRef;
	
    MethodReference mItem = (MethodReference) ((ReferenceInstruction) instruction).getReference();
    String tItem = mItem.getDefiningClass();

    String className = tItem;
    Debug.printDbg("tItem: ", tItem ," class name: ", className);
      if (className.startsWith("[")) {
        className = "java.lang.Object";
      } else {
        className = dottedClassName (tItem);
      }

    SootClass sc = SootResolver.v().makeClassRef(className);
    if (invType == InvocationType.Interface && sc.isPhantom())
    	sc.setModifiers(sc.getModifiers() | Modifier.INTERFACE);
    String methodName = mItem.getName();

    Type returnType = DexType.toSoot(mItem.getReturnType());
    List<Type> parameterTypes = new ArrayList<Type>();
    List<? extends CharSequence> paramTypes = mItem.getParameterTypes();
    if (paramTypes != null)
        for (CharSequence type : paramTypes)
            parameterTypes.add(DexType.toSoot(type.toString()));

    Debug.printDbg("sc: ", sc);
    Debug.printDbg("methodName: ", methodName);
    Debug.printDbg("parameterTypes: ");
    for (Type t: parameterTypes)
      Debug.printDbg(" t: ", t);
    Debug.printDbg("returnType: ", returnType);
    Debug.printDbg("isStatic: ", invType == InvocationType.Static);
    methodRef = Scene.v().makeMethodRef(sc, methodName, parameterTypes, returnType,
    		invType == InvocationType.Static);
    
    return methodRef;
}
 
Example 28
Source Project: HeyGirl   Source File: InstructionWriter.java    License: Apache License 2.0 5 votes vote down vote up
private int getReferenceIndex(ReferenceInstruction referenceInstruction) {
    switch (referenceInstruction.getOpcode().referenceType) {
        case ReferenceType.FIELD:
            return fieldSection.getItemIndex((FieldRefKey)referenceInstruction.getReference());
        case ReferenceType.METHOD:
            return methodSection.getItemIndex((MethodRefKey)referenceInstruction.getReference());
        case ReferenceType.STRING:
            return stringSection.getItemIndex((StringRef)referenceInstruction.getReference());
        case ReferenceType.TYPE:
            return typeSection.getItemIndex((TypeRef)referenceInstruction.getReference());
        default:
            throw new ExceptionWithContext("Unknown reference type: %d",
                    referenceInstruction.getOpcode().referenceType);
    }
}
 
Example 29
Source Project: ZjDroid   Source File: InstructionWriter.java    License: Apache License 2.0 5 votes vote down vote up
private int getReferenceIndex(ReferenceInstruction referenceInstruction) {
    switch (referenceInstruction.getOpcode().referenceType) {
        case ReferenceType.FIELD:
            return fieldSection.getItemIndex((FieldRefKey)referenceInstruction.getReference());
        case ReferenceType.METHOD:
            return methodSection.getItemIndex((MethodRefKey)referenceInstruction.getReference());
        case ReferenceType.STRING:
            return stringSection.getItemIndex((StringRef)referenceInstruction.getReference());
        case ReferenceType.TYPE:
            return typeSection.getItemIndex((TypeRef)referenceInstruction.getReference());
        default:
            throw new ExceptionWithContext("Unknown reference type: %d",
                    referenceInstruction.getOpcode().referenceType);
    }
}
 
Example 30
Source Project: ZjDroid   Source File: DexWriter.java    License: Apache License 2.0 4 votes vote down vote up
private void writeDebugAndCodeItems(@Nonnull DexDataWriter offsetWriter,
                                    @Nonnull DeferredOutputStream temp) throws IOException {
    ByteArrayOutputStream ehBuf = new ByteArrayOutputStream();
    debugSectionOffset = offsetWriter.getPosition();
    DebugWriter<StringKey, TypeKey> debugWriter =
            new DebugWriter<StringKey, TypeKey>(stringSection, typeSection, offsetWriter);

    DexDataWriter codeWriter = new DexDataWriter(temp, 0);

    List<CodeItemOffset<MethodKey>> codeOffsets = Lists.newArrayList();

    for (ClassKey classKey: classSection.getSortedClasses()) {
        Collection<? extends MethodKey> directMethods = classSection.getSortedDirectMethods(classKey);
        Collection<? extends MethodKey> virtualMethods = classSection.getSortedVirtualMethods(classKey);

        Iterable<MethodKey> methods = Iterables.concat(directMethods, virtualMethods);

        for (MethodKey methodKey: methods) {
            List<? extends TryBlock<? extends ExceptionHandler>> tryBlocks =
                    classSection.getTryBlocks(methodKey);
            Iterable<? extends Instruction> instructions = classSection.getInstructions(methodKey);
            Iterable<? extends DebugItem> debugItems = classSection.getDebugItems(methodKey);

            if (instructions != null && stringSection.hasJumboIndexes()) {
                boolean needsFix = false;
                for (Instruction instruction: instructions) {
                    if (instruction.getOpcode() == Opcode.CONST_STRING) {
                        if (stringSection.getItemIndex(
                                (StringRef)((ReferenceInstruction)instruction).getReference()) >= 65536) {
                            needsFix = true;
                            break;
                        }
                    }
                }

                if (needsFix) {
                    MutableMethodImplementation mutableMethodImplementation =
                            classSection.makeMutableMethodImplementation(methodKey);
                    fixInstructions(mutableMethodImplementation);

                    instructions = mutableMethodImplementation.getInstructions();
                    tryBlocks = mutableMethodImplementation.getTryBlocks();
                    debugItems = mutableMethodImplementation.getDebugItems();
                }
            }

            int debugItemOffset = writeDebugItem(offsetWriter, debugWriter,
                    classSection.getParameterNames(methodKey), debugItems);
            int codeItemOffset = writeCodeItem(codeWriter, ehBuf, methodKey, tryBlocks, instructions, debugItemOffset);

            if (codeItemOffset != -1) {
                codeOffsets.add(new CodeItemOffset<MethodKey>(methodKey, codeItemOffset));
            }
        }
    }

    offsetWriter.align();
    codeSectionOffset = offsetWriter.getPosition();

    codeWriter.close();
    temp.writeTo(offsetWriter);
    temp.close();

    for (CodeItemOffset<MethodKey> codeOffset: codeOffsets) {
        classSection.setCodeItemOffset(codeOffset.method, codeSectionOffset + codeOffset.codeOffset);
    }
}