Java Code Examples for org.eclipse.jdt.core.IJavaElement#JAVA_PROJECT

The following examples show how to use org.eclipse.jdt.core.IJavaElement#JAVA_PROJECT . 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: FindStringsToExternalizeAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private boolean computeEnablementState(IStructuredSelection selection) throws JavaModelException {
	if (selection.isEmpty())
		return false;
	for (Iterator<?> iter= selection.iterator(); iter.hasNext();) {
		Object element= iter.next();
		if (!(element instanceof IJavaElement))
			return false;
		IJavaElement javaElement= (IJavaElement)element;
		if (! javaElement.exists() || javaElement.isReadOnly())
			return false;
		int elementType= javaElement.getElementType();
		if (elementType != IJavaElement.PACKAGE_FRAGMENT &&
			elementType != IJavaElement.PACKAGE_FRAGMENT_ROOT &&
			elementType != IJavaElement.JAVA_PROJECT)
			return false;
		if (elementType == IJavaElement.PACKAGE_FRAGMENT_ROOT){
			IPackageFragmentRoot root= (IPackageFragmentRoot)javaElement;
			if (root.isExternal() || ReorgUtils.isClassFolder(root))
				return false;
		}
	}
	return true;
}
 
Example 2
Source File: ClasspathUpdateHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private Set<String> processDelta(IJavaElementDelta delta, Set<String> uris) {
	IJavaElement element = delta.getElement();
	switch (element.getElementType()) {
	case IJavaElement.JAVA_MODEL:
		uris = processDeltaChildren(delta, uris);
		break;
	case IJavaElement.JAVA_PROJECT:
		if (isClasspathChanged(delta.getFlags())) {
			if (uris == null) {
				uris = new HashSet<String>();
			}
			IJavaProject javaProject = (IJavaProject) element;
			uris.add(ProjectUtils.getProjectRealFolder(javaProject.getProject()).toFile().toURI().toString());
		}
		break;
	default:
		break;
	}
	return uris;
}
 
Example 3
Source File: JavaSearchScope.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private IPath getPath(IJavaElement element, boolean relativeToRoot) {
	switch (element.getElementType()) {
		case IJavaElement.JAVA_MODEL:
			return Path.EMPTY;
		case IJavaElement.JAVA_PROJECT:
			return element.getPath();
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			if (relativeToRoot)
				return Path.EMPTY;
			return element.getPath();
		case IJavaElement.PACKAGE_FRAGMENT:
			String relativePath = Util.concatWith(((PackageFragment) element).names, '/');
			return getPath(element.getParent(), relativeToRoot).append(new Path(relativePath));
		case IJavaElement.COMPILATION_UNIT:
		case IJavaElement.CLASS_FILE:
			return getPath(element.getParent(), relativeToRoot).append(new Path(element.getElementName()));
		default:
			return getPath(element.getParent(), relativeToRoot);
	}
}
 
Example 4
Source File: FindStringsToExternalizeAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private NonNLSElement[] doRun(IStructuredSelection selection, IProgressMonitor pm) throws CoreException {
	List<?> elements= getSelectedElementList(selection);
	if (elements == null || elements.isEmpty())
		return new NonNLSElement[0];

	pm.beginTask(ActionMessages.FindStringsToExternalizeAction_find_strings, elements.size());

	try{
		List<NonNLSElement> l= new ArrayList<NonNLSElement>();
		for (Iterator<?> iter= elements.iterator(); iter.hasNext();) {
			IJavaElement element= (IJavaElement) iter.next();
			if (element.getElementType() == IJavaElement.PACKAGE_FRAGMENT)
				l.addAll(analyze((IPackageFragment) element, new SubProgressMonitor(pm, 1)));
			else if (element.getElementType() == IJavaElement.PACKAGE_FRAGMENT_ROOT)
				l.addAll(analyze((IPackageFragmentRoot) element, new SubProgressMonitor(pm, 1)));
			if (element.getElementType() == IJavaElement.JAVA_PROJECT)
				l.addAll(analyze((IJavaProject) element, new SubProgressMonitor(pm, 1)));
		}
		return l.toArray(new NonNLSElement[l.size()]);
	} finally{
		pm.done();
	}
}
 
