Example #1
0
  /**
   * Processes the fields of the given class.
   *
   * @param cf {@code non-null;} class being translated
   * @param out {@code non-null;} output class
   */
  private static void processFields(DirectClassFile cf, ClassDefItem out) {
    CstType thisClass = cf.getThisClass();
    FieldList fields = cf.getFields();
    int sz = fields.size();

    for (int i = 0; i < sz; i++) {
      Field one = fields.get(i);
      try {
        CstFieldRef field = new CstFieldRef(thisClass, one.getNat());
        int accessFlags = one.getAccessFlags();

        if (AccessFlags.isStatic(accessFlags)) {
          TypedConstant constVal = one.getConstantValue();
          EncodedField fi = new EncodedField(field, accessFlags);
          if (constVal != null) {
            constVal = coerceConstant(constVal, field.getType());
          }
          out.addStaticField(fi, constVal);
        } else {
          EncodedField fi = new EncodedField(field, accessFlags);
          out.addInstanceField(fi);
        }

        Annotations annotations = AttributeTranslator.getAnnotations(one.getAttributes());
        if (annotations.size() != 0) {
          out.addFieldAnnotations(field, annotations);
        }
      } catch (RuntimeException ex) {
        String msg =
            "...while processing " + one.getName().toHuman() + " " + one.getDescriptor().toHuman();
        throw ExceptionWithContext.withContext(ex, msg);
      }
    }
  }
Example #2
0
    ClassDefItem toClassDefItem() {
      if (!declared) {
        throw new IllegalStateException(
            "Undeclared type "
                + type
                + " declares members: "
                + fields.keySet()
                + " "
                + methods.keySet());
      }

      DexOptions dexOptions = new DexOptions();
      dexOptions.targetApiLevel = DexFormat.API_NO_EXTENDED_OPCODES;

      CstType thisType = type.constant;

      ClassDefItem out =
          new ClassDefItem(
              thisType, flags, supertype.constant, interfaces.ropTypes, new CstString(sourceFile));

      for (MethodDeclaration method : methods.values()) {
        EncodedMethod encoded = method.toEncodedMethod(dexOptions);
        if (method.isDirect()) {
          out.addDirectMethod(encoded);
        } else {
          out.addVirtualMethod(encoded);
        }
      }
      for (FieldDeclaration field : fields.values()) {
        EncodedField encoded = field.toEncodedField();
        if (field.isStatic()) {
          out.addStaticField(encoded, Constants.getConstant(field.staticValue));
        } else {
          out.addInstanceField(encoded);
        }
      }

      return out;
    }