|
| 1 | + |
| 2 | +package org.scijava.ops.engine.impl; |
| 3 | + |
| 4 | +import java.lang.reflect.AnnotatedElement; |
| 5 | +import java.lang.reflect.Field; |
| 6 | +import java.lang.reflect.InvocationTargetException; |
| 7 | +import java.lang.reflect.Method; |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Objects; |
| 11 | +import java.util.stream.Collectors; |
| 12 | + |
| 13 | +import org.scijava.Priority; |
| 14 | +import org.scijava.discovery.Discoverer; |
| 15 | +import org.scijava.discovery.Discovery; |
| 16 | +import org.scijava.function.Functions; |
| 17 | +import org.scijava.ops.api.OpInfo; |
| 18 | +import org.scijava.ops.api.OpInfoGenerator; |
| 19 | +import org.scijava.ops.api.OpUtils; |
| 20 | +import org.scijava.ops.engine.hint.DefaultHints; |
| 21 | +import org.scijava.ops.engine.matcher.impl.OpClassInfo; |
| 22 | +import org.scijava.ops.engine.matcher.impl.OpFieldInfo; |
| 23 | +import org.scijava.ops.engine.matcher.impl.OpMethodInfo; |
| 24 | +import org.scijava.util.VersionUtils; |
| 25 | + |
| 26 | +/** |
| 27 | + * Generates {@link OpInfo}s using a {@link Discovery}. The tag syntax is |
| 28 | + * expected to be as follows: |
| 29 | + * <p> |
| 30 | + * {@code @implNote op names=<op names, comma delimited> [priority=<op priority>]} |
| 31 | + * <p> |
| 32 | + * For example, an op wishing to be discoverable with names "foo.bar" and |
| 33 | + * "foo.baz", with a priority of 100, should be declared as |
| 34 | + * <p> |
| 35 | + * {@code @implNote op names=foo.bar,foo.baz priority=100} |
| 36 | + * <p> |
| 37 | + * |
| 38 | + * @author |
| 39 | + */ |
| 40 | +public class TagBasedOpInfoGenerator implements OpInfoGenerator { |
| 41 | + |
| 42 | + private static final String TAGTYPE = "op"; |
| 43 | + |
| 44 | + private final List<Discoverer> discoverers; |
| 45 | + |
| 46 | + public TagBasedOpInfoGenerator(Discoverer... d) { |
| 47 | + this.discoverers = Arrays.asList(d); |
| 48 | + } |
| 49 | + |
| 50 | + Functions.Arity3<Class<?>, Double, String[], OpClassInfo> opClassGenerator = // |
| 51 | + (cls, priority, names) -> { |
| 52 | + String version = VersionUtils.getVersion(cls); |
| 53 | + return new OpClassInfo(cls, version, new DefaultHints(), priority, names); |
| 54 | + }; |
| 55 | + |
| 56 | + Functions.Arity3<Method, Double, String[], OpMethodInfo> opMethodGenerator = // |
| 57 | + (m, priority, names) -> { |
| 58 | + String version = VersionUtils.getVersion(m.getDeclaringClass()); |
| 59 | + return new OpMethodInfo(m, version, new DefaultHints(), names); |
| 60 | + }; |
| 61 | + |
| 62 | + Functions.Arity3<Field, Double, String[], OpFieldInfo> opFieldGenerator = // |
| 63 | + (f, priority, names) -> { |
| 64 | + String version = VersionUtils.getVersion(f.getDeclaringClass()); |
| 65 | + Object instance; |
| 66 | + try { |
| 67 | + instance = f.getDeclaringClass().getDeclaredConstructor().newInstance(); |
| 68 | + } |
| 69 | + catch (InstantiationException | IllegalAccessException |
| 70 | + | IllegalArgumentException | InvocationTargetException |
| 71 | + | NoSuchMethodException | SecurityException exc) |
| 72 | + { |
| 73 | + return null; |
| 74 | + } |
| 75 | + return new OpFieldInfo(instance, f, version, new DefaultHints(), names); |
| 76 | + }; |
| 77 | + |
| 78 | + @Override |
| 79 | + public List<OpInfo> generateInfos() { |
| 80 | + List<OpInfo> infos = discoverers.stream() // |
| 81 | + .flatMap(d -> d.elementsTaggedWith("op").stream()) // |
| 82 | + .map(discovery -> { |
| 83 | + AnnotatedElement e = discovery.discovery(); |
| 84 | + String[] names = getOpNames(discovery.tag()); |
| 85 | + double priority = getOpPriority(discovery.tag()); |
| 86 | + if (e instanceof Class) { |
| 87 | + return opClassGenerator.apply((Class<?>) e, priority, names); |
| 88 | + } |
| 89 | + else if (e instanceof Method) { |
| 90 | + return opMethodGenerator.apply((Method) e, priority, names); |
| 91 | + } |
| 92 | + else if (e instanceof Field) { |
| 93 | + return opFieldGenerator.apply((Field) e, priority, names); |
| 94 | + } |
| 95 | + else return null; |
| 96 | + }) // |
| 97 | + .filter(Objects::nonNull) // |
| 98 | + .collect(Collectors.toList()); |
| 99 | + return infos; |
| 100 | + } |
| 101 | + |
| 102 | + private static String[] getOpNames(String tag) { |
| 103 | + int tagTypeIndex = tag.indexOf(TAGTYPE + "="); |
| 104 | + String joinedNames = tag.substring(tagTypeIndex + TAGTYPE.length() + 1) |
| 105 | + .split(" ")[0]; |
| 106 | + return OpUtils.parseOpNames(joinedNames); |
| 107 | + } |
| 108 | + |
| 109 | + private static double getOpPriority(String tag) { |
| 110 | + int tagTypeIndex = tag.indexOf("priority="); |
| 111 | + if (tagTypeIndex == -1) return Priority.NORMAL; |
| 112 | + return Double.parseDouble(tag.substring(tagTypeIndex + "priority=".length()) |
| 113 | + .split(" ")[0]); |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments