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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
import org.checkerframework.checker.guieffect.qual.AlwaysSafe;
import org.checkerframework.checker.guieffect.qual.PolyUI;
import org.checkerframework.checker.guieffect.qual.PolyUIEffect;
import org.checkerframework.checker.guieffect.qual.PolyUIType;
import org.checkerframework.checker.guieffect.qual.UI;
import org.checkerframework.checker.guieffect.qual.UIEffect;
public class Java8Lambdas {
private interface SafeFunctionalInterface<T> {
public void executeAlwaysSafe(T arg);
}
private interface UIFunctionalInterface<T> {
@UIEffect
public void executeUI(T arg);
}
@PolyUIType
private interface PolymorphicFunctionalInterface<T> {
@PolyUIEffect
public void executePolymorphic(T arg);
}
private static class LambdaRunner {
private final UIElement arg;
public LambdaRunner(UIElement arg) {
this.arg = arg;
}
public void doSafe(SafeFunctionalInterface<UIElement> func) {
func.executeAlwaysSafe(this.arg);
}
@UIEffect
public void doUI(UIFunctionalInterface<UIElement> func) {
func.executeUI(this.arg);
}
// Needs to be @UIEffect, because the functional interface method is @UIEffect
public void unsafeDoUI(UIFunctionalInterface<UIElement> func) {
// :: error: (call.invalid.ui)
func.executeUI(this.arg);
}
public void doEither(@PolyUI PolymorphicFunctionalInterface<UIElement> func) {
// In a real program some intelligent dispatch could be done here to avoid running on UI
// thread unless needed.
arg.runOnUIThread(
new IAsyncUITask() {
final UIElement e2 = arg;
@Override
public void doStuff() { // should inherit UI effect
func.executePolymorphic(e2); // should be okay
}
});
}
public void doUISafely(@UI PolymorphicFunctionalInterface<UIElement> func) {
// In a real program some intelligent dispatch could be done here to avoid running on UI
// thread unless needed.
arg.runOnUIThread(
new IAsyncUITask() {
final UIElement e2 = arg;
@Override
public void doStuff() { // should inherit UI effect
func.executePolymorphic(e2); // should be okay
}
});
}
}
@PolyUIType
private static class PolymorphicLambdaRunner {
private final UIElement arg;
public PolymorphicLambdaRunner(UIElement arg) {
this.arg = arg;
}
@PolyUIEffect
public void doEither(@PolyUI PolymorphicFunctionalInterface<UIElement> func) {
func.executePolymorphic(this.arg);
}
}
public static void safeContextTestCases(UIElement elem) {
LambdaRunner runner = new LambdaRunner(elem);
runner.doSafe(e -> e.repaint());
// :: error: (call.invalid.ui)
runner.doSafe(e -> e.dangerous()); // Not allowed in doSafe
// :: error: (call.invalid.ui)
runner.doUI(e -> e.repaint()); // Not allowed in safe context
// :: error: (call.invalid.ui)
runner.doUI(e -> e.dangerous()); // Not allowed in safe context
runner.doEither(e -> e.repaint());
runner.doEither(e -> e.dangerous());
runner.doUISafely(e -> e.dangerous());
@AlwaysSafe PolymorphicLambdaRunner safePolymorphicLambdaRunner = new PolymorphicLambdaRunner(elem);
safePolymorphicLambdaRunner.doEither(e -> e.repaint());
// This next two are ok for this patch since the behavior is the same (no report) for
// lambdas as for annon classes. However, shouldn't this be (argument.type.incompatible)
// just because safePolymorphicLambdaRunner is not an @UI PolymorphicLambdaRunner ? Or,
// failing that (call.invalid.ui) since doEither is @PolyUIEffect ?
safePolymorphicLambdaRunner.doEither(e -> e.dangerous());
safePolymorphicLambdaRunner.doEither(
new @UI PolymorphicFunctionalInterface<UIElement>() {
public void executePolymorphic(UIElement arg) {
arg.dangerous();
}
});
@UI PolymorphicLambdaRunner uiPolymorphicLambdaRunner = new @UI PolymorphicLambdaRunner(elem);
// :: error: (call.invalid.ui)
uiPolymorphicLambdaRunner.doEither(
e -> e.repaint()); // Safe at runtime, but not by the type system!
// :: error: (call.invalid.ui)
uiPolymorphicLambdaRunner.doEither(e -> e.dangerous());
PolymorphicFunctionalInterface<UIElement> func1 = e -> e.repaint();
// :: error: (assignment.type.incompatible)
PolymorphicFunctionalInterface<UIElement> func2 = e -> e.dangerous(); // Incompatible types!
PolymorphicFunctionalInterface<UIElement> func2p =
// :: error: (assignment.type.incompatible)
(new @UI PolymorphicFunctionalInterface<UIElement>() {
public void executePolymorphic(UIElement arg) {
arg.dangerous();
}
});
@UI PolymorphicFunctionalInterface<UIElement> func3 = e -> e.dangerous();
safePolymorphicLambdaRunner.doEither(func1);
safePolymorphicLambdaRunner.doEither(func2);
// :: error: (call.invalid.ui)
uiPolymorphicLambdaRunner.doEither(func1);
// :: error: (call.invalid.ui)
uiPolymorphicLambdaRunner.doEither(func2);
// :: error: (call.invalid.ui)
uiPolymorphicLambdaRunner.doEither(func3);
}
@UIEffect
public static void uiContextTestCases(UIElement elem) {
LambdaRunner runner = new LambdaRunner(elem);
// :: error: (call.invalid.ui)
runner.doSafe(e -> e.dangerous());
runner.doUI(e -> e.repaint());
runner.doUI(e -> e.dangerous());
PolymorphicLambdaRunner safePolymorphicLambdaRunner = new PolymorphicLambdaRunner(elem);
// No error, why? :: error: (argument.type.incompatible)
safePolymorphicLambdaRunner.doEither(e -> e.dangerous());
@UI PolymorphicLambdaRunner uiPolymorphicLambdaRunner = new @UI PolymorphicLambdaRunner(elem);
uiPolymorphicLambdaRunner.doEither(e -> e.dangerous());
}
public @UI PolymorphicFunctionalInterface<UIElement> returnLambdasTest1() {
return e -> e.dangerous();
}
// This should be an error without an @UI annotation on the return type. No?
public PolymorphicFunctionalInterface<UIElement> returnLambdasTest2() {
// :: error: (return.type.incompatible)
return e -> {
e.dangerous();
};
}
// Just to check
public PolymorphicFunctionalInterface<UIElement> returnLambdasTest3() {
// :: error: (return.type.incompatible)
return (new @UI PolymorphicFunctionalInterface<UIElement>() {
public void executePolymorphic(UIElement arg) {
arg.dangerous();
}
});
}
}
|