package org.jf.dexlib2.analysis;
import com.google.common.io.Files;
import org.jf.dexlib2.iface.ClassDef;
import org.jf.dexlib2.iface.Method;
import org.jf.dexlib2.iface.instruction.InlineIndexInstruction;
import org.jf.dexlib2.immutable.ImmutableMethod;
import org.jf.dexlib2.immutable.ImmutableMethodParameter;
import org.jf.dexlib2.immutable.reference.ImmutableMethodReference;
import org.jf.dexlib2.immutable.util.ParamUtil;
import javax.annotation.Nonnull;
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CustomInlineMethodResolver extends InlineMethodResolver {
@Nonnull private final ClassPath classPath;
@Nonnull private final Method[] inlineMethods;
public CustomInlineMethodResolver(@Nonnull ClassPath classPath, @Nonnull String inlineTable) {
this.classPath = classPath;
StringReader reader = new StringReader(inlineTable);
List<String> lines = new ArrayList<String>();
BufferedReader br = new BufferedReader(reader);
try {
String line = br.readLine();
while (line != null) {
if (line.length() > 0) {
lines.add(line);
}
line = br.readLine();
}
} catch (IOException ex) {
throw new RuntimeException("Error while parsing inline table", ex);
}
inlineMethods = new Method[lines.size()];
for (int i=0; i<inlineMethods.length; i++) {
inlineMethods[i] = parseAndResolveInlineMethod(lines.get(i));
}
}
public CustomInlineMethodResolver(@Nonnull ClassPath classPath, @Nonnull File inlineTable) throws IOException {
this(classPath, Files.toString(inlineTable, Charset.forName("UTF-8")));
}
@Override
@Nonnull
public Method resolveExecuteInline(@Nonnull AnalyzedInstruction analyzedInstruction) {
InlineIndexInstruction instruction = (InlineIndexInstruction)analyzedInstruction.instruction;
int methodIndex = instruction.getInlineIndex();
if (methodIndex < 0 || methodIndex >= inlineMethods.length) {
throw new RuntimeException("Invalid method index: " + methodIndex);
}
return inlineMethods[methodIndex];
}
private static final Pattern longMethodPattern = Pattern.compile("(L[^;]+;)->([^(]+)\\(([^)]*)\\)(.+)");
@Nonnull
private Method parseAndResolveInlineMethod(@Nonnull String inlineMethod) {
Matcher m = longMethodPattern.matcher(inlineMethod);
if (!m.matches()) {
assert false;
throw new RuntimeException("Invalid method descriptor: " + inlineMethod);
}
String className = m.group(1);
String methodName = m.group(2);
Iterable<ImmutableMethodParameter> methodParams = ParamUtil.parseParamString(m.group(3));
String methodRet = m.group(4);
ImmutableMethodReference methodRef = new ImmutableMethodReference(className, methodName, methodParams,
methodRet);
int accessFlags = 0;
boolean resolved = false;
TypeProto typeProto = classPath.getClass(className);
if (typeProto instanceof ClassProto) {
ClassDef classDef = ((ClassProto)typeProto).getClassDef();
for (Method method: classDef.getMethods()) {
if (method.equals(methodRef)) {
resolved = true;
accessFlags = method.getAccessFlags();
break;
}
}
}
if (!resolved) {
throw new RuntimeException("Cannot resolve inline method: " + inlineMethod);
}
return new ImmutableMethod(className, methodName, methodParams, methodRet, accessFlags, null, null, null);
}
}