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

The following examples show how to use org.eclipse.jdt.core.IJavaElement#IMPORT_DECLARATION . 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: JavaCompareUtilities.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
static ImageDescriptor getImageDescriptor(int type) {
	switch (type) {
	case IJavaElement.INITIALIZER:
	case IJavaElement.METHOD:
		return getImageDescriptor("obj16/compare_method.gif"); //$NON-NLS-1$
	case IJavaElement.FIELD:
		return getImageDescriptor("obj16/compare_field.gif"); //$NON-NLS-1$
	case IJavaElement.PACKAGE_DECLARATION:
		return JavaPluginImages.DESC_OBJS_PACKDECL;
	case IJavaElement.IMPORT_DECLARATION:
		return JavaPluginImages.DESC_OBJS_IMPDECL;
	case IJavaElement.IMPORT_CONTAINER:
		return JavaPluginImages.DESC_OBJS_IMPCONT;
	case IJavaElement.COMPILATION_UNIT:
		return JavaPluginImages.DESC_OBJS_CUNIT;
	}
	return ImageDescriptor.getMissingImageDescriptor();
}
 
Example 2
Source File: ASTNodeSearchUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static ASTNode[] getDeclarationNodes(IJavaElement element, CompilationUnit cuNode) throws JavaModelException {
	switch(element.getElementType()){
		case IJavaElement.FIELD:
			return new ASTNode[]{getFieldOrEnumConstantDeclaration((IField) element, cuNode)};
		case IJavaElement.IMPORT_CONTAINER:
			return getImportNodes((IImportContainer)element, cuNode);
		case IJavaElement.IMPORT_DECLARATION:
			return new ASTNode[]{getImportDeclarationNode((IImportDeclaration)element, cuNode)};
		case IJavaElement.INITIALIZER:
			return new ASTNode[]{getInitializerNode((IInitializer)element, cuNode)};
		case IJavaElement.METHOD:
			return new ASTNode[]{getMethodOrAnnotationTypeMemberDeclarationNode((IMethod) element, cuNode)};
		case IJavaElement.PACKAGE_DECLARATION:
			return new ASTNode[]{getPackageDeclarationNode((IPackageDeclaration)element, cuNode)};
		case IJavaElement.TYPE:
			return new ASTNode[]{getAbstractTypeDeclarationNode((IType) element, cuNode)};
		default:
			Assert.isTrue(false, String.valueOf(element.getElementType()));
			return null;
	}
}
 
Example 3
Source File: ASTNodeSearchUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static ASTNode[] getDeclarationNodes(IJavaElement element, CompilationUnit cuNode) throws JavaModelException {
	switch(element.getElementType()){
		case IJavaElement.FIELD:
			return new ASTNode[]{getFieldOrEnumConstantDeclaration((IField) element, cuNode)};
		case IJavaElement.IMPORT_CONTAINER:
			return getImportNodes((IImportContainer)element, cuNode);
		case IJavaElement.IMPORT_DECLARATION:
			return new ASTNode[]{getImportDeclarationNode((IImportDeclaration)element, cuNode)};
		case IJavaElement.INITIALIZER:
			return new ASTNode[]{getInitializerNode((IInitializer)element, cuNode)};
		case IJavaElement.METHOD:
			return new ASTNode[]{getMethodOrAnnotationTypeMemberDeclarationNode((IMethod) element, cuNode)};
		case IJavaElement.PACKAGE_DECLARATION:
			return new ASTNode[]{getPackageDeclarationNode((IPackageDeclaration)element, cuNode)};
		case IJavaElement.TYPE:
			return new ASTNode[]{getAbstractTypeDeclarationNode((IType) element, cuNode)};
		default:
			Assert.isTrue(false, String.valueOf(element.getElementType()));
			return null;
	}
}
 
