@@ -182,46 +182,59 @@ private static List<JavaClass> populateAnonymousClasses(String source, List<Java
182
182
183
183
String _source = JavaSourceUtils .maskClasses (source , innerClasses );
184
184
185
- Pattern pattern = Pattern .compile (JavaSourceUtils .ANONYMOUS_CLASS_REGEX );
186
- Matcher matcher = pattern .matcher (_source );
185
+ Pattern pattern = Pattern .compile (JavaSourceUtils .ANONYMOUS_CLASS_START_REGEX );
186
+ Matcher startMatcher = pattern .matcher (_source );
187
+ Matcher endMatcher = null ;
187
188
188
- while (startOffset <= endOffset && matcher .find (startOffset )) {
189
- startOffset = matcher .end ();
189
+ while (startOffset <= endOffset && startMatcher .find (startOffset )) {
190
+ startOffset = startMatcher .end ();
190
191
191
192
// Generics
192
- if (_source .charAt (startOffset ) == '<' ) { // NOI18N
193
- startOffset = JavaSourceUtils .skipBlock (_source , startOffset , '<' , '>' ); // NOI18N
194
- startOffset = JavaSourceUtils .skipWhiteSpaces (_source , startOffset );
193
+ if (_source .charAt (startOffset - 1 ) == '<' ) { // NOI18N
194
+ startOffset = JavaSourceUtils .skipBlock (_source , startOffset - 1 , '<' , '>' ); // NOI18N
195
195
}
196
196
197
- // Array definition
198
- if (_source .charAt (startOffset ) == '[' ) { // NOI18N
199
- while (_source .charAt (startOffset ) == '[' && startOffset < endOffset ) // NOI18N
200
- startOffset = JavaSourceUtils .skipBlock (_source , startOffset , '[' , ']' ); // NOI18N
201
- startOffset = JavaSourceUtils .skipWhiteSpaces (_source , startOffset );
202
- // Anonymous class
203
- } else {
204
- // Not expected
205
- if (_source .charAt (startOffset ) != '(' ) { // NOI18N
206
- // nothing we can do here, just search again
207
- } else {
208
- // TODO do not skipBlock to search the parameters as well
209
- // but the numbering of anonymous classes becomes broken
210
- startOffset = JavaSourceUtils .skipBlock (_source , startOffset , '(' , ')' ); // NOI18N
197
+ if (endMatcher == null ) {
198
+ pattern = Pattern .compile (JavaSourceUtils .ANONYMOUS_CLASS_END_REGEX );
199
+ endMatcher = pattern .matcher (_source );
200
+ }
201
+
202
+ if (endMatcher .find (startOffset )) {
203
+ startOffset = endMatcher .end ();
204
+
205
+ // Generics
206
+ if (_source .charAt (startOffset ) == '<' ) { // NOI18N
207
+ startOffset = JavaSourceUtils .skipBlock (_source , startOffset , '<' , '>' ); // NOI18N
208
+ startOffset = JavaSourceUtils .skipWhiteSpaces (_source , startOffset );
209
+ }
210
+
211
+ // Array definition
212
+ if (_source .charAt (startOffset ) == '[' ) { // NOI18N
213
+ while (_source .charAt (startOffset ) == '[' && startOffset < endOffset ) // NOI18N
214
+ startOffset = JavaSourceUtils .skipBlock (_source , startOffset , '[' , ']' ); // NOI18N
211
215
startOffset = JavaSourceUtils .skipWhiteSpaces (_source , startOffset );
212
- // Object creation only
213
- if (_source .charAt (startOffset ) != '{' ) { // NOI18N
216
+ // Anonymous class
217
+ } else {
218
+ // Not expected
219
+ if (_source .charAt (startOffset ) != '(' ) { // NOI18N
214
220
// nothing we can do here, just search again
215
- // Anonymous class
216
221
} else {
217
- int nameOffset = JavaSourceUtils .skipWhiteSpaces (_source , matcher .start () + "_new" .length ()); // NOI18N
218
-
219
- int [] bodyOffsets = JavaSourceUtils .getBlockBounds (_source , startOffset , '{' , '}' ); // NOI18N
220
- if (bodyOffsets [0 ] == -1 || bodyOffsets [1 ] == -1 || bodyOffsets [1 ] > endOffset ) break ;
221
-
222
- classes .add (new JavaClass (Integer .toString (classes .size () + 1 ), source , nameOffset , bodyOffsets [0 ], bodyOffsets [1 ]));
223
-
224
- startOffset = bodyOffsets [1 ] + 1 ;
222
+ // TODO do not skipBlock to search the parameters as well
223
+ // but the numbering of anonymous classes becomes broken
224
+ startOffset = JavaSourceUtils .skipBlock (_source , startOffset , '(' , ')' ); // NOI18N
225
+ startOffset = JavaSourceUtils .skipWhiteSpaces (_source , startOffset );
226
+ // Object creation only
227
+ if (_source .charAt (startOffset ) != '{' ) { // NOI18N
228
+ // nothing we can do here, just search again
229
+ // Anonymous class
230
+ } else {
231
+ int [] bodyOffsets = JavaSourceUtils .getBlockBounds (_source , startOffset , '{' , '}' ); // NOI18N
232
+ if (bodyOffsets [0 ] == -1 || bodyOffsets [1 ] == -1 || bodyOffsets [1 ] > endOffset ) break ;
233
+
234
+ classes .add (new JavaClass (Integer .toString (classes .size () + 1 ), source , endMatcher .start (JavaSourceUtils .REGEX_GROUP_NAME ), bodyOffsets [0 ], bodyOffsets [1 ]));
235
+
236
+ startOffset = bodyOffsets [1 ] + 1 ;
237
+ }
225
238
}
226
239
}
227
240
}
0 commit comments