Package jadx.core.dex.nodes

Examples of jadx.core.dex.nodes.MethodNode


      // check argument types for overloaded methods
      case INVOKE: {
        boolean change = false;
        InvokeNode inv = (InvokeNode) insn;
        MethodInfo callMth = inv.getCallMth();
        MethodNode node = mth.dex().resolveMethod(callMth);
        if (node != null && node.isArgsOverload()) {
          List<ArgType> args = callMth.getArgumentsTypes();
          int j = inv.getArgsCount() - 1;
          for (int i = args.size() - 1; i >= 0; i--) {
            ArgType argType = args.get(i);
            InsnArg insnArg = inv.getArg(j--);
View Full Code Here


      } else if (f.getAccessFlags().isSynthetic()) {
        f.add(AFlag.DONT_GENERATE);
      }
    }

    MethodNode staticMethod = null;

    ArgType clsType = cls.getClassInfo().getType();
    String enumConstructor = "<init>(Ljava/lang/String;I)V";
    String valuesOfMethod = "valueOf(Ljava/lang/String;)" + TypeGen.signature(clsType);
    String valuesMethod = "values()" + TypeGen.signature(ArgType.array(clsType));

    // remove synthetic methods
    for (Iterator<MethodNode> it = cls.getMethods().iterator(); it.hasNext(); ) {
      MethodNode mth = it.next();
      MethodInfo mi = mth.getMethodInfo();
      if (mi.isClassInit()) {
        staticMethod = mth;
      } else {
        String shortId = mi.getShortId();
        boolean isSynthetic = mth.getAccessFlags().isSynthetic();
        if (mi.isConstructor() && !isSynthetic) {
          if (shortId.equals(enumConstructor)) {
            it.remove();
          }
        } else if (isSynthetic
            || shortId.equals(valuesMethod)
            || shortId.equals(valuesOfMethod)) {
          it.remove();
        }
      }
    }

    EnumClassAttr attr = new EnumClassAttr(enumFields.size());
    cls.addAttr(attr);

    if (staticMethod == null) {
      ErrorsCounter.classError(cls, "Enum class init method not found");
      // for this broken enum puts found fields and mark as inconsistent
      for (FieldNode field : enumFields) {
        attr.getFields().add(new EnumField(field.getName(), 0));
      }
      return false;
    }
    attr.setStaticMethod(staticMethod);

    // move enum specific instruction from static method to separate list
    BlockNode staticBlock = staticMethod.getBasicBlocks().get(0);
    List<InsnNode> insns = new ArrayList<InsnNode>();
    List<InsnNode> list = staticBlock.getInstructions();
    int size = list.size();
    for (int i = 0; i < size; i++) {
      InsnNode insn = list.get(i);
      insns.add(insn);
      if (insn.getType() == InsnType.SPUT) {
        IndexInsnNode fp = (IndexInsnNode) insn;
        FieldInfo f = (FieldInfo) fp.getIndex();
        if (f.getName().equals("$VALUES")) {
          if (i == size - 1) {
            cls.getMethods().remove(staticMethod);
          } else {
            list.subList(0, i + 1).clear();
          }
          break;
        }
      }
    }

    for (InsnNode insn : insns) {
      if (insn.getType() == InsnType.CONSTRUCTOR) {
        ConstructorInsn co = (ConstructorInsn) insn;
        if (insn.getArgsCount() < 2) {
          continue;
        }
        ClassInfo clsInfo = co.getClassType();
        ClassNode constrCls = cls.dex().resolveClass(clsInfo);
        if (constrCls == null) {
          continue;
        }
        if (!clsInfo.equals(cls.getClassInfo()) && !constrCls.getAccessFlags().isEnum()) {
          continue;
        }
        RegisterArg nameArg = (RegisterArg) insn.getArg(0);
        // InsnArg pos = insn.getArg(1);
        // TODO add check: pos == j
        String name = (String) nameArg.getConstValue(cls.dex());
        if (name == null) {
          throw new JadxException("Unknown enum field name: " + cls);
        }
        EnumField field = new EnumField(name, insn.getArgsCount() - 2);
        attr.getFields().add(field);
        for (int i = 2; i < insn.getArgsCount(); i++) {
          InsnArg constrArg;
          InsnArg iArg = insn.getArg(i);
          if (iArg.isLiteral()) {
            constrArg = iArg;
          } else {
            constrArg = CodeShrinker.inlineArgument(staticMethod, (RegisterArg) iArg);
            if (constrArg == null) {
              throw new JadxException("Can't inline constructor arg in enum: " + cls);
            }
          }
          field.getArgs().add(constrArg);
        }

        if (co.getClassType() != cls.getClassInfo()) {
          // enum contains additional methods
          for (ClassNode innerCls : cls.getInnerClasses()) {
            if (innerCls.getClassInfo().equals(co.getClassType())) {
              // remove constructor, because it is anonymous class
              for (Iterator<?> mit = innerCls.getMethods().iterator(); mit.hasNext(); ) {
                MethodNode innerMth = (MethodNode) mit.next();
                if (innerMth.getAccessFlags().isConstructor()) {
                  mit.remove();
                }
              }
              field.setCls(innerCls);
              innerCls.add(AFlag.DONT_GENERATE);
View Full Code Here

    }
    return true;
  }

  private static void checkFieldsInit(ClassNode cls) {
    MethodNode clinit = cls.searchMethodByName("<clinit>()V");
    if (clinit == null
        || !clinit.getAccessFlags().isStatic()
        || clinit.isNoCode()) {
      return;
    }

    for (BlockNode block : clinit.getBasicBlocks()) {
      for (InsnNode insn : block.getInstructions()) {
        if (insn.getType() == InsnType.SPUT) {
          processStaticFieldAssign(cls, (IndexInsnNode) insn);
        }
      }
View Full Code Here

      FieldNode f = cls.searchFieldById(section.readInt());
      f.addAttr(readAnnotationSet(section.readInt()));
    }

    for (int i = 0; i < annotatedMethodsCount; i++) {
      MethodNode m = cls.searchMethodById(section.readInt());
      m.addAttr(readAnnotationSet(section.readInt()));
    }

    for (int i = 0; i < annotatedParametersCount; i++) {
      MethodNode mth = cls.searchMethodById(section.readInt());
      // read annotation ref list
      Section ss = dex.openSection(section.readInt());
      int size = ss.readInt();
      MethodParameters params = new MethodParameters(size);
      for (int j = 0; j < size; j++) {
        params.getParamList().add(readAnnotationSet(ss.readInt()));
      }
      mth.addAttr(params);
    }
  }
View Full Code Here

      return mapAttr.getMap(field);
    }
    mapAttr = new EnumMapAttr();
    syntheticClass.addAttr(mapAttr);

    MethodNode clsInitMth = syntheticClass.searchMethodByName("<clinit>()V");
    if (clsInitMth == null || clsInitMth.isNoCode()) {
      return null;
    }
    if (clsInitMth.getBasicBlocks() == null) {
      try {
        clsInitMth.load();
      } catch (DecodeException e) {
        LOG.error("Load failed", e);
        return null;
      }
      if (clsInitMth.getBasicBlocks() == null) {
        // TODO:
        return null;
      }
    }
    for (BlockNode block : clsInitMth.getBasicBlocks()) {
      for (InsnNode insn : block.getInstructions()) {
        if (insn.getType() == InsnType.APUT) {
          addToEnumMap(mth, mapAttr, insn);
        }
      }
View Full Code Here

  }

  @Test
  public void test() {
    ClassNode cls = getClassNode(TestCls.class);
    MethodNode mth = getMethod(cls, "method");

    String code = cls.getCode().toString();
    assertThat(code, containsString("return (int[]) o;"));

    List<InsnNode> insns = mth.getBasicBlocks().get(1).getInstructions();
    assertEquals(insns.size(), 1);
    InsnNode insnNode = insns.get(0);
    assertEquals(InsnType.RETURN, insnNode.getType());
    assertTrue(insnNode.getArg(0).isInsnWrap());
    InsnNode wrapInsn = ((InsnWrapArg) insnNode.getArg(0)).getWrapInsn();
View Full Code Here

  public void test() {
    ClassNode cls = getClassNode(TestCls.class);
    String code = cls.getCode().toString();

    FieldNode field = cls.searchFieldByName("field");
    MethodNode func = cls.searchMethodByName("func()V");
    ClassNode inner = cls.getInnerClasses().get(0);
    MethodNode innerFunc = inner.searchMethodByName("innerFunc()V");
    MethodNode innerFunc2 = inner.searchMethodByName("innerFunc2()V");
    MethodNode innerFunc3 = inner.searchMethodByName("innerFunc3()V");
    FieldNode innerField = inner.searchFieldByName("innerField");

    // check source lines (available only for instructions and methods)
    int testClassLine = 18;
    assertEquals(testClassLine + 3, func.getSourceLine());
    assertEquals(testClassLine + 9, innerFunc.getSourceLine());
    assertEquals(testClassLine + 12, innerFunc2.getSourceLine());
    assertEquals(testClassLine + 20, innerFunc3.getSourceLine());

    // check decompiled lines
    String[] lines = code.split(CodeWriter.NL);
    checkLine(lines, field, "int field;");
    checkLine(lines, func, "public void func() {");
View Full Code Here

      ConstructorInsn co = new ConstructorInsn(mth, inv);
      boolean remove = false;
      if (co.isSuper() && (co.getArgsCount() == 0 || parentClass.isEnum())) {
        remove = true;
      } else if (co.isThis() && co.getArgsCount() == 0) {
        MethodNode defCo = parentClass.searchMethodByName(callMth.getShortId());
        if (defCo == null || defCo.isNoCode()) {
          // default constructor not implemented
          remove = true;
        }
      }
      // remove super() call in instance initializer
View Full Code Here

  /**
   * Replace call of synthetic constructor
   */
  private static ConstructorInsn processConstructor(MethodNode mth, ConstructorInsn co) {
    MethodNode callMth = mth.dex().resolveMethod(co.getCallMth());
    if (callMth == null
        || !callMth.getAccessFlags().isSynthetic()
        || !allArgsNull(co)) {
      return null;
    }
    ClassNode classNode = mth.dex().resolveClass(callMth.getParentClass().getClassInfo());
    if (classNode == null) {
      return null;
    }
    boolean passThis = co.getArgsCount() >= 1 && co.getArg(0).isThis();
    String ctrId = "<init>(" + (passThis ? TypeGen.signature(co.getArg(0).getType()) : "") + ")V";
    MethodNode defCtr = classNode.searchMethodByName(ctrId);
    if (defCtr == null) {
      return null;
    }
    ConstructorInsn newInsn = new ConstructorInsn(defCtr.getMethodInfo(), co.getCallType(), co.getInstanceArg());
    newInsn.setResult(co.getResult());
    return newInsn;
  }
View Full Code Here

        parent = cls.getInterfaces().get(0);
      } else {
        parent = cls.getSuperClass();
      }
      cls.add(AFlag.DONT_GENERATE);
      MethodNode defCtr = cls.getDefaultConstructor();
      if (defCtr != null) {
        if (RegionUtils.notEmpty(defCtr.getRegion())) {
          defCtr.add(AFlag.ANONYMOUS_CONSTRUCTOR);
        } else {
          defCtr.add(AFlag.DONT_GENERATE);
        }
      }
      code.add("new ");
      if (parent == null) {
        code.add("Object");
View Full Code Here

TOP

Related Classes of jadx.core.dex.nodes.MethodNode

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.