org.jf.dexlib2.iface.value.EncodedValue Java Examples

The following examples show how to use org.jf.dexlib2.iface.value.EncodedValue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ArrayEncodedValueAdaptor.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
public static void writeTo(IndentingWriter writer, ArrayEncodedValue arrayEncodedValue) throws IOException {
    writer.write('{');
    Collection<? extends EncodedValue> values = arrayEncodedValue.getValue();
    if (values.size() == 0) {
        writer.write('}');
        return;
    }

    writer.write('\n');
    writer.indent(4);
    boolean first = true;
    for (EncodedValue encodedValue: values) {
        if (!first) {
            writer.write(",\n");
        }
        first = false;

        EncodedValueAdaptor.writeTo(writer, encodedValue);
    }
    writer.deindent(4);
    writer.write("\n}");
}
 
Example #2
Source File: ClassPool.java    From zjdroid with Apache License 2.0 6 votes vote down vote up
@Nullable @Override public Collection<? extends EncodedValue> getStaticInitializers(
        @Nonnull PoolClassDef classDef) {
    final SortedSet<Field> sortedStaticFields = classDef.getStaticFields();

    final int lastIndex = CollectionUtils.lastIndexOf(sortedStaticFields, HAS_INITIALIZER);
    if (lastIndex > -1) {
        return new AbstractCollection<EncodedValue>() {
            @Nonnull @Override public Iterator<EncodedValue> iterator() {
                return FluentIterable.from(sortedStaticFields)
                        .limit(lastIndex+1)
                        .transform(GET_INITIAL_VALUE).iterator();
            }

            @Override public int size() {
                return lastIndex+1;
            }
        };
    }
    return null;
}
 
Example #3
Source File: DexBackedArrayEncodedValue.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public List<? extends EncodedValue> getValue() {
    return new VariableSizeList<EncodedValue>(dexFile, encodedArrayOffset, elementCount) {
        @Nonnull
        @Override
        protected EncodedValue readNextItem(@Nonnull DexReader dexReader, int index) {
            return DexBackedEncodedValue.readFrom(dexReader);
        }
    };
}
 
Example #4
Source File: StaticInitialValueIterator.java    From zjdroid with Apache License 2.0 5 votes vote down vote up
@Nullable
public EncodedValue getNextOrNull() {
    if (index < size) {
        index++;
        return DexBackedEncodedValue.readFrom(reader);
    }
    return null;
}
 
Example #5
Source File: DexField.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Add constant tag. Should only be called if field is final.
 * @param df
 * @param sf
 */
private static void addConstantTag(SootField df, Field sf) {
    Tag tag = null;

    EncodedValue ev = sf.getInitialValue();

    if (ev instanceof BooleanEncodedValue) {
      tag = new IntegerConstantValueTag(((BooleanEncodedValue) ev).getValue() ==true?1:0);
    } else if (ev instanceof ByteEncodedValue) {
      tag = new IntegerConstantValueTag(((ByteEncodedValue) ev).getValue());
    } else if (ev instanceof CharEncodedValue) {
      tag = new IntegerConstantValueTag(((CharEncodedValue) ev).getValue());
    } else if (ev instanceof DoubleEncodedValue) {
      tag = new DoubleConstantValueTag(((DoubleEncodedValue) ev).getValue());
    } else if (ev instanceof FloatEncodedValue) {
      tag = new FloatConstantValueTag(((FloatEncodedValue) ev).getValue());
    } else if (ev instanceof IntEncodedValue) {
      tag = new IntegerConstantValueTag(((IntEncodedValue) ev).getValue());
    } else if (ev instanceof LongEncodedValue) {
      tag = new LongConstantValueTag(((LongEncodedValue) ev).getValue());
    } else if (ev instanceof ShortEncodedValue) {
      tag = new IntegerConstantValueTag(((ShortEncodedValue) ev).getValue());
    } else if (ev instanceof StringEncodedValue) {
      tag = new StringConstantValueTag(((StringEncodedValue) ev).getValue());
    }

    if (tag != null)
      df.addTag(tag);
}
 
Example #6
Source File: BaseAnnotationEncodedValue.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
@Override
public int compareTo(@Nonnull EncodedValue o) {
    int res = Ints.compare(getValueType(), o.getValueType());
    if (res != 0) return res;
    AnnotationEncodedValue other = (AnnotationEncodedValue)o;
    res = getType().compareTo(other.getType());
    if (res != 0) return res;
    return CollectionUtils.compareAsSet(getElements(), other.getElements());
}
 
