Skip to content

Commit 85f9165

Browse files
committed
Go To Source for instance initializers
1 parent ff2509e commit 85f9165

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

plugins/sources/src/org/graalvm/visualvm/sources/java/JavaMethod.java

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,21 @@ static JavaMethod findMethod(String methodName, String methodSignature, JavaClas
9191

9292
String source = JavaSourceUtils.maskNonBlock(cls.getSource(), '{', '}', cls.getBodyStart(), cls.getBodyEnd()); // NOI18N
9393

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
9596

9697
// 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;
99100

100101
// Native method without body (abstract & interface methods not displayed in results)
101102
return findMethodWithoutBody(methodName, methodSignature, cls, source);
102103
}
103104

104-
private static JavaMethod findStaticInitializer(JavaClass cls, String source) {
105+
private static JavaMethod findClassInitializer(JavaClass cls, String source) {
105106
int offset = cls.getBodyStart() + 1;
106107

107-
String patternS = JavaSourceUtils.STATIC_INITIALIZER_REGEX;
108+
String patternS = JavaSourceUtils.CLASS_INITIALIZER_REGEX;
108109
Pattern pattern = Pattern.compile(patternS);
109110
Matcher matcher = pattern.matcher(source);
110111

@@ -114,27 +115,51 @@ private static JavaMethod findStaticInitializer(JavaClass cls, String source) {
114115
offset = matcher.end();
115116
if (offset > bodyEnd) return null;
116117

117-
offset--; // STATIC_INITIALIZER_REGEX matched the opening '{'
118+
offset--; // CLASS_INITIALIZER_REGEX matched the opening '{'
118119
int[] bodyOffsets = JavaSourceUtils.getBlockBounds(source, offset, '{', '}'); // NOI18N
119120
if (bodyOffsets[0] == -1 || bodyOffsets[1] == -1 || bodyOffsets[1] > bodyEnd) return null;
120121

121122
return new JavaMethod("<clinit>", null, source, offset, bodyOffsets[0], bodyOffsets[1]); // NOI18N
122123
}
123124

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,
127152
JavaSourceUtils.DEFINED_METHOD_WITHBODY_START_REGEX,
128153
JavaSourceUtils.DEFINED_METHOD_WITHBODY_END_REGEX, false);
129154
}
130155

131156
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,
133158
JavaSourceUtils.DEFINED_METHOD_WITHOUTBODY_START_REGEX,
134159
JavaSourceUtils.DEFINED_METHOD_WITHOUTBODY_END_REGEX, true);
135160
}
136161

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) {
138163
int offset = cls.getBodyStart() + 1;
139164
int bodyEnd = cls.getBodyEnd();
140165

@@ -170,7 +195,7 @@ private static JavaMethod findMethod(String methodName, String methodSignature,
170195
int[] bodyOffsets = JavaSourceUtils.getBlockBounds(source, offset, '{', '}'); // NOI18N
171196
if (bodyOffsets[0] == -1 || bodyOffsets[1] == -1 || bodyOffsets[1] > bodyEnd) return null;
172197

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
174199
}
175200
}
176201
}

plugins/sources/src/org/graalvm/visualvm/sources/java/JavaSourceUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ final class JavaSourceUtils {
5757
static final String CLASS_REGEX = "(^|[\\W&&[^.]])(class|interface|enum)\\s+(?<" + REGEX_GROUP_NAME + ">" + IDENTIFIER_REGEX + ")[^\\p{javaJavaIdentifierPart}]"; // NOI18N
5858
static final String ANONYMOUS_CLASS_REGEX = "\\Wnew\\s+" + FULLY_QUALIFIED_IDENTIFIER_REGEX + "\\s*"; // NOI18N
5959

60-
static final String STATIC_INITIALIZER_REGEX = "\\Wstatic\\s*\\{"; // NOI18N
60+
static final String CLASS_INITIALIZER_REGEX = "\\Wstatic\\s*\\{"; // NOI18N
61+
static final String INSTANCE_INITIALIZER_REGEX = "[\\{\\};]\\s*\\{"; // NOI18N
62+
6163
static final String DEFINED_METHOD_WITHBODY_START_REGEX = "(?<" + REGEX_GROUP_PREFIX + ">[\\s\\>])" + REGEX_PARAMETER_0 + "\\s*\\("; // NOI18N
6264
static final String DEFINED_METHOD_WITHBODY_END_REGEX = "\\s*(" + THROWS_REGEX + ")??\\s*\\{"; // NOI18N
6365
/* TODO: review */ static final String DEFINED_METHOD_WITHOUTBODY_START_REGEX = "(?<" + REGEX_GROUP_PREFIX + ">\\Wnative[\\s\\S&&[^;]&&[^\\(]]*?[\\s\\>])" + REGEX_PARAMETER_0 + "\\s*\\("; // NOI18N

0 commit comments

Comments
 (0)