ValueEncoderpublic final class ValueEncoder extends Object Handler for writing out {@code encoded_values} and parts
thereof. |
Fields Summary |
---|
private static final int | VALUE_BYTEannotation value type constant: {@code byte} | private static final int | VALUE_SHORTannotation value type constant: {@code short} | private static final int | VALUE_CHARannotation value type constant: {@code char} | private static final int | VALUE_INTannotation value type constant: {@code int} | private static final int | VALUE_LONGannotation value type constant: {@code long} | private static final int | VALUE_FLOATannotation value type constant: {@code float} | private static final int | VALUE_DOUBLEannotation value type constant: {@code double} | private static final int | VALUE_STRINGannotation value type constant: {@code string} | private static final int | VALUE_TYPEannotation value type constant: {@code type} | private static final int | VALUE_FIELDannotation value type constant: {@code field} | private static final int | VALUE_METHODannotation value type constant: {@code method} | private static final int | VALUE_ENUMannotation value type constant: {@code enum} | private static final int | VALUE_ARRAYannotation value type constant: {@code array} | private static final int | VALUE_ANNOTATIONannotation value type constant: {@code annotation} | private static final int | VALUE_NULLannotation value type constant: {@code null} | private static final int | VALUE_BOOLEANannotation value type constant: {@code boolean} | private final DexFile | file{@code non-null;} file being written | private final com.android.dx.util.AnnotatedOutput | out{@code non-null;} output stream to write to |
Constructors Summary |
---|
public ValueEncoder(DexFile file, com.android.dx.util.AnnotatedOutput out)Construct an instance.
if (file == null) {
throw new NullPointerException("file == null");
}
if (out == null) {
throw new NullPointerException("out == null");
}
this.file = file;
this.out = out;
|
Methods Summary |
---|
public static void | addContents(DexFile file, com.android.dx.rop.annotation.Annotation annotation)Helper for {@code addContents()} methods, which adds
contents for a particular {@link Annotation}, calling itself
recursively should it encounter a nested annotation.
TypeIdsSection typeIds = file.getTypeIds();
StringIdsSection stringIds = file.getStringIds();
typeIds.intern(annotation.getType());
for (NameValuePair pair : annotation.getNameValuePairs()) {
stringIds.intern(pair.getName());
addContents(file, pair.getValue());
}
| public static void | addContents(DexFile file, com.android.dx.rop.cst.Constant cst)Helper for {@code addContents()} methods, which adds
contents for a particular constant, calling itself recursively
should it encounter a {@link CstArray} and calling {@link
#addContents(DexFile,Annotation)} recursively should it
encounter a {@link CstAnnotation}.
if (cst instanceof CstAnnotation) {
addContents(file, ((CstAnnotation) cst).getAnnotation());
} else if (cst instanceof CstArray) {
CstArray.List list = ((CstArray) cst).getList();
int size = list.size();
for (int i = 0; i < size; i++) {
addContents(file, list.get(i));
}
} else {
file.internIfAppropriate(cst);
}
| public static java.lang.String | constantToHuman(com.android.dx.rop.cst.Constant cst)Gets the colloquial type name and human form of the type of the
given constant, when used as an encoded value.
int type = constantToValueType(cst);
if (type == VALUE_NULL) {
return "null";
}
StringBuilder sb = new StringBuilder();
sb.append(cst.typeName());
sb.append(' ");
sb.append(cst.toHuman());
return sb.toString();
| private static int | constantToValueType(com.android.dx.rop.cst.Constant cst)Gets the value type for the given constant.
/*
* TODO: Constant should probable have an associated enum, so this
* can be a switch().
*/
if (cst instanceof CstByte) {
return VALUE_BYTE;
} else if (cst instanceof CstShort) {
return VALUE_SHORT;
} else if (cst instanceof CstChar) {
return VALUE_CHAR;
} else if (cst instanceof CstInteger) {
return VALUE_INT;
} else if (cst instanceof CstLong) {
return VALUE_LONG;
} else if (cst instanceof CstFloat) {
return VALUE_FLOAT;
} else if (cst instanceof CstDouble) {
return VALUE_DOUBLE;
} else if (cst instanceof CstString) {
return VALUE_STRING;
} else if (cst instanceof CstType) {
return VALUE_TYPE;
} else if (cst instanceof CstFieldRef) {
return VALUE_FIELD;
} else if (cst instanceof CstMethodRef) {
return VALUE_METHOD;
} else if (cst instanceof CstEnumRef) {
return VALUE_ENUM;
} else if (cst instanceof CstArray) {
return VALUE_ARRAY;
} else if (cst instanceof CstAnnotation) {
return VALUE_ANNOTATION;
} else if (cst instanceof CstKnownNull) {
return VALUE_NULL;
} else if (cst instanceof CstBoolean) {
return VALUE_BOOLEAN;
} else {
throw new RuntimeException("Shouldn't happen");
}
| public void | writeAnnotation(com.android.dx.rop.annotation.Annotation annotation, boolean topLevel)Writes out the encoded form of the given annotation, that is,
as an {@code encoded_annotation} and not including a
{@code value_type} prefix. If the output stream keeps
(debugging) annotations and {@code topLevel} is
{@code true}, then this method will write (debugging)
annotations.
boolean annotates = topLevel && out.annotates();
StringIdsSection stringIds = file.getStringIds();
TypeIdsSection typeIds = file.getTypeIds();
CstType type = annotation.getType();
int typeIdx = typeIds.indexOf(type);
if (annotates) {
out.annotate(" type_idx: " + Hex.u4(typeIdx) + " // " +
type.toHuman());
}
out.writeUleb128(typeIds.indexOf(annotation.getType()));
Collection<NameValuePair> pairs = annotation.getNameValuePairs();
int size = pairs.size();
if (annotates) {
out.annotate(" size: " + Hex.u4(size));
}
out.writeUleb128(size);
int at = 0;
for (NameValuePair pair : pairs) {
CstString name = pair.getName();
int nameIdx = stringIds.indexOf(name);
Constant value = pair.getValue();
if (annotates) {
out.annotate(0, " elements[" + at + "]:");
at++;
out.annotate(" name_idx: " + Hex.u4(nameIdx) + " // " +
name.toHuman());
}
out.writeUleb128(nameIdx);
if (annotates) {
out.annotate(" value: " + constantToHuman(value));
}
writeConstant(value);
}
if (annotates) {
out.endAnnotation();
}
| public void | writeArray(com.android.dx.rop.cst.CstArray array, boolean topLevel)Writes out the encoded form of the given array, that is, as
an {@code encoded_array} and not including a
{@code value_type} prefix. If the output stream keeps
(debugging) annotations and {@code topLevel} is
{@code true}, then this method will write (debugging)
annotations.
boolean annotates = topLevel && out.annotates();
CstArray.List list = ((CstArray) array).getList();
int size = list.size();
if (annotates) {
out.annotate(" size: " + Hex.u4(size));
}
out.writeUleb128(size);
for (int i = 0; i < size; i++) {
Constant cst = list.get(i);
if (annotates) {
out.annotate(" [" + Integer.toHexString(i) + "] " +
constantToHuman(cst));
}
writeConstant(cst);
}
if (annotates) {
out.endAnnotation();
}
| public void | writeConstant(com.android.dx.rop.cst.Constant cst)Writes out the encoded form of the given constant.
int type = constantToValueType(cst);
int arg;
switch (type) {
case VALUE_BYTE:
case VALUE_SHORT:
case VALUE_INT:
case VALUE_LONG: {
long value = ((CstLiteralBits) cst).getLongBits();
EncodedValueCodec.writeSignedIntegralValue(out, type, value);
break;
}
case VALUE_CHAR: {
long value = ((CstLiteralBits) cst).getLongBits();
EncodedValueCodec.writeUnsignedIntegralValue(out, type, value);
break;
}
case VALUE_FLOAT: {
// Shift value left 32 so that right-zero-extension works.
long value = ((CstFloat) cst).getLongBits() << 32;
EncodedValueCodec.writeRightZeroExtendedValue(out, type, value);
break;
}
case VALUE_DOUBLE: {
long value = ((CstDouble) cst).getLongBits();
EncodedValueCodec.writeRightZeroExtendedValue(out, type, value);
break;
}
case VALUE_STRING: {
int index = file.getStringIds().indexOf((CstString) cst);
EncodedValueCodec.writeUnsignedIntegralValue(out, type, (long) index);
break;
}
case VALUE_TYPE: {
int index = file.getTypeIds().indexOf((CstType) cst);
EncodedValueCodec.writeUnsignedIntegralValue(out, type, (long) index);
break;
}
case VALUE_FIELD: {
int index = file.getFieldIds().indexOf((CstFieldRef) cst);
EncodedValueCodec.writeUnsignedIntegralValue(out, type, (long) index);
break;
}
case VALUE_METHOD: {
int index = file.getMethodIds().indexOf((CstMethodRef) cst);
EncodedValueCodec.writeUnsignedIntegralValue(out, type, (long) index);
break;
}
case VALUE_ENUM: {
CstFieldRef fieldRef = ((CstEnumRef) cst).getFieldRef();
int index = file.getFieldIds().indexOf(fieldRef);
EncodedValueCodec.writeUnsignedIntegralValue(out, type, (long) index);
break;
}
case VALUE_ARRAY: {
out.writeByte(type);
writeArray((CstArray) cst, false);
break;
}
case VALUE_ANNOTATION: {
out.writeByte(type);
writeAnnotation(((CstAnnotation) cst).getAnnotation(),
false);
break;
}
case VALUE_NULL: {
out.writeByte(type);
break;
}
case VALUE_BOOLEAN: {
int value = ((CstBoolean) cst).getIntBits();
out.writeByte(type | (value << 5));
break;
}
default: {
throw new RuntimeException("Shouldn't happen");
}
}
|
|