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