Java Code Examples for org.jf.dexlib2.builder.BuilderTryBlock

Following code examples demonstrate how to use org.jf.dexlib2.builder.BuilderTryBlockfrom android. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.jf.dexlib2.builder.BuilderTryBlockand various code implementation of this class.

    private void removeEmptyTryCatchBlocks() {
        

        ListIterator<BuilderTryBlock> iter = implementation.getTryBlocks().listIterator();
        TIntList removeIndexes = new TIntArrayList();
        while (iter.hasNext()) {
            int index = iter.nextIndex();
            BuilderTryBlock tryBlock = iter.next();
            MethodLocation start = getLocation(tryBlock.start);
            MethodLocation end = getLocation(tryBlock.end);
            if (start == null || end == null || start.getCodeAddress() == end.getCodeAddress()) {

                removeIndexes.add(index);

            }
        }

        ArrayList<BuilderTryBlock> tryBlocks = null;
        try {
            java.lang.reflect.Field f = implementation.getClass().getDeclaredField("tryBlocks");
            tryBlocks = (ArrayList<BuilderTryBlock>) f.get(implementation);
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
            e.printStackTrace();
        }

        removeIndexes.sort();
        removeIndexes.reverse();
        for (int index : removeIndexes.toArray()) {
            tryBlocks.remove(index);
        }
    } 


    private void removeEmptyTryCatchBlocks() {
        
        ArrayList<BuilderTryBlock> tryBlocks = null;
        try {
            java.lang.reflect.Field f = implementation.getClass().getDeclaredField("tryBlocks");
            tryBlocks = (ArrayList<BuilderTryBlock>) f.get(implementation);
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
            e.printStackTrace();
        }

        TIntList indexes = new TIntArrayList();
        for (BuilderTryBlock tryBlock : implementation.getTryBlocks()) {
            MethodLocation startLocation = tryBlock.start.getLocation();
            MethodLocation endLocation = tryBlock.end.getLocation();
            if (startLocation.getCodeAddress() == endLocation.getCodeAddress()) {

                indexes.add(tryBlocks.indexOf(tryBlock));

                List<Label> remove = new ArrayList<Label>();
                remove.add(tryBlock.start);
                remove.add(tryBlock.end);
                remove.add(tryBlock.exceptionHandler.getHandler());
                startLocation.getLabels().removeAll(remove);
            }
        }

        indexes.sort();
        indexes.reverse();
        for (int index : indexes.toArray()) {
            tryBlocks.remove(index);
        }
    } 

    private static BuilderTryBlock buildTryBlock(int startAddress, int codeUnits, List handlers) {
        BuilderTryBlock tryBlock = mock(BuilderTryBlock.class);
        when(tryBlock.getStartCodeAddress()).thenReturn(0);
        when(tryBlock.getCodeUnitCount()).thenReturn(10);
        when(tryBlock.getExceptionHandlers()).thenReturn(handlers);

        return tryBlock;
    } 

    public TryNode(BuilderTryBlock block) {
        this.start = block.start;
        this.end = block.end;
        this.exceptionHandlers = new ArrayList<>();
        this.exceptionHandlers.add(block.exceptionHandler);
    } 

    public static List<TryNode> getTryCatches(List<BuilderTryBlock> tryBlocks) {
        List<TryNode> nodes = new ArrayList<>();
        Map<TryNode, TryNode> maps = new HashMap<>();
        for (BuilderTryBlock tryBlock : tryBlocks) {
            TryNode node = new TryNode(tryBlock);
            if (maps.containsKey(node)) {
                maps.get(node).exceptionHandlers.add(tryBlock.exceptionHandler);
            } else {
                nodes.add(node);
                maps.put(node, node);
            }
        }
        return nodes;
    } 

    public ClassManager(File smaliPath, DexBuilder dexBuilder) throws IOException {
        smaliFileFactory = new SmaliFileFactory();
        Set<SmaliFile> smaliFiles = smaliFileFactory.getSmaliFiles(smaliPath);
        classNameToSmaliFile = new HashMap<String, SmaliFile>();
        for (SmaliFile smaliFile : smaliFiles) {
            classNameToSmaliFile.put(smaliFile.getClassName(), smaliFile);
        }
        this.dexBuilder = dexBuilder;
        classNameToClassDef = new HashMap<String, BuilderClassDef>();
        methodDescriptorToMethod = new HashMap<String, BuilderMethod>();
        methodDescriptorToParameterTypes = new HashMap<String, List<String>>();
        methodDescriptorToTryBlocks = new HashMap<String, List<BuilderTryBlock>>();
        classNameToFieldNameAndType = new HashMap<String, List<String>>();
    } 

    public List<BuilderTryBlock> getTryBlocks(String methodDescriptor) {
        dexifyClassIfNecessary(methodDescriptor);

        return methodDescriptorToTryBlocks.get(methodDescriptor);
    } 

    private static Set<Integer> getExceptionHandlerAddresses(ExecutionGraphManipulator manipulator) {
        int[] allAddresses = manipulator.getAddresses();
        Arrays.sort(allAddresses);
        int highestAddress = allAddresses[allAddresses.length - 1];
        Set<Integer> handlerAddresses = new HashSet<>();
        List<BuilderTryBlock> tryBlocks = manipulator.getTryBlocks();
        for (BuilderTryBlock tryBlock : tryBlocks) {
            List<? extends BuilderExceptionHandler> handlers = tryBlock.getExceptionHandlers();
            for (BuilderExceptionHandler handler : handlers) {
                int address = handler.getHandlerCodeAddress();
                BuilderInstruction instruction = manipulator.getInstruction(address);
                while (address < highestAddress) {
                    handlerAddresses.add(address);
                    address += instruction.getCodeUnits();
                    instruction = manipulator.getInstruction(address);
                    if (!instruction.getOpcode().canContinue()) {
                        break;
                    }
                }
            }
        }

        return handlerAddresses;
    } 

    public List<BuilderTryBlock> getTryBlocks() {
        MutableMethodImplementation implementation = (MutableMethodImplementation) method.getImplementation();
        if (implementation == null) {
            return new LinkedList<>();
        }

        return implementation.getTryBlocks();
    } 

Advertisement
Advertisement