예제 #1
0
  @Test
  public void analyzingLambdaCreatedFromPrimitiveLambdaThatNeedsNarrowConversionFromLongToInt()
      throws Exception {
    class C {
      void m() {
        LambdaPrimitives.λ(l, idx, 0);
      }
    }

    LambdaAnalyzer lambda = lambdaIn(C.class);

    assertEquals(getType(Fn2LLtoL.class), lambda.lambdaType);

    assertEquals(list(LONG_TYPE, LONG_TYPE), list(lambda.sam.getArgumentTypes()));
    assertEquals(list(LONG_TYPE, INT_TYPE), lambda.getParameterTypes());

    assertFalse(lambda.parameterNeedsUnboxing("l"));
    assertFalse(lambda.parameterNeedsBoxing("l"));
    assertFalse(lambda.parameterNeedsNarrowConversionFromActualArgument("l"));
    assertEquals(-1, lambda.parameterNarrowConversionOpcode("l"));

    assertFalse(lambda.parameterNeedsUnboxing("idx"));
    assertFalse(lambda.parameterNeedsBoxing("idx"));
    assertTrue(lambda.parameterNeedsNarrowConversionFromActualArgument("idx"));
    assertEquals(L2I, lambda.parameterNarrowConversionOpcode("idx"));

    assertEquals(LONG_TYPE, lambda.expressionType);
    assertEquals(LONG_TYPE, lambda.sam.getReturnType());
  }
예제 #2
0
  @Test
  public void analyzingTwoArgumentsLambda() throws Exception {
    class C {
      void m() {
        λ(s, n, null);
      }
    }

    LambdaAnalyzer lambda = lambdaIn(C.class);

    assertTrue(lambda.locals.isEmpty());
    assertTrue(lambda.getMutableLocals().isEmpty());

    assertEquals(list("s", "n"), list(lambda.parameters.keySet()));
    assertEquals(list(getType(String.class), INT_TYPE), lambda.getParameterTypes());

    assertEquals(list(object, object), lambda.newLambdaParameterTypes);

    assertEquals(object, lambda.expressionType);
    assertEquals(getType(Fn2.class), lambda.lambdaType);

    assertEquals("call", lambda.sam.getName());
    assertEquals(list(object, object), list(lambda.sam.getArgumentTypes()));
    assertEquals(object, lambda.sam.getReturnType());

    assertFalse(lambda.parameterNeedsUnboxing("s"));
    assertTrue(lambda.parameterNeedsUnboxing("n"));
  }
예제 #3
0
  @Test
  public void analyzingTwoArgumentLambdaWithDefaultValue() throws Exception {
    class C {
      void m() {
        λ(n, s = "", null);
      }
    }

    LambdaAnalyzer lambda = lambdaIn(C.class);

    assertEquals(list("n", "s"), list(lambda.parameters.keySet()));
    assertEquals(list(INT_TYPE, getType(String.class)), lambda.getParameterTypes());

    assertFalse(lambda.parametersWithDefaultValue.contains("n"));
    assertTrue(lambda.parametersWithDefaultValue.contains("s"));

    assertTrue(lambda.parameterDefaultValueNeedsBoxing("n"));
    assertFalse(lambda.parameterDefaultValueNeedsBoxing("s"));
  }
예제 #4
0
  @Test
  public void analyzingLambdaCreatedFromPrimitiveLambdaThatNeedsNarrowConversionFromDoubleToFloat()
      throws Exception {
    class C {
      void m() {
        LambdaPrimitives.λ(d, fl, 0);
      }
    }

    LambdaAnalyzer lambda = lambdaIn(C.class);

    assertEquals(getType(Fn2DDtoD.class), lambda.lambdaType);
    assertEquals(list(DOUBLE_TYPE, DOUBLE_TYPE), list(lambda.sam.getArgumentTypes()));
    assertEquals(list(DOUBLE_TYPE, FLOAT_TYPE), lambda.getParameterTypes());

    assertFalse(lambda.parameterNeedsUnboxing("fl"));
    assertFalse(lambda.parameterNeedsBoxing("fl"));
    assertTrue(lambda.parameterNeedsNarrowConversionFromActualArgument("fl"));
    assertEquals(D2F, lambda.parameterNarrowConversionOpcode("fl"));
  }
