All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.reandroid.dex.data.CodeItem Maven / Gradle / Ivy

/*
 *  Copyright (C) 2022 github.com/REAndroid
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.reandroid.dex.data;

import com.reandroid.arsc.base.Block;
import com.reandroid.arsc.base.BlockRefresh;
import com.reandroid.arsc.item.IndirectInteger;
import com.reandroid.arsc.io.BlockReader;
import com.reandroid.arsc.item.IntegerReference;
import com.reandroid.dex.base.*;
import com.reandroid.dex.common.SectionItem;
import com.reandroid.dex.debug.DebugElement;
import com.reandroid.dex.id.IdItem;
import com.reandroid.dex.ins.ExtraLine;
import com.reandroid.dex.ins.Label;
import com.reandroid.dex.key.DataKey;
import com.reandroid.dex.key.Key;
import com.reandroid.dex.key.ModifiableKeyItem;
import com.reandroid.dex.reference.DataItemIndirectReference;
import com.reandroid.dex.common.RegistersTable;
import com.reandroid.dex.ins.TryBlock;
import com.reandroid.dex.sections.SectionType;
import com.reandroid.dex.smali.SmaliDirective;
import com.reandroid.dex.smali.SmaliFormat;
import com.reandroid.dex.smali.SmaliWriter;
import com.reandroid.dex.smali.model.SmaliCodeTryItem;
import com.reandroid.dex.smali.model.SmaliMethod;
import com.reandroid.utils.ObjectsUtil;
import com.reandroid.utils.collection.CombiningIterator;
import com.reandroid.utils.collection.EmptyIterator;

import java.io.IOException;
import java.util.Iterator;

public class CodeItem extends DataItem implements RegistersTable, PositionAlignedItem, ModifiableKeyItem,
        SmaliFormat {

    private final Header header;
    private final InstructionList instructionList;
    private TryBlock tryBlock;

    private final DataKey codeItemKey;

    private MethodDef methodDef;

    public CodeItem() {
        super(3);
        this.header = new Header(this);
        this.instructionList = new InstructionList(this);
        this.tryBlock = null;

        this.codeItemKey = new DataKey<>(this);

        addChild(0, header);
        addChild(1, instructionList);
    }

    @Override
    public DataKey getKey() {
        return codeItemKey;
    }
    @SuppressWarnings("unchecked")
    @Override
    public void setKey(Key key){
        DataKey codeItemKey = (DataKey) key;
        merge(codeItemKey.getItem());
    }
    @Override
    public SectionType getSectionType() {
        return SectionType.CODE;
    }

    @Override
    public int getRegistersCount(){
        return header.registersCount.get();
    }
    @Override
    public void setRegistersCount(int count){
        header.registersCount.set(count);
    }
    @Override
    public int getParameterRegistersCount(){
        return header.parameterRegisters.get();
    }
    @Override
    public void setParameterRegistersCount(int count){
        header.parameterRegisters.set(count);
    }
    @Override
    public boolean ensureLocalRegistersCount(int locals){
        if(locals == 0){
            return true;
        }
        if(locals <= getLocalRegistersCount()){
            return true;
        }
        int params = getParameterRegistersCount();
        int current = getLocalRegistersCount();
        int diff = locals - current;
        InstructionList instructionList = getInstructionList();
        if(!instructionList.canAddLocalRegisters(diff)) {
            return false;
        }
        if(diff > 0) {
            instructionList.addLocalRegisters(diff);
        }
        setRegistersCount(locals + params);
        return true;
    }


    public Iterator getDebugLabels() {
        DebugInfo debugInfo = getDebugInfo();
        if(debugInfo != null) {
            return debugInfo.getExtraLines();
        }
        return EmptyIterator.of();
    }
    public DebugInfo getDebugInfo(){
        return header.debugInfoOffset.getItem();
    }
    public DebugInfo getOrCreateDebugInfo(){
        return header.debugInfoOffset.getOrCreateUniqueItem(this);
    }
    public void removeDebugInfo(){
        if(getDebugInfo() == null){
            return;
        }
        setDebugInfo(null);
    }
    public void setDebugInfo(DebugInfo debugInfo){
        header.debugInfoOffset.setItem(debugInfo);
    }
    public InstructionList getInstructionList() {
        return instructionList;
    }
    public IntegerReference getTryCountReference(){
        return header.tryBlockCount;
    }

    public Iterable getExtraLines() {
        return () -> CombiningIterator.two(
                CodeItem.this.getTryBlockLabels(),
                CodeItem.this.getDebugLabels());
    }
    public Iterator




© 2015 - 2025 Weber Informatics LLC | Privacy Policy