| @@ -1,21 +1,34 | |||||
| 1 | package org.implab.gradle.common.utils; |
|
1 | package org.implab.gradle.common.utils; | |
| 2 |
|
2 | |||
| 3 | import groovy.lang.Closure; |
|
3 | import groovy.lang.Closure; | |
| 4 |
|
4 | |||
| 5 | import org.eclipse.jdt.annotation.NonNullByDefault; |
|
5 | import org.eclipse.jdt.annotation.NonNullByDefault; | |
| 6 | import org.gradle.api.Action; |
|
6 | import org.gradle.api.Action; | |
| 7 |
|
7 | |||
| 8 | @NonNullByDefault |
|
8 | @NonNullByDefault | |
| 9 | public final class Closures { |
|
9 | public final class Closures { | |
| 10 | private Closures() { |
|
10 | private Closures() { | |
| 11 | } |
|
11 | } | |
| 12 |
|
12 | |||
|
|
13 | /** | |||
|
|
14 | * Wraps {@link Action} around the specified closure. The parameter | |||
|
|
15 | * of the action will be used as delegate in the specified closure. | |||
|
|
16 | * | |||
|
|
17 | * @param <T> The type of the action parameter | |||
|
|
18 | * @param closure The closure | |||
|
|
19 | * @return | |||
|
|
20 | */ | |||
| 13 | public static <T> Action<T> action(Closure<?> closure) { |
|
21 | public static <T> Action<T> action(Closure<?> closure) { | |
| 14 | return arg -> apply(closure, arg); |
|
22 | return arg -> apply(closure, arg); | |
| 15 | } |
|
23 | } | |
| 16 |
|
24 | |||
| 17 | public static void apply(Closure<?> action, Object target) { |
|
25 | public static void apply(Closure<?> action, Object target) { | |
|
|
26 | var prevDelegate = action.getDelegate(); | |||
|
|
27 | try { | |||
| 18 | action.setDelegate(target); |
|
28 | action.setDelegate(target); | |
| 19 | action.call(target); |
|
29 | action.call(target); | |
|
|
30 | } finally { | |||
|
|
31 | action.setDelegate(prevDelegate); | |||
| 20 | } |
|
32 | } | |
| 21 | } |
|
33 | } | |
|
|
34 | } | |||
General Comments 0
You need to be logged in to leave comments.
Login now