Example #7
Source File: BaseAnnotationEncodedValue.java    From zjdroid with Apache License 2.0 5 votes vote down vote up
@Override
public int compareTo(@Nonnull EncodedValue o) {
    int res = Ints.compare(getValueType(), o.getValueType());
    if (res != 0) return res;
    AnnotationEncodedValue other = (AnnotationEncodedValue)o;
    res = getType().compareTo(other.getType());
    if (res != 0) return res;
    return CollectionUtils.compareAsSet(getElements(), other.getElements());
}
 
Example #8
Source File: ImmutableField.java    From zjdroid with Apache License 2.0 5 votes vote down vote up
public ImmutableField(@Nonnull String definingClass,
                      @Nonnull String name,
                      @Nonnull String type,
                      int accessFlags,
                      @Nullable EncodedValue initialValue,
                      @Nullable Collection<? extends Annotation> annotations) {
    this.definingClass = definingClass;
    this.name = name;
    this.type = type;
    this.accessFlags = accessFlags;
    this.initialValue = ImmutableEncodedValueFactory.ofNullable(initialValue);
    this.annotations = ImmutableAnnotation.immutableSetOf(annotations);
}
 
Example #9
Source File: FieldDefinition.java    From zjdroid with Apache License 2.0 5 votes vote down vote up
public static void writeTo(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(':');
    writer.write(field.getType());
    if (initialValue != null) {
        writer.write(" = ");
        EncodedValueAdaptor.writeTo(writer, initialValue);
    }

    writer.write('\n');

    Collection<? extends Annotation> annotations = field.getAnnotations();
    if (annotations.size() > 0) {
        writer.indent(4);
        AnnotationFormatter.writeTo(writer, annotations);
        writer.deindent(4);
        writer.write(".end field\n");
    }
}
 
Example #10
Source File: FieldObject.java    From atlas with Apache License 2.0 5 votes vote down vote up
public FieldObject(String definingClass,String fieldName, String valueType, int accessFlags, EncodedValue initialValue,ImmutableSet<? extends ImmutableAnnotation>annotations) {
    this.definingClass= definingClass;
    this.fieldName = fieldName;
    this.valueType = valueType;
    this.accessFlags = accessFlags;
    this.initialValue = initialValue;
    this.annotations = annotations;
}
 
Example #11
Source File: ClassPool.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
@Override
public EncodedValue apply(Field input) {
    EncodedValue initialValue = input.getInitialValue();
    if (initialValue == null) {
        return ImmutableEncodedValueFactory.defaultValueForType(input.getType());
    }
    return initialValue;
}
 
Example #12
Source File: ApkPatch.java    From atlas with Apache License 2.0 5 votes vote down vote up
public static void getMethodAnnotaionPrepareClasses(DexDiffInfo dexDiffInfo, Set<String> prepareclasses){

        for (DexBackedMethod method:dexDiffInfo.getModifiedMethods()){
            Set<? extends Annotation>annotations = method.getAnnotations();
            if (annotations == null){
                continue;
            }
            for (Annotation annotation:annotations){
                String type = annotation.getType();
                if (type!= null&&type.startsWith("L")&&type.endsWith(";")){
                    prepareclasses.add(type.substring(1, type.length() - 1).replace('/', '.'));
                    System.out.println("prepare class: " + type);
                }
                Set<? extends AnnotationElement> elements = annotation.getElements();
                for (AnnotationElement dexBackedAnnotationElement:elements){
                    if (dexBackedAnnotationElement.getValue() instanceof DexBackedArrayEncodedValue){
                        List<? extends EncodedValue> values = ((DexBackedArrayEncodedValue) dexBackedAnnotationElement.getValue()).getValue();
                        for (EncodedValue encodedValue:values) {
                            if (encodedValue instanceof TypeEncodedValue) {
                                prepareclasses.add(((TypeEncodedValue) encodedValue).getValue().substring(1, ((TypeEncodedValue) encodedValue).getValue().length() - 1).replace('/', '.'));
                                System.out.println("prepare class: " + ((TypeEncodedValue) encodedValue).getValue());
                            }
                        }

                    }else if (dexBackedAnnotationElement.getValue() instanceof DexBackedTypeEncodedValue){
                        String value = ((DexBackedTypeEncodedValue) dexBackedAnnotationElement.getValue()).getValue();
                        prepareclasses.add(value.substring(1, value.length() - 1).replace('/', '.'));
                        System.out.println("prepare class: " + value);
                    }
                }
            }
        }

    }
 
Example #13
Source File: DexBackedArrayEncodedValue.java    From zjdroid with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public List<? extends EncodedValue> getValue() {
    return new VariableSizeList<EncodedValue>(dexFile, encodedArrayOffset, elementCount) {
        @Nonnull
        @Override
        protected EncodedValue readNextItem(@Nonnull DexReader dexReader, int index) {
            return DexBackedEncodedValue.readFrom(dexReader);
        }
    };
}
 
