@@ -91,20 +91,21 @@ static JavaMethod findMethod(String methodName, String methodSignature, JavaClas
91
91
92
92
String source = JavaSourceUtils .maskNonBlock (cls .getSource (), '{' , '}' , cls .getBodyStart (), cls .getBodyEnd ()); // NOI18N
93
93
94
- if ("<clinit>" .equals (methodName )) return findStaticInitializer (cls , source ); // NOI18N
94
+ if ("<clinit>" .equals (methodName )) return findClassInitializer (cls , source ); // NOI18N
95
+ else if ("<init>" .equals (methodName )) return findInstanceInitializer (cls , methodSignature , source ); // NOI18N
95
96
96
97
// Regular method with body
97
- JavaMethod method = findMethodWithBody (methodName , methodSignature , cls , source );
98
- if (method != null || methodName . startsWith ( "<" )) return method ; // NOI18N
98
+ JavaMethod method = findMethodWithBody (methodName , methodName , methodSignature , cls , source );
99
+ if (method != null ) return method ;
99
100
100
101
// Native method without body (abstract & interface methods not displayed in results)
101
102
return findMethodWithoutBody (methodName , methodSignature , cls , source );
102
103
}
103
104
104
- private static JavaMethod findStaticInitializer (JavaClass cls , String source ) {
105
+ private static JavaMethod findClassInitializer (JavaClass cls , String source ) {
105
106
int offset = cls .getBodyStart () + 1 ;
106
107
107
- String patternS = JavaSourceUtils .STATIC_INITIALIZER_REGEX ;
108
+ String patternS = JavaSourceUtils .CLASS_INITIALIZER_REGEX ;
108
109
Pattern pattern = Pattern .compile (patternS );
109
110
Matcher matcher = pattern .matcher (source );
110
111
@@ -114,27 +115,51 @@ private static JavaMethod findStaticInitializer(JavaClass cls, String source) {
114
115
offset = matcher .end ();
115
116
if (offset > bodyEnd ) return null ;
116
117
117
- offset --; // STATIC_INITIALIZER_REGEX matched the opening '{'
118
+ offset --; // CLASS_INITIALIZER_REGEX matched the opening '{'
118
119
int [] bodyOffsets = JavaSourceUtils .getBlockBounds (source , offset , '{' , '}' ); // NOI18N
119
120
if (bodyOffsets [0 ] == -1 || bodyOffsets [1 ] == -1 || bodyOffsets [1 ] > bodyEnd ) return null ;
120
121
121
122
return new JavaMethod ("<clinit>" , null , source , offset , bodyOffsets [0 ], bodyOffsets [1 ]); // NOI18N
122
123
}
123
124
124
- private static JavaMethod findMethodWithBody (String methodName , String methodSignature , JavaClass cls , String source ) {
125
- if ("<init>" .equals (methodName )) methodName = cls .getName (); // NOI18N
126
- return findMethod (methodName , methodSignature , cls , source ,
125
+ private static JavaMethod findInstanceInitializer (JavaClass cls , String methodSignature , String source ) {
126
+ JavaMethod constructor = findMethod (cls .getName (), "<init>" , methodSignature , cls , source , // NOI18N
127
+ JavaSourceUtils .DEFINED_METHOD_WITHBODY_START_REGEX ,
128
+ JavaSourceUtils .DEFINED_METHOD_WITHBODY_END_REGEX , false );
129
+ if (constructor != null ) return constructor ;
130
+
131
+ int offset = cls .getBodyStart ();
132
+
133
+ String patternS = JavaSourceUtils .INSTANCE_INITIALIZER_REGEX ;
134
+ Pattern pattern = Pattern .compile (patternS );
135
+ Matcher matcher = pattern .matcher (source );
136
+
137
+ if (!matcher .find (offset )) return null ;
138
+
139
+ int bodyEnd = cls .getBodyEnd ();
140
+ offset = matcher .end ();
141
+ if (offset > bodyEnd ) return null ;
142
+
143
+ offset --; // INSTANCE_INITIALIZER_REGEX matched the opening '{'
144
+ int [] bodyOffsets = JavaSourceUtils .getBlockBounds (source , offset , '{' , '}' ); // NOI18N
145
+ if (bodyOffsets [0 ] == -1 || bodyOffsets [1 ] == -1 || bodyOffsets [1 ] > bodyEnd ) return null ;
146
+
147
+ return new JavaMethod ("<init>" , null , source , offset , bodyOffsets [0 ], bodyOffsets [1 ]); // NOI18N
148
+ }
149
+
150
+ private static JavaMethod findMethodWithBody (String methodName , String modelName , String methodSignature , JavaClass cls , String source ) {
151
+ return findMethod (methodName , modelName , methodSignature , cls , source ,
127
152
JavaSourceUtils .DEFINED_METHOD_WITHBODY_START_REGEX ,
128
153
JavaSourceUtils .DEFINED_METHOD_WITHBODY_END_REGEX , false );
129
154
}
130
155
131
156
private static JavaMethod findMethodWithoutBody (String methodName , String methodSignature , JavaClass cls , String source ) {
132
- return findMethod (methodName , methodSignature , cls , source ,
157
+ return findMethod (methodName , methodName , methodSignature , cls , source ,
133
158
JavaSourceUtils .DEFINED_METHOD_WITHOUTBODY_START_REGEX ,
134
159
JavaSourceUtils .DEFINED_METHOD_WITHOUTBODY_END_REGEX , true );
135
160
}
136
161
137
- private static JavaMethod findMethod (String methodName , String methodSignature , JavaClass cls , String source , String startRegEx , String endRegEx , boolean withoutBody ) {
162
+ private static JavaMethod findMethod (String methodName , String modelName , String methodSignature , JavaClass cls , String source , String startRegEx , String endRegEx , boolean withoutBody ) {
138
163
int offset = cls .getBodyStart () + 1 ;
139
164
int bodyEnd = cls .getBodyEnd ();
140
165
@@ -170,7 +195,7 @@ private static JavaMethod findMethod(String methodName, String methodSignature,
170
195
int [] bodyOffsets = JavaSourceUtils .getBlockBounds (source , offset , '{' , '}' ); // NOI18N
171
196
if (bodyOffsets [0 ] == -1 || bodyOffsets [1 ] == -1 || bodyOffsets [1 ] > bodyEnd ) return null ;
172
197
173
- return new JavaMethod (methodName , methodSignature , source , nameStart , bodyOffsets [0 ], bodyOffsets [1 ]); // NOI18N
198
+ return new JavaMethod (modelName , methodSignature , source , nameStart , bodyOffsets [0 ], bodyOffsets [1 ]); // NOI18N
174
199
}
175
200
}
176
201
}
0 commit comments