Example 4
Source File: JavaElementTypeComparator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
int getIdForJavaElementType(int elementType) {
	switch (elementType) {
		case IJavaElement.JAVA_MODEL:
			return 130;
		case IJavaElement.JAVA_PROJECT:
			return 120;
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			return 110;
		case IJavaElement.PACKAGE_FRAGMENT:
			return 100;
		case IJavaElement.COMPILATION_UNIT:
			return 90;
		case IJavaElement.CLASS_FILE:
			return 80;
		case IJavaElement.TYPE:
			return 70;
		case IJavaElement.FIELD:
			return 60;
		case IJavaElement.METHOD:
			return 50;
		case IJavaElement.INITIALIZER:
			return 40;
		case IJavaElement.PACKAGE_DECLARATION:
			return 30;
		case IJavaElement.IMPORT_CONTAINER:
			return 20;
		case IJavaElement.IMPORT_DECLARATION:
			return 10;
		default :
			return 1;
	}
}
 
Example 5
Source File: JavaSearchPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
final static boolean isSearchableType(IJavaElement element) {
	switch (element.getElementType()) {
		case IJavaElement.PACKAGE_FRAGMENT:
		case IJavaElement.PACKAGE_DECLARATION:
		case IJavaElement.IMPORT_DECLARATION:
		case IJavaElement.TYPE:
		case IJavaElement.FIELD:
		case IJavaElement.METHOD:
			return true;
	}
	return false;
}
 
Example 6
Source File: PasteAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private ASTNode createNewNodeToInsertToCu(TypedSource source, ASTRewrite rewrite) {
	switch(source.getType()){
		case IJavaElement.TYPE:
			return rewrite.createStringPlaceholder(source.getSource(), ASTNode.TYPE_DECLARATION);
		case IJavaElement.PACKAGE_DECLARATION:
			return rewrite.createStringPlaceholder(source.getSource(), ASTNode.PACKAGE_DECLARATION);
		case IJavaElement.IMPORT_DECLARATION:
			return rewrite.createStringPlaceholder(source.getSource(), ASTNode.IMPORT_DECLARATION);
		default: Assert.isTrue(false, String.valueOf(source.getType()));
			return null;
	}
}
 
Example 7
Source File: DeleteElementsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Deletes this element from its compilation unit.
 * @see MultiOperation
 */
protected void processElement(IJavaElement element) throws JavaModelException {
	ICompilationUnit cu = (ICompilationUnit) element;

	// keep track of the import statements - if all are removed, delete
	// the import container (and report it in the delta)
	int numberOfImports = cu.getImports().length;

	JavaElementDelta delta = new JavaElementDelta(cu);
	IJavaElement[] cuElements = ((IRegion) this.childrenToRemove.get(cu)).getElements();
	for (int i = 0, length = cuElements.length; i < length; i++) {
		IJavaElement e = cuElements[i];
		if (e.exists()) {
			deleteElement(e, cu);
			delta.removed(e);
			if (e.getElementType() == IJavaElement.IMPORT_DECLARATION) {
				numberOfImports--;
				if (numberOfImports == 0) {
					delta.removed(cu.getImportContainer());
				}
			}
		}
	}
	if (delta.getAffectedChildren().length > 0) {
		cu.save(getSubProgressMonitor(1), this.force);
		if (!cu.isWorkingCopy()) { // if unit is working copy, then save will have already fired the delta
			addDelta(delta);
			setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
		}
	}
}
 
Example 8
Source File: TypedSource.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean canCreateForType(int type){
	return 		type == IJavaElement.FIELD
			|| 	type == IJavaElement.TYPE
			|| 	type == IJavaElement.IMPORT_CONTAINER
			|| 	type == IJavaElement.IMPORT_DECLARATION
			|| 	type == IJavaElement.INITIALIZER
			|| 	type == IJavaElement.METHOD
			|| 	type == IJavaElement.PACKAGE_DECLARATION;
}
 
Example 9
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean hasOnlyImportDeclarations(IJavaElement[] javaElements) {
	for (int i= 0; i < javaElements.length; i++) {
		if (javaElements[i].getElementType() != IJavaElement.IMPORT_DECLARATION)
			return false;
	}
	return true;
}
 
