Search in sources :

Example 1 with Field

use of org.jf.dexlib2.iface.Field in project atlas by alibaba.

the class ClassDefinition method writeInstanceFields.

private void writeInstanceFields(IndentingWriter writer, Set<String> staticFields) throws IOException {
    if (!fullMethod && !DexDiffInfo.addedClasses.contains(classDef)) {
        return;
    }
    boolean wroteHeader = false;
    Set<String> writtenFields = new HashSet<String>();
    Iterable<? extends Field> instanceFields;
    if (classDef instanceof DexBackedClassDef) {
        instanceFields = ((DexBackedClassDef) classDef).getInstanceFields(false);
    } else {
        instanceFields = classDef.getInstanceFields();
    }
    for (Field field : instanceFields) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# instance fields");
            wroteHeader = true;
        }
        writer.write('\n');
        IndentingWriter fieldWriter = writer;
        String fieldString = ReferenceUtil.getShortFieldDescriptor(field);
        if (!writtenFields.add(fieldString)) {
            writer.write("# duplicate field ignored\n");
            fieldWriter = new CommentingIndentingWriter(writer);
            System.err.println(String.format("Ignoring duplicate field: %s->%s", classDef.getType(), fieldString));
        } else if (staticFields.contains(fieldString)) {
            System.err.println(String.format("Duplicate static+instance field found: %s->%s", classDef.getType(), fieldString));
            System.err.println("You will need to rename one of these fields, including all references.");
            writer.write("# There is both a static and instance field with this signature.\n" + "# You will need to rename one of these fields, including all references.\n");
        }
        FieldDefinition.writeTo(options, fieldWriter, field, false);
    }
}
Also used : Field(org.jf.dexlib2.iface.Field) IndentingWriter(org.jf.util.IndentingWriter) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef) HashSet(java.util.HashSet)

Example 2 with Field

use of org.jf.dexlib2.iface.Field in project atlas by alibaba.

the class ClassDefinition method writeStaticFields.

private Set<String> writeStaticFields(IndentingWriter writer) throws IOException {
    if (!fullMethod && !DexDiffInfo.addedClasses.contains(classDef)) {
        return null;
    }
    boolean wroteHeader = false;
    Set<String> writtenFields = new HashSet<String>();
    Iterable<? extends Field> staticFields;
    if (classDef instanceof DexBackedClassDef) {
        staticFields = ((DexBackedClassDef) classDef).getStaticFields(false);
    } else {
        staticFields = classDef.getStaticFields();
    }
    for (Field field : staticFields) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# static fields");
            wroteHeader = true;
        }
        writer.write('\n');
        boolean setInStaticConstructor;
        IndentingWriter fieldWriter = writer;
        String fieldString = ReferenceUtil.getShortFieldDescriptor(field);
        if (!writtenFields.add(fieldString)) {
            writer.write("# duplicate field ignored\n");
            fieldWriter = new CommentingIndentingWriter(writer);
            System.err.println(String.format("Ignoring duplicate field: %s->%s", classDef.getType(), fieldString));
            setInStaticConstructor = false;
        } else {
            setInStaticConstructor = fieldsSetInStaticConstructor.contains(fieldString);
        }
        FieldDefinition.writeTo(options, fieldWriter, field, setInStaticConstructor);
    }
    return writtenFields;
}
Also used : Field(org.jf.dexlib2.iface.Field) IndentingWriter(org.jf.util.IndentingWriter) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef) HashSet(java.util.HashSet)

Example 3 with Field

use of org.jf.dexlib2.iface.Field in project atlas by alibaba.

the class FieldDefinition method writeTo.

public static void writeTo(baksmaliOptions options, IndentingWriter writer, Field field, boolean setInStaticConstructor) throws IOException {
    EncodedValue initialValue = field.getInitialValue();
    int accessFlags = field.getAccessFlags();
    if (setInStaticConstructor && AccessFlags.STATIC.isSet(accessFlags) && AccessFlags.FINAL.isSet(accessFlags) && initialValue != null) {
        if (!EncodedValueUtils.isDefaultValue(initialValue)) {
            writer.write("# The value of this static final field might be set in the static constructor\n");
        } else {
            // don't write out the default initial value for static final fields that get set in the static
            // constructor
            initialValue = null;
        }
    }
    writer.write(".field ");
    writeAccessFlags(writer, field.getAccessFlags());
    writer.write(field.getName());
    writer.write(':');
    String clazz = TypeGenUtil.newType(field.getType());
    writer.write(clazz);
    if (initialValue != null) {
        writer.write(" = ");
        String containingClass = null;
        if (options.useImplicitReferences) {
            containingClass = field.getDefiningClass();
        }
        EncodedValueAdaptor.writeTo(writer, initialValue, containingClass);
    }
    writer.write('\n');
    Collection<? extends Annotation> annotations = field.getAnnotations();
    if (annotations.size() > 0) {
        writer.indent(4);
        String containingClass = null;
        if (options.useImplicitReferences) {
            containingClass = field.getDefiningClass();
        }
        AnnotationFormatter.writeTo(writer, annotations, containingClass);
        writer.deindent(4);
        writer.write(".end field\n");
    }
}
Also used : EncodedValue(org.jf.dexlib2.iface.value.EncodedValue)

