我想知道给出 Chapter 4 The class file Format - Section 4.3.3 中指定的方法描述符的方法的参数数量。是否有任何内置函数提供此实用程序?
最佳答案
我不知道内置方法,但偶尔我会实现一个。
private static Pattern allParamsPattern = Pattern.compile("(\\(.*?\\))");
private static Pattern paramsPattern = Pattern.compile("(\\[?)(C|Z|S|I|J|F|D|(:?L[^;]+;))");
int getMethodParamCount(String methodRefType) {
Matcher m = allParamsPattern.matcher(methodRefType);
if (!m.find()) {
throw new IllegalArgumentException("Method signature does not contain parameters");
}
String paramsDescriptor = m.group(1);
Matcher mParam = paramsPattern.matcher(paramsDescriptor);
int count = 0;
while (mParam.find()) {
count++;
}
return count;
}
欢迎您引用完整源码here .
关于java - 如何计算方法描述符的参数数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11087404/