Example 10
Source File: DeleteChangeCreator.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static Change createDeleteChange(IJavaElement javaElement) throws JavaModelException {
	Assert.isTrue(! ReorgUtils.isInsideCompilationUnit(javaElement));

	switch(javaElement.getElementType()){
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			return createPackageFragmentRootDeleteChange((IPackageFragmentRoot)javaElement);

		case IJavaElement.PACKAGE_FRAGMENT:
			return createSourceManipulationDeleteChange((IPackageFragment)javaElement);

		case IJavaElement.COMPILATION_UNIT:
			return createSourceManipulationDeleteChange((ICompilationUnit)javaElement);

		case IJavaElement.CLASS_FILE:
			//if this assert fails, it means that a precondition is missing
			Assert.isTrue(((IClassFile)javaElement).getResource() instanceof IFile);
			return createDeleteChange(((IClassFile)javaElement).getResource());

		case IJavaElement.JAVA_MODEL: //cannot be done
			Assert.isTrue(false);
			return null;

		case IJavaElement.JAVA_PROJECT: //handled differently
			Assert.isTrue(false);
			return null;

		case IJavaElement.TYPE:
		case IJavaElement.FIELD:
		case IJavaElement.METHOD:
		case IJavaElement.INITIALIZER:
		case IJavaElement.PACKAGE_DECLARATION:
		case IJavaElement.IMPORT_CONTAINER:
		case IJavaElement.IMPORT_DECLARATION:
			Assert.isTrue(false);//not done here
			return new NullChange();
		default:
			Assert.isTrue(false);//there's no more kinds
			return new NullChange();
	}
}
 
Example 11
Source File: MembersView.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Finds the closest Java element which can be used as input for
 * this part and has the given Java element as child.
 *
 * @param 	je 	the Java element for which to search the closest input
 * @return	the closest Java element used as input for this part, or <code>null</code>
 */
@Override
protected IJavaElement findInputForJavaElement(IJavaElement je) {
	if (je == null || !je.exists() || (je.getJavaProject() != null && !je.getJavaProject().isOnClasspath(je)))
		return null;

	switch (je.getElementType()) {
		case IJavaElement.TYPE:
			IType type= ((IType)je).getDeclaringType();
			if (type == null)
				return je;
			else
				return findInputForJavaElement(type);
		case IJavaElement.COMPILATION_UNIT:
			return getTypeForCU((ICompilationUnit)je);
		case IJavaElement.CLASS_FILE:
			return findInputForJavaElement(((IClassFile)je).getType());
		case IJavaElement.IMPORT_DECLARATION:
			return findInputForJavaElement(je.getParent());
		case IJavaElement.PACKAGE_DECLARATION:
		case IJavaElement.IMPORT_CONTAINER:
			IJavaElement parent= je.getParent();
			if (parent instanceof ICompilationUnit) {
				return getTypeForCU((ICompilationUnit)parent);
			}
			else if (parent instanceof IClassFile)
				return findInputForJavaElement(parent);
			return null;
		default:
			if (je instanceof IMember)
				return findInputForJavaElement(((IMember)je).getDeclaringType());
	}
	return null;
}
 
Example 12
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;
}
 
