Skip to content

Instantly share code, notes, and snippets.

@duqicauc
Created August 15, 2018 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duqicauc/f723a6769c09a0f147d1f87205c1842a to your computer and use it in GitHub Desktop.
Save duqicauc/f723a6769c09a0f147d1f87205c1842a to your computer and use it in GitHub Desktop.
将类的JVM规范名称转换为SimpleName
import com.google.common.collect.ImmutableMap;
import java.util.Map;
/**
* @author duqi
* @createTime 2018/8/14 下午3:18
**/
public class ClassNameUtil {
private static final Map<String, String> descriptorsMap = ImmutableMap.<String, String>builder()
.put("B", "byte") //signed byte
.put("C", "char") //Unicode character code point in the Basic Multilingual Plane, encoded with UTF-16
.put("D", "double") //double-precision floating-point value
.put("F", "float") //single-precision floating-point value
.put("I", "int") //integer
.put("J", "long") //long integer
.put("S", "short") //signed short
.put("Z", "boolean") //true or false
.build();
// L ClassName ;,用于表示一个类名
// [ 开头,表示是一个数组
public static String convertNameToSimpleName(String sigName) {
if (sigName == null) {
return null;
}
if (sigName.contains(";")) {
sigName = sigName.substring(0, sigName.length() - 1);
}
StringBuilder builder = new StringBuilder();
int length = sigName.length();
int arrLen = sigName.lastIndexOf("[");
if (sigName.charAt(arrLen + 1) == 'L') {
builder.append(sigName, arrLen + 2, length);
} else {
String simpSig = descriptorsMap.get(sigName.substring(arrLen + 1, length));
if (simpSig == null) {
builder.append(sigName);
} else {
builder.append(simpSig);
}
}
while (arrLen-- > -1) {
builder.append("[]");
}
return builder.toString();
}
public static void main(String[] args) {
System.out.println(convertNameToSimpleName("[I"));
System.out.println(convertNameToSimpleName("[[I"));
System.out.println(convertNameToSimpleName("[Ljava.lang.String;"));
System.out.println(convertNameToSimpleName("[Ljava.lang.management.MemoryUsage;"));
System.out.println(convertNameToSimpleName("java.util.TreeMap"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment