Skip to content

Commit 6ddc66a

Browse files
committed
Wrap up questionable changes
1 parent 8e2381d commit 6ddc66a

File tree

5 files changed

+33
-46
lines changed

5 files changed

+33
-46
lines changed

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/links/DefaultJavaElementLocationProvider.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2018, 2023 Pivotal, Inc.
2+
* Copyright (c) 2018, 2024 Pivotal, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -11,8 +11,6 @@
1111
package org.springframework.ide.vscode.boot.java.links;
1212

1313
import java.net.URI;
14-
import java.net.URL;
15-
import java.nio.file.Paths;
1614
import java.util.Optional;
1715
import java.util.concurrent.atomic.AtomicReference;
1816

@@ -55,13 +53,12 @@ public Location findLocation(IJavaProject project, IMember member) {
5553
URI docUri = javaDocUriProvider.docUri(project, fqName);
5654
if (docUri != null) {
5755
loc.setUri(docUri.toASCIIString());
58-
Optional<URL> url = SourceLinks.source(project, fqName);
59-
if (url.isPresent()) {
56+
Optional<URI> uriOpt = SourceLinks.source(project, fqName);
57+
if (uriOpt.isPresent()) {
6058
String memberBindingKey = member.getBindingKey();
6159

6260
try {
63-
URL sourceUrl = url.get();
64-
URI uri = sourceUrl.getProtocol().equals("file") ? Paths.get(sourceUrl.toURI()).toFile().toPath().toUri() : sourceUrl.toURI();
61+
URI uri = uriOpt.get();
6562
Range r = cuCache.withCompilationUnit(project, uri, (cu) -> {
6663
AtomicReference<Range> range = new AtomicReference<>(null);
6764
if (cu == null) {

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/links/SourceLinks.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2018, 2019 Pivotal, Inc.
2+
* Copyright (c) 2018, 2024 Pivotal, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -14,6 +14,7 @@
1414
import java.io.IOException;
1515
import java.net.JarURLConnection;
1616
import java.net.MalformedURLException;
17+
import java.net.URI;
1718
import java.net.URISyntaxException;
1819
import java.net.URL;
1920
import java.net.URLConnection;
@@ -72,6 +73,7 @@ public static Optional<Path> sourceFromSourceFolder(String fqName, IClasspath cl
7273
})
7374
.map(url -> {
7475
try {
76+
// .toFile().toPath() to get on Win: file:
7577
return Paths.get(url.toURI());
7678
} catch (URISyntaxException e) {
7779
log.warn("Failed to convert URL {} to path. {}", url, fqName, e);
@@ -82,29 +84,23 @@ public static Optional<Path> sourceFromSourceFolder(String fqName, IClasspath cl
8284
.findFirst();
8385
}
8486

85-
public static Optional<URL> source(IJavaProject project, String fqName) {
87+
public static Optional<URI> source(IJavaProject project, String fqName) {
8688
// Try to find in a source JAR
8789
IJavaModuleData classpathResourceContainer = project.getIndex().findClasspathResourceContainer(fqName);
8890
if (classpathResourceContainer != null) {
89-
Optional<URL> url = IClasspathUtil.sourceContainer(project.getClasspath(), classpathResourceContainer.getContainer()).map(file -> {
91+
Optional<URI> uri = IClasspathUtil.sourceContainer(project.getClasspath(), classpathResourceContainer.getContainer()).map(file -> {
9092
try {
91-
return TypeUrlProviderFromContainerUrl.JAR_SOURCE_URL_PROVIDER.url(file, fqName, classpathResourceContainer.getModule());
93+
return TypeUrlProviderFromContainerUrl.JAR_SOURCE_URL_PROVIDER.url(file, fqName, classpathResourceContainer.getModule()).toURI();
9294
} catch (Exception e) {
9395
throw new IllegalStateException(e);
9496
}
9597
});
9698

97-
if (!url.isPresent()) {
99+
if (!uri.isPresent()) {
98100
// Try Source folder
99-
url = sourceFromSourceFolder(fqName, project.getClasspath()).map(p -> {
100-
try {
101-
return p.toUri().toURL();
102-
} catch (MalformedURLException e) {
103-
throw new IllegalStateException(e);
104-
}
105-
});
101+
uri = sourceFromSourceFolder(fqName, project.getClasspath()).map(p -> p.toUri());
106102
}
107-
return url;
103+
return uri;
108104
}
109105
return Optional.empty();
110106
}

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/spel/SpelDefinitionProvider.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import java.net.URI;
1414
import java.net.URISyntaxException;
15-
import java.net.URL;
1615
import java.util.ArrayList;
1716
import java.util.Arrays;
1817
import java.util.Collections;
@@ -149,9 +148,9 @@ private List<LocationLink> findLocationLinksForMethodRef(String methodName, Stri
149148
try {
150149
if (className.startsWith("T")) {
151150
String classFqName = className.substring(2, className.length() - 1);
152-
Optional<URL> sourceUrl = SourceLinks.source(project, classFqName);
153-
if (sourceUrl.isPresent()) {
154-
docUri = sourceUrl.get().toURI();
151+
Optional<URI> sourceUriOpt = SourceLinks.source(project, classFqName);
152+
if (sourceUriOpt.isPresent()) {
153+
docUri = sourceUriOpt.get();
155154
}
156155
} else if (className.startsWith("@")) {
157156
String bean = className.substring(1);

headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/utils/test/AstParserTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2019 Pivotal, Inc.
2+
* Copyright (c) 2019, 2024 Pivotal, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -14,7 +14,7 @@
1414
import static org.junit.jupiter.api.Assertions.assertTrue;
1515

1616
import java.net.URI;
17-
import java.net.URL;
17+
import java.nio.charset.Charset;
1818

1919
import org.apache.commons.io.IOUtils;
2020
import org.eclipse.jdt.core.dom.ASTVisitor;
@@ -50,13 +50,11 @@ public void setup() throws Exception {
5050

5151
@Test
5252
void test1() throws Exception {
53-
URL sourceUrl = SourceLinks.source(jp, "org.springframework.boot.SpringApplication").get();
54-
55-
URI uri = sourceUrl.toURI();
53+
URI uri = SourceLinks.source(jp, "org.springframework.boot.SpringApplication").get();
5654

5755
String unitName = "SpringApplication";
5856

59-
char[] content = IOUtils.toString(uri).toCharArray();
57+
char[] content = IOUtils.toString(uri, Charset.defaultCharset()).toCharArray();
6058

6159
CompilationUnit cu = CompilationUnitCache.parse2(content, uri.toASCIIString(), unitName, jp);
6260

headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/test/DefinitionLinkAsserts.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2018, 2023 Pivotal, Inc.
2+
* Copyright (c) 2018, 2024 Pivotal, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -11,8 +11,6 @@
1111
package org.springframework.ide.vscode.boot.test;
1212

1313
import java.net.URI;
14-
import java.net.URL;
15-
import java.nio.file.Paths;
1614
import java.util.ArrayList;
1715
import java.util.Arrays;
1816
import java.util.List;
@@ -85,17 +83,17 @@ public String toString() {
8583

8684
public Location getLocation(CompilationUnitCache cuCache, JavaDocumentUriProvider javaDocumentUriProvider, IJavaProject project) throws Exception {
8785
Location loc = new Location();
88-
Optional<URL> sourceUrl = SourceLinks.source(project, fqName);
89-
if (sourceUrl.isPresent()) {
86+
Optional<URI> sourceUriOpt = SourceLinks.source(project, fqName);
87+
if (sourceUriOpt.isPresent()) {
9088

9189
URI docUri = javaDocumentUriProvider.docUri(project, fqName);
9290
loc.setUri(docUri.toASCIIString());
9391

94-
URI sourceUri = sourceUrl.get().toURI();
92+
URI sourceUri = sourceUriOpt.get();
9593
Range r = cuCache.withCompilationUnit(project, sourceUri, (cu) -> {
9694
try {
9795
AtomicReference<Range> range = new AtomicReference<>(null);
98-
TextDocument doc = new TextDocument(sourceUrl.get().toString(), LanguageId.JAVA);
96+
TextDocument doc = new TextDocument(sourceUri.toASCIIString(), LanguageId.JAVA);
9997
doc.setText(cuCache.fetchContent(sourceUri));
10098
String typeName = fqName.substring(fqName.lastIndexOf('.') + 1);
10199
String[] nameTokens = typeName.split("\\$");
@@ -162,10 +160,10 @@ public String toString() {
162160
public Location getLocation(CompilationUnitCache cuCache, JavaDocumentUriProvider javaDocumentUriProvider, IJavaProject project) throws Exception {
163161
Location loc = new Location();
164162

165-
Optional<URL> sourceUrl = SourceLinks.source(project, fqName);
166-
if (sourceUrl.isPresent()) {
163+
Optional<URI> sourceUriOpt = SourceLinks.source(project, fqName);
164+
if (sourceUriOpt.isPresent()) {
167165

168-
URI sourceUri = sourceUrl.get().toURI();
166+
URI sourceUri = sourceUriOpt.get();
169167

170168
URI docUri = javaDocumentUriProvider.docUri(project, fqName);
171169
loc.setUri(docUri.toASCIIString());
@@ -175,7 +173,7 @@ public Location getLocation(CompilationUnitCache cuCache, JavaDocumentUriProvide
175173
Range r = cuCache.withCompilationUnit(project, sourceUri, (cu) -> {
176174
try {
177175
AtomicReference<Range> range = new AtomicReference<>(null);
178-
TextDocument doc = new TextDocument(sourceUrl.get().toString(), LanguageId.JAVA);
176+
TextDocument doc = new TextDocument(sourceUri.toASCIIString(), LanguageId.JAVA);
179177
doc.setText(cuCache.fetchContent(sourceUri));
180178
cu.accept(new ASTVisitor() {
181179

@@ -260,18 +258,17 @@ public JavaType(String fqName) {
260258

261259
public Location getLocation(CompilationUnitCache cuCache, JavaDocumentUriProvider javaDocumentUriProvider, IJavaProject project) throws Exception {
262260
Location loc = new Location();
263-
Optional<URL> sourceUrl = SourceLinks.source(project, fqName);
264-
if (sourceUrl.isPresent()) {
261+
Optional<URI> sourceUriOpt = SourceLinks.source(project, fqName);
262+
if (sourceUriOpt.isPresent()) {
265263

266264
URI docUri = javaDocumentUriProvider.docUri(project, fqName);
267265
loc.setUri(docUri.toASCIIString());
268266

269267
String typeName = fqName.substring(fqName.lastIndexOf('.') + 1);
270-
URL url = sourceUrl.get();
271-
URI sourceUri = url.getProtocol().equals("file") ? Paths.get(sourceUrl.get().toURI()).toFile().toPath().toUri() : url.toURI();
268+
URI sourceUri = sourceUriOpt.get();
272269
Range r = cuCache.withCompilationUnit(project, sourceUri, (cu) -> {
273270
try {
274-
TextDocument doc = new TextDocument(sourceUrl.get().toString(), LanguageId.JAVA);
271+
TextDocument doc = new TextDocument(sourceUri.toASCIIString(), LanguageId.JAVA);
275272
doc.setText(cuCache.fetchContent(sourceUri));
276273
AtomicReference<Range> range = new AtomicReference<>(null);
277274
String[] nameTokens = typeName.split("\\$");

0 commit comments

Comments
 (0)