Example 13
Source File: GenericRefactoringHandleTransplanter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public final IJavaElement transplantHandle(IJavaElement element) {
	IJavaElement parent= element.getParent();
	if (parent != null)
		parent= transplantHandle(parent); // recursive

	switch (element.getElementType()) {
		case IJavaElement.JAVA_MODEL:
			return transplantHandle((IJavaModel) element);

		case IJavaElement.JAVA_PROJECT:
			return transplantHandle((IJavaProject) element);

		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			return transplantHandle((IJavaProject) parent, (IPackageFragmentRoot) element);

		case IJavaElement.PACKAGE_FRAGMENT:
			return transplantHandle((IPackageFragmentRoot) parent, (IPackageFragment) element);

		case IJavaElement.COMPILATION_UNIT:
			return transplantHandle((IPackageFragment) parent, (ICompilationUnit) element);

		case IJavaElement.CLASS_FILE:
			return transplantHandle((IPackageFragment) parent, (IClassFile) element);

		case IJavaElement.TYPE:
			return transplantHandle(parent, (IType) element);

		case IJavaElement.FIELD:
			return transplantHandle((IType) parent, (IField) element);

		case IJavaElement.METHOD:
			return transplantHandle((IType) parent, (IMethod) element);

		case IJavaElement.INITIALIZER:
			return transplantHandle((IType) parent, (IInitializer) element);

		case IJavaElement.PACKAGE_DECLARATION:
			return transplantHandle((ICompilationUnit) parent, (IPackageDeclaration) element);

		case IJavaElement.IMPORT_CONTAINER:
			return transplantHandle((ICompilationUnit) parent, (IImportContainer) element);

		case IJavaElement.IMPORT_DECLARATION:
			return transplantHandle((IImportContainer) parent, (IImportDeclaration) element);

		case IJavaElement.LOCAL_VARIABLE:
			return transplantHandle((ILocalVariable) element);

		case IJavaElement.TYPE_PARAMETER:
			return transplantHandle((IMember) parent, (ITypeParameter) element);

		case IJavaElement.ANNOTATION:
			return transplantHandle((IAnnotatable) parent, (IAnnotation) element);

		default:
			throw new IllegalArgumentException(element.toString());
	}

}
 
Example 14
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 15
Source File: JavaElementLabelComposer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.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);
		fBuffer.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:
			fBuffer.append(element.getElementName());
			break;
		default:
			fBuffer.append(element.getElementName());
	}

	if (root != null && getFlag(flags, JavaElementLabels.APPEND_ROOT_PATH)) {
		int offset= fBuffer.length();
		fBuffer.append(JavaElementLabels.CONCAT_STRING);
		appendPackageFragmentRootLabel(root, JavaElementLabels.ROOT_QUALIFIED);

		if (getFlag(flags, JavaElementLabels.COLORIZE)) {
			fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
		}

	}
}
 
Example 16
Source File: JavaEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Computes and returns the source reference that includes the caret and
 * serves as provider for the outline page selection and the editor range
 * indication.
 *
 * @return the computed source reference
 * @since 3.0
 */
protected ISourceReference computeHighlightRangeSourceReference() {
	ISourceViewer sourceViewer= getSourceViewer();
	if (sourceViewer == null)
		return null;

	StyledText styledText= sourceViewer.getTextWidget();
	if (styledText == null)
		return null;

	int caret= 0;
	if (sourceViewer instanceof ITextViewerExtension5) {
		ITextViewerExtension5 extension= (ITextViewerExtension5)sourceViewer;
		caret= extension.widgetOffset2ModelOffset(styledText.getCaretOffset());
	} else {
		int offset= sourceViewer.getVisibleRegion().getOffset();
		caret= offset + styledText.getCaretOffset();
	}

	IJavaElement element= getElementAt(caret, false);

	if ( !(element instanceof ISourceReference))
		return null;

	if (element.getElementType() == IJavaElement.IMPORT_DECLARATION) {

		IImportDeclaration declaration= (IImportDeclaration) element;
		IImportContainer container= (IImportContainer) declaration.getParent();
		ISourceRange srcRange= null;

		try {
			srcRange= container.getSourceRange();
		} catch (JavaModelException e) {
		}

		if (srcRange != null && srcRange.getOffset() == caret)
			return container;
	}

	return (ISourceReference) element;
}
 
