Skip to content

Commit

Permalink
Wrap up questionable changes
Browse files Browse the repository at this point in the history
  • Loading branch information
BoykoAlex committed Dec 8, 2024
1 parent 8e2381d commit 6ddc66a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018, 2023 Pivotal, Inc.
* Copyright (c) 2018, 2024 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -11,8 +11,6 @@
package org.springframework.ide.vscode.boot.java.links;

import java.net.URI;
import java.net.URL;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -55,13 +53,12 @@ public Location findLocation(IJavaProject project, IMember member) {
URI docUri = javaDocUriProvider.docUri(project, fqName);
if (docUri != null) {
loc.setUri(docUri.toASCIIString());
Optional<URL> url = SourceLinks.source(project, fqName);
if (url.isPresent()) {
Optional<URI> uriOpt = SourceLinks.source(project, fqName);
if (uriOpt.isPresent()) {
String memberBindingKey = member.getBindingKey();

try {
URL sourceUrl = url.get();
URI uri = sourceUrl.getProtocol().equals("file") ? Paths.get(sourceUrl.toURI()).toFile().toPath().toUri() : sourceUrl.toURI();
URI uri = uriOpt.get();
Range r = cuCache.withCompilationUnit(project, uri, (cu) -> {
AtomicReference<Range> range = new AtomicReference<>(null);
if (cu == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018, 2019 Pivotal, Inc.
* Copyright (c) 2018, 2024 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -14,6 +14,7 @@
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
Expand Down Expand Up @@ -72,6 +73,7 @@ public static Optional<Path> sourceFromSourceFolder(String fqName, IClasspath cl
})
.map(url -> {
try {
// .toFile().toPath() to get on Win: file:
return Paths.get(url.toURI());
} catch (URISyntaxException e) {
log.warn("Failed to convert URL {} to path. {}", url, fqName, e);
Expand All @@ -82,29 +84,23 @@ public static Optional<Path> sourceFromSourceFolder(String fqName, IClasspath cl
.findFirst();
}

public static Optional<URL> source(IJavaProject project, String fqName) {
public static Optional<URI> source(IJavaProject project, String fqName) {
// Try to find in a source JAR
IJavaModuleData classpathResourceContainer = project.getIndex().findClasspathResourceContainer(fqName);
if (classpathResourceContainer != null) {
Optional<URL> url = IClasspathUtil.sourceContainer(project.getClasspath(), classpathResourceContainer.getContainer()).map(file -> {
Optional<URI> uri = IClasspathUtil.sourceContainer(project.getClasspath(), classpathResourceContainer.getContainer()).map(file -> {
try {
return TypeUrlProviderFromContainerUrl.JAR_SOURCE_URL_PROVIDER.url(file, fqName, classpathResourceContainer.getModule());
return TypeUrlProviderFromContainerUrl.JAR_SOURCE_URL_PROVIDER.url(file, fqName, classpathResourceContainer.getModule()).toURI();
} catch (Exception e) {
throw new IllegalStateException(e);
}
});

if (!url.isPresent()) {
if (!uri.isPresent()) {
// Try Source folder
url = sourceFromSourceFolder(fqName, project.getClasspath()).map(p -> {
try {
return p.toUri().toURL();
} catch (MalformedURLException e) {
throw new IllegalStateException(e);
}
});
uri = sourceFromSourceFolder(fqName, project.getClasspath()).map(p -> p.toUri());
}
return url;
return uri;
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -149,9 +148,9 @@ private List<LocationLink> findLocationLinksForMethodRef(String methodName, Stri
try {
if (className.startsWith("T")) {
String classFqName = className.substring(2, className.length() - 1);
Optional<URL> sourceUrl = SourceLinks.source(project, classFqName);
if (sourceUrl.isPresent()) {
docUri = sourceUrl.get().toURI();
Optional<URI> sourceUriOpt = SourceLinks.source(project, classFqName);
if (sourceUriOpt.isPresent()) {
docUri = sourceUriOpt.get();
}
} else if (className.startsWith("@")) {
String bean = className.substring(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019 Pivotal, Inc.
* Copyright (c) 2019, 2024 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -14,7 +14,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;

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

@Test
void test1() throws Exception {
URL sourceUrl = SourceLinks.source(jp, "org.springframework.boot.SpringApplication").get();

URI uri = sourceUrl.toURI();
URI uri = SourceLinks.source(jp, "org.springframework.boot.SpringApplication").get();

String unitName = "SpringApplication";

char[] content = IOUtils.toString(uri).toCharArray();
char[] content = IOUtils.toString(uri, Charset.defaultCharset()).toCharArray();

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018, 2023 Pivotal, Inc.
* Copyright (c) 2018, 2024 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -11,8 +11,6 @@
package org.springframework.ide.vscode.boot.test;

import java.net.URI;
import java.net.URL;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -85,17 +83,17 @@ public String toString() {

public Location getLocation(CompilationUnitCache cuCache, JavaDocumentUriProvider javaDocumentUriProvider, IJavaProject project) throws Exception {
Location loc = new Location();
Optional<URL> sourceUrl = SourceLinks.source(project, fqName);
if (sourceUrl.isPresent()) {
Optional<URI> sourceUriOpt = SourceLinks.source(project, fqName);
if (sourceUriOpt.isPresent()) {

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

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

Optional<URL> sourceUrl = SourceLinks.source(project, fqName);
if (sourceUrl.isPresent()) {
Optional<URI> sourceUriOpt = SourceLinks.source(project, fqName);
if (sourceUriOpt.isPresent()) {

URI sourceUri = sourceUrl.get().toURI();
URI sourceUri = sourceUriOpt.get();

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

Expand Down Expand Up @@ -260,18 +258,17 @@ public JavaType(String fqName) {

public Location getLocation(CompilationUnitCache cuCache, JavaDocumentUriProvider javaDocumentUriProvider, IJavaProject project) throws Exception {
Location loc = new Location();
Optional<URL> sourceUrl = SourceLinks.source(project, fqName);
if (sourceUrl.isPresent()) {
Optional<URI> sourceUriOpt = SourceLinks.source(project, fqName);
if (sourceUriOpt.isPresent()) {

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

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

0 comments on commit 6ddc66a

Please sign in to comment.