@@ -141,14 +141,6 @@ public Type genericType(Object obj) {
141141 return typeService .reify (obj );
142142 }
143143
144- @ Override
145- public <T > T opify (final T op , Type reifiedType ) {
146- if (wrappers == null ) initWrappers ();
147- @ SuppressWarnings ("unchecked" )
148- final OpWrapper <T > wrapper = (OpWrapper <T >) wrappers .get (Types .raw (reifiedType ));
149- return wrapper .wrap (op , reifiedType );
150- }
151-
152144 @ SuppressWarnings ("unchecked" )
153145 private <T > T findOpInstance (final String opName , final Nil <T > specialType , final Nil <?>[] inTypes ,
154146 final Nil <?> outType ) throws OpMatchingException {
@@ -157,6 +149,10 @@ private <T> T findOpInstance(final String opName, final Nil<T> specialType, fina
157149 return (T ) findOpInstance (opName , ref , true );
158150 }
159151
152+ private Type [] toTypes (Nil <?>... nils ) {
153+ return Arrays .stream (nils ).filter (n -> n != null ).map (n -> n .getType ()).toArray (Type []::new );
154+ }
155+
160156 /**
161157 * Finds an Op instance matching the request described by {@link OpRef}
162158 * {@code ref}. NB the return must be an {@link Object} here (instead of some
@@ -178,10 +174,13 @@ private Object findOpInstance(final String opName, final OpRef ref,
178174 if (cachedOp != null ) return cachedOp ;
179175
180176 // obtain suitable OpCandidate
181- OpCandidate match = findOpCandidate (ref , adaptable );
177+ OpCandidate candidate = findOpCandidate (ref , adaptable );
182178
183- // obtain (wrapped) Op instance
184- Object wrappedOp = wrappedOpFromCandidate (match );
179+ // obtain Op instance (with dependencies)
180+ Object op = instantiateOp (candidate );
181+
182+ // wrap Op
183+ Object wrappedOp = wrapOp (op , candidate .opInfo (), candidate .typeVarAssigns ());
185184
186185 // cache instance
187186 opCache .putIfAbsent (ref , wrappedOp );
@@ -221,26 +220,84 @@ private OpCandidate findOpCandidate(OpRef ref, boolean adaptable) throws OpMatch
221220 }
222221 }
223222
224- // TODO: This method does two things. Would be nice to split into a method
225- // that creates the Op, and another that wraps it.
226- // Problem is that we need the OpInfo and TypeVarAssigns to wrap the Op
227- private Object wrappedOpFromCandidate (final OpCandidate candidate )
223+ /**
224+ * Creates an instance of the Op from the {@link OpCandidate} <b>with its
225+ * required {@link OpDependency} fields</b>.
226+ *
227+ * @param candidate
228+ * @return an Op with all needed dependencies
229+ * @throws OpMatchingException
230+ */
231+ private Object instantiateOp (final OpCandidate candidate )
228232 throws OpMatchingException
229233 {
230234 final List <Object > dependencies = resolveOpDependencies (candidate );
231- final Object op = candidate .createOp (dependencies );
235+ return candidate .createOp (dependencies );
236+ }
237+
238+ /**
239+ * Wraps the matched op into an {@link Op} that knows its generic typing.
240+ *
241+ * @param op - the op to wrap.
242+ * @param opInfo - from which we determine the {@link Type} of the {@code Op}
243+ *
244+ * @return an {@link Op} wrapping of op.
245+ */
246+ private <T > T wrapOp (T op , OpInfo opInfo , Map <TypeVariable <?>, Type > typeVarAssigns ) {
247+ if (wrappers == null )
248+ initWrappers ();
249+
250+ Type opType = opInfo .opType ();
251+ try {
252+ // find the opWrapper that wraps this type of Op
253+ Class <?> wrapper = getWrapperClass (op , opInfo );
254+ // obtain the generic type of the Op w.r.t. the Wrapper class
255+ Type exactSuperType = Types .getExactSuperType (opType , wrapper );
256+ Type reifiedSuperType = Types .substituteTypeVariables (exactSuperType , typeVarAssigns );
257+ // wrap the Op
258+ return opify (op , reifiedSuperType );
259+ } catch (IllegalArgumentException | SecurityException exc ) {
260+ log .error (exc .getMessage () != null ? exc .getMessage () : "Cannot wrap " + op .getClass ());
261+ return op ;
262+ } catch (NullPointerException e ) {
263+ log .error ("No wrapper exists for " + Types .raw (opType ).toString () + "." );
264+ return op ;
265+ }
266+ }
232267
233- return wrapOp (op , candidate .opInfo (), candidate .typeVarAssigns ());
268+ private Class <?> getWrapperClass (Object op , OpInfo info ) {
269+ Class <?>[] suitableWrappers = wrappers .keySet ().stream ().filter (wrapper -> wrapper .isInstance (op ))
270+ .toArray (Class []::new );
271+ if (suitableWrappers .length == 0 )
272+ throw new IllegalArgumentException (info .implementationName () + ": matched op Type " + info .opType ().getClass ()
273+ + " does not match a wrappable Op type." );
274+ if (suitableWrappers .length > 1 )
275+ throw new IllegalArgumentException (
276+ "Matched op Type " + info .opType ().getClass () + " matches multiple Op types: " + wrappers .toString ());
277+ if (!Types .isAssignable (Types .raw (info .opType ()), suitableWrappers [0 ]))
278+ throw new IllegalArgumentException (Types .raw (info .opType ()) + "cannot be wrapped as a " + suitableWrappers [0 ].getClass ());
279+ return suitableWrappers [0 ];
234280 }
235281
236- private Type [] toTypes (Nil <?>... nils ) {
237- return Arrays .stream (nils ).filter (n -> n != null ).map (n -> n .getType ()).toArray (Type []::new );
282+ @ Override
283+ public <T > T opify (final T op , Type reifiedType ) {
284+ if (wrappers == null ) initWrappers ();
285+ @ SuppressWarnings ("unchecked" )
286+ final OpWrapper <T > wrapper = (OpWrapper <T >) wrappers .get (Types .raw (reifiedType ));
287+ return wrapper .wrap (op , reifiedType );
238288 }
239-
289+
240290 private List <Object > resolveOpDependencies (OpCandidate candidate ) throws OpMatchingException {
241291 return resolveOpDependencies (candidate .opInfo (), candidate .typeVarAssigns ());
242292 }
243293
294+ private void initWrappers () {
295+ wrappers = new HashMap <>();
296+ for (OpWrapper <?> wrapper : pluginService .createInstancesOfType (OpWrapper .class )) {
297+ wrappers .put (wrapper .type (), wrapper );
298+ }
299+ }
300+
244301 /**
245302 * Attempts to inject {@link OpDependency} annotated fields of the specified
246303 * object by looking for Ops matching the field type and the name specified in
@@ -273,23 +330,6 @@ private List<Object> resolveOpDependencies(OpInfo info, Map<TypeVariable<?>, Typ
273330 return resolvedDependencies ;
274331 }
275332
276- private boolean adaptOpOutputSatisfiesRefTypes (Type adaptTo , Map <TypeVariable <?>, Type > map , OpRef ref ) {
277- for (Type opType : ref .getTypes ()) {
278- // TODO: clean this logic
279- if (opType instanceof ParameterizedType ) {
280- if (!MatchingUtils .checkGenericAssignability (adaptTo ,
281- (ParameterizedType ) opType , map , true ))
282- {
283- return false ;
284- }
285- }
286- else if (!Types .isAssignable (opType , adaptTo , map )) {
287- return false ;
288- }
289- }
290- return true ;
291- }
292-
293333 /**
294334 * Adapts an Op with the name of ref into a type that can be SAFELY cast to
295335 * ref.
@@ -355,54 +395,21 @@ private OpCandidate adaptOp(OpRef ref) throws OpMatchingException {
355395 "Op adaptation failed: no adaptable Ops of type " + ref .getName ());
356396 }
357397
358- /**
359- * Wraps the matched op into an {@link Op} that knows its generic typing.
360- *
361- * @param op - the op to wrap.
362- * @param opInfo - from which we determine the {@link Type} of the {@code Op}
363- *
364- * @return an {@link Op} wrapping of op.
365- */
366- private <T > T wrapOp (T op , OpInfo opInfo , Map <TypeVariable <?>, Type > typeVarAssigns ) {
367- if (wrappers == null )
368- initWrappers ();
369-
370- Type opType = opInfo .opType ();
371- try {
372- // find the opWrapper that wraps this type of Op
373- Class <?> wrapper = getWrapperClass (op , opInfo );
374- Type exactSuperType = Types .getExactSuperType (opType , wrapper );
375- Type reifiedSuperType = Types .substituteTypeVariables (exactSuperType , typeVarAssigns );
376-
377- return opify (op , reifiedSuperType );
378- } catch (IllegalArgumentException | SecurityException exc ) {
379- log .error (exc .getMessage () != null ? exc .getMessage () : "Cannot wrap " + op .getClass ());
380- return op ;
381- } catch (NullPointerException e ) {
382- log .error ("No wrapper exists for " + Types .raw (opType ).toString () + "." );
383- return op ;
384- }
385- }
386-
387- private void initWrappers () {
388- wrappers = new HashMap <>();
389- for (OpWrapper <?> wrapper : pluginService .createInstancesOfType (OpWrapper .class )) {
390- wrappers .put (wrapper .type (), wrapper );
398+ private boolean adaptOpOutputSatisfiesRefTypes (Type adaptTo , Map <TypeVariable <?>, Type > map , OpRef ref ) {
399+ for (Type opType : ref .getTypes ()) {
400+ // TODO: clean this logic
401+ if (opType instanceof ParameterizedType ) {
402+ if (!MatchingUtils .checkGenericAssignability (adaptTo ,
403+ (ParameterizedType ) opType , map , true ))
404+ {
405+ return false ;
406+ }
407+ }
408+ else if (!Types .isAssignable (opType , adaptTo , map )) {
409+ return false ;
410+ }
391411 }
392- }
393-
394- private Class <?> getWrapperClass (Object op , OpInfo info ) {
395- Class <?>[] suitableWrappers = wrappers .keySet ().stream ().filter (wrapper -> wrapper .isInstance (op ))
396- .toArray (Class []::new );
397- if (suitableWrappers .length == 0 )
398- throw new IllegalArgumentException (info .implementationName () + ": matched op Type " + info .opType ().getClass ()
399- + " does not match a wrappable Op type." );
400- if (suitableWrappers .length > 1 )
401- throw new IllegalArgumentException (
402- "Matched op Type " + info .opType ().getClass () + " matches multiple Op types: " + wrappers .toString ());
403- if (!Types .isAssignable (Types .raw (info .opType ()), suitableWrappers [0 ]))
404- throw new IllegalArgumentException (Types .raw (info .opType ()) + "cannot be wrapped as a " + suitableWrappers [0 ].getClass ());
405- return suitableWrappers [0 ];
412+ return true ;
406413 }
407414
408415 private OpRef inferOpRef (OpDependencyMember <?> dependency ,
0 commit comments