Example #14
Source File: FieldDefinition.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
public static void writeTo(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(':');
    writer.write(field.getType());
    if (initialValue != null) {
        writer.write(" = ");
        EncodedValueAdaptor.writeTo(writer, initialValue);
    }

    writer.write('\n');

    Collection<? extends Annotation> annotations = field.getAnnotations();
    if (annotations.size() > 0) {
        writer.indent(4);
        AnnotationFormatter.writeTo(writer, annotations);
        writer.deindent(4);
        writer.write(".end field\n");
    }
}
 
Example #15
Source File: StaticInitialValueIterator.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
@Nullable
public EncodedValue getNextOrNull() {
    if (index < size) {
        index++;
        return DexBackedEncodedValue.readFrom(reader);
    }
    return null;
}
 
Example #16
Source File: BaseEnumEncodedValue.java    From zjdroid with Apache License 2.0 4 votes vote down vote up
@Override
public int compareTo(@Nonnull EncodedValue o) {
    int res = Ints.compare(getValueType(), o.getValueType());
    if (res != 0) return res;
    return getValue().compareTo(((EnumEncodedValue)o).getValue());
}
 
Example #17
Source File: BaseFieldEncodedValue.java    From zjdroid with Apache License 2.0 4 votes vote down vote up
@Override
public int compareTo(@Nonnull EncodedValue o) {
    int res = Ints.compare(getValueType(), o.getValueType());
    if (res != 0) return res;
    return getValue().compareTo(((FieldEncodedValue)o).getValue());
}
 
Example #18
Source File: BaseStringEncodedValue.java    From zjdroid with Apache License 2.0 4 votes vote down vote up
@Override
public int compareTo(@Nonnull EncodedValue o) {
    int res = Ints.compare(getValueType(), o.getValueType());
    if (res != 0) return res;
    return getValue().compareTo(((StringEncodedValue)o).getValue());
}
 
Example #19
Source File: FieldRewriter.java    From ZjDroid with Apache License 2.0 4 votes vote down vote up
@Nullable public EncodedValue getInitialValue() {
    return RewriterUtils.rewriteNullable(rewriters.getEncodedValueRewriter(), field.getInitialValue());
}
 
Example #20
Source File: BaseDoubleEncodedValue.java    From zjdroid with Apache License 2.0 4 votes vote down vote up
@Override
public int compareTo(@Nonnull EncodedValue o) {
    int res = Ints.compare(getValueType(), o.getValueType());
    if (res != 0) return res;
    return Double.compare(getValue(), ((DoubleEncodedValue)o).getValue());
}
 
Example #21
Source File: AnnotationElementRewriter.java    From zjdroid with Apache License 2.0 4 votes vote down vote up
@Nonnull @Override public EncodedValue getValue() {
    return rewriters.getEncodedValueRewriter().rewrite(annotationElement.getValue());
}
 
Example #22
Source File: BaseBooleanEncodedValue.java    From zjdroid with Apache License 2.0 4 votes vote down vote up
@Override
public int compareTo(@Nonnull EncodedValue o) {
    int res = Ints.compare(getValueType(), o.getValueType());
    if (res != 0) return res;
    return Booleans.compare(getValue(), ((BooleanEncodedValue)o).getValue());
}
 
Example #23
Source File: BaseNullEncodedValue.java    From zjdroid with Apache License 2.0 4 votes vote down vote up
@Override
public int compareTo(@Nonnull EncodedValue o) {
    return Ints.compare(getValueType(), o.getValueType());
}
 
Example #24
Source File: BaseArrayEncodedValue.java    From zjdroid with Apache License 2.0 4 votes vote down vote up
@Override public int compareTo(@Nonnull EncodedValue o) {
    int res = Ints.compare(getValueType(), o.getValueType());
    if (res != 0) return res;
    return CollectionUtils.compareAsList(getValue(), ((ArrayEncodedValue)o).getValue());
}
 
Example #25
Source File: BaseCharEncodedValue.java    From zjdroid with Apache License 2.0 4 votes vote down vote up
@Override
public int compareTo(@Nonnull EncodedValue o) {
    int res = Ints.compare(getValueType(), o.getValueType());
    if (res != 0) return res;
    return Chars.compare(getValue(), ((CharEncodedValue)o).getValue());
}
 
Example #26
Source File: BaseShortEncodedValue.java    From zjdroid with Apache License 2.0 4 votes vote down vote up
@Override
public int compareTo(@Nonnull EncodedValue o) {
    int res = Ints.compare(getValueType(), o.getValueType());
    if (res != 0) return res;
    return Shorts.compare(getValue(), ((ShortEncodedValue)o).getValue());
}
 
