|
| 1 | + |
| 2 | +package org.scijava.ops.engine.yaml; |
| 3 | + |
| 4 | +import java.lang.reflect.Method; |
| 5 | +import java.net.URI; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +import org.scijava.common3.Classes; |
| 9 | +import org.scijava.ops.api.Hints; |
| 10 | +import org.scijava.ops.api.OpInfo; |
| 11 | +import org.scijava.ops.api.features.YAMLOpInfoCreator; |
| 12 | +import org.scijava.ops.engine.matcher.impl.OpMethodInfo; |
| 13 | + |
| 14 | +/** |
| 15 | + * A {@link YAMLOpInfoCreator} specialized for Java {@link Method}s. |
| 16 | + * |
| 17 | + * @author Gabriel Selzer |
| 18 | + */ |
| 19 | +public class JavaMethodYAMLInfoCreator extends AbstractYAMLOpInfoCreator { |
| 20 | + |
| 21 | + @Override |
| 22 | + public boolean canCreateFrom(URI identifier) { |
| 23 | + return identifier.getScheme().startsWith("javaMethod"); |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + OpInfo create(String identifier, String[] names, double priority, Hints hints, |
| 28 | + String version, Map<String, Object> yaml) throws Exception |
| 29 | + { |
| 30 | + // parse class |
| 31 | + int clsIndex = identifier.lastIndexOf('.', identifier.indexOf('(')); |
| 32 | + String clsString = identifier.substring(0, clsIndex); |
| 33 | + Class<?> src = Classes.load(clsString); |
| 34 | + // parse method |
| 35 | + String methodString = identifier.substring(clsIndex + 1, identifier.indexOf( |
| 36 | + '(')); |
| 37 | + String[] paramStrings = identifier.substring(identifier.indexOf('(') + 1, |
| 38 | + identifier.indexOf(')')).split("\\s*,\\s*"); |
| 39 | + Class<?>[] paramClasses = new Class<?>[paramStrings.length]; |
| 40 | + for (int i = 0; i < paramStrings.length; i++) { |
| 41 | + paramClasses[i] = Classes.load(paramStrings[i]); |
| 42 | + } |
| 43 | + Method method = src.getMethod(methodString, paramClasses); |
| 44 | + // parse op type |
| 45 | + String typeString = (String) yaml.get("type"); |
| 46 | + Class<?> opType = Classes.load(typeString); |
| 47 | + // create the OpInfo |
| 48 | + return new OpMethodInfo(method, opType, null, priority, names); |
| 49 | + } |
| 50 | +} |
0 commit comments