Search in sources :

Example 1 with LambdaInfo

use of ninja.utils.Lambdas.LambdaInfo in project ninja by ninjaframework.

the class LambdasTest method staticMethodReference.

@Test
public void staticMethodReference() throws Exception {
    Function1<Long, String> lambda = LambdasTest::longToString;
    LambdaInfo lambdaInfo = Lambdas.reflect(lambda);
    assertThat(lambdaInfo.getKind(), is(Kind.STATIC_METHOD_REFERENCE));
    SerializedLambda serializedLambda = lambdaInfo.getSerializedLambda();
    assertThat(serializedLambda.getFunctionalInterfaceMethodName(), is("apply"));
    assertThat(serializedLambda.getImplClass().replace('/', '.'), is(LambdasTest.class.getCanonicalName()));
    assertThat(serializedLambda.getImplMethodName(), is("longToString"));
    // 6 = static method
    assertThat(serializedLambda.getImplMethodKind(), is(6));
    assertThat(serializedLambda.getCapturedArgCount(), is(0));
    // verify it can be dynamically invoked
    String value = (String) lambdaInfo.getImplementationMethod().invoke(null, 1L);
    assertThat(value, is("1"));
}
Also used : LambdaInfo(ninja.utils.Lambdas.LambdaInfo) SerializedLambda(java.lang.invoke.SerializedLambda) Test(org.junit.Test)

Example 2 with LambdaInfo

use of ninja.utils.Lambdas.LambdaInfo in project ninja by ninjaframework.

the class LambdasTest method specificInstanceMethodReference.

@Test
public void specificInstanceMethodReference() throws Exception {
    Calculator calc = new Calculator(1L);
    Function1<Long, String> lambda = calc::l2s;
    LambdaInfo lambdaInfo = Lambdas.reflect(lambda);
    assertThat(lambdaInfo.getKind(), is(Kind.SPECIFIC_INSTANCE_METHOD_REFERENCE));
    SerializedLambda serializedLambda = lambdaInfo.getSerializedLambda();
    assertThat(serializedLambda.getFunctionalInterfaceMethodName(), is("apply"));
    assertThat(serializedLambda.getImplClass().replace('/', '.').replace('$', '.'), is(Calculator.class.getCanonicalName()));
    assertThat(serializedLambda.getImplMethodName(), is("l2s"));
    assertThat(serializedLambda.getImplMethodSignature(), is("(Ljava/lang/Long;)Ljava/lang/String;"));
    // captured "this"
    assertThat(serializedLambda.getCapturedArgCount(), is(1));
    // captured "this"
    assertThat(serializedLambda.getCapturedArg(0), is(calc));
    // verify it can be dynamically invoked
    String value = (String) lambdaInfo.getFunctionalMethod().invoke(lambda, 1L);
    //String value = (String)lambda.getClass().getMethod("apply", Long.class).invoke(calc, 1L);
    assertThat(value, is("2"));
}
Also used : LambdaInfo(ninja.utils.Lambdas.LambdaInfo) SerializedLambda(java.lang.invoke.SerializedLambda) Test(org.junit.Test)

Example 3 with LambdaInfo

use of ninja.utils.Lambdas.LambdaInfo in project ninja by ninjaframework.

the class LambdasTest method anyInstanceMethodReference.

@Test
public void anyInstanceMethodReference() throws Exception {
    ControllerMethod1<LambdasTest> lambda = LambdasTest::home;
    LambdaInfo lambdaInfo = Lambdas.reflect(lambda);
    assertThat(lambdaInfo.getKind(), is(Kind.ANY_INSTANCE_METHOD_REFERENCE));
    SerializedLambda serializedLambda = lambdaInfo.getSerializedLambda();
    assertThat(serializedLambda.getFunctionalInterfaceMethodName(), is("apply"));
    assertThat(serializedLambda.getImplClass().replace('/', '.'), is(LambdasTest.class.getCanonicalName()));
    assertThat(serializedLambda.getImplMethodName(), is("home"));
    assertThat(serializedLambda.getImplMethodSignature(), is("()Lninja/Result;"));
    assertThat(serializedLambda.getCapturedArgCount(), is(0));
}
Also used : LambdaInfo(ninja.utils.Lambdas.LambdaInfo) SerializedLambda(java.lang.invoke.SerializedLambda) Test(org.junit.Test)

Example 4 with LambdaInfo

use of ninja.utils.Lambdas.LambdaInfo in project ninja by ninjaframework.

the class LambdasTest method anonymousMethodReference.

@Test
public void anonymousMethodReference() throws Exception {
    ControllerMethod1<Context> lambda = (Context context) -> Results.html().renderRaw("".getBytes(StandardCharsets.UTF_8));
    LambdaInfo lambdaInfo = Lambdas.reflect(lambda);
    assertThat(lambdaInfo.getKind(), is(Kind.ANONYMOUS_METHOD_REFERENCE));
    SerializedLambda serializedLambda = lambdaInfo.getSerializedLambda();
    assertThat(serializedLambda.getFunctionalInterfaceMethodName(), is("apply"));
    assertThat(serializedLambda.getImplClass().replace('/', '.'), is(LambdasTest.class.getCanonicalName()));
    assertThat(serializedLambda.getImplMethodName(), startsWith("lambda$"));
    assertThat(serializedLambda.getInstantiatedMethodType(), is("(Lninja/Context;)Lninja/Result;"));
    // includes captured args btw...
    assertThat(serializedLambda.getImplMethodSignature(), is("(Lninja/Context;)Lninja/Result;"));
    // 6 = REF_invokeStatic
    assertThat(serializedLambda.getImplMethodKind(), is(6));
    assertThat(serializedLambda.getCapturedArgCount(), is(0));
}
Also used : Context(ninja.Context) LambdaInfo(ninja.utils.Lambdas.LambdaInfo) SerializedLambda(java.lang.invoke.SerializedLambda) Test(org.junit.Test)

Example 5 with LambdaInfo

use of ninja.utils.Lambdas.LambdaInfo in project ninja by ninjaframework.

the class LambdasTest method anonymousClassReference.

@Test
public void anonymousClassReference() throws Exception {
    @SuppressWarnings("Convert2Lambda") ControllerMethod1<Context> lambda = new ControllerMethod1<Context>() {

        @Override
        public Result apply(Context a) {
            return Results.html().renderRaw("".getBytes(StandardCharsets.UTF_8));
        }
    };
    try {
        LambdaInfo lambdaInfo = Lambdas.reflect(lambda);
        fail();
    } catch (IllegalArgumentException e) {
    // expected
    }
}
Also used : ControllerMethod1(ninja.ControllerMethods.ControllerMethod1) Context(ninja.Context) LambdaInfo(ninja.utils.Lambdas.LambdaInfo) Test(org.junit.Test)

Aggregations

LambdaInfo (ninja.utils.Lambdas.LambdaInfo)5 Test (org.junit.Test)5 SerializedLambda (java.lang.invoke.SerializedLambda)4 Context (ninja.Context)2 ControllerMethod1 (ninja.ControllerMethods.ControllerMethod1)1