use of com.taobao.android.dx.rop.cst.Constant in project atlas by alibaba.
the class CfTranslator method translate0.
/**
* Performs the main act of translation. This method is separated
* from {@link #translate} just to keep things a bit simpler in
* terms of exception handling.
*
* @param cf {@code non-null;} the class file
* @param bytes {@code non-null;} contents of the file
* @param cfOptions options for class translation
* @param dexOptions options for dex output
* @param dexFile {@code non-null;} dex output
* @param optimizerOptions options for the optimizer
* @return {@code non-null;} the translated class
*/
private static ClassDefItem translate0(DirectClassFile cf, byte[] bytes, CfOptions cfOptions, DexOptions dexOptions, OptimizerOptions optimizerOptions, DexFile dexFile) {
optimizerOptions.loadOptimizeLists(cfOptions.optimizeListFile, cfOptions.dontOptimizeListFile);
// Build up a class to output.
CstType thisClass = cf.getThisClass();
int classAccessFlags = cf.getAccessFlags() & ~AccessFlags.ACC_SUPER;
CstString sourceFile = (cfOptions.positionInfo == PositionList.NONE) ? null : cf.getSourceFile();
ClassDefItem out = new ClassDefItem(thisClass, classAccessFlags, cf.getSuperclass(), cf.getInterfaces(), sourceFile);
Annotations classAnnotations = AttributeTranslator.getClassAnnotations(cf, cfOptions);
if (classAnnotations.size() != 0) {
out.setClassAnnotations(classAnnotations, dexFile);
}
FieldIdsSection fieldIdsSection = dexFile.getFieldIds();
MethodIdsSection methodIdsSection = dexFile.getMethodIds();
processFields(cf, out, dexFile);
processMethods(cf, cfOptions, dexOptions, optimizerOptions, out, dexFile);
// intern constant pool method, field and type references
ConstantPool constantPool = cf.getConstantPool();
int constantPoolSize = constantPool.size();
for (int i = 0; i < constantPoolSize; i++) {
Constant constant = constantPool.getOrNull(i);
if (constant instanceof CstMethodRef) {
methodIdsSection.intern((CstBaseMethodRef) constant);
} else if (constant instanceof CstInterfaceMethodRef) {
methodIdsSection.intern(((CstInterfaceMethodRef) constant).toMethodRef());
} else if (constant instanceof CstFieldRef) {
fieldIdsSection.intern((CstFieldRef) constant);
} else if (constant instanceof CstEnumRef) {
fieldIdsSection.intern(((CstEnumRef) constant).getFieldRef());
}
}
return out;
}
use of com.taobao.android.dx.rop.cst.Constant in project atlas by alibaba.
the class ClassReferenceListBuilder method addDependencies.
private void addDependencies(ConstantPool pool) {
for (Constant constant : pool.getEntries()) {
if (constant instanceof CstType) {
Type type = ((CstType) constant).getClassType();
String descriptor = type.getDescriptor();
if (descriptor.endsWith(";")) {
int lastBrace = descriptor.lastIndexOf('[');
if (lastBrace < 0) {
addClassWithHierachy(descriptor.substring(1, descriptor.length() - 1));
} else {
assert descriptor.length() > lastBrace + 3 && descriptor.charAt(lastBrace + 1) == 'L';
addClassWithHierachy(descriptor.substring(lastBrace + 2, descriptor.length() - 1));
}
}
}
}
}
use of com.taobao.android.dx.rop.cst.Constant in project atlas by alibaba.
the class CfTranslator method updateDexStatistics.
/**
* Helper that updates the dex statistics.
*/
private static void updateDexStatistics(CfOptions cfOptions, DexOptions dexOptions, RopMethod optRmeth, RopMethod nonOptRmeth, LocalVariableInfo locals, int paramSize, int originalByteCount) {
/*
* Run rop->dex again on optimized vs. non-optimized method to
* collect statistics. We have to totally convert both ways,
* since converting the "real" method getting added to the
* file would corrupt it (by messing with its constant pool
* indices).
*/
DalvCode optCode = RopTranslator.translate(optRmeth, cfOptions.positionInfo, locals, paramSize, dexOptions);
DalvCode nonOptCode = RopTranslator.translate(nonOptRmeth, cfOptions.positionInfo, locals, paramSize, dexOptions);
/*
* Fake out the indices, so code.getInsns() can work well enough
* for the current purpose.
*/
DalvCode.AssignIndicesCallback callback = new DalvCode.AssignIndicesCallback() {
public int getIndex(Constant cst) {
// Everything is at index 0!
return 0;
}
};
optCode.assignIndices(callback);
nonOptCode.assignIndices(callback);
CodeStatistics.updateDexStatistics(nonOptCode, optCode);
CodeStatistics.updateOriginalByteCount(originalByteCount);
}
use of com.taobao.android.dx.rop.cst.Constant in project atlas by alibaba.
the class ConstantPoolParser method parse.
/**
* Does the actual parsing.
*/
private void parse() {
determineOffsets();
if (observer != null) {
observer.parsed(bytes, 8, 2, "constant_pool_count: " + Hex.u2(offsets.length));
observer.parsed(bytes, 10, 0, "\nconstant_pool:");
observer.changeIndent(1);
}
/*
* Track the constant value's original string type. True if constants[i] was
* a CONSTANT_Utf8, false for any other type including CONSTANT_string.
*/
BitSet wasUtf8 = new BitSet(offsets.length);
for (int i = 1; i < offsets.length; i++) {
int offset = offsets[i];
if ((offset != 0) && (pool.getOrNull(i) == null)) {
parse0(i, wasUtf8);
}
}
if (observer != null) {
for (int i = 1; i < offsets.length; i++) {
Constant cst = pool.getOrNull(i);
if (cst == null) {
continue;
}
int offset = offsets[i];
int nextOffset = endOffset;
for (int j = i + 1; j < offsets.length; j++) {
int off = offsets[j];
if (off != 0) {
nextOffset = off;
break;
}
}
String human = wasUtf8.get(i) ? Hex.u2(i) + ": utf8{\"" + cst.toHuman() + "\"}" : Hex.u2(i) + ": " + cst.toString();
observer.parsed(bytes, offset, nextOffset - offset, human);
}
observer.changeIndent(-1);
observer.parsed(bytes, endOffset, 0, "end constant_pool");
}
}
use of com.taobao.android.dx.rop.cst.Constant in project atlas by alibaba.
the class AnnotationParser method parseElement.
/**
* Parses a {@link NameValuePair}.
*
* @return {@code non-null;} the parsed element
*/
private NameValuePair parseElement() throws IOException {
requireLength(5);
int elementNameIndex = input.readUnsignedShort();
CstString elementName = (CstString) pool.get(elementNameIndex);
if (observer != null) {
parsed(2, "element_name: " + elementName.toHuman());
parsed(0, "value: ");
changeIndent(1);
}
Constant value = parseValue();
if (observer != null) {
changeIndent(-1);
}
return new NameValuePair(elementName, value);
}
Aggregations