use of org.eclipse.jdt.internal.core.JavaProject in project che by eclipse.
the class IndexManager method indexAll.
/**
* Trigger addition of the entire content of a project
* Note: the actual operation is performed in background
*/
public void indexAll(IProject project) {
// determine the new children
try {
JavaModel model = JavaModelManager.getJavaModelManager().getJavaModel();
JavaProject javaProject = (JavaProject) model.getJavaProject(project);
// only consider immediate libraries - each project will do the same
// NOTE: force to resolve CP variables before calling indexer - 19303, so that initializers
// will be run in the current thread.
IClasspathEntry[] entries = javaProject.getResolvedClasspath();
for (int i = 0; i < entries.length; i++) {
IClasspathEntry entry = entries[i];
if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY)
indexLibrary(entry.getPath(), project, ((ClasspathEntry) entry).getLibraryIndexLocation());
}
} catch (JavaModelException e) {
// cannot retrieve classpath info
}
// check if the same request is not already in the queue
IndexRequest request = new IndexAllProject(project, this);
if (!isJobWaiting(request))
request(request);
}
use of org.eclipse.jdt.internal.core.JavaProject in project che by eclipse.
the class JavaSearchScope method add.
/**
* Add a path to current java search scope or all project fragment roots if null.
* Use project resolved classpath to retrieve and store access restriction on each classpath entry.
* Recurse if dependent projects are found.
* @param javaProject Project used to get resolved classpath entries
* @param pathToAdd Path to add in case of single element or null if user want to add all project package fragment roots
* @param includeMask Mask to apply on classpath entries
* @param projectsToBeAdded Set to avoid infinite recursion
* @param visitedProjects Set to avoid adding twice the same project
* @param referringEntry Project raw entry in referring project classpath
* @throws JavaModelException May happen while getting java model info
*/
void add(JavaProject javaProject, IPath pathToAdd, int includeMask, HashSet projectsToBeAdded, HashSet visitedProjects, IClasspathEntry referringEntry) throws JavaModelException {
IProject project = javaProject.getProject();
if (!project.isAccessible() || !visitedProjects.add(project))
return;
IPath projectPath = project.getFullPath();
String projectPathString = projectPath.toString();
addEnclosingProjectOrJar(projectPath);
IClasspathEntry[] entries = javaProject.getResolvedClasspath();
IJavaModel model = javaProject.getJavaModel();
JavaModelManager.PerProjectInfo perProjectInfo = javaProject.getPerProjectInfo();
for (int i = 0, length = entries.length; i < length; i++) {
IClasspathEntry entry = entries[i];
AccessRuleSet access = null;
ClasspathEntry cpEntry = (ClasspathEntry) entry;
if (referringEntry != null) {
// Source folder are implicitly exported.
if (!entry.isExported() && entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
continue;
}
cpEntry = cpEntry.combineWith((ClasspathEntry) referringEntry);
// cpEntry = ((ClasspathEntry)referringEntry).combineWith(cpEntry);
}
access = cpEntry.getAccessRuleSet();
switch(entry.getEntryKind()) {
case IClasspathEntry.CPE_LIBRARY:
IClasspathEntry rawEntry = null;
Map rootPathToRawEntries = perProjectInfo.rootPathToRawEntries;
if (rootPathToRawEntries != null) {
rawEntry = (IClasspathEntry) rootPathToRawEntries.get(entry.getPath());
}
if (rawEntry == null)
break;
rawKind: switch(rawEntry.getEntryKind()) {
case IClasspathEntry.CPE_LIBRARY:
case IClasspathEntry.CPE_VARIABLE:
if ((includeMask & APPLICATION_LIBRARIES) != 0) {
IPath path = entry.getPath();
if (pathToAdd == null || pathToAdd.equals(path)) {
Object target = JavaModel.getTarget(path, false);
if (// case of an external folder
target instanceof IFolder)
path = ((IFolder) target).getFullPath();
String pathToString = path.getDevice() == null ? path.toString() : path.toOSString();
//$NON-NLS-1$
add(projectPath.toString(), "", pathToString, false, /*not a package*/
access);
addEnclosingProjectOrJar(entry.getPath());
}
}
break;
case IClasspathEntry.CPE_CONTAINER:
IClasspathContainer container = JavaCore.getClasspathContainer(rawEntry.getPath(), javaProject);
if (container == null)
break;
switch(container.getKind()) {
case IClasspathContainer.K_APPLICATION:
if ((includeMask & APPLICATION_LIBRARIES) == 0)
break rawKind;
break;
case IClasspathContainer.K_SYSTEM:
case IClasspathContainer.K_DEFAULT_SYSTEM:
if ((includeMask & SYSTEM_LIBRARIES) == 0)
break rawKind;
break;
default:
break rawKind;
}
IPath path = entry.getPath();
if (pathToAdd == null || pathToAdd.equals(path)) {
Object target = JavaModel.getTarget(path, false);
if (// case of an external folder
target instanceof IFolder)
path = ((IFolder) target).getFullPath();
String pathToString = path.getDevice() == null ? path.toString() : path.toOSString();
//$NON-NLS-1$
add(projectPath.toString(), "", pathToString, false, /*not a package*/
access);
addEnclosingProjectOrJar(entry.getPath());
}
break;
}
break;
case IClasspathEntry.CPE_PROJECT:
if ((includeMask & REFERENCED_PROJECTS) != 0) {
IPath path = entry.getPath();
if (pathToAdd == null || pathToAdd.equals(path)) {
JavaProject referencedProject = (JavaProject) model.getJavaProject(path.toOSString());
if (!projectsToBeAdded.contains(referencedProject)) {
// do not recurse if depending project was used to create the scope
add(referencedProject, null, includeMask, projectsToBeAdded, visitedProjects, cpEntry);
}
}
}
break;
case IClasspathEntry.CPE_SOURCE:
if ((includeMask & SOURCES) != 0) {
IPath path = entry.getPath();
if (pathToAdd == null || pathToAdd.equals(path)) {
add(projectPath.toString(), Util.relativePath(path, projectPath.segmentCount()), projectPathString, false, /*not a package*/
access);
}
}
break;
}
}
}
use of org.eclipse.jdt.internal.core.JavaProject in project che by eclipse.
the class FindReferencesTest method testFind2.
@Test
public void testFind2() throws Exception {
IPackageFragmentRoot root = ((JavaProject) JUnitSourceSetup.getProject()).getPackageFragmentRoot(new Path(JUnitSourceSetup.SRC_CONTAINER));
IPackageFragment packageFragment = root.createPackageFragment("che", true, null);
StringBuilder a = new StringBuilder();
a.append("package che;\n");
a.append("public class A{}\n");
packageFragment.createCompilationUnit("A.java", a.toString(), true, null);
StringBuilder b = new StringBuilder();
b.append("package che;\n");
b.append("import java.util.Comparator;\n");
b.append("public class B extends A implements Comparator<A>{\n");
b.append(" @Override\n");
b.append(" public int compare(A o1, A o2) {\n");
b.append(" A bb = null;\n");
b.append(" return 0;\n");
b.append(" }\n");
b.append("}\n");
packageFragment.createCompilationUnit("B.java", b.toString(), true, null);
JavaSearchQuery query = SearchTestHelper.runTypeRefQuery("che.A");
ISearchResult result = query.getSearchResult();
JavaSearchResult javaResult = ((JavaSearchResult) result);
Assertions.assertThat(javaResult.getElements()).hasSize(2);
}
use of org.eclipse.jdt.internal.core.JavaProject in project che by eclipse.
the class FindReferencesTest method testSearchCantFindElement.
@Test(expected = SearchException.class)
public void testSearchCantFindElement() throws Exception {
IPackageFragmentRoot root = ((JavaProject) JUnitSourceSetup.getProject()).getPackageFragmentRoot(new Path(JUnitSourceSetup.SRC_CONTAINER));
IPackageFragment packageFragment = root.createPackageFragment("che", true, null);
StringBuilder a = new StringBuilder();
a.append("package che;\n");
a.append("public class A{}\n");
ICompilationUnit compilationUnitA = packageFragment.createCompilationUnit("A.java", a.toString(), true, null);
SearchManager manager = new SearchManager();
manager.findUsage(JUnitSourceSetup.getProject(), compilationUnitA.getResource().getFullPath().toOSString(), 24);
}
use of org.eclipse.jdt.internal.core.JavaProject in project che by eclipse.
the class FindReferencesTest method testSearchManagerFindUsage.
@Test
public void testSearchManagerFindUsage() throws Exception {
IJavaProject aProject = JUnitSourceSetup.getProject();
IPackageFragmentRoot root = ((JavaProject) aProject).getPackageFragmentRoot(new Path(JUnitSourceSetup.SRC_CONTAINER));
IPackageFragment packageFragment = root.createPackageFragment("che", true, null);
StringBuilder a = new StringBuilder();
a.append("package che;\n");
a.append("public class A{}\n");
ICompilationUnit compilationUnitA = packageFragment.createCompilationUnit("A.java", a.toString(), true, null);
StringBuilder b = new StringBuilder();
b.append("package che;\n");
b.append("import java.util.Comparator;\n");
b.append("import che.A;\n");
b.append("public class B extends A implements Comparator<A>{\n");
b.append(" private A a = null;\n");
b.append(" static{\n");
b.append(" A ddd = null;\n");
b.append(" }\n");
b.append(" @Override\n");
b.append(" public int compare(A o1, A o2) {\n");
b.append(" A bb = null;\n");
b.append(" return 0;\n");
b.append(" }\n");
b.append(" class SubB{\n");
b.append(" public A ccc = null;\n");
b.append(" }\n");
b.append("}\n");
b.append("class SubB2{\n");
b.append(" private final A foo = null;\n");
b.append("}\n");
packageFragment.createCompilationUnit("B.java", b.toString(), true, null);
SearchManager manager = new SearchManager();
FindUsagesResponse response = manager.findUsage(aProject, "che.A", 26);
Assertions.assertThat(response.getSearchElementLabel()).isEqualTo("A");
List<org.eclipse.che.ide.ext.java.shared.dto.model.JavaProject> projects = response.getProjects();
Assertions.assertThat(projects).isNotNull().isNotEmpty().hasSize(1);
String expectedProjectPath = JUnitSourceSetup.getProject().getPath().toOSString();
org.eclipse.che.ide.ext.java.shared.dto.model.JavaProject project = projects.get(0);
Assertions.assertThat(project.getName()).isEqualTo(JUnitSourceSetup.PROJECT_NAME);
Assertions.assertThat(project.getPath()).isEqualTo(expectedProjectPath);
Assertions.assertThat(project.getPackageFragmentRoots()).isNotNull().isNotEmpty().hasSize(1);
PackageFragmentRoot fragmentRoot = project.getPackageFragmentRoots().get(0);
Assertions.assertThat(fragmentRoot.getElementName()).isEqualTo(JUnitSourceSetup.SRC_CONTAINER);
Assertions.assertThat(fragmentRoot.getProjectPath()).isEqualTo(expectedProjectPath);
Assertions.assertThat(fragmentRoot.getPackageFragments()).isNotNull().isNotEmpty().hasSize(1);
PackageFragment fragment = fragmentRoot.getPackageFragments().get(0);
Assertions.assertThat(fragment.getElementName()).isEqualTo("che");
Assertions.assertThat(fragment.getProjectPath()).isEqualTo(expectedProjectPath);
Assertions.assertThat(fragment.getPath()).isEqualTo(expectedProjectPath + "/" + JUnitSourceSetup.SRC_CONTAINER + "/che");
Assertions.assertThat(fragment.getClassFiles()).isNotNull().isEmpty();
Assertions.assertThat(fragment.getCompilationUnits()).isNotNull().isNotEmpty().hasSize(1);
CompilationUnit compilationUnit = fragment.getCompilationUnits().get(0);
Assertions.assertThat(compilationUnit.getElementName()).isEqualTo("B.java");
Assertions.assertThat(compilationUnit.getPath()).isEqualTo(expectedProjectPath + "/" + JUnitSourceSetup.SRC_CONTAINER + "/che/B.java");
Assertions.assertThat(compilationUnit.getImports()).hasSize(1);
Assertions.assertThat(compilationUnit.getTypes()).hasSize(2);
}
Aggregations