23
23
24
24
class OrderingInference {
25
25
26
- private Objenesis objenesis = new ObjenesisStd ( );
26
+ private static final Logger LOGGER = Logger . getLogger ( LoggerNames . LOGGER_NAME );
27
27
28
28
private IClassHierarchy classHierarchy ;
29
29
30
- private static final Logger LOGGER = Logger . getLogger ( LoggerNames . LOGGER_NAME );
30
+ private Objenesis objenesis = new ObjenesisStd ( );
31
31
32
32
public OrderingInference (IClassHierarchy classHierarchy ) {
33
33
this .classHierarchy = classHierarchy ;
34
34
}
35
35
36
- Ordering inferOrdering (Collection <TypeAbstraction > possibleTypes , IMethod calledMethod )
37
- throws InconsistentPossibleOrderingException , NoniterableException , NoninstantiableException ,
38
- CannotExtractSpliteratorException {
39
- return inferOrdering (possibleTypes , calledMethod .getElementName ());
36
+ private Object createInstance (Class <?> clazz ) throws NoninstantiableException {
37
+ try {
38
+ return clazz .newInstance ();
39
+ } catch (InstantiationException | IllegalAccessException e ) {
40
+ ObjectInstantiator <?> instantiator = objenesis .getInstantiatorOf (clazz );
41
+ try {
42
+ return instantiator .newInstance ();
43
+ } catch (InstantiationError e2 ) {
44
+ throw new NoninstantiableException (clazz + " cannot be instantiated: " + e2 .getCause (), e2 , clazz );
45
+ }
46
+ }
40
47
}
41
48
42
- private Ordering inferOrdering (Collection <TypeAbstraction > possibleTypes , String calledMethodName )
43
- throws InconsistentPossibleOrderingException , NoniterableException , NoninstantiableException ,
44
- CannotExtractSpliteratorException {
45
- Ordering ret = null ;
49
+ private String findStreamCreationMethod (Collection <TypeAbstraction > types ) {
50
+ // find the first one.
51
+ for ( TypeAbstraction typeAbstraction : types ) {
52
+ String methodName = findStreamCreationMethod ( typeAbstraction ) ;
46
53
47
- for (TypeAbstraction typeAbstraction : possibleTypes ) {
48
- if (typeAbstraction != TypeAbstraction .TOP ) {
49
- Ordering ordering = inferOrdering (typeAbstraction , calledMethodName );
54
+ if (methodName != null )
55
+ return methodName ;
56
+ }
57
+ // not found.
58
+ return null ;
59
+ }
50
60
51
- if (ret == null )
52
- ret = ordering ;
53
- else if (ret != ordering ) {
54
- throw new InconsistentPossibleOrderingException (
55
- "Types have inconsistent orderings: " + possibleTypes );
56
- }
57
- }
61
+ private String findStreamCreationMethod (IClass type ) {
62
+ Collection <com .ibm .wala .classLoader .IMethod > allMethods = type .getAllMethods ();
63
+ for (com .ibm .wala .classLoader .IMethod method : allMethods ) {
64
+ TypeReference typeToCheck = Util .getEvaluationType (method );
65
+
66
+ // find the first one that returns a stream.
67
+ if (Util .implementsBaseStream (typeToCheck , this .getClassHierarchy ()))
68
+ return method .getName ().toString ();
58
69
}
59
70
60
- return ret ;
71
+ return null ;
72
+ }
73
+
74
+ private String findStreamCreationMethod (TypeAbstraction typeAbstraction ) {
75
+ IClass type = typeAbstraction .getType ();
76
+ return findStreamCreationMethod (type );
77
+ }
78
+
79
+ protected IClassHierarchy getClassHierarchy () {
80
+ return classHierarchy ;
81
+ }
82
+
83
+ private Spliterator <?> getSpliterator (Object instance , String calledMethodName )
84
+ throws CannotExtractSpliteratorException {
85
+ Objects .requireNonNull (instance );
86
+ Objects .requireNonNull (calledMethodName );
87
+
88
+ Spliterator <?> spliterator = null ;
89
+
90
+ if (instance instanceof Iterable ) {
91
+ try {
92
+ spliterator = ((Iterable <?>) instance ).spliterator ();
93
+ } catch (NullPointerException e ) {
94
+ LOGGER .log (Level .WARNING , "Possible trouble creating instance (most likely private type)." , e );
95
+ return null ;
96
+ }
97
+ } else {
98
+ // try to call the stream() method to get the spliterator.
99
+ BaseStream <?, ?> baseStream = null ;
100
+ try {
101
+ Method streamCreationMethod = instance .getClass ().getMethod (calledMethodName );
102
+ Object stream = streamCreationMethod .invoke (instance );
103
+
104
+ if (stream instanceof BaseStream ) {
105
+ baseStream = (BaseStream <?, ?>) stream ;
106
+ spliterator = baseStream .spliterator ();
107
+ } else
108
+ throw new CannotExtractSpliteratorException (
109
+ "Returned object of type: " + stream .getClass () + " doesn't implement BaseStream." ,
110
+ stream .getClass ());
111
+ } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
112
+ | InvocationTargetException e ) {
113
+ throw new CannotExtractSpliteratorException (
114
+ "Cannot extract the spliterator from object of type: " + instance .getClass (), e ,
115
+ instance .getClass ());
116
+ } finally {
117
+ if (baseStream != null )
118
+ baseStream .close ();
119
+ }
120
+ }
121
+ return spliterator ;
61
122
}
62
123
63
124
Ordering inferOrdering (Collection <TypeAbstraction > possibleTypes ) throws InconsistentPossibleOrderingException ,
@@ -76,34 +137,31 @@ Ordering inferOrdering(Collection<TypeAbstraction> possibleTypes) throws Inconsi
76
137
}
77
138
}
78
139
79
- private String findStreamCreationMethod (Collection <TypeAbstraction > types ) {
80
- // find the first one.
81
- for (TypeAbstraction typeAbstraction : types ) {
82
- String methodName = findStreamCreationMethod (typeAbstraction );
83
-
84
- if (methodName != null )
85
- return methodName ;
86
- }
87
- // not found.
88
- return null ;
140
+ Ordering inferOrdering (Collection <TypeAbstraction > possibleTypes , IMethod calledMethod )
141
+ throws InconsistentPossibleOrderingException , NoniterableException , NoninstantiableException ,
142
+ CannotExtractSpliteratorException {
143
+ return inferOrdering (possibleTypes , calledMethod .getElementName ());
89
144
}
90
145
91
- private String findStreamCreationMethod ( TypeAbstraction typeAbstraction ) {
92
- IClass type = typeAbstraction . getType ();
93
- return findStreamCreationMethod ( type );
94
- }
146
+ private Ordering inferOrdering ( Collection < TypeAbstraction > possibleTypes , String calledMethodName )
147
+ throws InconsistentPossibleOrderingException , NoniterableException , NoninstantiableException ,
148
+ CannotExtractSpliteratorException {
149
+ Ordering ret = null ;
95
150
96
- private String findStreamCreationMethod (IClass type ) {
97
- Collection <com .ibm .wala .classLoader .IMethod > allMethods = type .getAllMethods ();
98
- for (com .ibm .wala .classLoader .IMethod method : allMethods ) {
99
- TypeReference typeToCheck = Util .getEvaluationType (method );
151
+ for (TypeAbstraction typeAbstraction : possibleTypes ) {
152
+ if (typeAbstraction != TypeAbstraction .TOP ) {
153
+ Ordering ordering = inferOrdering (typeAbstraction , calledMethodName );
100
154
101
- // find the first one that returns a stream.
102
- if (Util .implementsBaseStream (typeToCheck , this .getClassHierarchy ()))
103
- return method .getName ().toString ();
155
+ if (ret == null )
156
+ ret = ordering ;
157
+ else if (ret != ordering ) {
158
+ throw new InconsistentPossibleOrderingException (
159
+ "Types have inconsistent orderings: " + possibleTypes );
160
+ }
161
+ }
104
162
}
105
163
106
- return null ;
164
+ return ret ;
107
165
}
108
166
109
167
// TODO: Cache this?
@@ -150,60 +208,6 @@ private Ordering inferOrdering(String className, String calledMethodName)
150
208
}
151
209
}
152
210
153
- private Object createInstance (Class <?> clazz ) throws NoninstantiableException {
154
- try {
155
- return clazz .newInstance ();
156
- } catch (InstantiationException | IllegalAccessException e ) {
157
- ObjectInstantiator <?> instantiator = objenesis .getInstantiatorOf (clazz );
158
- try {
159
- return instantiator .newInstance ();
160
- } catch (InstantiationError e2 ) {
161
- throw new NoninstantiableException (clazz + " cannot be instantiated: " + e2 .getCause (), e2 , clazz );
162
- }
163
- }
164
- }
165
-
166
- private Spliterator <?> getSpliterator (Object instance , String calledMethodName )
167
- throws CannotExtractSpliteratorException {
168
- Objects .requireNonNull (instance );
169
- Objects .requireNonNull (calledMethodName );
170
-
171
- Spliterator <?> spliterator = null ;
172
-
173
- if (instance instanceof Iterable ) {
174
- try {
175
- spliterator = ((Iterable <?>) instance ).spliterator ();
176
- } catch (NullPointerException e ) {
177
- LOGGER .log (Level .WARNING , "Possible trouble creating instance (most likely private type)." , e );
178
- return null ;
179
- }
180
- } else {
181
- // try to call the stream() method to get the spliterator.
182
- BaseStream <?, ?> baseStream = null ;
183
- try {
184
- Method streamCreationMethod = instance .getClass ().getMethod (calledMethodName );
185
- Object stream = streamCreationMethod .invoke (instance );
186
-
187
- if (stream instanceof BaseStream ) {
188
- baseStream = (BaseStream <?, ?>) stream ;
189
- spliterator = baseStream .spliterator ();
190
- } else
191
- throw new CannotExtractSpliteratorException (
192
- "Returned object of type: " + stream .getClass () + " doesn't implement BaseStream." ,
193
- stream .getClass ());
194
- } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
195
- | InvocationTargetException e ) {
196
- throw new CannotExtractSpliteratorException (
197
- "Cannot extract the spliterator from object of type: " + instance .getClass (), e ,
198
- instance .getClass ());
199
- } finally {
200
- if (baseStream != null )
201
- baseStream .close ();
202
- }
203
- }
204
- return spliterator ;
205
- }
206
-
207
211
private Ordering inferOrdering (TypeAbstraction type , String calledMethodName )
208
212
throws NoniterableException , NoninstantiableException , CannotExtractSpliteratorException {
209
213
TypeReference typeReference = type .getTypeReference ();
@@ -215,8 +219,4 @@ private Ordering inferOrdering(TypeAbstraction type, String calledMethodName)
215
219
String binaryName = Util .getBinaryName (typeReference );
216
220
return inferOrdering (binaryName , calledMethodName );
217
221
}
218
-
219
- protected IClassHierarchy getClassHierarchy () {
220
- return classHierarchy ;
221
- }
222
222
}
0 commit comments