From b1dd2677074c2e8f5a40566821fa2535a034a86c Mon Sep 17 00:00:00 2001 From: Eric Bruneton Date: Sat, 31 Mar 2018 14:46:04 +0200 Subject: [PATCH 1/2] Improve the code quality of Method. --- .../org/objectweb/asm/commons/Method.java | 153 ++++++++++-------- 1 file changed, 83 insertions(+), 70 deletions(-) diff --git a/asm-commons/src/main/java/org/objectweb/asm/commons/Method.java b/asm-commons/src/main/java/org/objectweb/asm/commons/Method.java index f1befb11..fa244946 100644 --- a/asm-commons/src/main/java/org/objectweb/asm/commons/Method.java +++ b/asm-commons/src/main/java/org/objectweb/asm/commons/Method.java @@ -45,33 +45,33 @@ public class Method { private final String name; /** The method descriptor. */ - private final String desc; + private final String descriptor; - /** Maps primitive Java type names to their descriptors. */ - private static final Map DESCRIPTORS; + /** The descriptors of the primitive Java types (plus void). */ + private static final Map PRIMITIVE_TYPE_DESCRIPTORS; static { - DESCRIPTORS = new HashMap(); - DESCRIPTORS.put("void", "V"); - DESCRIPTORS.put("byte", "B"); - DESCRIPTORS.put("char", "C"); - DESCRIPTORS.put("double", "D"); - DESCRIPTORS.put("float", "F"); - DESCRIPTORS.put("int", "I"); - DESCRIPTORS.put("long", "J"); - DESCRIPTORS.put("short", "S"); - DESCRIPTORS.put("boolean", "Z"); + PRIMITIVE_TYPE_DESCRIPTORS = new HashMap(); + PRIMITIVE_TYPE_DESCRIPTORS.put("void", "V"); + PRIMITIVE_TYPE_DESCRIPTORS.put("byte", "B"); + PRIMITIVE_TYPE_DESCRIPTORS.put("char", "C"); + PRIMITIVE_TYPE_DESCRIPTORS.put("double", "D"); + PRIMITIVE_TYPE_DESCRIPTORS.put("float", "F"); + PRIMITIVE_TYPE_DESCRIPTORS.put("int", "I"); + PRIMITIVE_TYPE_DESCRIPTORS.put("long", "J"); + PRIMITIVE_TYPE_DESCRIPTORS.put("short", "S"); + PRIMITIVE_TYPE_DESCRIPTORS.put("boolean", "Z"); } /** * Constructs a new {@link Method}. * * @param name the method's name. - * @param desc the method's descriptor. + * @param descriptor the method's descriptor. */ - public Method(final String name, final String desc) { + public Method(final String name, final String descriptor) { this.name = name; - this.desc = desc; + this.descriptor = descriptor; } /** @@ -88,21 +88,21 @@ public class Method { /** * Creates a new {@link Method}. * - * @param m a java.lang.reflect method descriptor + * @param method a java.lang.reflect method descriptor * @return a {@link Method} corresponding to the given Java method declaration. */ - public static Method getMethod(java.lang.reflect.Method m) { - return new Method(m.getName(), Type.getMethodDescriptor(m)); + public static Method getMethod(final java.lang.reflect.Method method) { + return new Method(method.getName(), Type.getMethodDescriptor(method)); } /** * Creates a new {@link Method}. * - * @param c a java.lang.reflect constructor descriptor + * @param constructor a java.lang.reflect constructor descriptor * @return a {@link Method} corresponding to the given Java constructor declaration. */ - public static Method getMethod(java.lang.reflect.Constructor c) { - return new Method("", Type.getConstructorDescriptor(c)); + public static Method getMethod(final java.lang.reflect.Constructor constructor) { + return new Method("", Type.getConstructorDescriptor(constructor)); } /** @@ -115,7 +115,7 @@ public class Method { * @return a {@link Method} corresponding to the given Java method declaration. * @throws IllegalArgumentException if method could not get parsed. */ - public static Method getMethod(final String method) throws IllegalArgumentException { + public static Method getMethod(final String method) { return getMethod(method, false); } @@ -133,63 +133,76 @@ public class Method { * @return a {@link Method} corresponding to the given Java method declaration. * @throws IllegalArgumentException if method could not get parsed. */ - public static Method getMethod(final String method, final boolean defaultPackage) - throws IllegalArgumentException { - int space = method.indexOf(' '); - int start = method.indexOf('(', space) + 1; - int end = method.indexOf(')', start); - if (space == -1 || start == 0 || end == -1) { + public static Method getMethod(final String method, final boolean defaultPackage) { + int spaceIndex = method.indexOf(' '); + int currentArgumentStartIndex = method.indexOf('(', spaceIndex) + 1; + int endIndex = method.indexOf(')', currentArgumentStartIndex); + if (spaceIndex == -1 || currentArgumentStartIndex == 0 || endIndex == -1) { throw new IllegalArgumentException(); } - String returnType = method.substring(0, space); - String methodName = method.substring(space + 1, start - 1).trim(); - StringBuilder sb = new StringBuilder(); - sb.append('('); - int p; + String returnType = method.substring(0, spaceIndex); + String methodName = method.substring(spaceIndex + 1, currentArgumentStartIndex - 1).trim(); + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append('('); + int currentArgumentEndIndex; do { - String s; - p = method.indexOf(',', start); - if (p == -1) { - s = map(method.substring(start, end).trim(), defaultPackage); + String argumentDescriptor; + currentArgumentEndIndex = method.indexOf(',', currentArgumentStartIndex); + if (currentArgumentEndIndex == -1) { + argumentDescriptor = + getDescriptor( + method.substring(currentArgumentStartIndex, endIndex).trim(), defaultPackage); } else { - s = map(method.substring(start, p).trim(), defaultPackage); - start = p + 1; + argumentDescriptor = + getDescriptor( + method.substring(currentArgumentStartIndex, currentArgumentEndIndex).trim(), + defaultPackage); + currentArgumentStartIndex = currentArgumentEndIndex + 1; } - sb.append(s); - } while (p != -1); - sb.append(')'); - sb.append(map(returnType, defaultPackage)); - return new Method(methodName, sb.toString()); + stringBuilder.append(argumentDescriptor); + } while (currentArgumentEndIndex != -1); + stringBuilder.append(')'); + stringBuilder.append(getDescriptor(returnType, defaultPackage)); + return new Method(methodName, stringBuilder.toString()); } - private static String map(final String type, final boolean defaultPackage) { + /** + * Returns the descriptor corresponding to the given type name. + * + * @param type a Java type name. + * @param defaultPackage true if unqualified class names belong to the default package, or false + * if they correspond to java.lang classes. For instance "Object" means "Object" if this + * option is true, or "java.lang.Object" otherwise. + * @return the descriptor corresponding to the given type name. + */ + private static String getDescriptor(final String type, final boolean defaultPackage) { if ("".equals(type)) { return type; } - StringBuilder sb = new StringBuilder(); - int index = 0; - while ((index = type.indexOf("[]", index) + 1) > 0) { - sb.append('['); + StringBuilder stringBuilder = new StringBuilder(); + int arrayBracketsIndex = 0; + while ((arrayBracketsIndex = type.indexOf("[]", arrayBracketsIndex) + 1) > 0) { + stringBuilder.append('['); } - String t = type.substring(0, type.length() - sb.length() * 2); - String desc = DESCRIPTORS.get(t); - if (desc != null) { - sb.append(desc); + String elementType = type.substring(0, type.length() - stringBuilder.length() * 2); + String descriptor = PRIMITIVE_TYPE_DESCRIPTORS.get(elementType); + if (descriptor != null) { + stringBuilder.append(descriptor); } else { - sb.append('L'); - if (t.indexOf('.') < 0) { + stringBuilder.append('L'); + if (elementType.indexOf('.') < 0) { if (!defaultPackage) { - sb.append("java/lang/"); + stringBuilder.append("java/lang/"); } - sb.append(t); + stringBuilder.append(elementType); } else { - sb.append(t.replace('.', '/')); + stringBuilder.append(elementType.replace('.', '/')); } - sb.append(';'); + stringBuilder.append(';'); } - return sb.toString(); + return stringBuilder.toString(); } /** @@ -207,7 +220,7 @@ public class Method { * @return the descriptor of the method described by this object. */ public String getDescriptor() { - return desc; + return descriptor; } /** @@ -216,7 +229,7 @@ public class Method { * @return the return type of the method described by this object. */ public Type getReturnType() { - return Type.getReturnType(desc); + return Type.getReturnType(descriptor); } /** @@ -225,25 +238,25 @@ public class Method { * @return the argument types of the method described by this object. */ public Type[] getArgumentTypes() { - return Type.getArgumentTypes(desc); + return Type.getArgumentTypes(descriptor); } @Override public String toString() { - return name + desc; + return name + descriptor; } @Override - public boolean equals(final Object o) { - if (!(o instanceof Method)) { + public boolean equals(final Object other) { + if (!(other instanceof Method)) { return false; } - Method other = (Method) o; - return name.equals(other.name) && desc.equals(other.desc); + Method otherMethod = (Method) other; + return name.equals(otherMethod.name) && descriptor.equals(otherMethod.descriptor); } @Override public int hashCode() { - return name.hashCode() ^ desc.hashCode(); + return name.hashCode() ^ descriptor.hashCode(); } } -- GitLab From 4fa2c941c04871b3cb68027444fe8732068728c6 Mon Sep 17 00:00:00 2001 From: Eric Bruneton Date: Thu, 5 Apr 2018 20:31:44 +0200 Subject: [PATCH 2/2] Address the merge request comments. --- .../org/objectweb/asm/commons/Method.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/asm-commons/src/main/java/org/objectweb/asm/commons/Method.java b/asm-commons/src/main/java/org/objectweb/asm/commons/Method.java index fa244946..c502f2f4 100644 --- a/asm-commons/src/main/java/org/objectweb/asm/commons/Method.java +++ b/asm-commons/src/main/java/org/objectweb/asm/commons/Method.java @@ -51,16 +51,17 @@ public class Method { private static final Map PRIMITIVE_TYPE_DESCRIPTORS; static { - PRIMITIVE_TYPE_DESCRIPTORS = new HashMap(); - PRIMITIVE_TYPE_DESCRIPTORS.put("void", "V"); - PRIMITIVE_TYPE_DESCRIPTORS.put("byte", "B"); - PRIMITIVE_TYPE_DESCRIPTORS.put("char", "C"); - PRIMITIVE_TYPE_DESCRIPTORS.put("double", "D"); - PRIMITIVE_TYPE_DESCRIPTORS.put("float", "F"); - PRIMITIVE_TYPE_DESCRIPTORS.put("int", "I"); - PRIMITIVE_TYPE_DESCRIPTORS.put("long", "J"); - PRIMITIVE_TYPE_DESCRIPTORS.put("short", "S"); - PRIMITIVE_TYPE_DESCRIPTORS.put("boolean", "Z"); + HashMap descriptors = new HashMap(); + descriptors.put("void", "V"); + descriptors.put("byte", "B"); + descriptors.put("char", "C"); + descriptors.put("double", "D"); + descriptors.put("float", "F"); + descriptors.put("int", "I"); + descriptors.put("long", "J"); + descriptors.put("short", "S"); + descriptors.put("boolean", "Z"); + PRIMITIVE_TYPE_DESCRIPTORS = descriptors; } /** -- GitLab