From 0b7e78d9f94e072dff32b1940e16e2c4a7a017a3 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sat, 1 Aug 2015 14:32:15 +0300 Subject: [PATCH] Fix bug in dependency checker. Additional test cases for lambdas --- .../dependency/DependencyGraphBuilder.java | 2 +- .../teavm/classlib/java/lang/LambdaTest.java | 47 ++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/teavm-core/src/main/java/org/teavm/dependency/DependencyGraphBuilder.java b/teavm-core/src/main/java/org/teavm/dependency/DependencyGraphBuilder.java index e9a3dc7a0..06cc57e8a 100644 --- a/teavm-core/src/main/java/org/teavm/dependency/DependencyGraphBuilder.java +++ b/teavm-core/src/main/java/org/teavm/dependency/DependencyGraphBuilder.java @@ -337,7 +337,7 @@ class DependencyGraphBuilder { } ClassReaderSource classSource = checker.getClassSource(); - if (classSource.isSuperType(filterClass.getName(), className).orElse(false)) { + if (!classSource.isSuperType(filterClass.getName(), className).orElse(false)) { return; } MethodReference methodRef = new MethodReference(className, methodDesc); diff --git a/teavm-tests/src/test/java/org/teavm/classlib/java/lang/LambdaTest.java b/teavm-tests/src/test/java/org/teavm/classlib/java/lang/LambdaTest.java index d96117932..5e02ac667 100644 --- a/teavm-tests/src/test/java/org/teavm/classlib/java/lang/LambdaTest.java +++ b/teavm-tests/src/test/java/org/teavm/classlib/java/lang/LambdaTest.java @@ -1,6 +1,7 @@ package org.teavm.classlib.java.lang; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; import java.lang.reflect.Array; import org.junit.Test; @@ -16,9 +17,33 @@ public class LambdaTest { assertArrayEquals(new Integer[] { 3, 5, 7 }, array); } + @Test + public void lambdaWorksWithLiteral() { + A[] src = { new A(1), new A(2), new A(3) }; + A[] array = map(src, A::inc); + assertEquals(3, array.length); + assertEquals(2, array[0].value); + assertEquals(3, array[1].value); + } + + @Test + public void lambdaWorksWithFieldLiteral() { + A[] src = { new A(1), new A(2), new A(3) }; + int[] array = mapToInt(src, a -> a.value); + assertArrayEquals(new int[] { 1, 2, 3 }, array); + } + static T[] map(T[] array, Function f) { @SuppressWarnings("unchecked") - T[] result = (T[])Array.newInstance(array.getClass().getComponentType(), array.length); + T[] result = (T[]) Array.newInstance(array.getClass().getComponentType(), array.length); + for (int i = 0; i < result.length; ++i) { + result[i] = f.apply(array[i]); + } + return result; + } + + static int[] mapToInt(T[] array, IntFunction f) { + int[] result = new int[array.length]; for (int i = 0; i < result.length; ++i) { result[i] = f.apply(array[i]); } @@ -28,4 +53,24 @@ public class LambdaTest { interface Function { T apply(T value); } + + interface IntFunction { + int apply(T value); + } + + static class A { + int value; + + A(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + A inc() { + return new A(value + 1); + } + } }