package org.jf.dexlib2.analysis;
import com.google.common.collect.ImmutableList;
import org.jf.dexlib2.iface.Method;
import org.jf.dexlib2.iface.instruction.InlineIndexInstruction;
import org.jf.dexlib2.iface.instruction.VariableRegisterInstruction;
import org.jf.dexlib2.immutable.ImmutableMethod;
import org.jf.dexlib2.immutable.ImmutableMethodParameter;
import org.jf.dexlib2.immutable.util.ParamUtil;
import javax.annotation.Nonnull;
public abstract class InlineMethodResolver {
public static final int STATIC = 0x8;
public static final int VIRTUAL = 0x1;
public static final int DIRECT = 0x2;
@Nonnull
public static InlineMethodResolver createInlineMethodResolver(int odexVersion) {
if (odexVersion == 35) {
return new InlineMethodResolver_version35();
} else if (odexVersion == 36) {
return new InlineMethodResolver_version36();
} else {
throw new RuntimeException(String.format("odex version %d is not supported yet", odexVersion));
}
}
protected InlineMethodResolver() {
}
@Nonnull
private static Method inlineMethod(int accessFlags, @Nonnull String cls, @Nonnull String name,
@Nonnull String params, @Nonnull String returnType) {
ImmutableList<ImmutableMethodParameter> paramList = ImmutableList.copyOf(ParamUtil.parseParamString(params));
return new ImmutableMethod(cls, name, paramList, returnType, accessFlags, null, null, null);
}
@Nonnull public abstract Method resolveExecuteInline(@Nonnull AnalyzedInstruction instruction);
private static class InlineMethodResolver_version35 extends InlineMethodResolver
{
private final Method[] inlineMethods;
public InlineMethodResolver_version35() {
inlineMethods = new Method[] {
inlineMethod(STATIC, "Lorg/apache/harmony/dalvik/NativeTestTarget;", "emptyInlineMethod", "", "V"),
inlineMethod(VIRTUAL, "Ljava/lang/String;", "charAt", "I", "C"),
inlineMethod(VIRTUAL, "Ljava/lang/String;", "compareTo", "Ljava/lang/String;", "I"),
inlineMethod(VIRTUAL, "Ljava/lang/String;", "equals", "Ljava/lang/Object;", "Z"),
inlineMethod(VIRTUAL, "Ljava/lang/String;", "length", "", "I"),
inlineMethod(STATIC, "Ljava/lang/Math;", "abs", "I", "I"),
inlineMethod(STATIC, "Ljava/lang/Math;", "abs", "J", "J"),
inlineMethod(STATIC, "Ljava/lang/Math;", "abs", "F", "F"),
inlineMethod(STATIC, "Ljava/lang/Math;", "abs", "D", "D"),
inlineMethod(STATIC, "Ljava/lang/Math;", "min", "II", "I"),
inlineMethod(STATIC, "Ljava/lang/Math;", "max", "II", "I"),
inlineMethod(STATIC, "Ljava/lang/Math;", "sqrt", "D", "D"),
inlineMethod(STATIC, "Ljava/lang/Math;", "cos", "D", "D"),
inlineMethod(STATIC, "Ljava/lang/Math;", "sin", "D", "D")
};
}
@Override
@Nonnull
public Method resolveExecuteInline(@Nonnull AnalyzedInstruction analyzedInstruction) {
InlineIndexInstruction instruction = (InlineIndexInstruction)analyzedInstruction.instruction;
int inlineIndex = instruction.getInlineIndex();
if (inlineIndex < 0 || inlineIndex >= inlineMethods.length) {
throw new RuntimeException("Invalid inline index: " + inlineIndex);
}
return inlineMethods[inlineIndex];
}
}
private static class InlineMethodResolver_version36 extends InlineMethodResolver
{
private final Method[] inlineMethods;
private final Method indexOfIMethod;
private final Method indexOfIIMethod;
private final Method fastIndexOfMethod;
private final Method isEmptyMethod;
public InlineMethodResolver_version36() {
indexOfIMethod = inlineMethod(VIRTUAL, "Ljava/lang/String;", "indexOf", "I", "I");
indexOfIIMethod = inlineMethod(VIRTUAL, "Ljava/lang/String;", "indexOf", "II", "I");
fastIndexOfMethod = inlineMethod(DIRECT, "Ljava/lang/String;", "fastIndexOf", "II", "I");
isEmptyMethod = inlineMethod(VIRTUAL, "Ljava/lang/String;", "isEmpty", "", "Z");
inlineMethods = new Method[] {
inlineMethod(STATIC, "Lorg/apache/harmony/dalvik/NativeTestTarget;", "emptyInlineMethod", "", "V"),
inlineMethod(VIRTUAL, "Ljava/lang/String;", "charAt", "I", "C"),
inlineMethod(VIRTUAL, "Ljava/lang/String;", "compareTo", "Ljava/lang/String;", "I"),
inlineMethod(VIRTUAL, "Ljava/lang/String;", "equals", "Ljava/lang/Object;", "Z"),
null,
null,
inlineMethod(VIRTUAL, "Ljava/lang/String;", "length", "", "I"),
inlineMethod(STATIC, "Ljava/lang/Math;", "abs", "I", "I"),
inlineMethod(STATIC, "Ljava/lang/Math;", "abs", "J", "J"),
inlineMethod(STATIC, "Ljava/lang/Math;", "abs", "F", "F"),
inlineMethod(STATIC, "Ljava/lang/Math;", "abs", "D", "D"),
inlineMethod(STATIC, "Ljava/lang/Math;", "min", "II", "I"),
inlineMethod(STATIC, "Ljava/lang/Math;", "max", "II", "I"),
inlineMethod(STATIC, "Ljava/lang/Math;", "sqrt", "D", "D"),
inlineMethod(STATIC, "Ljava/lang/Math;", "cos", "D", "D"),
inlineMethod(STATIC, "Ljava/lang/Math;", "sin", "D", "D"),
inlineMethod(STATIC, "Ljava/lang/Float;", "floatToIntBits", "F", "I"),
inlineMethod(STATIC, "Ljava/lang/Float;", "floatToRawIntBits", "F", "I"),
inlineMethod(STATIC, "Ljava/lang/Float;", "intBitsToFloat", "I", "F"),
inlineMethod(STATIC, "Ljava/lang/Double;", "doubleToLongBits", "D", "J"),
inlineMethod(STATIC, "Ljava/lang/Double;", "doubleToRawLongBits", "D", "J"),
inlineMethod(STATIC, "Ljava/lang/Double;", "longBitsToDouble", "J", "D"),
inlineMethod(STATIC, "Ljava/lang/StrictMath;", "abs", "I", "I"),
inlineMethod(STATIC, "Ljava/lang/StrictMath;", "abs", "J", "J"),
inlineMethod(STATIC, "Ljava/lang/StrictMath;", "abs", "F", "F"),
inlineMethod(STATIC, "Ljava/lang/StrictMath;", "abs", "D", "D"),
inlineMethod(STATIC, "Ljava/lang/StrictMath;", "min", "II", "I"),
inlineMethod(STATIC, "Ljava/lang/StrictMath;", "max", "II", "I"),
inlineMethod(STATIC, "Ljava/lang/StrictMath;", "sqrt", "D", "D"),
};
}
@Override
@Nonnull
public Method resolveExecuteInline(@Nonnull AnalyzedInstruction analyzedInstruction) {
InlineIndexInstruction instruction = (InlineIndexInstruction)analyzedInstruction.instruction;
int inlineIndex = instruction.getInlineIndex();
if (inlineIndex < 0 || inlineIndex >= inlineMethods.length) {
throw new RuntimeException("Invalid method index: " + inlineIndex);
}
if (inlineIndex == 4) {
int parameterCount = ((VariableRegisterInstruction)instruction).getRegisterCount();
if (parameterCount == 2) {
return indexOfIMethod;
} else if (parameterCount == 3) {
return fastIndexOfMethod;
} else {
throw new RuntimeException("Could not determine the correct inline method to use");
}
} else if (inlineIndex == 5) {
int parameterCount = ((VariableRegisterInstruction)instruction).getRegisterCount();
if (parameterCount == 3) {
return indexOfIIMethod;
} else if (parameterCount == 1) {
return isEmptyMethod;
} else {
throw new RuntimeException("Could not determine the correct inline method to use");
}
}
return inlineMethods[inlineIndex];
}
}
}