diff options
Diffstat (limited to 'tests/bugs183')
-rw-r--r-- | tests/bugs183/445395/Code.java | 15 | ||||
-rwxr-xr-x | tests/bugs183/445395/ControlFlow.java | 75 | ||||
-rwxr-xr-x | tests/bugs183/445395/ControlFlowCommand.java | 7 | ||||
-rwxr-xr-x | tests/bugs183/445395/ControlFlowDriver.java | 75 | ||||
-rwxr-xr-x | tests/bugs183/445395/ForReplacer.java | 44 |
5 files changed, 216 insertions, 0 deletions
diff --git a/tests/bugs183/445395/Code.java b/tests/bugs183/445395/Code.java new file mode 100644 index 000000000..647bc0401 --- /dev/null +++ b/tests/bugs183/445395/Code.java @@ -0,0 +1,15 @@ +import java.util.*; + +public class Code { + public void m() { } + public static void main(String []argv) { + new Code().m(); + } +} + +aspect X { + void around(): execution(* m(..)) { + Arrays.asList(4, 5, 6).forEach((i) -> { System.out.println(i);}); + } +} + diff --git a/tests/bugs183/445395/ControlFlow.java b/tests/bugs183/445395/ControlFlow.java new file mode 100755 index 000000000..3be5325f5 --- /dev/null +++ b/tests/bugs183/445395/ControlFlow.java @@ -0,0 +1,75 @@ +//package org.acmsl.pocs.lambdafor; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.function.Function; +import java.util.stream.Collectors; + +public class ControlFlow +{ + public <C extends Collection<I>, I, R> Collection<R> forloop( final C items, final Function<I, R> lambda) + { + return functionalForLoop(items, lambda); + } + + public <C extends Collection<I>, I, R> Collection<R> functionalForLoop( final C items, final Function<I, R> lambda) + { + return items.stream().map(lambda::apply).collect(Collectors.toList()); + } + + + public Collection iterativeForloop( final Collection items, final Function lambda) + { + final List<Object> result = new ArrayList<>(); + + for (final Object item: items) + { + result.add(lambda.<Object>apply(item)); + } + + return result; + } + + public <C extends Collection<I>, I, R> Collection<R> externallyDrivenForloop( + final ControlFlowDriver driver, final C items, final Function<I, R> lambda) + { + final List<R> result = new ArrayList<>(items.size()); + + final List<I> list = new ArrayList<>(items); + + int position = -1; + + while (true) + { + ControlFlowCommand command = driver.waitForCommand(); + + switch (command) + { + case NEXT: + position++; + break; + case PREVIOUS: + position++; + break; + case RELOAD: + break; + default: + break; + } + + if (position < 0) + { + position = 0; + } + else if (position > list.size() - 1) + { + break; + } + + result.set(position, lambda.apply(list.get(position))); + } + + return result; + } +} diff --git a/tests/bugs183/445395/ControlFlowCommand.java b/tests/bugs183/445395/ControlFlowCommand.java new file mode 100755 index 000000000..55c80d6cd --- /dev/null +++ b/tests/bugs183/445395/ControlFlowCommand.java @@ -0,0 +1,7 @@ +//package org.acmsl.pocs.lambdafor; + +public enum ControlFlowCommand { + NEXT, + PREVIOUS, + RELOAD; +} diff --git a/tests/bugs183/445395/ControlFlowDriver.java b/tests/bugs183/445395/ControlFlowDriver.java new file mode 100755 index 000000000..318362c79 --- /dev/null +++ b/tests/bugs183/445395/ControlFlowDriver.java @@ -0,0 +1,75 @@ +//package org.acmsl.pocs.lambdafor; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.function.Function; + +public class ControlFlowDriver { + + private static boolean m__bUsed = false; + + public ControlFlowDriver() { + } + + protected static void immutableSetUsed(final boolean used) { + m__bUsed = used; + } + + protected static void setUsed(final boolean used) { + immutableSetUsed(used); + } + + public static boolean isUsed() { + return m__bUsed; + } + + public <C extends Collection<I>, I, R> Collection<R> forloop(final C items, + final Function<I, R> lambda) { + setUsed(true); + + final List<R> result = new ArrayList<R>(items.size()); + + final List<I> list = new ArrayList<I>(items); + + int position = -1; + + while (true) { + ControlFlowCommand command = waitForCommand(); + + switch (command) { + case NEXT: + position++; + break; + case PREVIOUS: + position++; + break; + case RELOAD: + break; + default: + break; + } + + if (position < 0) { + position = 0; + } else if (position > list.size() - 1) { + break; + } + + result.set(position, lambda.apply(list.get(position))); + } + + return result; + } + + protected ControlFlowCommand waitForCommand() { + try { + Thread.sleep(1000); + } catch (final InterruptedException interruptedException) { + // whatever + } + + return ControlFlowCommand.NEXT; + } + +} diff --git a/tests/bugs183/445395/ForReplacer.java b/tests/bugs183/445395/ForReplacer.java new file mode 100755 index 000000000..25b7ca22a --- /dev/null +++ b/tests/bugs183/445395/ForReplacer.java @@ -0,0 +1,44 @@ +//package org.acmsl.pocs.lambdafor; + +import java.util.Arrays; + +public aspect ForReplacer +{ + /** + * Intercepting for loop constructs. + */ + pointcut forLoopPointcut(): +// execution(* Sample.sampleCode(..)); // -> error + execution(* forloop(..)); +// && args(Collect, ..); + + /** + * Replacing the loop construct. + */ + Object around() : forLoopPointcut() + { +// return proceed(); +// Collection<Integer> result = new ArrayList<>(); +// result.addAll(new ControlFlow().externallyDrivenForloop(new ControlFlowDriver(), Arrays.asList(4, 5, 6), (i) -> { System.out.println(i); return i;})); +// return result; + return new ControlFlow().externallyDrivenForloop(new ControlFlowDriver(), Arrays.asList(4, 5, 6), (i) -> { System.out.println(i); return i;}); + } + + /** + * Intercepting for loop constructs. + * + pointcut forLoopPointcut(ControlFlow loop): + call(* ControlFlow.forloop(..)) + && target(loop); +// && args(items, ..); + + /** + * Replacing the loop construct. + * + Collection around(ControlFlow loop) : forLoopPointcut(loop) + { + return loop.externallyDrivenForloop(new ControlFlowDriver(), Arrays.asList(4, 5, 6), (i) -> { System.out.println(i); return i;}); +// return new ControlFlow().externallyDrivenForloop(new ControlFlowDriver(), Arrays.asList(4, 5, 6), (i) -> { System.out.println(i); return i;}); + } + */ +} |