Example 5
Source File: JavaModelCache.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 *  Returns the info for this element without
 *  disturbing the cache ordering.
 */
protected Object peekAtInfo(IJavaElement element) {
	switch (element.getElementType()) {
		case IJavaElement.JAVA_MODEL:
			return this.modelInfo;
		case IJavaElement.JAVA_PROJECT:
			return this.projectCache.get(element);
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			return this.rootCache.peek(element);
		case IJavaElement.PACKAGE_FRAGMENT:
			return this.pkgCache.peek(element);
		case IJavaElement.COMPILATION_UNIT:
		case IJavaElement.CLASS_FILE:
			return this.openableCache.peek(element);
		case IJavaElement.TYPE:
			Object result = this.jarTypeCache.peek(element);
			if (result != null)
				return result;
			else
				return this.childrenCache.get(element);
		default:
			return this.childrenCache.get(element);
	}
}
 
Example 6
Source File: JavaModelCache.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 *  Returns the info for the element.
 */
public Object getInfo(IJavaElement element) {
	switch (element.getElementType()) {
		case IJavaElement.JAVA_MODEL:
			return this.modelInfo;
		case IJavaElement.JAVA_PROJECT:
			return this.projectCache.get(element);
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			return this.rootCache.get(element);
		case IJavaElement.PACKAGE_FRAGMENT:
			return this.pkgCache.get(element);
		case IJavaElement.COMPILATION_UNIT:
		case IJavaElement.CLASS_FILE:
			return this.openableCache.get(element);
		case IJavaElement.TYPE:
			Object result = this.jarTypeCache.get(element);
			if (result != null)
				return result;
			else
				return this.childrenCache.get(element);
		default:
			return this.childrenCache.get(element);
	}
}
 
Example 7
Source File: JavaElementResourceMapping.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static ResourceMapping create(IJavaElement element) {
	switch (element.getElementType()) {
		case IJavaElement.TYPE:
			return create((IType)element);
		case IJavaElement.COMPILATION_UNIT:
			return create((ICompilationUnit)element);
		case IJavaElement.CLASS_FILE:
			return create((IClassFile)element);
		case IJavaElement.PACKAGE_FRAGMENT:
			return create((IPackageFragment)element);
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			return create((IPackageFragmentRoot)element);
		case IJavaElement.JAVA_PROJECT:
			return create((IJavaProject)element);
		case IJavaElement.JAVA_MODEL:
			return create((IJavaModel)element);
		default:
			return null;
	}

}
 
Example 8
Source File: ClasspathChangedListener.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
public void elementChanged(ElementChangedEvent event) {
  for (IJavaElementDelta delta : event.getDelta().getChangedChildren()) {
    int flags = delta.getFlags();
    if ((flags & IJavaElementDelta.F_CLASSPATH_CHANGED) != 0) {
      IJavaElement element = delta.getElement();
      if (element.getElementType() != IJavaElement.JAVA_PROJECT) {
        continue;
      }

      classpathChanged((IJavaProject) element);
    }
  }
}
 
Example 9
Source File: ContainerFilter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean select(Viewer viewer, Object parent, Object element) {
	boolean isContainer= element instanceof IContainer;
	if (!isContainer && element instanceof IJavaElement) {
		int type= ((IJavaElement)element).getElementType();
		isContainer= type == IJavaElement.JAVA_MODEL
					|| type == IJavaElement.JAVA_PROJECT
					|| type == IJavaElement.PACKAGE_FRAGMENT
					|| type ==IJavaElement.PACKAGE_FRAGMENT_ROOT;
	}
	return (fFilterContainers && !isContainer) || (!fFilterContainers && isContainer);
}
 
