Skip to content

Commit

Permalink
Showing 1 changed file with 8 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -12,23 +12,12 @@
import com.oracle.truffle.api.dsl.Specialization;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

public class AmbiguousOptionalArgumentChecker {

private static final Method GET_PARAMETERS = checkParametersNamesAvailable();
public static boolean SUCCESS = true;

private static Method checkParametersNamesAvailable() {
try {
return Method.class.getMethod("getParameters");
} catch (NoSuchMethodException | SecurityException e) {
// Java 7 or could not find how to get names of method parameters
System.err.println("Could not find method Method.getParameters()");
System.exit(1);
return null;
}
}

public static void verifyNoAmbiguousOptionalArguments(CoreMethodNodeManager.MethodDetails methodDetails) {
try {
verifyNoAmbiguousOptionalArgumentsWithReflection(methodDetails);
@@ -60,15 +49,15 @@ private static void verifyNoAmbiguousOptionalArgumentsWithReflection(CoreMethodN
n--; // ignore final Object[] argument
}
Class<?> parameterType = parameterTypes[n];
Object[] parameters = (Object[]) GET_PARAMETERS.invoke(method);
Parameter[] parameters = method.getParameters();

Object parameter = parameters[n];
boolean isNamePresent = (boolean) parameter.getClass().getMethod("isNamePresent").invoke(parameter);
Parameter parameter = parameters[n];
boolean isNamePresent = parameter.isNamePresent();
if (!isNamePresent) {
System.err.println("Method parameters names are not available for " + method);
System.exit(1);
}
String name = (String) parameter.getClass().getMethod("getName").invoke(parameter);
String name = parameter.getName();

if (parameterType == Object.class && !name.startsWith("unused") && !name.equals("maybeBlock")) {
String[] guards = method.getAnnotation(Specialization.class).guards();
@@ -101,12 +90,12 @@ private static boolean isGuarded(String name, String[] guards) {
return false;
}

private static String methodToString(Method method, Class<?>[] parameterTypes, Object[] parameters) throws ReflectiveOperationException {
private static String methodToString(Method method, Class<?>[] parameterTypes, Parameter[] parameters) throws ReflectiveOperationException {
StringBuilder str = new StringBuilder();
str.append(method.getName()).append("(");
for (int i = 0; i < parameters.length; i++) {
Object parameter = parameters[i];
String name = (String) parameter.getClass().getMethod("getName").invoke(parameter);
Parameter parameter = parameters[i];
String name = parameter.getName();
str.append(parameterTypes[i].getSimpleName()).append(" ").append(name);
if (i < parameters.length - 1) {
str.append(", ");

1 comment on commit 63b893c

@eregon
Copy link
Member

@eregon eregon commented on 63b893c May 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, this should make this piece of code much nicer 😃
We still need special compilation for the ambiguous args detection to work since by default parameters names are not included in the bytecode.

Sorry, something went wrong.

Please sign in to comment.