-
Notifications
You must be signed in to change notification settings - Fork 5
/
DirectSelect.java
55 lines (47 loc) · 1.72 KB
/
DirectSelect.java
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
package org.genericsystem.reactor.annotations;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.InvocationTargetException;
import java.util.function.BiConsumer;
import org.genericsystem.reactor.AnnotationsManager;
import org.genericsystem.reactor.Tag;
import org.genericsystem.reactor.annotations.DirectSelect.DirectSelects;
import org.genericsystem.reactor.annotations.DirectSelect.Processor;
import org.genericsystem.reactor.gscomponents.TagImpl;
/**
* @author Nicolas Feybesse
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
@Repeatable(DirectSelects.class)
@Process(Processor.class)
public @interface DirectSelect {
Class<? extends TagImpl>[] path() default {};
Class<?>[] value();
int[] pos() default {};
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
public @interface DirectSelects {
DirectSelect[] value();
}
public static class Processor implements BiConsumer<Annotation, Tag> {
@Override
public void accept(Annotation annotation, Tag tag) {
try {
Class<?>[] path = (Class<?>[]) annotation.annotationType().getDeclaredMethod("path").invoke(annotation);
Class<?>[] selects = ((DirectSelect) annotation).value();
if (selects.length == 1)
tag.select(selects[0]);
else
tag.select(selects[AnnotationsManager.position(tag, path[path.length - 1])]);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
throw new IllegalStateException(e);
}
}
}
}