Apply quick fix to project fixed

This commit is contained in:
aboyko
2022-09-27 14:08:27 -04:00
parent ec8a5a87ba
commit 7d4c7df7ef
6 changed files with 69 additions and 52 deletions

View File

@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018, 2019 Pivotal, Inc.
* Copyright (c) 2018, 2022 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
@@ -14,10 +14,13 @@ import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.slf4j.Logger;
@@ -167,5 +170,17 @@ public class IClasspathUtil {
}
public static Set<String> getBinaryClasspathEntries(IJavaProject project) throws Exception {
if (project == null) {
return Collections.emptySet();
} else {
IClasspath classpath = project.getClasspath();
Stream<File> classpathEntries = IClasspathUtil.getAllBinaryRoots(classpath).stream();
return classpathEntries
.filter(file -> file.exists())
.map(file -> file.getAbsolutePath()).collect(Collectors.toSet());
}
}
}

View File

@@ -10,9 +10,13 @@
*******************************************************************************/
package org.springframework.ide.vscode.commons.rewrite.java;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.channels.ClosedByInterruptException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
@@ -20,6 +24,7 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.openrewrite.ExecutionContext;
import org.openrewrite.InMemoryExecutionContext;
@@ -40,6 +45,10 @@ import org.openrewrite.java.tree.J.CompilationUnit;
import org.openrewrite.marker.Range;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ide.vscode.commons.java.IClasspathUtil;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleTextDocumentService;
import org.springframework.ide.vscode.commons.util.text.TextDocument;
public class ORAstUtils {
@@ -221,6 +230,45 @@ public class ORAstUtils {
// return node.getMarkers().findFirst(ParentMarker.class).map(m -> m.getParent()).orElse(null);
// }
public static JavaParser createJavaParser(IJavaProject project) {
try {
List<Path> classpath = IClasspathUtil.getBinaryClasspathEntries(project).stream().map(s -> new File(s).toPath()).collect(Collectors.toList());
JavaParser jp = JavaParser.fromJavaVersion().build();
jp.setClasspath(classpath);
return jp;
} catch (Exception e) {
log.error("{}", e);
return null;
}
}
public static List<CompilationUnit> parse(SimpleTextDocumentService documents, IJavaProject project) {
List<Parser.Input> inputs = IClasspathUtil.getProjectJavaSourceFolders(project.getClasspath()).flatMap(folder -> {
try {
return Files.walk(folder.toPath());
} catch (IOException e) {
log.error("", e);
}
return Stream.empty();
}).filter(Files::isRegularFile).filter(p -> p.getFileName().toString().endsWith(".java")).map(p -> {
TextDocument doc = documents.getLatestSnapshot(p.toUri().toString());
if (doc == null) {
return new Parser.Input(p, () -> {
try {
return Files.newInputStream(p);
} catch (IOException e) {
log.error("", e);
return new ByteArrayInputStream(new byte[0]);
}
});
} else {
return new Parser.Input(p, () -> new ByteArrayInputStream(doc.get().getBytes()));
}
}).collect(Collectors.toList());
JavaParser javaParser = createJavaParser(project);
return ORAstUtils.parseInputs(javaParser, inputs);
}
public static List<CompilationUnit> parse(JavaParser parser, Iterable<Path> sourceFiles) {
InMemoryExecutionContext ctx = new InMemoryExecutionContext(ORAstUtils::logExceptionWhileParsing);
// ctx.putMessage(JavaParser.SKIP_SOURCE_SET_TYPE_GENERATION, true);

View File

@@ -289,7 +289,7 @@ public class BootLanguageServerInitializer implements InitializingBean {
components.getReconcileEngine().ifPresent(reconcileEngine -> {
Map<IJavaProject, List<TextDocumentIdentifier>> projectsToDocs = new HashMap<>();
for (String f : files) {
URI uri = Path.of(f).toUri();
URI uri = URI.create(f);
TextDocumentIdentifier docId = new TextDocumentIdentifier(uri.toString());
TextDocument doc = server.getTextDocumentService().getLatestSnapshot(docId.getUri());
if (doc == null) {

View File

@@ -11,13 +11,8 @@
package org.springframework.ide.vscode.boot.java.rewrite;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
@@ -27,8 +22,6 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.io.IOUtils;
import org.eclipse.lsp4j.TextDocumentIdentifier;
@@ -38,8 +31,6 @@ import org.openrewrite.java.tree.J.CompilationUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ide.vscode.boot.java.utils.DocumentContentProvider;
import org.springframework.ide.vscode.commons.java.IClasspath;
import org.springframework.ide.vscode.commons.java.IClasspathUtil;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
import org.springframework.ide.vscode.commons.languageserver.java.ProjectObserver;
@@ -161,40 +152,15 @@ public class RewriteCompilationUnitCache implements DocumentContentProvider, Dis
}
}
public static JavaParser createJavaParser(IJavaProject project) {
try {
List<Path> classpath = getClasspathEntries(project).stream().map(s -> new File(s).toPath()).collect(Collectors.toList());
JavaParser jp = JavaParser.fromJavaVersion().build();
jp.setClasspath(classpath);
return jp;
} catch (Exception e) {
logger.error("{}", e);
return null;
}
}
private JavaParser loadJavaParser(IJavaProject project) {
try {
return javaParsers.get(project, () -> createJavaParser(project));
return javaParsers.get(project, () -> ORAstUtils.createJavaParser(project));
} catch (ExecutionException e) {
logger.error("{}", e);
return null;
}
}
private static Set<String> getClasspathEntries(IJavaProject project) throws Exception {
if (project == null) {
return Collections.emptySet();
} else {
IClasspath classpath = project.getClasspath();
Stream<File> classpathEntries = IClasspathUtil.getAllBinaryRoots(classpath).stream();
return classpathEntries
.filter(file -> file.exists())
.map(file -> file.getAbsolutePath()).collect(Collectors.toSet());
}
}
private void invalidateCuForJavaFile(String uriStr) {
URI uri = URI.create(uriStr);
uriToCu.invalidate(uri);
@@ -304,17 +270,4 @@ public class RewriteCompilationUnitCache implements DocumentContentProvider, Dis
return requestor.apply(null);
}
public List<CompilationUnit> getCompiulationUnits(IJavaProject project) {
List<Path> javaFiles = IClasspathUtil.getProjectJavaSourceFolders(project.getClasspath()).flatMap(folder -> {
try {
return Files.walk(folder.toPath());
} catch (IOException e) {
logger.error("", e);
}
return Stream.empty();
}).filter(Files::isRegularFile).filter(p -> p.getFileName().toString().endsWith(".java")).collect(Collectors.toList());
JavaParser javaParser = loadJavaParser(project);
return ORAstUtils.parse(javaParser, javaFiles);
}
}

View File

@@ -136,7 +136,7 @@ public class RewriteReconciler implements JavaReconciler {
try {
List<RecipeSpringJavaProblemDescriptor> descriptors = getProblemRecipeDescriptors(project);
JavaParser javaParser = RewriteCompilationUnitCache.createJavaParser(project);
JavaParser javaParser = ORAstUtils.createJavaParser(project);
List<CompilationUnit> cus = ORAstUtils.parseInputs(javaParser, docs.stream().map(d -> new Parser.Input(Path.of(d.getUri()), () -> {
return new ByteArrayInputStream(d.get().getBytes());
})).collect(Collectors.toList()));

View File

@@ -139,7 +139,7 @@ public class RewriteRefactorings implements CodeActionResolver, QuickfixHandler
boolean projectWide = data.recipeScope == RecipeScope.PROJECT;
Recipe r = createRecipe(data);
if (projectWide) {
return applyRecipe(r, project.get(), cuCache.getCompiulationUnits(project.get()));
return applyRecipe(r, project.get(), ORAstUtils.parse(documents, project.get()));
} else {
CompilationUnit cu = cuCache.getCU(project.get(), URI.create(data.docUri));
if (cu == null) {
@@ -150,6 +150,7 @@ public class RewriteRefactorings implements CodeActionResolver, QuickfixHandler
}
return null;
}
private Recipe createRecipe(Data d) {
Recipe r = recipeRepo.getRecipe(d.id).orElse(null);