1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
/*******************************************************************************
* Copyright (c) 2020 Jesper Steen Møller and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Jesper Steen Møller - initial API and implementation
*******************************************************************************/
import java.util.Objects;
import java.util.function.Function;
public class FunctionalCaptureTest18 {
FunctionalCaptureTest18(int field) {
publicField = field;
}
public static void main(String[] args) {
new FunctionalCaptureTest18(1).runAssertions(1);
}
static <I, O> O assertFunctionalExpression(Function<I, O> functional, I input, O expected) {
O result = functional.apply(input);
if (!Objects.equals(result, expected)) {
throw new RuntimeException("Expected " + expected + ", got " + result);
}
return result;
}
@Override
public int hashCode() {
return 1992;
}
@Override
public boolean equals(Object obj) {
if (this == obj || obj == null)
return false;
return getClass() == obj.getClass();
}
final public int publicField;
private int privateField = 2;
public static int publicStaticField = 3;
private static int privateStaticField = 4;
public Integer publicMethod() {
return 5;
}
private Integer privateMethod() {
return 6;
}
public int publicArgMethod(int i) {
return i + 6;
}
private int privateArgMethod(int i) {
return i + 7;
}
public static int publicStaticMethod(int i) {
return i - 8;
}
private static int privateStaticMethod(int i) {
return i - 9;
}
public void runAssertions(int parameter /* = 1 */) {
int localConstI = -3;
System.out.println("Go!");
/* CHECK EXPRESSIONS BELOW */
/* Nothing captured */
assertFunctionalExpression(n -> -3, 42, -3);
assertFunctionalExpression(n -> n / 2 - 7, 10, -2);
/* Capture locals */
/* But not yet on project's types */
assertFunctionalExpression(n -> n + localConstI, 2, -1);
assertFunctionalExpression(n -> n - parameter, parameter, 0);
/* Capture instance fields */
/* But not yet on enclosing instance's instance fields */
assertFunctionalExpression(n -> n - publicField, 2, 1);
assertFunctionalExpression(n -> n - this.publicField, 2, 1);
assertFunctionalExpression(n -> n - privateField, 4, 2);/* SKIP */
assertFunctionalExpression(n -> n - this.privateField, 4, 2);/* SKIP */
/* Capture static fields */
/* But not yet on non-public fields */
assertFunctionalExpression(n -> n - publicStaticField, 6, 3);
assertFunctionalExpression(n -> n - privateStaticField, 8, 4);/* SKIP */
/* Evaluate unbound method references */
/* But not yet on project's non-public methods */
assertFunctionalExpression(FunctionalCaptureTest18::publicMethod, this, 5);
assertFunctionalExpression(FunctionalCaptureTest18::privateMethod, this, 6);/* SKIP */
/* Evaluate instance method references */
assertFunctionalExpression("Hello, "::concat, "World", "Hello, World");
/* But not yet this-references */
assertFunctionalExpression(this::publicArgMethod, 1, 7);
assertFunctionalExpression(this::privateArgMethod, 1, 8);/* SKIP */
/* Evaluate static method references */
assertFunctionalExpression(Integer::valueOf, "16", 16);
assertFunctionalExpression(FunctionalCaptureTest18::publicStaticMethod, 17, 9);
/* But not yet on project's non-public methods */
assertFunctionalExpression(FunctionalCaptureTest18::privateStaticMethod, 19, 10);/* SKIP */
/* Capture methods */
assertFunctionalExpression(s -> Integer.valueOf(s, 16), "0B", 11);
assertFunctionalExpression(obj -> obj.publicMethod() + 7, this, 12);
assertFunctionalExpression(obj -> this.publicMethod() + 8, this, 13);
assertFunctionalExpression(obj -> publicMethod() + 8, this, 13);
assertFunctionalExpression(obj -> obj.privateMethod() + 8, this, 14);
assertFunctionalExpression(obj -> this.privateMethod() + 9, this, 15);
assertFunctionalExpression(obj -> privateMethod() + 9, this, 15);
/* Constructor references */
assertFunctionalExpression(String::new, new char[] { 'a','b','c' }, "abc");
assertFunctionalExpression(FunctionalCaptureTest18::new, 42, new FunctionalCaptureTest18(42));
/* END OF TESTS */
System.out.println("OK");
}
}
|