Fix bug in dependency checker. Additional test cases for lambdas

This commit is contained in:
Alexey Andreev 2015-08-01 14:32:15 +03:00
parent 8cd134bc90
commit 0b7e78d9f9
2 changed files with 47 additions and 2 deletions

View File

@ -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);

View File

@ -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> T[] map(T[] array, Function<T> 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 <T> int[] mapToInt(T[] array, IntFunction<T> 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> {
T apply(T value);
}
interface IntFunction<T> {
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);
}
}
}