Example 10
Source File: BuildpathIndicatorLabelDecorator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void processDelta(IJavaElementDelta delta, List<IJavaElement> result) {
	IJavaElement elem= delta.getElement();

	boolean isChanged= delta.getKind() == IJavaElementDelta.CHANGED;
	boolean isRemoved= delta.getKind() == IJavaElementDelta.REMOVED;
	int flags= delta.getFlags();

	switch (elem.getElementType()) {
		case IJavaElement.JAVA_MODEL:
			processChildrenDelta(delta, result);
			return;
		case IJavaElement.JAVA_PROJECT:
			if (isRemoved || (isChanged &&
					(flags & IJavaElementDelta.F_CLOSED) != 0)) {
				return;
			}
			processChildrenDelta(delta, result);
			return;
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			if (isRemoved) {
				return;
			}
			try {
				if ((((flags & IJavaElementDelta.F_REMOVED_FROM_CLASSPATH) != 0) ||
						((flags & IJavaElementDelta.F_ADDED_TO_CLASSPATH) != 0))
						&& ((IPackageFragmentRoot)elem).getKind() != IPackageFragmentRoot.K_SOURCE) {
					result.add(elem);
				}
			} catch (JavaModelException e) {
				// don't update
			}
			return;
		default:
			return;
	}
}
 
Example 11
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean canChildrenBeDestinations(IJavaElement javaElement) {
	switch (javaElement.getElementType()) {
		case IJavaElement.JAVA_MODEL:
		case IJavaElement.JAVA_PROJECT:
			return true;
		default:
			return false;
	}
}
 
Example 12
Source File: OrganizeImportsCommand.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void collectCompilationUnits(Object element, Collection<IJavaElement> result, String packagePrefix) {
	try {
		if (element instanceof IJavaElement) {
			IJavaElement elem = (IJavaElement) element;
			if (elem.exists()) {
				switch (elem.getElementType()) {
					case IJavaElement.TYPE:
						if (elem.getParent().getElementType() == IJavaElement.COMPILATION_UNIT) {
							result.add(elem.getParent());
						}
						break;
					case IJavaElement.COMPILATION_UNIT:
						result.add(elem);
						break;
					case IJavaElement.IMPORT_CONTAINER:
						result.add(elem.getParent());
						break;
					case IJavaElement.PACKAGE_FRAGMENT:
						collectCompilationUnits((IPackageFragment) elem, result);
						break;
					case IJavaElement.PACKAGE_FRAGMENT_ROOT:
						collectCompilationUnits((IPackageFragmentRoot) elem, result, packagePrefix);
						break;
					case IJavaElement.JAVA_PROJECT:
						IPackageFragmentRoot[] roots = ((IJavaProject) elem).getPackageFragmentRoots();
						for (int k = 0; k < roots.length; k++) {
							collectCompilationUnits(roots[k], result, null);
						}
						break;
				}
			}
		}
	} catch (CoreException e) {
		JavaLanguageServerPlugin.logException("Problem collection compilation unit ", e);
	}
}
 