예제 #5
0
  @Test
  public void analyzingLambdaCreatedFromGenericCastWithTypesThatNeedConversion() throws Exception {
    class C {
      void m() {
        I i = delegate(n, o1, dbl, 0);
      }
    }

    LambdaAnalyzer lambda = lambdaIn(C.class);

    assertEquals(getType(I.class), lambda.lambdaType);
    assertEquals("invoke", lambda.sam.getName());

    assertEquals(list("n", "o1", "dbl"), list(lambda.parameters.keySet()));

    assertEquals(list(INT_TYPE, object, DOUBLE_TYPE), list(lambda.sam.getArgumentTypes()));
    assertEquals(list(INT_TYPE, object, getType(Double.class)), lambda.getParameterTypes());

    assertFalse(lambda.parameterNeedsUnboxing("n"));
    assertFalse(lambda.parameterNeedsBoxing("n"));

    assertFalse(lambda.parameterNeedsUnboxing("o1"));
    assertFalse(lambda.parameterNeedsBoxing("o1"));

    assertFalse(lambda.parameterNeedsUnboxing("dbl"));
    assertTrue(lambda.parameterNeedsBoxing("dbl"));

    assertEquals(object, lambda.expressionType);
    assertEquals(LONG_TYPE, lambda.sam.getReturnType());

    assertTrue(lambda.returnNeedsUnboxing());
    assertFalse(lambda.returnNeedsBoxing());
  }
예제 #6
0
  @Test
  public void analyzingZeroArgumentLambda() throws Exception {
    class C {
      void m() {
        λ();
      }
    }

    LambdaTreeWeaver weaver = analyze(C.class);
    assertEquals(getType(C.class).getInternalName(), weaver.c.name);
    assertEquals(2, weaver.methods.size());

    MethodAnalyzer constructor = weaver.methods.get(0);
    assertEquals("<init>", constructor.m.name);
    assertTrue(constructor.lambdas.isEmpty());

    MethodAnalyzer method = weaver.methods.get(1);
    assertEquals("m", method.m.name);

    assertEquals(1, method.lambdas.size());
    LambdaAnalyzer lambda = method.lambdas.get(0);

    assertTrue(lambda.locals.isEmpty());
    assertTrue(lambda.getMutableLocals().isEmpty());

    assertTrue(lambda.parameters.isEmpty());
    assertTrue(lambda.newLambdaParameterTypes.isEmpty());

    assertEquals(object, lambda.expressionType);
    assertEquals(getType(Fn0.class), lambda.lambdaType);

    assertEquals("call", lambda.sam.getName());
    assertEquals(0, lambda.sam.getArgumentTypes().length);

    assertEquals(object, lambda.sam.getReturnType());
    assertFalse(lambda.returnNeedsUnboxing());
    assertFalse(lambda.returnNeedsBoxing());
  }
예제 #7
0
  @Test
  public void analyzingLambdaCreatedFromGenericCast() throws Exception {
    class C {
      void m() {
        Runnable r = delegate(null);
      }
    }

    LambdaAnalyzer lambda = lambdaIn(C.class);

    assertTrue(lambda.parameters.isEmpty());
    assertTrue(lambda.newLambdaParameterTypes.isEmpty());

    assertEquals(object, lambda.expressionType);
    assertEquals(getType(Runnable.class), lambda.lambdaType);

    assertEquals("run", lambda.sam.getName());
    assertEquals(0, lambda.sam.getArgumentTypes().length);

    assertEquals(VOID_TYPE, lambda.sam.getReturnType());
    assertFalse(lambda.returnNeedsBoxing());
  }
예제 #8
0
  @Test
  public void analyzingLambdaCreatedFromGenericCastWhichAlsoDefinesMethodFromObject()
      throws Exception {
    class C {
      void m() {
        Comparator<?> c = delegate(o1, o2, 0);
      }
    }

    LambdaAnalyzer lambda = lambdaIn(C.class);

    assertEquals(list("o1", "o2"), list(lambda.parameters.keySet()));
    assertEquals(list(object, object), lambda.newLambdaParameterTypes);
    assertEquals(object, lambda.expressionType);

    assertEquals(getType(Comparator.class), lambda.lambdaType);

    assertEquals("compare", lambda.sam.getName());
    assertEquals(list(object, object), list(lambda.sam.getArgumentTypes()));

    assertEquals(INT_TYPE, lambda.sam.getReturnType());
    assertTrue(lambda.returnNeedsUnboxing());
    assertFalse(lambda.returnNeedsBoxing());
  }