use of org.springframework.ide.vscode.commons.java.IJavaProject in project sts4 by spring-projects.
the class MavenProjectCacheTest method testPomFileChanges.
@Test
public void testPomFileChanges() throws Exception {
MavenProjectCache cache = new MavenProjectCache(server, MavenCore.getDefault(), false, null);
IJavaProject[] projectChanged = new IJavaProject[] { null };
IJavaProject[] projectDeleted = new IJavaProject[] { null };
cache.addListener(new Listener() {
@Override
public void created(IJavaProject project) {
}
@Override
public void changed(IJavaProject project) {
projectChanged[0] = project;
}
@Override
public void deleted(IJavaProject project) {
projectDeleted[0] = project;
}
});
// Get the project from cache
MavenJavaProject cachedProject = cache.project(pomFile);
assertNotNull(cachedProject);
ImmutableList<Path> calculatedClassPath = cachedProject.getClasspath().getClasspathEntries();
assertEquals(48, calculatedClassPath.size());
fileObserver.notifyFileChanged(pomFile.toURI().toString());
assertNull(projectChanged[0]);
writeContent(pomFile, new String(Files.readAllBytes(testProjectPath.resolve("pom.newxml")), Charset.defaultCharset()));
fileObserver.notifyFileChanged(pomFile.toURI().toString());
assertNotNull(projectChanged[0]);
assertEquals(cachedProject, projectChanged[0]);
calculatedClassPath = cachedProject.getClasspath().getClasspathEntries();
assertEquals(49, calculatedClassPath.size());
fileObserver.notifyFileDeleted(pomFile.toURI().toString());
assertEquals(cachedProject, projectDeleted[0]);
}
use of org.springframework.ide.vscode.commons.java.IJavaProject in project sts4 by spring-projects.
the class SpringIndexer method getClasspathEntries.
private String[] getClasspathEntries(IJavaProject project) throws Exception {
IClasspath classpath = project.getClasspath();
Stream<Path> classpathEntries = classpath.getClasspathEntries().stream();
return classpathEntries.filter(path -> path.toFile().exists()).map(path -> path.toAbsolutePath().toString()).toArray(String[]::new);
}
use of org.springframework.ide.vscode.commons.java.IJavaProject in project sts4 by spring-projects.
the class SpringIndexer method createDocument.
public CompletableFuture<Void> createDocument(String docURI) {
synchronized (this) {
if (docURI.endsWith(".java") && lastInitializeItem != null) {
try {
Optional<IJavaProject> maybeProject = projectFinder.find(new TextDocumentIdentifier(docURI));
if (maybeProject.isPresent()) {
String[] classpathEntries = getClasspathEntries(maybeProject.get());
String content = FileUtils.readFileToString(new File(new URI(docURI)));
UpdateItem updateItem = new UpdateItem(docURI, content, classpathEntries);
updateQueue.put(updateItem);
return updateItem.getFuture();
}
} catch (Exception e) {
log.error("", e);
return Futures.error(e);
}
}
}
return CompletableFuture.completedFuture(null);
}
use of org.springframework.ide.vscode.commons.java.IJavaProject in project sts4 by spring-projects.
the class CompilationUnitCache method getClasspathEntries.
private static String[] getClasspathEntries(TextDocument document, IJavaProject project) throws Exception {
if (project == null) {
return new String[0];
} else {
IClasspath classpath = project.getClasspath();
Stream<Path> classpathEntries = classpath.getClasspathEntries().stream();
return classpathEntries.filter(path -> path.toFile().exists()).map(path -> path.toAbsolutePath().toString()).toArray(String[]::new);
}
}
use of org.springframework.ide.vscode.commons.java.IJavaProject in project sts4 by spring-projects.
the class CompilationUnitCache method withCompilationUnit.
/**
* Retrieves a CompiationUnitn AST from the cache and passes it to a requestor callback, applying
* proper thread synchronization around the requestor.
* <p>
* Warning: Callers should take care to do all AST processing inside of the requestor callback and
* not pass of AST nodes to helper functions that work aynchronously or store AST nodes or ITypeBindings
* for later use. The JDT ASTs are not thread safe!
*/
public <T> T withCompilationUnit(TextDocument document, Function<CompilationUnit, T> requestor) {
URI uri = URI.create(document.getUri());
IJavaProject project = projectFinder.find(document.getId()).orElse(null);
if (project != null) {
readLock.lock();
CompilationUnit cu = null;
try {
cu = uriToCu.get(uri, () -> {
CompilationUnit cUnit = parse(document, project);
projectToDocs.get(project, () -> new HashSet<>()).add(URI.create(document.getUri()));
return cUnit;
});
if (cu != null) {
projectToDocs.get(project, () -> new HashSet<>()).add(URI.create(document.getUri()));
}
} catch (Exception e) {
Log.log(e);
} finally {
readLock.unlock();
}
if (cu != null) {
try {
synchronized (cu.getAST()) {
return requestor.apply(cu);
}
} catch (Exception e) {
Log.log(e);
}
}
}
return requestor.apply(null);
}
Aggregations