|
33 | 33 | import java.lang.reflect.Parameter; |
34 | 34 | import java.net.URI; |
35 | 35 | import java.util.Map; |
36 | | -import java.util.regex.Pattern; |
37 | 36 |
|
38 | 37 | import org.scijava.common3.Classes; |
39 | 38 | import org.scijava.function.Computers; |
@@ -107,30 +106,39 @@ private Class<?> deriveOpType(String identifier, String typeString, |
107 | 106 | " 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'\""); |
108 | 107 | } |
109 | 108 | } |
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 | + } |
119 | 129 | } |
120 | 130 | } |
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; |
131 | 141 | } |
132 | | - // Finally, pass off to the class loader function. |
133 | | - return deriveType(identifier, typeString); |
134 | 142 | } |
135 | 143 |
|
136 | 144 | private Class<?> deriveType(String identifier, String typeString) { |
|
0 commit comments