Example 17
Source File: ReorgUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static String createNamePattern(IJavaElement element) throws JavaModelException {
	switch(element.getElementType()){
		case IJavaElement.CLASS_FILE:
			return RefactoringCoreMessages.ReorgUtils_3;
		case IJavaElement.COMPILATION_UNIT:
			return RefactoringCoreMessages.ReorgUtils_4;
		case IJavaElement.FIELD:
			return RefactoringCoreMessages.ReorgUtils_5;
		case IJavaElement.IMPORT_CONTAINER:
			return RefactoringCoreMessages.ReorgUtils_6;
		case IJavaElement.IMPORT_DECLARATION:
			return RefactoringCoreMessages.ReorgUtils_7;
		case IJavaElement.INITIALIZER:
			return RefactoringCoreMessages.ReorgUtils_8;
		case IJavaElement.JAVA_PROJECT:
			return RefactoringCoreMessages.ReorgUtils_9;
		case IJavaElement.METHOD:
			if (((IMethod)element).isConstructor()) {
				return RefactoringCoreMessages.ReorgUtils_10;
			} else {
				return RefactoringCoreMessages.ReorgUtils_11;
			}
		case IJavaElement.PACKAGE_DECLARATION:
			return RefactoringCoreMessages.ReorgUtils_12;
		case IJavaElement.PACKAGE_FRAGMENT:
			if (JavaElementUtil.isDefaultPackage(element)) {
				return RefactoringCoreMessages.ReorgUtils_13;
			} else {
				return RefactoringCoreMessages.ReorgUtils_14;
			}
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			if (((IPackageFragmentRoot) element).isArchive()) {
				return RefactoringCoreMessages.ReorgUtils_21;
			}
			if (isSourceFolder(element)) {
				return RefactoringCoreMessages.ReorgUtils_15;
			}
			if (isClassFolder(element)) {
				return RefactoringCoreMessages.ReorgUtils_16;
			}
			return RefactoringCoreMessages.ReorgUtils_17;
		case IJavaElement.TYPE:
			IType type= (IType)element;
			if (type.isAnonymous()) {
				return RefactoringCoreMessages.ReorgUtils_20;
			}
			return RefactoringCoreMessages.ReorgUtils_18;
		default:
			Assert.isTrue(false);
			return null;
	}
}
 
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: GenericRefactoringHandleTransplanter.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public final IJavaElement transplantHandle(IJavaElement element) {
	IJavaElement parent= element.getParent();
	if (parent != null)
	 {
		parent= transplantHandle(parent); // recursive
	}

	switch (element.getElementType()) {
		case IJavaElement.JAVA_MODEL:
			return transplantHandle((IJavaModel) element);

		case IJavaElement.JAVA_PROJECT:
			return transplantHandle((IJavaProject) element);

		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			return transplantHandle((IJavaProject) parent, (IPackageFragmentRoot) element);

		case IJavaElement.PACKAGE_FRAGMENT:
			return transplantHandle((IPackageFragmentRoot) parent, (IPackageFragment) element);

		case IJavaElement.COMPILATION_UNIT:
			return transplantHandle((IPackageFragment) parent, (ICompilationUnit) element);

		case IJavaElement.CLASS_FILE:
			return transplantHandle((IPackageFragment) parent, (IClassFile) element);

		case IJavaElement.TYPE:
			return transplantHandle(parent, (IType) element);

		case IJavaElement.FIELD:
			return transplantHandle((IType) parent, (IField) element);

		case IJavaElement.METHOD:
			return transplantHandle((IType) parent, (IMethod) element);

		case IJavaElement.INITIALIZER:
			return transplantHandle((IType) parent, (IInitializer) element);

		case IJavaElement.PACKAGE_DECLARATION:
			return transplantHandle((ICompilationUnit) parent, (IPackageDeclaration) element);

		case IJavaElement.IMPORT_CONTAINER:
			return transplantHandle((ICompilationUnit) parent, (IImportContainer) element);

		case IJavaElement.IMPORT_DECLARATION:
			return transplantHandle((IImportContainer) parent, (IImportDeclaration) element);

		case IJavaElement.LOCAL_VARIABLE:
			return transplantHandle((ILocalVariable) element);

		case IJavaElement.TYPE_PARAMETER:
			return transplantHandle((IMember) parent, (ITypeParameter) element);

		case IJavaElement.ANNOTATION:
			return transplantHandle((IAnnotatable) parent, (IAnnotation) element);

		default:
			throw new IllegalArgumentException(element.toString());
	}

}