Search in sources :

Example 1 with ClassParameter

use of org.robolectric.util.ReflectionHelpers.ClassParameter in project robolectric by robolectric.

the class AndroidInterceptorsIntegrationTest method invokeDynamic.

@SuppressWarnings({ "unchecked", "TypeParameterUnusedInFormals" })
private static <T> T invokeDynamic(Class<?> cls, String methodName, Class<?> returnType, ClassParameter<?>... params) throws Throwable {
    MethodType methodType = MethodType.methodType(returnType, Arrays.stream(params).map(param -> param.clazz).toArray(Class[]::new));
    CallSite callsite = InvokeDynamicSupport.bootstrapIntrinsic(MethodHandles.lookup(), methodName, methodType, cls.getName());
    return (T) callsite.dynamicInvoker().invokeWithArguments(Arrays.stream(params).map(param -> param.val).collect(Collectors.toList()));
}
Also used : PrintStream(java.io.PrintStream) Socket(java.net.Socket) Arrays(java.util.Arrays) InvokeDynamicSupport(org.robolectric.internal.bytecode.InvokeDynamicSupport) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ShadowLooper(org.robolectric.shadows.ShadowLooper) ClassParameter(org.robolectric.util.ReflectionHelpers.ClassParameter) MethodHandles(java.lang.invoke.MethodHandles) RunWith(org.junit.runner.RunWith) SystemClock(android.os.SystemClock) Test(org.junit.Test) LooperMode(org.robolectric.annotation.LooperMode) Truth.assertThat(com.google.common.truth.Truth.assertThat) Field(java.lang.reflect.Field) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) Collectors(java.util.stream.Collectors) MethodType(java.lang.invoke.MethodType) CallSite(java.lang.invoke.CallSite) ShadowSystemClock(org.robolectric.shadows.ShadowSystemClock) FileDescriptor(java.io.FileDescriptor) Duration(java.time.Duration) Pattern(java.util.regex.Pattern) MethodType(java.lang.invoke.MethodType) CallSite(java.lang.invoke.CallSite)

Example 2 with ClassParameter

use of org.robolectric.util.ReflectionHelpers.ClassParameter in project robolectric by robolectric.

the class ShadowSensorManager method createSensorEvent.

/**
 * Creates a {@link SensorEvent} for the given {@link Sensor} type with the given value array
 * size, which the caller should set based on the type of sensor which is being emulated.
 *
 * <p>Callers can then specify individual values for the event. For example, for a proximity event
 * a caller may wish to specify the distance value:
 *
 * <pre>{@code
 * event.values[0] = distance;
 * }</pre>
 *
 * <p>See {@link SensorEvent#values} for more information about values.
 */
public static SensorEvent createSensorEvent(int valueArraySize, int sensorType) {
    checkArgument(valueArraySize > 0);
    ClassParameter<Integer> valueArraySizeParam = new ClassParameter<>(int.class, valueArraySize);
    SensorEvent sensorEvent = ReflectionHelpers.callConstructor(SensorEvent.class, valueArraySizeParam);
    sensorEvent.sensor = ShadowSensor.newInstance(sensorType);
    return sensorEvent;
}
Also used : ClassParameter(org.robolectric.util.ReflectionHelpers.ClassParameter) SensorEvent(android.hardware.SensorEvent)

Aggregations

ClassParameter (org.robolectric.util.ReflectionHelpers.ClassParameter)2 SensorEvent (android.hardware.SensorEvent)1 SystemClock (android.os.SystemClock)1 AndroidJUnit4 (androidx.test.ext.junit.runners.AndroidJUnit4)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileDescriptor (java.io.FileDescriptor)1 PrintStream (java.io.PrintStream)1 CallSite (java.lang.invoke.CallSite)1 MethodHandles (java.lang.invoke.MethodHandles)1 MethodType (java.lang.invoke.MethodType)1 Field (java.lang.reflect.Field)1 Socket (java.net.Socket)1 Duration (java.time.Duration)1 Arrays (java.util.Arrays)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 Test (org.junit.Test)1 RunWith (org.junit.runner.RunWith)1 LooperMode (org.robolectric.annotation.LooperMode)1