Skip to content

Commit d37cb10

Browse files
Improve ASTParser options when project is resolved. Fix #488 (#490)
* Improve ASTParser options when project is resolved. Fix #488 Co-authored-by: Jinbo Wang <[email protected]>
1 parent a011049 commit d37cb10

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JdtSourceLookUpProvider.java

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ public void initialize(IDebugAdapterContext context, Map<String, Object> props)
100100
throw new IllegalArgumentException("argument is null");
101101
}
102102
options.putAll(props);
103-
// During initialization, trigger a background job to load the source containers to improve the perf.
103+
// During initialization, trigger a background job to load the source containers
104+
// to improve the perf.
104105
new Thread(() -> {
105106
getSourceContainers();
106107
}).start();
@@ -142,15 +143,16 @@ public String[] getFullyQualifiedName(String uri, int[] lines, int[] columns) th
142143
return Stream.of(locations).map(location -> {
143144
if (location.className() != null && location.methodName() != null) {
144145
return location.className()
145-
.concat("#").concat(location.methodName())
146-
.concat("#").concat(location.methodSignature());
146+
.concat("#").concat(location.methodName())
147+
.concat("#").concat(location.methodSignature());
147148
}
148149
return location.className();
149150
}).toArray(String[]::new);
150151
}
151152