Example 13
Source File: JavaElementAdapterFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IResource getResource(IJavaElement element) {
// can't use IJavaElement.getResource directly as we are interested in the
// corresponding resource
switch (element.getElementType()) {
	case IJavaElement.TYPE:
		// top level types behave like the CU
		IJavaElement parent= element.getParent();
		if (parent instanceof ICompilationUnit) {
			return ((ICompilationUnit) parent).getPrimary().getResource();
		}
		return null;
	case IJavaElement.COMPILATION_UNIT:
		return ((ICompilationUnit) element).getPrimary().getResource();
	case IJavaElement.CLASS_FILE:
	case IJavaElement.PACKAGE_FRAGMENT:
		// test if in a archive
		IPackageFragmentRoot root= (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
		if (!root.isArchive() && !root.isExternal()) {
			return element.getResource();
		}
		return null;
	case IJavaElement.PACKAGE_FRAGMENT_ROOT:
	case IJavaElement.JAVA_PROJECT:
	case IJavaElement.JAVA_MODEL:
		return element.getResource();
	default:
		return null;
}
  }
 
Example 14
Source File: RenameSupport.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static RenameSupport create(IJavaElement element, String newName, int flags) throws CoreException {
	switch (element.getElementType()) {
		case IJavaElement.JAVA_PROJECT:
			//return RenameSupport.create((IJavaProject) element, newName, flags);
			return null;
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			//return RenameSupport.create((IPackageFragmentRoot) element, newName);
			return null;
		case IJavaElement.PACKAGE_FRAGMENT:
			return RenameSupport.create((IPackageFragment) element, newName, flags);
		case IJavaElement.COMPILATION_UNIT:
			return RenameSupport.create((ICompilationUnit) element, newName, flags);
		case IJavaElement.TYPE:
			return RenameSupport.create((IType) element, newName, flags);
		case IJavaElement.METHOD:
			final IMethod method = (IMethod) element;
			if (method.isConstructor()) {
				return create(method.getDeclaringType(), newName, flags);
			} else {
				return RenameSupport.create((IMethod) element, newName, flags);
			}
		case IJavaElement.FIELD:
			return RenameSupport.create((IField) element, newName, flags);
		case IJavaElement.TYPE_PARAMETER:
			return RenameSupport.create((ITypeParameter) element, newName, flags);
		case IJavaElement.LOCAL_VARIABLE:
			return RenameSupport.create((ILocalVariable) element, newName, flags);
	}
	return null;
}
 
Example 15
Source File: RefactoringExecutionStarter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static RenameSupport createRenameSupport(IJavaElement element, String newName, int flags) throws CoreException {
	switch (element.getElementType()) {
		case IJavaElement.JAVA_PROJECT:
			return RenameSupport.create((IJavaProject) element, newName, flags);
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			return RenameSupport.create((IPackageFragmentRoot) element, newName);
		case IJavaElement.PACKAGE_FRAGMENT:
			return RenameSupport.create((IPackageFragment) element, newName, flags);
		case IJavaElement.COMPILATION_UNIT:
			return RenameSupport.create((ICompilationUnit) element, newName, flags);
		case IJavaElement.TYPE:
			return RenameSupport.create((IType) element, newName, flags);
		case IJavaElement.METHOD:
			final IMethod method= (IMethod) element;
			if (method.isConstructor())
				return createRenameSupport(method.getDeclaringType(), newName, flags);
			else
				return RenameSupport.create((IMethod) element, newName, flags);
		case IJavaElement.FIELD:
			return RenameSupport.create((IField) element, newName, flags);
		case IJavaElement.TYPE_PARAMETER:
			return RenameSupport.create((ITypeParameter) element, newName, flags);
		case IJavaElement.LOCAL_VARIABLE:
			return RenameSupport.create((ILocalVariable) element, newName, flags);
	}
	return null;
}
 
Example 16
Source File: JavaElementLabelComposer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Appends the label for a Java element with the flags as defined by this class.
 *
 * @param element the element to render
 * @param flags the rendering flags.
 */
public void appendElementLabel(IJavaElement element, long flags) {
	int type= element.getElementType();
	IPackageFragmentRoot root= null;

	if (type != IJavaElement.JAVA_MODEL && type != IJavaElement.JAVA_PROJECT && type != IJavaElement.PACKAGE_FRAGMENT_ROOT) {
		root= JavaModelUtil.getPackageFragmentRoot(element);
	}
	if (root != null && getFlag(flags, JavaElementLabels.PREPEND_ROOT_PATH)) {
		appendPackageFragmentRootLabel(root, JavaElementLabels.ROOT_QUALIFIED);
		fBuilder.append(JavaElementLabels.CONCAT_STRING);
	}

	switch (type) {
	case IJavaElement.METHOD:
		appendMethodLabel((IMethod) element, flags);
		break;
	case IJavaElement.FIELD:
		appendFieldLabel((IField) element, flags);
		break;
	case IJavaElement.LOCAL_VARIABLE:
		appendLocalVariableLabel((ILocalVariable) element, flags);
		break;
	case IJavaElement.TYPE_PARAMETER:
		appendTypeParameterLabel((ITypeParameter) element, flags);
		break;
	case IJavaElement.INITIALIZER:
		appendInitializerLabel((IInitializer) element, flags);
		break;
	case IJavaElement.TYPE:
		appendTypeLabel((IType) element, flags);
		break;
	case IJavaElement.CLASS_FILE:
		appendClassFileLabel((IClassFile) element, flags);
		break;
	case IJavaElement.COMPILATION_UNIT:
		appendCompilationUnitLabel((ICompilationUnit) element, flags);
		break;
	case IJavaElement.PACKAGE_FRAGMENT:
		appendPackageFragmentLabel((IPackageFragment) element, flags);
		break;
	case IJavaElement.PACKAGE_FRAGMENT_ROOT:
		appendPackageFragmentRootLabel((IPackageFragmentRoot) element, flags);
		break;
	case IJavaElement.IMPORT_CONTAINER:
	case IJavaElement.IMPORT_DECLARATION:
	case IJavaElement.PACKAGE_DECLARATION:
		appendDeclarationLabel(element, flags);
		break;
	case IJavaElement.JAVA_PROJECT:
	case IJavaElement.JAVA_MODEL:
		fBuilder.append(element.getElementName());
		break;
	default:
		fBuilder.append(element.getElementName());
	}

	if (root != null && getFlag(flags, JavaElementLabels.APPEND_ROOT_PATH)) {
		fBuilder.append(JavaElementLabels.CONCAT_STRING);
		appendPackageFragmentRootLabel(root, JavaElementLabels.ROOT_QUALIFIED);
	}
}
 
Example 17
Source File: CompilationUnitContextType.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public Project() {
	super("enclosing_project", JavaTemplateMessages.CompilationUnitContextType_variable_description_enclosing_project, IJavaElement.JAVA_PROJECT);  //$NON-NLS-1$
}
 
Example 18
Source File: OpenTypeHierarchyAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static IStatus compileCandidates(List<IJavaElement> result, List<IJavaElement> elements) {
	IStatus ok= Status.OK_STATUS;
	boolean onlyContainers= true;
	for (Iterator<IJavaElement> iter= elements.iterator(); iter.hasNext();) {
		IJavaElement elem= iter.next();
		try {
			switch (elem.getElementType()) {
				case IJavaElement.INITIALIZER:
				case IJavaElement.METHOD:
				case IJavaElement.FIELD:
				case IJavaElement.TYPE:
					onlyContainers= false;
					//$FALL-THROUGH$
				case IJavaElement.PACKAGE_FRAGMENT_ROOT:
				case IJavaElement.JAVA_PROJECT:
					result.add(elem);
					break;
				case IJavaElement.PACKAGE_FRAGMENT:
					if (((IPackageFragment)elem).containsJavaResources())
						result.add(elem);
					break;
				case IJavaElement.PACKAGE_DECLARATION:
					result.add(elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT));
					break;
				case IJavaElement.IMPORT_DECLARATION:
					IImportDeclaration decl= (IImportDeclaration)elem;
					if (decl.isOnDemand())
						elem= JavaModelUtil.findTypeContainer(elem.getJavaProject(), Signature.getQualifier(elem.getElementName()));
					else
						elem= elem.getJavaProject().findType(elem.getElementName());
					if (elem != null) {
						onlyContainers= false;
						result.add(elem);
					}
					break;
				case IJavaElement.CLASS_FILE:
					onlyContainers= false;
					result.add(((IClassFile)elem).getType());
					break;
				case IJavaElement.COMPILATION_UNIT:
					ICompilationUnit cu= (ICompilationUnit)elem;
					IType[] types= cu.getTypes();
					if (types.length > 0) {
						onlyContainers= false;
						result.addAll(Arrays.asList(types));
					}
			}
		} catch (JavaModelException e) {
			return e.getStatus();
		}
	}
	int size= result.size();
	if (size == 0 || (size > 1 && !onlyContainers))
		return createStatus(ActionMessages.OpenTypeHierarchyAction_messages_no_valid_java_element);
	return ok;
}
 
