Skip to content
Permalink
Browse files
Tweak guards for receiver and parameters
  • Loading branch information
daniellansun committed May 14, 2022
1 parent 87a04f0 commit 9dbd2406e2b3eb99ef7353f7945456d79bcda3ac
Showing 2 changed files with 25 additions and 25 deletions.
@@ -59,7 +59,7 @@ public class IndyGuardsFiltersAndSignatures {
INVOKER = MethodType.methodType(Object.class, Object.class, String.class, Object[].class);

protected static final MethodHandle
SAME_CLASS, SAME_MC, IS_NULL,
SAME_CLASS, SAME_CLASSES, SAME_MC, IS_NULL,
UNWRAP_METHOD, UNWRAP_EXCEPTION,
HAS_CATEGORY_IN_CURRENT_THREAD_GUARD,
META_METHOD_INVOKER, GROOVY_OBJECT_INVOKER, GROOVY_OBJECT_GET_PROPERTY,
@@ -78,6 +78,7 @@ public class IndyGuardsFiltersAndSignatures {
static {
try {
SAME_CLASS = LOOKUP.findStatic(IndyGuardsFiltersAndSignatures.class, "sameClass", MethodType.methodType(boolean.class, Class.class, Object.class));
SAME_CLASSES = LOOKUP.findStatic(IndyGuardsFiltersAndSignatures.class, "sameClasses", MethodType.methodType(boolean.class, Object[].class, Object[].class));
SAME_MC = LOOKUP.findStatic(IndyGuardsFiltersAndSignatures.class, "isSameMetaClass", MethodType.methodType(boolean.class, MetaClass.class, Object.class));
IS_NULL = LOOKUP.findStatic(IndyGuardsFiltersAndSignatures.class, "isNull", OBJECT_GUARD);
UNWRAP_METHOD = LOOKUP.findStatic(IndyGuardsFiltersAndSignatures.class, "unwrap", OBJECT_FILTER);
@@ -196,4 +197,22 @@ public static boolean sameClass(Class<?> c, Object o) {
if (o == null) return false;
return o.getClass() == c;
}

/**
* Guard to check if the provided objects have the same
* class as the other provided objects.
*/
public static boolean sameClasses(Object[] cs, Object[] os) {
for (int i = 0; i < cs.length; i++) {
Object c = cs[i];
if (null == c) continue;

Object o = os[i];
if (null == o) return false;

if (o.getClass() != c.getClass())
return false;
}
return true;
}
}
@@ -85,7 +85,7 @@
import static org.codehaus.groovy.vmplugin.v8.IndyGuardsFiltersAndSignatures.MOP_INVOKE_CONSTRUCTOR;
import static org.codehaus.groovy.vmplugin.v8.IndyGuardsFiltersAndSignatures.MOP_INVOKE_METHOD;
import static org.codehaus.groovy.vmplugin.v8.IndyGuardsFiltersAndSignatures.NULL_REF;
import static org.codehaus.groovy.vmplugin.v8.IndyGuardsFiltersAndSignatures.SAME_CLASS;
import static org.codehaus.groovy.vmplugin.v8.IndyGuardsFiltersAndSignatures.SAME_CLASSES;
import static org.codehaus.groovy.vmplugin.v8.IndyGuardsFiltersAndSignatures.SAME_MC;
import static org.codehaus.groovy.vmplugin.v8.IndyGuardsFiltersAndSignatures.SAM_CONVERSION;
import static org.codehaus.groovy.vmplugin.v8.IndyGuardsFiltersAndSignatures.UNWRAP_EXCEPTION;
@@ -923,29 +923,10 @@ public void setGuards(Object receiver) {

// guards for receiver and parameter
Class<?>[] pt = handle.type().parameterArray();
for (int i = 0; i < args.length; i++) {
Object arg = args[i];
Class<?> paramType = pt[i];
MethodHandle test;

if (arg == null) {
test = IS_NULL.asType(MethodType.methodType(boolean.class, paramType));
if (LOG_ENABLED) LOG.info("added null argument check at pos " + i);
} else {
if (Modifier.isFinal(paramType.getModifiers())) {
// primitive types are also `final`
continue;
}
test = SAME_CLASS.
bindTo(arg.getClass()).
asType(MethodType.methodType(boolean.class, paramType));
if (LOG_ENABLED) LOG.info("added same class check at pos " + i);
}
Class<?>[] drops = new Class[i];
System.arraycopy(pt, 0, drops, 0, drops.length);
test = MethodHandles.dropArguments(test, 0, drops);
handle = MethodHandles.guardWithTest(test, handle, fallback);
}
MethodHandle test = SAME_CLASSES.bindTo(args)
.asCollector(Object[].class, pt.length)
.asType(MethodType.methodType(boolean.class, pt));
handle = MethodHandles.guardWithTest(test, handle, fallback);
}

/**

0 comments on commit 9dbd240

Please sign in to comment.