Skip to content

Instantly share code, notes, and snippets.

@chiehmin
Created December 10, 2013 14:38
Show Gist options
  • Save chiehmin/7891610 to your computer and use it in GitHub Desktop.
Save chiehmin/7891610 to your computer and use it in GitHub Desktop.
package tw.fatminmin.intentparser;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Parser {
List<String> typeList;
String seperateLine = "-----------------------------------------------------------";
public void parseDirectory(File dir) {
for(File file : dir.listFiles()) {
if(file.isDirectory()) {
parseDirectory(file);
}
else {
if(file.getName().contains("smali")) {
parseFile(file);
}
}
}
}
public void parseFile(File file) {
boolean first = true;
BufferedReader br;
Map<String, String> variableMap = new HashMap<String, String>();
typeList = getTypeList();
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line;
int parameterNum = 0;
while((line = br.readLine()) != null) {
line = line.trim();
if(line.contains("Intent;-><init>")) {
if(first) {
first = false;
System.out.println("File: " + file.getPath());
System.out.println(seperateLine);
}
System.out.println(line);
for(Map.Entry<String, String> entry : variableMap.entrySet()) {
if(line.contains(entry.getKey())) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
System.out.println(seperateLine);
}
else if(isVarDecl(line)) {
String[] arr = line.split("\\s+");
String variable, value;
variable = arr[1].substring(0, arr[1].indexOf(','));
value = arr[2];
variableMap.put(variable, value);
}
else if(isParameterDecl(line)) {
if(line.split("\\s+").length > 1) {
String var = "p" + (++parameterNum);
String value = line.split("\\s+")[1];
variableMap.put(var, value);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private boolean isVarDecl(String line) {
for(String type : typeList) {
if(line.contains(type)) {
return true;
}
}
return false;
}
private boolean isParameterDecl(String line) {
return line.contains(".parameter");
}
private List<String> getTypeList() {
List<String> list = new ArrayList<String>();
list.add("const-string");
list.add("const-class");
list.add("new-instance");
return list;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment