use of org.eclipse.jdt.internal.core.JavaProject in project che by eclipse.
the class CheCompilationUnitResolver method resolve.
public static CompilationUnitDeclaration resolve(org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit, IJavaProject javaProject, List classpaths, NodeSearcher nodeSearcher, Map options, WorkingCopyOwner owner, int flags, IProgressMonitor monitor) throws JavaModelException {
CompilationUnitDeclaration unit = null;
INameEnvironmentWithProgress environment = null;
CancelableProblemFactory problemFactory = null;
CheCompilationUnitResolver resolver = null;
try {
if (javaProject == null) {
FileSystem.Classpath[] allEntries = new FileSystem.Classpath[classpaths.size()];
classpaths.toArray(allEntries);
environment = new NameEnvironmentWithProgress(allEntries, null, monitor);
} else {
environment = new CancelableNameEnvironment((JavaProject) javaProject, owner, monitor);
}
problemFactory = new CancelableProblemFactory(monitor);
CompilerOptions compilerOptions = CompilationUnitResolver.getCompilerOptions(options, (flags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0);
boolean ignoreMethodBodies = (flags & ICompilationUnit.IGNORE_METHOD_BODIES) != 0;
compilerOptions.ignoreMethodBodies = ignoreMethodBodies;
resolver = new CheCompilationUnitResolver(environment, CompilationUnitResolver.getHandlingPolicy(), compilerOptions, CompilationUnitResolver.getRequestor(), problemFactory, monitor, javaProject != null);
boolean analyzeAndGenerateCode = !ignoreMethodBodies;
unit = resolver.resolve(// no existing compilation unit declaration
null, sourceUnit, nodeSearcher, // method verification
true, // analyze code
analyzeAndGenerateCode, // generate code
analyzeAndGenerateCode);
if (resolver.hasCompilationAborted) {
// the bindings could not be resolved due to missing types in name environment
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=86541
CompilationUnitDeclaration unitDeclaration = CompilationUnitResolver.parse(sourceUnit, nodeSearcher, options, flags);
if (unit != null) {
final int problemCount = unit.compilationResult.problemCount;
if (problemCount != 0) {
unitDeclaration.compilationResult.problems = new CategorizedProblem[problemCount];
System.arraycopy(unit.compilationResult.problems, 0, unitDeclaration.compilationResult.problems, 0, problemCount);
unitDeclaration.compilationResult.problemCount = problemCount;
}
} else if (resolver.abortProblem != null) {
unitDeclaration.compilationResult.problemCount = 1;
unitDeclaration.compilationResult.problems = new CategorizedProblem[] { resolver.abortProblem };
}
return unitDeclaration;
}
if (NameLookup.VERBOSE && environment instanceof CancelableNameEnvironment) {
CancelableNameEnvironment cancelableNameEnvironment = (CancelableNameEnvironment) environment;
//$NON-NLS-1$ //$NON-NLS-2$
System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " + cancelableNameEnvironment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms");
//$NON-NLS-1$ //$NON-NLS-2$
System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " + cancelableNameEnvironment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms");
}
return unit;
} finally {
if (environment != null) {
// don't hold a reference to this external object
environment.setMonitor(null);
}
if (problemFactory != null) {
// don't hold a reference to this external object
problemFactory.monitor = null;
}
}
}
use of org.eclipse.jdt.internal.core.JavaProject in project che by eclipse.
the class WorkspaceTest method testClasspathMultimoduleProject.
@Test
public void testClasspathMultimoduleProject() throws Exception {
String pom = "<groupId>test</groupId>" + "<artifactId>testArtifact</artifactId>" + "<version>42</version>" + "<modules>" + " <module>module1</module>" + " <module>module2</module>" + "</modules>";
createTestProject("parent", pom);
String pomModule1 = "<groupId>test</groupId>" + "<artifactId>testModule1</artifactId>" + "<version>1</version>" + "<dependencies>" + " <dependency>" + " <groupId>junit</groupId>" + " <artifactId>junit</artifactId>" + " <version>4.12</version>" + " </dependency>" + "</dependencies>";
createTestProjectWithPackages("parent/module1", pomModule1, "org.eclipse.multi.module");
String pomModule2 = "<groupId>test</groupId>" + "<artifactId>testModule2</artifactId>" + "<version>2</version>" + "<dependencies>" + " <dependency>" + " <groupId>test</groupId>" + " <artifactId>testModule1</artifactId>" + " <version>1</version>" + " </dependency>" + "</dependencies>";
createTestProject("parent/module2", pomModule2);
IProject parent = ResourcesPlugin.getWorkspace().getRoot().getProject("parent");
mavenWorkspace.update(Collections.singletonList(parent));
mavenWorkspace.waitForUpdate();
IProject module2 = ResourcesPlugin.getWorkspace().getRoot().getProject("parent/module2");
JavaProject javaProject = (JavaProject) JavaCore.create(module2);
IJavaElement packageFragment = javaProject.findPackageFragment("org.eclipse.multi.module");
assertThat(packageFragment).isNotNull();
}
use of org.eclipse.jdt.internal.core.JavaProject in project che by eclipse.
the class WorkspaceTest method testSingleProjectClasspath.
@Test
public void testSingleProjectClasspath() throws Exception {
String pom = "<groupId>test</groupId>" + "<artifactId>testArtifact</artifactId>" + "<version>42</version>" + "<dependencies>" + " <dependency>" + " <groupId>junit</groupId>" + " <artifactId>junit</artifactId>" + " <version>4.12</version>" + " </dependency>" + "</dependencies>";
createTestProject("test", pom);
IProject test = ResourcesPlugin.getWorkspace().getRoot().getProject("test");
mavenWorkspace.update(Collections.singletonList(test));
mavenWorkspace.waitForUpdate();
JavaProject javaProject = (JavaProject) JavaCore.create(test);
IClasspathEntry[] classpath = javaProject.getResolvedClasspath();
assertThat(classpath).onProperty("path").is(new Condition<Object[]>() {
@Override
public boolean matches(Object[] value) {
return Stream.of(value).filter(o -> {
if (o instanceof IPath) {
return ((IPath) o).lastSegment().endsWith("junit-4.12.jar");
}
return false;
}).findFirst().isPresent();
}
});
}
use of org.eclipse.jdt.internal.core.JavaProject in project che by eclipse.
the class WorkspaceTest method testShouldContainsCustomTestSourceDirectory.
@Test
public void testShouldContainsCustomTestSourceDirectory() throws Exception {
String pom = "<groupId>test</groupId>" + "<artifactId>testArtifact</artifactId>" + "<version>42</version>" + "<dependencies>" + " <dependency>" + " <groupId>junit</groupId>" + " <artifactId>junit</artifactId>" + " <version>4.12</version>" + " </dependency>" + "</dependencies>" + "<build>" + "<testSourceDirectory>/mytest</testSourceDirectory>" + "</build>";
createTestProject("test", pom);
IProject test = ResourcesPlugin.getWorkspace().getRoot().getProject("test");
mavenWorkspace.update(Collections.singletonList(test));
mavenWorkspace.waitForUpdate();
JavaProject javaProject = (JavaProject) JavaCore.create(test);
IClasspathEntry[] classpath = javaProject.getResolvedClasspath();
assertThat(classpath).onProperty("path").is(new Condition<Object[]>() {
@Override
public boolean matches(Object[] value) {
return Stream.of(value).filter(o -> {
if (o instanceof IPath) {
return ((IPath) o).toOSString().endsWith("test");
}
return false;
}).findFirst().isPresent();
}
});
}
use of org.eclipse.jdt.internal.core.JavaProject in project che by eclipse.
the class IndexAllProject method execute.
/**
* Ensure consistency of a project index. Need to walk all nested resources,
* and discover resources which have either been changed, added or deleted
* since the index was produced.
*/
public boolean execute(IProgressMonitor progressMonitor) {
if (this.isCancelled || progressMonitor != null && progressMonitor.isCanceled())
return true;
// nothing to do
if (!this.project.isAccessible())
return true;
ReadWriteMonitor monitor = null;
try {
// Get source folder entries. Libraries are done as a separate job
JavaProject javaProject = (JavaProject) JavaCore.create(this.project);
// Do not create marker while getting raw classpath (see bug 41859)
IClasspathEntry[] entries = javaProject.getRawClasspath();
int length = entries.length;
IClasspathEntry[] sourceEntries = new IClasspathEntry[length];
int sourceEntriesNumber = 0;
for (int i = 0; i < length; i++) {
IClasspathEntry entry = entries[i];
if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE)
sourceEntries[sourceEntriesNumber++] = entry;
}
if (sourceEntriesNumber == 0) {
IPath projectPath = javaProject.getPath();
for (int i = 0; i < length; i++) {
IClasspathEntry entry = entries[i];
if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY && entry.getPath().equals(projectPath)) {
// the project is also a library folder (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=89815)
// ensure a job exists to index it as a binary folder
this.manager.indexLibrary(projectPath, this.project, ((ClasspathEntry) entry).getLibraryIndexLocation());
return true;
}
}
// nothing to index but want to save an empty index file so its not 'rebuilt' when part of a search request
Index index = this.manager.getIndexForUpdate(this.containerPath, true, /*reuse index file*/
true);
if (index != null)
this.manager.saveIndex(index);
return true;
}
if (sourceEntriesNumber != length)
System.arraycopy(sourceEntries, 0, sourceEntries = new IClasspathEntry[sourceEntriesNumber], 0, sourceEntriesNumber);
Index index = this.manager.getIndexForUpdate(this.containerPath, true, /*reuse index file*/
true);
if (index == null)
return true;
monitor = index.monitor;
// index got deleted since acquired
if (monitor == null)
return true;
// ask permission to read
monitor.enterRead();
// all file names //$NON-NLS-1$
String[] paths = index.queryDocumentNames("");
int max = paths == null ? 0 : paths.length;
final SimpleLookupTable indexedFileNames = new SimpleLookupTable(max == 0 ? 33 : max + 11);
//$NON-NLS-1$
final String OK = "OK";
//$NON-NLS-1$
final String DELETED = "DELETED";
if (paths != null) {
for (int i = 0; i < max; i++) indexedFileNames.put(paths[i], DELETED);
}
final long indexLastModified = max == 0 ? 0L : index.getIndexLastModified();
IWorkspaceRoot root = this.project.getWorkspace().getRoot();
for (int i = 0; i < sourceEntriesNumber; i++) {
if (this.isCancelled)
return false;
IClasspathEntry entry = sourceEntries[i];
IResource sourceFolder = root.findMember(entry.getPath());
if (sourceFolder != null) {
// collect output locations if source is project (see http://bugs.eclipse.org/bugs/show_bug.cgi?id=32041)
final HashSet outputs = new HashSet();
if (sourceFolder.getType() == IResource.PROJECT) {
// Do not create marker while getting output location (see bug 41859)
outputs.add(javaProject.getOutputLocation());
for (int j = 0; j < sourceEntriesNumber; j++) {
IPath output = sourceEntries[j].getOutputLocation();
if (output != null) {
outputs.add(output);
}
}
}
final boolean hasOutputs = !outputs.isEmpty();
final char[][] inclusionPatterns = ((ClasspathEntry) entry).fullInclusionPatternChars();
final char[][] exclusionPatterns = ((ClasspathEntry) entry).fullExclusionPatternChars();
if (max == 0) {
sourceFolder.accept(new IResourceProxyVisitor() {
public boolean visit(IResourceProxy proxy) {
if (IndexAllProject.this.isCancelled)
return false;
switch(proxy.getType()) {
case IResource.FILE:
if (org.eclipse.jdt.internal.core.util.Util.isJavaLikeFileName(proxy.getName())) {
IFile file = (IFile) proxy.requestResource();
if (exclusionPatterns != null || inclusionPatterns != null)
if (Util.isExcluded(file, inclusionPatterns, exclusionPatterns))
return false;
indexedFileNames.put(Util.relativePath(file.getFullPath(), project.getFullPath().segmentCount()), file);
}
return false;
case IResource.FOLDER:
if (exclusionPatterns != null && inclusionPatterns == null) {
// if there are inclusion patterns then we must walk the children
if (Util.isExcluded(proxy.requestFullPath(), inclusionPatterns, exclusionPatterns, true))
return false;
}
if (hasOutputs && outputs.contains(proxy.requestFullPath()))
return false;
}
return true;
}
}, IResource.NONE);
} else {
sourceFolder.accept(new IResourceProxyVisitor() {
public boolean visit(IResourceProxy proxy) throws CoreException {
if (IndexAllProject.this.isCancelled)
return false;
switch(proxy.getType()) {
case IResource.FILE:
if (org.eclipse.jdt.internal.core.util.Util.isJavaLikeFileName(proxy.getName())) {
IFile file = (IFile) proxy.requestResource();
URI location = file.getLocationURI();
if (location == null)
return false;
if (exclusionPatterns != null || inclusionPatterns != null)
if (Util.isExcluded(file, inclusionPatterns, exclusionPatterns))
return false;
String relativePathString = Util.relativePath(file.getFullPath(), project.getFullPath().segmentCount());
indexedFileNames.put(relativePathString, indexedFileNames.get(relativePathString) == null || indexLastModified < EFS.getStore(location).fetchInfo().getLastModified() ? (Object) file : (Object) OK);
}
return false;
case IResource.FOLDER:
if (exclusionPatterns != null || inclusionPatterns != null)
if (Util.isExcluded(proxy.requestResource(), inclusionPatterns, exclusionPatterns))
return false;
if (hasOutputs && outputs.contains(proxy.requestFullPath()))
return false;
}
return true;
}
}, IResource.NONE);
}
}
}
SourceElementParser parser = this.manager.getSourceElementParser(javaProject, null);
Object[] names = indexedFileNames.keyTable;
Object[] values = indexedFileNames.valueTable;
for (int i = 0, namesLength = names.length; i < namesLength; i++) {
String name = (String) names[i];
if (name != null) {
if (this.isCancelled)
return false;
Object value = values[i];
if (value != OK) {
if (value == DELETED)
this.manager.remove(name, this.containerPath);
else
this.manager.addSource((IFile) value, this.containerPath, parser);
}
}
}
// request to save index when all cus have been indexed... also sets state to SAVED_STATE
this.manager.request(new SaveIndex(this.containerPath, this.manager));
} catch (CoreException e) {
if (JobManager.VERBOSE) {
Util.verbose("-> failed to index " + this.project + " because of the following exception:", //$NON-NLS-1$ //$NON-NLS-2$
System.err);
e.printStackTrace();
}
this.manager.removeIndex(this.containerPath);
return false;
} catch (IOException e) {
if (JobManager.VERBOSE) {
Util.verbose("-> failed to index " + this.project + " because of the following exception:", //$NON-NLS-1$ //$NON-NLS-2$
System.err);
e.printStackTrace();
}
this.manager.removeIndex(this.containerPath);
return false;
} finally {
if (monitor != null)
// free read lock
monitor.exitRead();
}
return true;
}
Aggregations