7676import org .scijava .ops .core .inplace .InplaceOps .Inplace6FirstOp ;
7777import org .scijava .ops .core .inplace .InplaceOps .Inplace7SecondOp ;
7878import org .scijava .ops .core .inplace .InplaceOps .InplaceOp ;
79+ import org .scijava .ops .matcher .DefaultOpMatcher ;
7980import org .scijava .ops .matcher .OpCandidate ;
8081import org .scijava .ops .matcher .OpClassInfo ;
8182import org .scijava .ops .matcher .OpFieldInfo ;
8283import org .scijava .ops .matcher .OpInfo ;
8384import org .scijava .ops .matcher .OpMatchingException ;
8485import org .scijava .ops .matcher .OpRef ;
85- import org .scijava .ops .matcher .OpTypeMatchingService ;
86+ import org .scijava .ops .matcher .OpMatcher ;
87+ import org .scijava .ops .transform .DefaultOpTransformationMatcher ;
8688import org .scijava .ops .transform .OpRunner ;
8789import org .scijava .ops .transform .OpTransformationCandidate ;
8890import org .scijava .ops .transform .OpTransformationException ;
89- import org .scijava .ops .transform .OpTransformerService ;
9091import org .scijava .ops .types .Any ;
92+ import org .scijava .ops .transform .OpTransformationMatcher ;
93+ import org .scijava .ops .transform .OpTransformer ;
9194import org .scijava .ops .types .Nil ;
9295import org .scijava .ops .types .TypeService ;
9396import org .scijava .param .FunctionalMethodType ;
@@ -115,14 +118,12 @@ public class OpService extends AbstractService implements SciJavaService, OpEnvi
115118 @ Parameter
116119 private PluginService pluginService ;
117120
118- @ Parameter
119- private OpTypeMatchingService matcher ;
121+ private OpMatcher opMatcher ;
120122
121123 @ Parameter
122124 private LogService log ;
123125
124- @ Parameter
125- private OpTransformerService transformer ;
126+ private OpTransformationMatcher transformationMatcher ;
126127
127128 @ Parameter
128129 private TypeService typeService ;
@@ -138,6 +139,8 @@ public class OpService extends AbstractService implements SciJavaService, OpEnvi
138139 */
139140 private Map <String , List <OpInfo >> opCache ;
140141
142+ private List <OpTransformer > transformerIndex ;
143+
141144 private static Map <Class <?>, Class <?>> wrappers = wrappers ();
142145
143146 private static Map <Class <?>, Class <?>> wrappers () {
@@ -186,8 +189,7 @@ public void initOpCache() {
186189 try {
187190 final Class <?> opClass = pluginInfo .loadClass ();
188191 OpInfo opInfo = new OpClassInfo (opClass );
189- addToCache (opInfo , pluginInfo .getName ());
190-
192+ addToOpIndex (opInfo , pluginInfo .getName ());
191193 } catch (InstantiableException exc ) {
192194 log .error ("Can't load class from plugin info: " + pluginInfo .toString (), exc );
193195 }
@@ -204,15 +206,15 @@ public void initOpCache() {
204206 instance = field .getDeclaringClass ().newInstance ();
205207 }
206208 OpInfo opInfo = new OpFieldInfo (isStatic ? null : instance , field );
207- addToCache (opInfo , field .getAnnotation (OpField .class ).names ());
209+ addToOpIndex (opInfo , field .getAnnotation (OpField .class ).names ());
208210 }
209211 } catch (InstantiableException | InstantiationException | IllegalAccessException exc ) {
210212 log .error ("Can't load class from plugin info: " + pluginInfo .toString (), exc );
211213 }
212214 }
213215 }
214216
215- private void addToCache ( OpInfo opInfo , String opNames ) {
217+ private void addToOpIndex ( final OpInfo opInfo , final String opNames ) {
216218 String [] parsedOpNames = OpUtils .parseOpNames (opNames );
217219 if (parsedOpNames == null || parsedOpNames .length == 0 ) {
218220 log .error ("Skipping Op " + opInfo .implementationName () + ":\n " + "Op implementation must provide name." );
@@ -230,6 +232,10 @@ private void addToCache(OpInfo opInfo, String opNames) {
230232 }
231233 }
232234
235+ public synchronized void initTransformerIndex () {
236+ transformerIndex = pluginService .createInstancesOfType (OpTransformer .class );
237+ }
238+
233239 @ Override
234240 public Iterable <OpInfo > infos () {
235241 if (opCache == null ) {
@@ -254,6 +260,27 @@ public Iterable<OpInfo> infos(String name) {
254260 return infos ;
255261 }
256262
263+ private OpMatcher getOpMatcher () {
264+ if (opMatcher == null ) {
265+ opMatcher = new DefaultOpMatcher (log );
266+ }
267+ return opMatcher ;
268+ }
269+
270+ private synchronized List <OpTransformer > getTransformerIndex () {
271+ if (transformerIndex == null ) {
272+ initTransformerIndex ();
273+ }
274+ return transformerIndex ;
275+ }
276+
277+ private OpTransformationMatcher getTransformationMatcher () {
278+ if (transformationMatcher == null ) {
279+ transformationMatcher = new DefaultOpTransformationMatcher (getOpMatcher ());
280+ }
281+ return transformationMatcher ;
282+ }
283+
257284 /**
258285 * Attempts to inject {@link OpDependency} annotated fields of the specified
259286 * object by looking for Ops matching the field type and the name specified in
@@ -310,7 +337,7 @@ public Object findOpInstance(final String opName, final OpRef ref) {
310337 OpTransformationCandidate transformation = null ;
311338 try {
312339 // Find single match which matches the specified types
313- match = matcher .findSingleMatch (this , ref );
340+ match = getOpMatcher () .findSingleMatch (this , ref );
314341 final List <Object > dependencies = resolveOpDependencies (match );
315342 op = match .createOp (dependencies );
316343 } catch (OpMatchingException e ) {
@@ -319,7 +346,7 @@ public Object findOpInstance(final String opName, final OpRef ref) {
319346
320347 // If we can't find an op matching the original request, we try to find a
321348 // transformation
322- transformation = transformer .findTransformation (this , ref );
349+ transformation = getTransformationMatcher () .findTransformation (this , getTransformerIndex () , ref );
323350 if (transformation == null ) {
324351 log .debug ("No matching Op transformation found" );
325352 throw new IllegalArgumentException (e );
0 commit comments