Package jadx.core.dex.attributes.nodes

Examples of jadx.core.dex.attributes.nodes.EnumClassAttr


          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;
View Full Code Here


    }
    return false;
  }

  private void addEnumFields(CodeWriter code) throws CodegenException {
    EnumClassAttr enumFields = cls.get(AType.ENUM_CLASS);
    if (enumFields == null) {
      return;
    }
    InsnGen igen = null;
    for (Iterator<EnumField> it = enumFields.getFields().iterator(); it.hasNext(); ) {
      EnumField f = it.next();
      code.startLine(f.getName());
      if (!f.getArgs().isEmpty()) {
        code.add('(');
        for (Iterator<InsnArg> aIt = f.getArgs().iterator(); aIt.hasNext(); ) {
          InsnArg arg = aIt.next();
          if (igen == null) {
            // don't init mth gen if this is simple enum
            MethodGen mthGen = new MethodGen(this, enumFields.getStaticMethod());
            igen = new InsnGen(mthGen, false);
          }
          igen.addArg(code, arg);
          if (aIt.hasNext()) {
            code.add(", ");
          }
        }
        code.add(')');
      }
      if (f.getCls() != null) {
        code.add(' ');
        new ClassGen(f.getCls(), this, fallback).addClassBody(code);
      }
      if (it.hasNext()) {
        code.add(',');
      }
    }
    if (isMethodsPresents() || isFieldsPresents() || isInnerClassesPresents()) {
      if (enumFields.getFields().isEmpty()) {
        code.startLine();
      }
      code.add(';');
    }
  }
View Full Code Here

TOP

Related Classes of jadx.core.dex.attributes.nodes.EnumClassAttr

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.