10
10
import java .util .Objects ;
11
11
import java .util .stream .Collectors ;
12
12
13
+ import org .scijava .Context ;
13
14
import org .scijava .Priority ;
14
15
import org .scijava .discovery .Discoverer ;
15
16
import org .scijava .discovery .Discovery ;
16
- import org .scijava .function .Functions ;
17
17
import org .scijava .log .LogService ;
18
18
import org .scijava .ops .api .OpInfo ;
19
19
import org .scijava .ops .api .OpInfoGenerator ;
@@ -45,51 +45,64 @@ public class TagBasedOpInfoGenerator implements OpInfoGenerator {
45
45
private final LogService log ;
46
46
private final List <Discoverer > discoverers ;
47
47
48
- public TagBasedOpInfoGenerator (LogService log , Discoverer ... d ) {
48
+ public TagBasedOpInfoGenerator (final LogService log , Discoverer ... d ) {
49
49
this .log = log ;
50
50
this .discoverers = Arrays .asList (d );
51
51
}
52
52
53
- Functions .Arity3 <Class <?>, Double , String [], OpClassInfo > opClassGenerator = //
54
- (cls , priority , names ) -> {
55
- String version = VersionUtils .getVersion (cls );
56
- return new OpClassInfo (cls , version , new DefaultHints (), priority , names );
57
- };
58
-
59
- Functions .Arity3 <Method , Double , String [], OpMethodInfo > opMethodGenerator = //
60
- (m , priority , names ) -> {
61
- String version = VersionUtils .getVersion (m .getDeclaringClass ());
62
- return new OpMethodInfo (m , version , new DefaultHints (), names );
63
- };
64
-
65
- Functions .Arity3 <Field , Double , String [], OpFieldInfo > opFieldGenerator = //
66
- (f , priority , names ) -> {
67
- String version = VersionUtils .getVersion (f .getDeclaringClass ());
68
- Object instance ;
69
- try {
70
- instance = f .getDeclaringClass ().getDeclaredConstructor ().newInstance ();
71
- }
72
- catch (InstantiationException | IllegalAccessException
73
- | IllegalArgumentException | InvocationTargetException
74
- | NoSuchMethodException | SecurityException exc )
53
+ private OpInfo opClassGenerator (Class <?> cls , double priority ,
54
+ String [] names )
55
+ {
56
+ String version = VersionUtils .getVersion (cls );
57
+ return new OpClassInfo (cls , version , new DefaultHints (), priority , names );
58
+ }
59
+
60
+ private OpInfo opMethodGenerator (Method m , String opType , double priority ,
61
+ String [] names )
62
+ {
63
+ Class <?> cls ;
64
+ try {
65
+ cls = Context .getClassLoader ().loadClass (opType );
66
+ }
67
+ catch (ClassNotFoundException exc ) {
68
+ log .warn ("Skipping method " + m + ": Cannot load Class" + opType );
69
+ return null ;
70
+ }
71
+ String version = VersionUtils .getVersion (m .getDeclaringClass ());
72
+ return new OpMethodInfo (m , cls , version , new DefaultHints (), priority ,
73
+ names );
74
+ }
75
+
76
+ private OpInfo opFieldGenerator (Field f , double priority , String [] names ) {
77
+ String version = VersionUtils .getVersion (f .getDeclaringClass ());
78
+ Object instance ;
79
+ try {
80
+ instance = f .getDeclaringClass ().getDeclaredConstructor ().newInstance ();
81
+ }
82
+ catch (InstantiationException | IllegalAccessException
83
+ | IllegalArgumentException | InvocationTargetException
84
+ | NoSuchMethodException | SecurityException exc )
75
85
{
76
- return null ;
77
- }
78
- return new OpFieldInfo (instance , f , version , new DefaultHints (), names );
79
- };
86
+ return null ;
87
+ }
88
+ return new OpFieldInfo (instance , f , version , new DefaultHints (), priority ,
89
+ names );
90
+ }
80
91
81
92
@ Override
82
93
public List <OpInfo > generateInfos () {
94
+ try {
83
95
List <OpInfo > infos = discoverers .stream () //
84
96
.flatMap (d -> d .elementsTaggedWith (TAGTYPE ).stream ()) //
85
97
.map (discovery -> {
86
98
// Obtain op metadata
87
99
String [] names ;
100
+ String opType ;
88
101
double priority ;
89
102
90
103
try {
91
104
names = getOpNames (discovery );
92
- System . out . println ( names );
105
+ opType = getOpType ( discovery );
93
106
priority = getOpPriority (discovery );
94
107
}
95
108
catch (IllegalArgumentException e ) {
@@ -101,19 +114,23 @@ public List<OpInfo> generateInfos() {
101
114
// Delegate to proper constructor
102
115
AnnotatedElement e = discovery .discovery ();
103
116
if (e instanceof Class ) {
104
- return opClassGenerator . apply ((Class <?>) e , priority , names );
117
+ return opClassGenerator ((Class <?>) e , priority , names );
105
118
}
106
119
else if (e instanceof Method ) {
107
- return opMethodGenerator . apply ((Method ) e , priority , names );
120
+ return opMethodGenerator ((Method ) e , opType , priority , names );
108
121
}
109
122
else if (e instanceof Field ) {
110
- return opFieldGenerator . apply ((Field ) e , priority , names );
123
+ return opFieldGenerator ((Field ) e , priority , names );
111
124
}
112
125
else return null ;
113
126
}) //
114
127
.filter (Objects ::nonNull ) //
115
128
.collect (Collectors .toList ());
116
129
return infos ;
130
+ } catch (NullPointerException e ) {
131
+ e .printStackTrace ();
132
+ return null ;
133
+ }
117
134
}
118
135
119
136
private String [] getOpNames (Discovery <AnnotatedElement > d ) {
@@ -123,10 +140,8 @@ private String[] getOpNames(Discovery<AnnotatedElement> d) {
123
140
throw new IllegalArgumentException ("Op discovery " + d + " does not record any names!" );
124
141
}
125
142
if (!names .isEmpty ()) {
126
- System .out .println (names );
127
143
return OpUtils .parseOpNames (names );
128
144
}
129
- System .out .println (name );
130
145
return OpUtils .parseOpNames (name );
131
146
}
132
147
@@ -135,4 +150,8 @@ private static double getOpPriority(Discovery<AnnotatedElement> d) {
135
150
return priority .isEmpty () ? Priority .NORMAL : Double .parseDouble (priority );
136
151
}
137
152
153
+ private static String getOpType (Discovery <AnnotatedElement > d ) {
154
+ return d .option ("type" );
155
+ }
156
+
138
157
}
0 commit comments