Example 19
Source File: ProblemsLabelDecorator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Computes the adornment flags for the given element.
 *
 * @param obj the element to compute the flags for
 *
 * @return the adornment flags
 */
protected int computeAdornmentFlags(Object obj) {
	try {
		if (obj instanceof IJavaElement) {
			IJavaElement element= (IJavaElement) obj;
			int type= element.getElementType();
			switch (type) {
				case IJavaElement.JAVA_MODEL:
				case IJavaElement.JAVA_PROJECT:
				case IJavaElement.PACKAGE_FRAGMENT_ROOT:
					int flags= getErrorTicksFromMarkers(element.getResource(), IResource.DEPTH_INFINITE, null);
					switch (type) {
						case IJavaElement.PACKAGE_FRAGMENT_ROOT:
							IPackageFragmentRoot root= (IPackageFragmentRoot) element;
							if (flags != ERRORTICK_ERROR && root.getKind() == IPackageFragmentRoot.K_SOURCE && isIgnoringOptionalProblems(root.getRawClasspathEntry())) {
								flags= ERRORTICK_IGNORE_OPTIONAL_PROBLEMS;
							}
							break;
						case IJavaElement.JAVA_PROJECT:
							IJavaProject project= (IJavaProject) element;
							if (flags != ERRORTICK_ERROR && flags != ERRORTICK_BUILDPATH_ERROR && isIgnoringOptionalProblems(project)) {
								flags= ERRORTICK_IGNORE_OPTIONAL_PROBLEMS;
							}
							break;
					}
					return flags;
				case IJavaElement.PACKAGE_FRAGMENT:
					return getPackageErrorTicksFromMarkers((IPackageFragment) element);
				case IJavaElement.COMPILATION_UNIT:
				case IJavaElement.CLASS_FILE:
					return getErrorTicksFromMarkers(element.getResource(), IResource.DEPTH_ONE, null);
				case IJavaElement.PACKAGE_DECLARATION:
				case IJavaElement.IMPORT_DECLARATION:
				case IJavaElement.IMPORT_CONTAINER:
				case IJavaElement.TYPE:
				case IJavaElement.INITIALIZER:
				case IJavaElement.METHOD:
				case IJavaElement.FIELD:
				case IJavaElement.LOCAL_VARIABLE:
					ICompilationUnit cu= (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
					if (cu != null) {
						ISourceReference ref= (type == IJavaElement.COMPILATION_UNIT) ? null : (ISourceReference) element;
						// The assumption is that only source elements in compilation unit can have markers
						IAnnotationModel model= isInJavaAnnotationModel(cu);
						int result= 0;
						if (model != null) {
							// open in Java editor: look at annotation model
							result= getErrorTicksFromAnnotationModel(model, ref);
						} else {
							result= getErrorTicksFromMarkers(cu.getResource(), IResource.DEPTH_ONE, ref);
						}
						fCachedRange= null;
						return result;
					}
					break;
				default:
			}
		} else if (obj instanceof IResource) {
			return getErrorTicksFromMarkers((IResource) obj, IResource.DEPTH_INFINITE, null);
		}
	} catch (CoreException e) {
		if (e instanceof JavaModelException) {
			if (((JavaModelException) e).isDoesNotExist()) {
				return 0;
			}
		}
		if (e.getStatus().getCode() == IResourceStatus.MARKER_NOT_FOUND) {
			return 0;
		}

		JavaPlugin.log(e);
	}
	return 0;
}
 
Example 20
Source File: OpenTypeHierarchyUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Converts the input to a possible input candidates
 * @param input input
 * @return the possible candidates
 */
public static IJavaElement[] getCandidates(Object input) {
	if (!(input instanceof IJavaElement)) {
		return null;
	}
	try {
		IJavaElement elem= (IJavaElement) input;
		switch (elem.getElementType()) {
			case IJavaElement.INITIALIZER:
			case IJavaElement.METHOD:
			case IJavaElement.FIELD:
			case IJavaElement.TYPE:
			case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			case IJavaElement.JAVA_PROJECT:
				return new IJavaElement[] { elem };
			case IJavaElement.PACKAGE_FRAGMENT:
				if (((IPackageFragment)elem).containsJavaResources())
					return new IJavaElement[] {elem};
				break;
			case IJavaElement.PACKAGE_DECLARATION:
				return new IJavaElement[] { elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT) };
			case IJavaElement.IMPORT_DECLARATION:
				IImportDeclaration decl= (IImportDeclaration) elem;
				if (decl.isOnDemand()) {
					elem= JavaModelUtil.findTypeContainer(elem.getJavaProject(), Signature.getQualifier(elem.getElementName()));
				} else {
					elem= elem.getJavaProject().findType(elem.getElementName());
				}
				if (elem == null)
					return null;
				return new IJavaElement[] {elem};

			case IJavaElement.CLASS_FILE:
				return new IJavaElement[] { ((IClassFile)input).getType() };
			case IJavaElement.COMPILATION_UNIT: {
				ICompilationUnit cu= (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT);
				if (cu != null) {
					IType[] types= cu.getTypes();
					if (types.length > 0) {
						return types;
					}
				}
				break;
			}
			default:
		}
	} catch (JavaModelException e) {
		JavaPlugin.log(e);
	}
	return null;
}