Package jadx.core.dex.trycatch

Examples of jadx.core.dex.trycatch.TryCatchBlock


    int hc = 0;
    Set<Integer> addrs = new HashSet<Integer>();
    List<TryCatchBlock> catches = new ArrayList<TryCatchBlock>(catchBlocks.length);

    for (CatchHandler handler : catchBlocks) {
      TryCatchBlock tcBlock = new TryCatchBlock();
      catches.add(tcBlock);
      for (int i = 0; i < handler.getAddresses().length; i++) {
        int addr = handler.getAddresses()[i];
        ClassInfo type = ClassInfo.fromDex(parentClass.dex(), handler.getTypeIndexes()[i]);
        tcBlock.addHandler(this, addr, type);
        addrs.add(addr);
        hc++;
      }
      int addr = handler.getCatchAllAddress();
      if (addr >= 0) {
        tcBlock.addHandler(this, addr, null);
        addrs.add(addr);
        hc++;
      }
    }

    if (hc > 0 && hc != addrs.size()) {
      // resolve nested try blocks:
      // inner block contains all handlers from outer block => remove these handlers from inner block
      // each handler must be only in one try/catch block
      for (TryCatchBlock ct1 : catches) {
        for (TryCatchBlock ct2 : catches) {
          if (ct1 != ct2 && ct2.containsAllHandlers(ct1)) {
            for (ExceptionHandler h : ct1.getHandlers()) {
              ct2.removeHandler(this, h);
              h.setTryBlock(ct1);
            }
          }
        }
      }
    }

    // attach EXC_HANDLER attributes to instructions
    addrs.clear();
    for (TryCatchBlock ct : catches) {
      for (ExceptionHandler eh : ct.getHandlers()) {
        int addr = eh.getHandleOffset();
        ExcHandlerAttr ehAttr = new ExcHandlerAttr(ct, eh);
        insnByOffset[addr].addAttr(ehAttr);
      }
    }

    // attach TRY_ENTER, TRY_LEAVE attributes to instructions
    for (Try aTry : tries) {
      int catchNum = aTry.getCatchHandlerIndex();
      TryCatchBlock catchBlock = catches.get(catchNum);
      int offset = aTry.getStartAddress();
      int end = offset + aTry.getInstructionCount() - 1;

      InsnNode insn = insnByOffset[offset];
      insn.add(AFlag.TRY_ENTER);
      while (offset <= end && offset >= 0) {
        insn = insnByOffset[offset];
        catchBlock.addInsn(insn);
        offset = InsnDecoder.getNextInsnOffset(insnByOffset, offset);
      }
      if (insnByOffset[end] != null) {
        insnByOffset[end].add(AFlag.TRY_LEAVE);
      } else {
View Full Code Here


            "Exception block dominator not found, method:" + mth + ". bs: " + bs);
      }

      BlockNode domBlock = domBlocks.get(0);

      TryCatchBlock prevTB = tryBlocksMap.put(domBlock, tb);
      if (prevTB != null) {
        LOG.info("!!! TODO: merge try blocks in {}", mth);
      }
    }
  }
View Full Code Here

  private static void checkAndWrap(MethodNode mth, Map<BlockNode, TryCatchBlock> tryBlocksMap, IRegion region) {
    // search dominator blocks in this region (don't need to go deeper)
    for (Map.Entry<BlockNode, TryCatchBlock> entry : tryBlocksMap.entrySet()) {
      BlockNode dominator = entry.getKey();
      if (region.getSubBlocks().contains(dominator)) {
        TryCatchBlock tb = tryBlocksMap.get(dominator);
        if (!wrapBlocks(region, tb, dominator)) {
          ErrorsCounter.methodError(mth, "Can't wrap try/catch for " + region);
        }
        tryBlocksMap.remove(dominator);
        return;
View Full Code Here

          for (InsnNode insn : excBlock.getInstructions()) {
            if (insn.getType() == InsnType.THROW) {
              CatchAttr catchAttr = insn.get(AType.CATCH_BLOCK);
              if (catchAttr != null) {
                TryCatchBlock handlerBlock = handlerAttr.getTryBlock();
                TryCatchBlock catchBlock = catchAttr.getTryBlock();
                if (handlerBlock != catchBlock) {
                  handlerBlock.merge(mth, catchBlock);
                  catchBlock.removeInsn(insn);
                }
              }
            }
          }
        }
View Full Code Here

      // process sub blocks
      for (IContainer b : r.getSubBlocks()) {
        // process try block
        CatchAttr cb = b.get(AType.CATCH_BLOCK);
        if (cb != null && (b instanceof IRegion)) {
          TryCatchBlock tb = cb.getTryBlock();
          for (ExceptionHandler eh : tb.getHandlers()) {
            if (isRegionContainsRegion(eh.getHandlerRegion(), region)) {
              return true;
            }
          }
          if (tb.getFinalRegion() != null
              && isRegionContainsRegion(tb.getFinalRegion(), region)) {
            return true;
          }
        }
        if (isRegionContainsRegion(b, region)) {
          return true;
View Full Code Here

      code.startLine().addIndent().add("break;");
    }
  }

  private void makeTryCatch(TryCatchRegion region, CodeWriter code) throws CodegenException {
    TryCatchBlock tryCatchBlock = region.geTryCatchBlock();
    code.startLine("try {");
    makeRegionIndent(code, region.getTryRegion());
    // TODO: move search of 'allHandler' to 'TryCatchRegion'
    ExceptionHandler allHandler = null;
    for (ExceptionHandler handler : tryCatchBlock.getHandlers()) {
      if (!handler.isCatchAll()) {
        makeCatchBlock(code, handler);
      } else {
        if (allHandler != null) {
          LOG.warn("Several 'all' handlers in try/catch block in {}", mth);
        }
        allHandler = handler;
      }
    }
    if (allHandler != null) {
      makeCatchBlock(code, allHandler);
    }
    if (tryCatchBlock.getFinalRegion() != null) {
      code.startLine("} finally {");
      makeRegionIndent(code, tryCatchBlock.getFinalRegion());
    }
    code.startLine('}');
  }
View Full Code Here

        // if 'throw' in exception handler block have 'catch' - merge these catch blocks
        for (InsnNode insn : excBlock.getInstructions()) {
          if (insn.getType() == InsnType.THROW) {
            CatchAttr catchAttr = insn.get(AType.CATCH_BLOCK);
            if (catchAttr != null) {
              TryCatchBlock handlerBlock = handlerAttr.getTryBlock();
              TryCatchBlock catchBlock = catchAttr.getTryBlock();
              if (handlerBlock != catchBlock) { // TODO: why it can be?
                handlerBlock.merge(mth, catchBlock);
                catchBlock.removeInsn(insn);
              }
            }
          }
        }
      }
View Full Code Here

  }

  public static List<IContainer> getExcHandlersForRegion(IContainer region) {
    CatchAttr cb = region.get(AType.CATCH_BLOCK);
    if (cb != null) {
      TryCatchBlock tb = cb.getTryBlock();
      List<IContainer> list = new ArrayList<IContainer>(tb.getHandlersCount());
      for (ExceptionHandler eh : tb.getHandlers()) {
        list.add(eh.getHandlerRegion());
      }
      return list;
    }
    return Collections.emptyList();
View Full Code Here

TOP

Related Classes of jadx.core.dex.trycatch.TryCatchBlock

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.