Skip to content

Commit a76e4b8

Browse files
committed
Rewrite alias checks
1 parent cc00fd8 commit a76e4b8

2 files changed

Lines changed: 31 additions & 23 deletions

File tree

scijava-ops-engine/src/main/java/org/scijava/ops/engine/yaml/impl/JavaMethodYAMLInfoCreator.java

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import java.lang.reflect.Parameter;
3434
import java.net.URI;
3535
import java.util.Map;
36-
import java.util.regex.Pattern;
3736

3837
import org.scijava.common3.Classes;
3938
import org.scijava.function.Computers;
@@ -107,30 +106,39 @@ private Class<?> deriveOpType(String identifier, String typeString,
107106
" could not be loaded: Computers and Inplaces must declare their Op type in their @implNote annotation For example, if your Inplace is designed to mutate the first argument, please write \"type='Inplace1'\"");
108107
}
109108
}
110-
// Handle op type inference
111-
if (Pattern.matches("^[Ii]nplace\\s*[0-9]*$", typeString)) {
112-
try {
113-
int ioIndex = Integer.parseInt(typeString.replaceAll("[^0-9]", "")) - 1;
114-
return Inplaces.inplaceOfArity(parameterCount, ioIndex);
115-
}
116-
catch (NumberFormatException e) {
117-
throw new RuntimeException("Op " + identifier +
118-
" could not be loaded: Inplaces must declare the index of the mutable parameter. For example, if your Inplace is designed to mutate the first argument, please write \"Inplace1\"");
109+
110+
Class<?> alias = findAlias(typeString.trim(), parameterCount);
111+
if (alias != null) return alias;
112+
113+
// Finally, pass off to the class loader function.
114+
return deriveType(identifier, typeString);
115+
}
116+
117+
private Class<?> findAlias(String typeString, int parameterCount) {
118+
// We match any aliases matching the regex pattern "(or_of_aliases)(\\d*)"
119+
int ioPosition = parameterCount - 1;
120+
int aliasEnd = typeString.length() - 1;
121+
// If the last char is a digit, we have a positional suffix
122+
if (Character.isDigit(typeString.charAt(aliasEnd))) {
123+
// Find the positional suffix
124+
for (; aliasEnd >= 0; aliasEnd--) {
125+
if (!Character.isDigit(typeString.charAt(aliasEnd))) {
126+
ioPosition = Integer.parseInt(typeString.substring(aliasEnd + 1)) - 1;
127+
break;
128+
}
119129
}
120130
}
121-
else if (Pattern.matches("^[Cc]omputer$", typeString)) {
122-
return Computers.computerOfArity(parameterCount - 1);
123-
}
124-
else if (Pattern.matches("^[Cc]omputer\\s*[0-9]*$", typeString)) {
125-
Integer a = Integer.parseInt(typeString.substring(typeString.indexOf(
126-
'r') + 1).trim());
127-
return Computers.computerOfArity(parameterCount - 1, a - 1);
128-
}
129-
else if (Pattern.matches("^[Ff]unction$", typeString)) {
130-
return Functions.functionOfArity(parameterCount);
131+
// Check if (typeString - positional suffix) is an alias
132+
switch (typeString.substring(0, aliasEnd + 1)) {
133+
case "Computer":
134+
return Computers.computerOfArity(parameterCount - 1, ioPosition);
135+
case "Function":
136+
return Functions.functionOfArity(parameterCount);
137+
case "Inplace":
138+
return Inplaces.inplaceOfArity(parameterCount, ioPosition);
139+
default:
140+
return null;
131141
}
132-
// Finally, pass off to the class loader function.
133-
return deriveType(identifier, typeString);
134142
}
135143

136144
private Class<?> deriveType(String identifier, String typeString) {

scijava-ops-engine/src/test/java/org/scijava/ops/engine/yaml/impl/ops/YAMLMethodOp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class YAMLMethodOp {
4242
/**
4343
* An example Op, implemented by a {@link Method}
4444
*
45-
* @implNote op name=example.sub
45+
* @implNote op name=example.sub, type=Function
4646
* @param aDouble the first double
4747
* @param aDouble2 the second double
4848
* @return the difference

0 commit comments

Comments
 (0)