Example 4 with Field

use of org.jf.dexlib2.iface.Field in project atlas by alibaba.

the class AbIClassDef method reClassDef.

@Override
public ClassDef reClassDef(ClassDef classDef) {
    Iterable<? extends Method> methods = classDef.getMethods();
    LinkedHashSet<Method> newMethods = new LinkedHashSet<Method>();
    Iterable<? extends Field> fields = classDef.getFields();
    LinkedHashSet<Field> newFields = new LinkedHashSet<Field>();
    Set<? extends Annotation> annotations = classDef.getAnnotations();
    Set<String> interfaces = classDef.getInterfaces();
    Set<String> newInterfaces = new HashSet<String>();
    Set<Annotation> immutableAnnotations = new HashSet<Annotation>();
    String type = classDef.getType();
    reType = reType(type);
    String superClass = classDef.getSuperclass();
    for (String inter : interfaces) {
        newInterfaces.add(reInterface(inter));
    }
    String reSuperClass = reSuperClass(superClass);
    for (Annotation annotation : annotations) {
        if (type.startsWith("Landroid/taobao/atlas/bundleInfo/AtlasBundleInfoManager;")) {
            System.out.println("xxxx");
        }
        immutableAnnotations.add(reAnnotation(annotation));
    }
    for (Field field : fields) {
        newFields.add(reField(field));
    }
    for (Method method : methods) {
        if (method.getName().equals("<cinit>") || method.getName().equals("<init>")) {
            newMethods.add(reMethod(method));
            continue;
        }
        //            if (method.getName().equals("getArchiveFile")) {
        newMethods.add(reMethod(method));
    //            }
    }
    return new ImmutableClassDef(reType, classDef.getAccessFlags(), reSuperClass, newInterfaces, classDef.getSourceFile(), immutableAnnotations, newFields, newMethods);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) Method(org.jf.dexlib2.iface.Method) Annotation(org.jf.dexlib2.iface.Annotation) Field(org.jf.dexlib2.iface.Field) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 5 with Field

use of org.jf.dexlib2.iface.Field in project android-classyshark by google.

the class MetaObjectDex method getDeclaredFields.

@Override
public FieldInfo[] getDeclaredFields() {
    List<FieldInfo> result = new ArrayList<>();
    Iterable<? extends Field> implFields = classDef.getFields();
    for (Field field : implFields) {
        FieldInfo fi = new FieldInfo();
        fi.typeName = DexlibAdapter.getTypeName(field.getType());
        fi.modifiers = field.getAccessFlags();
        fi.annotations = convertAnnotations(field.getAnnotations());
        fi.name = field.getName();
        result.add(fi);
    }
    FieldInfo[] array = new FieldInfo[result.size()];
    return result.toArray(array);
}
Also used : Field(org.jf.dexlib2.iface.Field) ArrayList(java.util.ArrayList)

Aggregations

Field (org.jf.dexlib2.iface.Field)6 ClassDef (org.jf.dexlib2.iface.ClassDef)5 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)4 FieldReference (org.jf.dexlib2.iface.reference.FieldReference)4 IndentingWriter (org.jf.util.IndentingWriter)4 Test (org.junit.Test)4 HashSet (java.util.HashSet)3 EncodedValue (org.jf.dexlib2.iface.value.EncodedValue)3 ExceptionWithContext (org.jf.util.ExceptionWithContext)3 Opcode (org.jf.dexlib2.Opcode)2 Method (org.jf.dexlib2.iface.Method)2 ImmutableFieldReference (org.jf.dexlib2.immutable.reference.ImmutableFieldReference)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 ClassProto (org.jf.dexlib2.analysis.ClassProto)1 DexReader (org.jf.dexlib2.dexbacked.DexReader)1 DexBackedInstruction (org.jf.dexlib2.dexbacked.instruction.DexBackedInstruction)1