Commit b1dd2677 authored by Eric Bruneton's avatar Eric Bruneton

Improve the code quality of Method.

parent 3224adf0
Pipeline #1221 passed with stage
in 6 minutes and 13 seconds
......@@ -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<String, String> DESCRIPTORS;
/** The descriptors of the primitive Java types (plus void). */
private static final Map<String, String> PRIMITIVE_TYPE_DESCRIPTORS;
static {
DESCRIPTORS = new HashMap<String, String>();
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<String, String>();
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("<init>", Type.getConstructorDescriptor(c));
public static Method getMethod(final java.lang.reflect.Constructor<?> constructor) {
return new Method("<init>", Type.getConstructorDescriptor(constructor));
}
/**
......@@ -115,7 +115,7 @@ public class Method {
* @return a {@link Method} corresponding to the given Java method declaration.
* @throws IllegalArgumentException if <code>method</code> 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 <code>method</code> 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();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment