org.eclipse.jdt.internal.core.JavaModel Java Examples

The following examples show how to use org.eclipse.jdt.internal.core.JavaModel. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SearchParticipant.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Schedules the indexing of the given document.
 * Once the document is ready to be indexed,
 * {@link #indexDocument(SearchDocument, IPath) indexDocument(document, indexPath)}
 * will be called in a different thread than the caller's thread.
 * <p>
 * The given index location must represent a path in the file system to a file that
 * either already exists or is going to be created. If it exists, it must be an index file,
 * otherwise its data might be overwritten.
 * </p><p>
 * When the index is no longer needed, clients should use {@link #removeIndex(IPath) }
 * to discard it.
 * </p>
 *
 * @param document the document to index
 * @param indexPath the location on the file system of the index
 */
public final void scheduleDocumentIndexing(SearchDocument document, IPath indexPath) {
	IPath documentPath = new Path(document.getPath());
	Object file = JavaModel.getTarget(documentPath, true);
	IPath containerPath = documentPath;
	if (file instanceof IResource) {
		containerPath = ((IResource)file).getProject().getFullPath();
	} else if (file == null) {
		containerPath = documentPath.removeLastSegments(1);
	}
	IndexManager manager = JavaModelManager.getIndexManager();
	// TODO (frederic) should not have to create index manually, should expose API that recreates index instead
	IndexLocation indexLocation;
	indexLocation = new FileIndexLocation(indexPath.toFile(), true);
	manager.ensureIndexExists(indexLocation, containerPath);
	manager.scheduleDocumentIndexing(document, containerPath, indexLocation, this);
	if (!indexPath.equals(this.lastIndexLocation)) {
		manager.updateParticipant(indexPath, containerPath);
		this.lastIndexLocation = indexPath;
	}
}
 
Example #2
Source File: SourceIndexer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void resolveDocument() {
	try {
		IPath path = new Path(this.document.getPath());
		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(path.segment(0));
		JavaModel model = JavaModelManager.getJavaModelManager().getJavaModel();
		JavaProject javaProject = (JavaProject) model.getJavaProject(project);

		this.options = new CompilerOptions(javaProject.getOptions(true));
		ProblemReporter problemReporter =
				new ProblemReporter(
						DefaultErrorHandlingPolicies.proceedWithAllProblems(),
						this.options,
						new DefaultProblemFactory());

		// Re-parse using normal parser, IndexingParser swallows several nodes, see comment above class.
		this.basicParser = new Parser(problemReporter, false);
		this.basicParser.reportOnlyOneSyntaxError = true;
		this.basicParser.scanner.taskTags = null;
		this.cud = this.basicParser.parse(this.compilationUnit, new CompilationResult(this.compilationUnit, 0, 0, this.options.maxProblemsPerUnit));

		// Use a non model name environment to avoid locks, monitors and such.
		INameEnvironment nameEnvironment = new JavaSearchNameEnvironment(javaProject, JavaModelManager.getJavaModelManager().getWorkingCopies(DefaultWorkingCopyOwner.PRIMARY, true/*add primary WCs*/));
		this.lookupEnvironment = new LookupEnvironment(this, this.options, problemReporter, nameEnvironment);
		reduceParseTree(this.cud);
		this.lookupEnvironment.buildTypeBindings(this.cud, null);
		this.lookupEnvironment.completeTypeBindings();
		this.cud.scope.faultInTypes();
		this.cud.resolve();
	} catch (Exception e) {
		if (JobManager.VERBOSE) {
			e.printStackTrace();
		}
	}
}
 
Example #3
Source File: JavaSearchScope.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @see AbstractJavaSearchScope#packageFragmentRoot(String, int, String)
 */
public IPackageFragmentRoot packageFragmentRoot(String resourcePathString, int jarSeparatorIndex, String jarPath) {
	int index = -1;
	boolean isJarFile = jarSeparatorIndex != -1;
	if (isJarFile) {
		// internal or external jar (case 3, 4, or 5)
		String relativePath = resourcePathString.substring(jarSeparatorIndex+1);
		index = indexOf(jarPath, relativePath);
	} else {
		// resource in workspace (case 1 or 2)
		index = indexOf(resourcePathString);
	}
	if (index >= 0) {
		int idx = this.projectIndexes[index];
		String projectPath = idx == -1 ? null : (String) this.projectPaths.get(idx);
		if (projectPath != null) {
			IJavaProject project =JavaCore.create(ResourcesPlugin.getWorkspace().getRoot().getProject(projectPath));
			if (isJarFile) {
				IResource resource = JavaModel.getWorkspaceTarget(new Path(jarPath));
				if (resource != null)
					return project.getPackageFragmentRoot(resource);
				return project.getPackageFragmentRoot(jarPath);
			}
			Object target = JavaModel.getWorkspaceTarget(new Path(this.containerPaths[index]+'/'+this.relativePaths[index]));
			if (target != null) {
				if (target instanceof IProject) {
					return project.getPackageFragmentRoot((IProject) target);
				}
				IJavaElement element = JavaModelManager.create((IResource) target, project);
				return (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
			}
		}
	}
	return null;
}
 
Example #4
Source File: JdtTypeProvider.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @see JavaProject computePackageFragmentRoots(IClasspathEntry, ObjectVector, HashSet, IClasspathEntry, boolean, boolean, java.util.Map)
 */
private void collectSourcePackageFragmentRoots(JavaProject javaProject, HashSet<String> rootIDs, IClasspathEntry referringEntry, ObjectVector result) throws JavaModelException {
	if (referringEntry == null){
		rootIDs.add(javaProject.rootID());
	} else if (rootIDs.contains(javaProject.rootID())) {
		return;
	}
	IWorkspaceRoot workspaceRoot = javaProject.getProject().getWorkspace().getRoot();
	for(IClasspathEntry entry: javaProject.getResolvedClasspath()) {
		switch(entry.getEntryKind()) {
			case IClasspathEntry.CPE_PROJECT:
				if (referringEntry != null && !entry.isExported())
					return;
				
				IPath pathToProject = entry.getPath();
				IResource referencedProject = workspaceRoot.findMember(pathToProject);
				if (referencedProject != null && referencedProject.getType() == IResource.PROJECT) {
					IProject casted = (IProject) referencedProject;
					if (JavaProject.hasJavaNature(casted)) {
						rootIDs.add(javaProject.rootID());
						JavaProject referencedJavaProject = (JavaProject) JavaCore.create(casted);
						collectSourcePackageFragmentRoots(referencedJavaProject, rootIDs, entry, result);
					}
				}
				break;
			case IClasspathEntry.CPE_SOURCE:
				// inlined from org.eclipse.jdt.internal.core.JavaProject
				// .computePackageFragmentRoots(IClasspathEntry, ObjectVector, HashSet, IClasspathEntry, boolean, boolean, Map)
				IPath projectPath = javaProject.getProject().getFullPath();
				IPath entryPath = entry.getPath();
				if (projectPath.isPrefixOf(entryPath)){
					Object target = JavaModel.getTarget(entryPath, true/*check existency*/);
					if (target != null) {
						if (target instanceof IFolder || target instanceof IProject){
							IPackageFragmentRoot root = javaProject.getPackageFragmentRoot((IResource)target);
							result.add(root);
						}
					}
				}
				break;
		}
	}
}
 
Example #5
Source File: JavaSearchNameEnvironment.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void computeClasspathLocations(IWorkspaceRoot workspaceRoot, JavaProject javaProject) {

	IPackageFragmentRoot[] roots = null;
	try {
		roots = javaProject.getAllPackageFragmentRoots();
	} catch (JavaModelException e) {
		// project doesn't exist
		this.locations = new ClasspathLocation[0];
		return;
	}
	int length = roots.length;
	ClasspathLocation[] cpLocations = new ClasspathLocation[length];
	int index = 0;
	JavaModelManager manager = JavaModelManager.getJavaModelManager();
	for (int i = 0; i < length; i++) {
		PackageFragmentRoot root = (PackageFragmentRoot) roots[i];
		IPath path = root.getPath();
		try {
			if (root.isArchive()) {
				ZipFile zipFile = manager.getZipFile(path);
				cpLocations[index++] = new ClasspathJar(zipFile, ((ClasspathEntry) root.getRawClasspathEntry()).getAccessRuleSet());
			} else {
				Object target = JavaModel.getTarget(path, true);
				if (target == null) {
					// target doesn't exist any longer
					// just resize cpLocations
					System.arraycopy(cpLocations, 0, cpLocations = new ClasspathLocation[cpLocations.length-1], 0, index);
				} else if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
					cpLocations[index++] = new ClasspathSourceDirectory((IContainer)target, root.fullExclusionPatternChars(), root.fullInclusionPatternChars());
				} else {
					cpLocations[index++] = ClasspathLocation.forBinaryFolder((IContainer) target, false, ((ClasspathEntry) root.getRawClasspathEntry()).getAccessRuleSet());
				}
			}
		} catch (CoreException e1) {
			// problem opening zip file or getting root kind
			// consider root corrupt and ignore
			// just resize cpLocations
			System.arraycopy(cpLocations, 0, cpLocations = new ClasspathLocation[cpLocations.length-1], 0, index);
		}
	}
	this.locations = cpLocations;
}
 
Example #6
Source File: JavaSearchScope.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public String toString() {
	StringBuffer result = new StringBuffer("JavaSearchScope on "); //$NON-NLS-1$
	if (this.elements != null) {
		result.append("["); //$NON-NLS-1$
		for (int i = 0, length = this.elements.size(); i < length; i++) {
			JavaElement element = (JavaElement)this.elements.get(i);
			result.append("\n\t"); //$NON-NLS-1$
			result.append(element.toStringWithAncestors());
		}
		result.append("\n]"); //$NON-NLS-1$
	} else {
		if (this.pathsCount == 0) {
			result.append("[empty scope]"); //$NON-NLS-1$
		} else {
			result.append("["); //$NON-NLS-1$
			String[] paths = new String[this.relativePaths.length];
			int index = 0;
			for (int i = 0; i < this.relativePaths.length; i++) {
				String path = this.relativePaths[i];
				if (path == null) continue;
				String containerPath;
				if (ExternalFoldersManager.isInternalPathForExternalFolder(new Path(this.containerPaths[i]))) {
					Object target = JavaModel.getWorkspaceTarget(new Path(this.containerPaths[i]));
					containerPath = ((IFolder) target).getLocation().toOSString();
				} else {
					containerPath = this.containerPaths[i];
				}
				if (path.length() > 0) {
					paths[index++] = containerPath + '/' + path;
				} else {
					paths[index++] = containerPath;
				}
			}
			System.arraycopy(paths, 0, paths = new String[index], 0, index);
			Util.sort(paths);
			for (int i = 0; i < index; i++) {
				result.append("\n\t"); //$NON-NLS-1$
				result.append(paths[i]);
			}
			result.append("\n]"); //$NON-NLS-1$
		}
	}
	return result.toString();
}
 
Example #7
Source File: ProjectClasspathFactory.java    From bonita-studio with GNU General Public License v2.0 4 votes vote down vote up
protected JavaModel javaModel() {
    return JavaModelManager.getJavaModelManager().getJavaModel();
}