bcv-vf/src/main/java/the/bytecode/club/bytecodeviewer/util/MethodParser.java

148 lines
4.5 KiB
Java
Raw Normal View History

2019-04-17 06:45:15 +00:00
package the.bytecode.club.bytecodeviewer.util;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Pattern;
/**
* Methods parser.
*
* @author DreamSworK
*/
2021-04-12 20:19:12 +00:00
public class MethodParser {
2019-04-17 06:45:15 +00:00
public static class Method {
public String name;
public List<String> params;
public Method(String name, List<String> params) {
this.name = name;
this.params = params;
}
2021-04-12 22:31:22 +00:00
@Override
2019-04-17 06:45:15 +00:00
public String toString() {
String params = this.params.toString();
return this.name + "(" + params.substring(1, params.length() - 1) + ")";
}
}
2021-04-12 20:19:12 +00:00
public static final Pattern regex = Pattern.compile("\\s*(?:static|public|private|protected|final|abstract)"
+ "[\\w\\s.<>\\[\\]]*\\s+(?<name>[\\w.]+)\\s*\\((?<params>[\\w\\s,.<>\\[\\]$?]*)\\)");
2019-04-17 06:45:15 +00:00
2021-04-12 20:19:12 +00:00
private final TreeMap<Integer, Method> methods = new TreeMap<>();
2019-04-17 06:45:15 +00:00
private static String removeBrackets(String string) {
if (string.indexOf('<') != -1 && string.indexOf('>') != -1) {
return removeBrackets(string.replaceAll("<[^<>]*>", ""));
}
return string;
}
private static String getLastPart(String string, int character) {
int ch = string.lastIndexOf(character);
if (ch != -1) {
string = string.substring(ch + 1);
}
return string;
}
public void addMethod(int line, String name, String params) {
if (!name.isEmpty()) {
2021-04-12 20:19:12 +00:00
name = getLastPart(name, '.');
2019-04-17 06:45:15 +00:00
String[] args = {};
if (!params.isEmpty()) {
params = removeBrackets(params);
args = params.split(",");
for (int i = 0; i < args.length; i++) {
args[i] = args[i].trim();
if (args[i].indexOf(' ') != -1) {
String[] strings = args[i].split(" ");
args[i] = strings[strings.length - 2];
}
2021-04-12 20:19:12 +00:00
args[i] = getLastPart(args[i], '.');
args[i] = getLastPart(args[i], '$');
2019-04-17 06:45:15 +00:00
}
}
Method method = new Method(name, Arrays.asList(args));
methods.put(line, method);
}
}
public boolean isEmpty() {
return methods.isEmpty();
}
public Method getMethod(int line) {
return methods.get(line);
}
public Integer[] getMethodsLines() {
Integer[] lines = new Integer[methods.size()];
return methods.keySet().toArray(lines);
}
public String getMethodName(int line) {
Method method = methods.get(line);
if (method != null) {
if (!method.name.isEmpty())
return method.name;
}
return "";
}
public List<String> getMethodParams(int line) {
Method method = methods.get(line);
if (method != null) {
if (!method.params.isEmpty())
return method.params;
}
return null;
}
public int findMethod(Method method) {
return findMethod(method.name, method.params);
}
public int findMethod(String name, List<String> params) {
2021-04-12 20:19:12 +00:00
for (Map.Entry<Integer, Method> entry : methods.entrySet()) {
2019-04-17 06:45:15 +00:00
if (name.equals(entry.getValue().name) && params.size() == entry.getValue().params.size()) {
if (params.equals(entry.getValue().params)) {
return entry.getKey();
}
}
}
return -1;
}
public int findActiveMethod(int line) {
if (!methods.isEmpty()) {
Map.Entry<Integer, Method> low = methods.floorEntry(line);
if (low != null) {
return low.getKey();
}
}
return -1;
}
public int findNearestMethod(int line) {
if (!methods.isEmpty()) {
if (methods.size() == 1) {
return methods.firstKey();
} else {
Map.Entry<Integer, Method> low = methods.floorEntry(line);
Map.Entry<Integer, Method> high = methods.ceilingEntry(line);
if (low != null && high != null) {
2021-04-12 20:19:12 +00:00
return Math.abs(line - low.getKey()) < Math.abs(line - high.getKey()) ? low.getKey() :
high.getKey();
} else if (low != null || high != null) {
2019-04-17 06:45:15 +00:00
return low != null ? low.getKey() : high.getKey();
}
}
}
return -1;
}
}