use of java.lang.invoke.MethodType in project seph by seph-lang.
the class SephCallSite method setSpecialized.
public void setSpecialized(TypeProfile allProfiles) {
MethodType type = this.type();
MethodHandle elsePath = slowPath;
elsePath = filterArguments(elsePath, 0, computeProfiler(true));
TypeProfile[] cases = allProfiles.cases();
int casesToCode = cases.length;
for (int i = casesToCode - 1; i >= 0; i--) {
MethodHandle[] guardAndFastPath = computeGuardAndFastPath(cases[i]);
if (guardAndFastPath == null) {
continue;
}
MethodHandle guard = guardAndFastPath[0];
MethodHandle fastPath = guardAndFastPath[1];
elsePath = guardWithTest(guard, fastPath, elsePath);
}
setTarget(elsePath);
}
use of java.lang.invoke.MethodType in project CorfuDB by CorfuDB.
the class ClientMsgHandler method generateHandlers.
/** Generate handlers for a particular client.
*
* @param caller The context that is being used. Call MethodHandles.lookup() to obtain.
* @param o The object that implements the client.
* @return
*/
public ClientMsgHandler generateHandlers(final MethodHandles.Lookup caller, final Object o) {
Arrays.stream(o.getClass().getDeclaredMethods()).filter(x -> x.isAnnotationPresent(ClientHandler.class)).forEach(x -> {
ClientHandler a = x.getAnnotation(ClientHandler.class);
if (!x.getParameterTypes()[0].isAssignableFrom(a.type().messageType.getRawType())) {
throw new RuntimeException("Incorrect message type, expected " + a.type().messageType.getRawType() + " but provided " + x.getParameterTypes()[0]);
}
if (handlerMap.containsKey(a.type())) {
throw new RuntimeException("Handler for " + a.type() + " already registered!");
}
try {
if (Modifier.isStatic(x.getModifiers())) {
MethodHandle mh = caller.unreflect(x);
MethodType mt = mh.type().changeParameterType(0, CorfuMsg.class);
handlerMap.put(a.type(), (ClientMsgHandler.Handler) LambdaMetafactory.metafactory(caller, "handle", MethodType.methodType(ClientMsgHandler.Handler.class), mt, mh, mh.type()).getTarget().invokeExact());
} else {
MethodType mt = MethodType.methodType(x.getReturnType(), x.getParameterTypes());
MethodHandle mh = caller.findVirtual(o.getClass(), x.getName(), mt);
MethodType mtGeneric = mh.type().changeParameterType(1, CorfuMsg.class).changeReturnType(Object.class);
handlerMap.put(a.type(), (ClientMsgHandler.Handler) LambdaMetafactory.metafactory(caller, "handle", MethodType.methodType(ClientMsgHandler.Handler.class, o.getClass()), mtGeneric.dropParameterTypes(0, 1), mh, mh.type().dropParameterTypes(0, 1)).getTarget().bindTo(o).invoke());
}
} catch (Throwable e) {
throw new RuntimeException(e);
}
});
return this;
}
use of java.lang.invoke.MethodType in project robolectric by robolectric.
the class InvokeDynamicSupport method bindWithFallback.
private static MethodHandle bindWithFallback(MethodHandle mh, RoboCallSite site, MethodHandle fallback) {
SwitchPoint switchPoint = getInvalidator(site.getCaller());
MethodType type = site.type();
MethodHandle boundFallback = foldArguments(exactInvoker(type), fallback.bindTo(site));
mh = switchPoint.guardWithTest(mh.asType(type), boundFallback);
site.setTarget(mh);
return mh;
}
use of java.lang.invoke.MethodType in project robolectric by robolectric.
the class ShadowWrangler method findShadowMethod.
@Override
public MethodHandle findShadowMethod(Class<?> caller, String name, MethodType type, boolean isStatic) throws IllegalAccessException {
ShadowConfig shadowConfig = shadowConfigs.get(caller);
if (shadowConfig == null)
return CALL_REAL_CODE;
ClassLoader classLoader = caller.getClassLoader();
MethodType actualType = isStatic ? type : type.dropParameterTypes(0, 1);
Method method = findShadowMethod(classLoader, shadowConfig, name, actualType.parameterArray());
if (method == null) {
return shadowConfig.callThroughByDefault ? CALL_REAL_CODE : DO_NOTHING;
}
Class<?> declaredShadowedClass = getShadowedClass(method);
if (declaredShadowedClass.equals(Object.class)) {
// e.g. for equals(), hashCode(), toString()
return CALL_REAL_CODE;
}
boolean shadowClassMismatch = !declaredShadowedClass.equals(caller);
if (shadowClassMismatch && !shadowConfig.inheritImplementationMethods) {
return CALL_REAL_CODE;
} else {
MethodHandle mh = LOOKUP.unreflect(method);
// in MessageQueue.nativeInit() which used to be void non-static in 4.2.
if (!isStatic && Modifier.isStatic(method.getModifiers())) {
return dropArguments(mh, 0, Object.class);
} else {
return mh;
}
}
}
use of java.lang.invoke.MethodType in project robolectric by robolectric.
the class ShadowWrangler method getShadowCreator.
@Override
public MethodHandle getShadowCreator(Class<?> caller) {
String shadowClassName = getShadowClassNameInvoke(caller);
if (shadowClassName == null)
return dropArguments(NO_SHADOW_HANDLE, 0, caller);
try {
Class<?> shadowClass = Class.forName(shadowClassName, false, caller.getClassLoader());
MethodHandle constructor = LOOKUP.findConstructor(shadowClass, methodType(void.class));
MetaShadow metaShadow = getMetaShadow(shadowClass);
// (instance)
MethodHandle mh = identity(shadowClass);
// (instance)
mh = dropArguments(mh, 1, caller);
for (Field field : metaShadow.realObjectFields) {
MethodHandle setter = LOOKUP.unreflectSetter(field);
MethodType setterType = mh.type().changeReturnType(void.class);
mh = foldArguments(mh, setter.asType(setterType));
}
// (shadow, instance)
mh = foldArguments(mh, constructor);
// (instance)
return mh;
} catch (NoSuchMethodException | IllegalAccessException e) {
throw new RuntimeException("Could not instantiate shadow, missing public empty constructor.", e);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Could not instantiate shadow", e);
}
}
Aggregations