152153
@Override
153-
public JavaBreakpointLocation[] getBreakpointLocations(String sourceUri, SourceBreakpoint[] sourceBreakpoints) throws DebugException {
154+
public JavaBreakpointLocation[] getBreakpointLocations(String sourceUri, SourceBreakpoint[] sourceBreakpoints)
155+
throws DebugException {
154156
if (sourceUri == null) {
155157
throw new IllegalArgumentException("sourceUri is null");
156158
}
@@ -161,8 +163,8 @@ public JavaBreakpointLocation[] getBreakpointLocations(String sourceUri, SourceB
161163

162164
CompilationUnit astUnit = asCompilationUnit(sourceUri);
163165
JavaBreakpointLocation[] sourceLocations = Stream.of(sourceBreakpoints)
164-
.map(sourceBreakpoint -> new JavaBreakpointLocation(sourceBreakpoint.line, sourceBreakpoint.column))
165-
.toArray(JavaBreakpointLocation[]::new);
166+
.map(sourceBreakpoint -> new JavaBreakpointLocation(sourceBreakpoint.line, sourceBreakpoint.column))
167+
.toArray(JavaBreakpointLocation[]::new);
166168
if (astUnit != null) {
167169
Map<Integer, BreakpointLocation[]> resolvedLocations = new HashMap<>();
168170
for (JavaBreakpointLocation sourceLocation : sourceLocations) {
@@ -171,7 +173,7 @@ public JavaBreakpointLocation[] getBreakpointLocations(String sourceUri, SourceB
171173
if (sourceColumn > -1) {
172174
// if we have a column, try to find the lambda expression at that column
173175
LambdaExpressionLocator lambdaExpressionLocator = new LambdaExpressionLocator(astUnit,
174-
sourceLine, sourceColumn);
176+
sourceLine, sourceColumn);
175177
astUnit.accept(lambdaExpressionLocator);
176178
if (lambdaExpressionLocator.isFound()) {
177179
sourceLocation.setClassName(lambdaExpressionLocator.getFullyQualifiedTypeName());
@@ -232,7 +234,8 @@ public JavaBreakpointLocation[] getBreakpointLocations(String sourceUri, SourceB
232234

233235
private BreakpointLocation[] getInlineBreakpointLocations(final CompilationUnit astUnit, int sourceLine) {
234236
List<BreakpointLocation> locations = new ArrayList<>();
235-
// The starting position of each line is the default breakpoint location for that line.
237+
// The starting position of each line is the default breakpoint location for
238+
// that line.
236239
locations.add(new BreakpointLocation(sourceLine, 0));
237240
astUnit.accept(new ASTVisitor() {
238241
@Override
@@ -284,18 +287,18 @@ private CompilationUnit asCompilationUnit(String uri) {
284287
parser.setProject(JavaCore.create(resource.getProject()));
285288
} else {
286289
parser.setEnvironment(new String[0], new String[0], null, true);
290+
/**
291+
* See the java doc for { @link ASTParser#setSource(char[]) },
292+
* the user need specify the compiler options explicitly.
293+
*/
294+
Map<String, String> javaOptions = JavaCore.getOptions();
295+
javaOptions.put(JavaCore.COMPILER_SOURCE, this.latestJavaVersion);
296+
javaOptions.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, this.latestJavaVersion);
297+
javaOptions.put(JavaCore.COMPILER_COMPLIANCE, this.latestJavaVersion);
298+
javaOptions.put(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
299+
parser.setCompilerOptions(javaOptions);
287300
}
288301
parser.setUnitName(Paths.get(filePath).getFileName().toString());
289-
/**
290-
* See the java doc for { @link ASTParser#setSource(char[]) },
291-
* the user need specify the compiler options explicitly.
292-
*/
293-
Map<String, String> javaOptions = JavaCore.getOptions();
294-
javaOptions.put(JavaCore.COMPILER_SOURCE, this.latestJavaVersion);
295-
javaOptions.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, this.latestJavaVersion);
296-
javaOptions.put(JavaCore.COMPILER_COMPLIANCE, this.latestJavaVersion);
297-
javaOptions.put(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
298-
parser.setCompilerOptions(javaOptions);
299302
astUnit = (CompilationUnit) parser.createAST(null);
300303
} else {
301304
// For non-file uri (e.g. jdt://contents/rt.jar/java.io/PrintStream.class),
@@ -336,7 +339,8 @@ public String getJavaRuntimeVersion(String projectName) {
336339

337340
return resolveSystemLibraryVersion(project, vmInstall);
338341
} catch (CoreException e) {
339-
logger.log(Level.SEVERE, "Failed to get Java runtime version for project '" + projectName + "': " + e.getMessage(), e);
342+
logger.log(Level.SEVERE,
343+
"Failed to get Java runtime version for project '" + projectName + "': " + e.getMessage(), e);
340344
}
341345
}
342346

@@ -345,6 +349,7 @@ public String getJavaRuntimeVersion(String projectName) {
345349

346350
/**
347351
* Get the project associated source containers.
352+
*
348353
* @return the initialized source container list
349354
*/
350355
public synchronized ISourceContainer[] getSourceContainers() {
@@ -373,7 +378,8 @@ private String getContents(IClassFile cf) {
373378
source = buffer.getContents();
374379
}
375380
} catch (JavaModelException e) {
376-
logger.log(Level.SEVERE, String.format("Failed to parse the source contents of the class file: %s", e.toString()), e);
381+
logger.log(Level.SEVERE,
382+
String.format("Failed to parse the source contents of the class file: %s", e.toString()), e);
377383
}
378384
if (source == null) {
379385
source = "";
@@ -388,7 +394,7 @@ private static String getFileURI(IClassFile classFile) {
388394
try {
389395
return new URI(JDT_SCHEME, "contents", PATH_SEPARATOR + jarName + PATH_SEPARATOR + packageName
390396
+ PATH_SEPARATOR + classFile.getElementName(), classFile.getHandleIdentifier(), null)
391-
.toASCIIString();
397+
.toASCIIString();
392398
} catch (URISyntaxException e) {
393399
return null;
394400
}
@@ -425,8 +431,7 @@ private static IClassFile resolveClassFile(String uriString) {
425431

426432
private static String readFile(String filePath) {
427433
StringBuilder builder = new StringBuilder();
428-
try (BufferedReader bufferReader =
429-
new BufferedReader(new InputStreamReader(new FileInputStream(filePath)))) {
434+
try (BufferedReader bufferReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)))) {
430435
final int BUFFER_SIZE = 4096;
431436
char[] buffer = new char[BUFFER_SIZE];
432437
while (true) {
@@ -442,7 +447,8 @@ private static String readFile(String filePath) {
442447
return builder.toString();
443448
}
444449

445-
private static String resolveSystemLibraryVersion(IJavaProject project, IVMInstall vmInstall) throws JavaModelException {
450+
private static String resolveSystemLibraryVersion(IJavaProject project, IVMInstall vmInstall)
451+
throws JavaModelException {
446452
LibraryLocation[] libraries = JavaRuntime.getLibraryLocations(vmInstall);
447453
if (libraries != null && libraries.length > 0) {
448454
IPackageFragmentRoot root = project.findPackageFragmentRoot(libraries[0].getSystemLibraryPath());

0 commit comments

Comments
 (0)