Example #27
Source File: BaseMethodEncodedValue.java    From zjdroid with Apache License 2.0 4 votes vote down vote up
@Override
public int compareTo(@Nonnull EncodedValue o) {
    int res = Ints.compare(getValueType(), o.getValueType());
    if (res != 0) return res;
    return getValue().compareTo(((MethodEncodedValue)o).getValue());
}
 
Example #28
Source File: RewriterModule.java    From zjdroid with Apache License 2.0 4 votes vote down vote up
@Nonnull public Rewriter<EncodedValue> getEncodedValueRewriter(@Nonnull Rewriters rewriters) {
    return new EncodedValueRewriter(rewriters);
}
 
Example #29
Source File: DexBackedEncodedValue.java    From ZjDroid with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static EncodedValue readFrom(@Nonnull DexReader reader) {
    int startOffset = reader.getOffset();

    try {
        int b = reader.readUbyte();
        int valueType = b & 0x1f;
        int valueArg = b >>> 5;

        switch (valueType) {
            case ValueType.BYTE:
                Preconditions.checkValueArg(valueArg, 0);
                return new ImmutableByteEncodedValue((byte)reader.readByte());
            case ValueType.SHORT:
                Preconditions.checkValueArg(valueArg, 1);
                return new ImmutableShortEncodedValue((short)reader.readSizedInt(valueArg + 1));
            case ValueType.CHAR:
                Preconditions.checkValueArg(valueArg, 1);
                return new ImmutableCharEncodedValue((char)reader.readSizedSmallUint(valueArg + 1));
            case ValueType.INT:
                Preconditions.checkValueArg(valueArg, 3);
                return new ImmutableIntEncodedValue(reader.readSizedInt(valueArg + 1));
            case ValueType.LONG:
                Preconditions.checkValueArg(valueArg, 7);
                return new ImmutableLongEncodedValue(reader.readSizedLong(valueArg + 1));
            case ValueType.FLOAT:
                Preconditions.checkValueArg(valueArg, 3);
                return new ImmutableFloatEncodedValue(Float.intBitsToFloat(
                        reader.readSizedRightExtendedInt(valueArg + 1)));
            case ValueType.DOUBLE:
                Preconditions.checkValueArg(valueArg, 7);
                return new ImmutableDoubleEncodedValue(Double.longBitsToDouble(
                        reader.readSizedRightExtendedLong(valueArg + 1)));
            case ValueType.STRING:
                Preconditions.checkValueArg(valueArg, 3);
                return new DexBackedStringEncodedValue(reader, valueArg);
            case ValueType.TYPE:
                Preconditions.checkValueArg(valueArg, 3);
                return new DexBackedTypeEncodedValue(reader, valueArg);
            case ValueType.FIELD:
                Preconditions.checkValueArg(valueArg, 3);
                return new DexBackedFieldEncodedValue(reader, valueArg);
            case ValueType.METHOD:
                Preconditions.checkValueArg(valueArg, 3);
                return new DexBackedMethodEncodedValue(reader, valueArg);
            case ValueType.ENUM:
                Preconditions.checkValueArg(valueArg, 3);
                return new DexBackedEnumEncodedValue(reader, valueArg);
            case ValueType.ARRAY:
                Preconditions.checkValueArg(valueArg, 0);
                return new DexBackedArrayEncodedValue(reader);
            case ValueType.ANNOTATION:
                Preconditions.checkValueArg(valueArg, 0);
                return new DexBackedAnnotationEncodedValue(reader);
            case ValueType.NULL:
                Preconditions.checkValueArg(valueArg, 0);
                return ImmutableNullEncodedValue.INSTANCE;
            case ValueType.BOOLEAN:
                Preconditions.checkValueArg(valueArg, 1);
                return ImmutableBooleanEncodedValue.forBoolean(valueArg == 1);
            default:
                throw new ExceptionWithContext("Invalid encoded_value type: 0x%x", valueType);
        }
    } catch (Exception ex) {
        throw ExceptionWithContext.withContext(ex, "Error while reading encoded value at offset 0x%x", startOffset);
    }
}
 
Example #30
Source File: BaseLongEncodedValue.java    From zjdroid with Apache License 2.0 4 votes vote down vote up
@Override
public int compareTo(@Nonnull EncodedValue o) {
    int res = Ints.compare(getValueType(), o.getValueType());
    if (res != 0) return res;
    return Longs.compare(getValue(), ((LongEncodedValue)o).getValue());
}