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

The following examples show how to use org.jf.dexlib2.iface.value.EncodedValue. These examples are extracted from open source projects. 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 Project: ZjDroid   Source File: ArrayEncodedValueAdaptor.java    License: 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 Project: zjdroid   Source File: ClassPool.java    License: 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 Project: JAADAS   Source File: DexField.java    License: 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 4
Source Project: ZjDroid   Source File: BaseAnnotationEncodedValue.java    License: 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 5
Source Project: zjdroid   Source File: FieldDefinition.java    License: 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 6
Source Project: ZjDroid   Source File: ClassPool.java    License: 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 7
Source Project: zjdroid   Source File: DexBackedArrayEncodedValue.java    License: 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 8
Source Project: ZjDroid   Source File: DexBackedArrayEncodedValue.java    License: 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 9
Source Project: ZjDroid   Source File: StaticInitialValueIterator.java    License: 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 10
Source Project: ZjDroid   Source File: FieldDefinition.java    License: 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 11
Source Project: atlas   Source File: ApkPatch.java    License: 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 12
Source Project: atlas   Source File: FieldObject.java    License: 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 13
Source Project: zjdroid   Source File: ImmutableField.java    License: 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 14
Source Project: zjdroid   Source File: BaseAnnotationEncodedValue.java    License: 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 15
Source Project: zjdroid   Source File: StaticInitialValueIterator.java    License: 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 Project: zjdroid   Source File: RewriterModule.java    License: Apache License 2.0 4 votes vote down vote up
@Nonnull public Rewriter<EncodedValue> getEncodedValueRewriter(@Nonnull Rewriters rewriters) {
    return new EncodedValueRewriter(rewriters);
}
 
Example 17
Source Project: ZjDroid   Source File: ImmutableArrayEncodedValue.java    License: Apache License 2.0 4 votes vote down vote up
public ImmutableArrayEncodedValue(@Nonnull Collection<? extends EncodedValue> value) {
    this.value = ImmutableEncodedValueFactory.immutableListOf(value);
}
 
Example 18
Source Project: JAADAS   Source File: DexPrinter.java    License: GNU General Public License v3.0 4 votes vote down vote up
private List<Annotation> buildMemberClassesAttribute(SootClass parentClass,
 		InnerClassAttribute t, Set<String> skipList) {
 	List<Annotation> anns = null;
 	Set<String> memberClasses = null;
 	
 	// Collect the inner classes
 	for (Tag t2 : t.getSpecs()) {
 		InnerClassTag icTag = (InnerClassTag) t2;
 		String outerClass = getOuterClassNameFromTag(icTag);

// Only classes with names are member classes
if (icTag.getOuterClass() != null
		&& parentClass.getName().equals(outerClass)) {
	if (memberClasses == null)
		memberClasses = new HashSet<String>();
	memberClasses.add(SootToDexUtils.getDexClassName(icTag.getInnerClass()));
}
 	}
 	
 	// Write the member classes
 	if (memberClasses != null
 			&& !memberClasses.isEmpty()
 			&& skipList.add("Ldalvik/annotation/MemberClasses;")) {
     	List<EncodedValue> classes = new ArrayList<EncodedValue>();
  	for (String memberClass : memberClasses) {
  		ImmutableTypeEncodedValue classValue = new ImmutableTypeEncodedValue(memberClass);
  		classes.add(classValue);
  	}
  	
 		ImmutableArrayEncodedValue classesValue =
 				new ImmutableArrayEncodedValue(classes);
 		ImmutableAnnotationElement element =
 				new ImmutableAnnotationElement("value", classesValue);
  	ImmutableAnnotation memberAnnotation =
 				new ImmutableAnnotation(AnnotationVisibility.SYSTEM,
 						"Ldalvik/annotation/MemberClasses;",
 						Collections.singletonList(element));
  	if (anns == null) anns = new ArrayList<Annotation>();
  	anns.add(memberAnnotation);
 	}
 	return anns;
 }
 
Example 19
Source Project: ZjDroid   Source File: BaseEnumEncodedValue.java    License: 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 20
Source Project: ZjDroid   Source File: BaseStringEncodedValue.java    License: 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 21
Source Project: ZjDroid   Source File: BaseIntEncodedValue.java    License: 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 Ints.compare(getValue(), ((IntEncodedValue)o).getValue());
}
 
Example 22
Source Project: zjdroid   Source File: FieldRewriter.java    License: Apache License 2.0 4 votes vote down vote up
@Nullable public EncodedValue getInitialValue() {
    return RewriterUtils.rewriteNullable(rewriters.getEncodedValueRewriter(), field.getInitialValue());
}
 
Example 23
Source Project: ZjDroid   Source File: BaseBooleanEncodedValue.java    License: 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 24
Source Project: ZjDroid   Source File: BaseNullEncodedValue.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public int compareTo(@Nonnull EncodedValue o) {
    return Ints.compare(getValueType(), o.getValueType());
}
 
Example 25
Source Project: ZjDroid   Source File: BaseArrayEncodedValue.java    License: 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 26
Source Project: ZjDroid   Source File: BaseShortEncodedValue.java    License: 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 Project: ZjDroid   Source File: BaseLongEncodedValue.java    License: 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());
}
 
Example 28
Source Project: ZjDroid   Source File: BaseFieldEncodedValue.java    License: 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 29
Source Project: ZjDroid   Source File: BaseCharEncodedValue.java    License: 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 30
Source Project: ZjDroid   Source File: BaseMethodEncodedValue.java    License: 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());
}