Java Code Examples for org.eclipse.jdt.core.IMember#getElementType()

The following examples show how to use org.eclipse.jdt.core.IMember#getElementType() . 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: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static boolean isPullUpAvailable(IMember member) throws JavaModelException {
	if (!member.exists())
		return false;
	final int type= member.getElementType();
	if (type != IJavaElement.METHOD && type != IJavaElement.FIELD && type != IJavaElement.TYPE)
		return false;
	if (JdtFlags.isEnum(member) && type != IJavaElement.TYPE)
		return false;
	if (!Checks.isAvailable(member))
		return false;
	if (member instanceof IType) {
		if (!JdtFlags.isStatic(member) && !JdtFlags.isEnum(member) && !JdtFlags.isAnnotation(member))
			return false;
	}
	if (member instanceof IMethod) {
		final IMethod method= (IMethod) member;
		if (method.isConstructor())
			return false;
		if (JdtFlags.isNative(method))
			return false;
		final IType declaring= method.getDeclaringType();
		if (declaring != null && declaring.isAnnotation())
			return false;
	}
	return true;
}
 
Example 2
Source File: JavaModelUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Evaluates if a member in the focus' element hierarchy is visible from
 * elements in a package.
 * @param member The member to test the visibility for
 * @param pack The package of the focus element focus
 * @return returns <code>true</code> if the member is visible from the package
 * @throws JavaModelException thrown when the member can not be accessed
 */
public static boolean isVisibleInHierarchy(IMember member, IPackageFragment pack) throws JavaModelException {
	int type= member.getElementType();
	if  (type == IJavaElement.INITIALIZER ||  (type == IJavaElement.METHOD && member.getElementName().startsWith("<"))) { //$NON-NLS-1$
		return false;
	}

	int otherflags= member.getFlags();

	IType declaringType= member.getDeclaringType();
	if (Flags.isPublic(otherflags) || Flags.isProtected(otherflags) || (declaringType != null && isInterfaceOrAnnotation(declaringType))) {
		return true;
	} else if (Flags.isPrivate(otherflags)) {
		return false;
	}

	IPackageFragment otherpack= (IPackageFragment) member.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
	return (pack != null && pack.equals(otherpack));
}
 
Example 3
Source File: StubCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected void appendFlags(final IMember member) throws JavaModelException {
	if (member instanceof IAnnotatable)
		for (IAnnotation annotation : ((IAnnotatable) member).getAnnotations()) {
			appendAnnotation(annotation);
		}
	
	int flags= member.getFlags();
	final int kind= member.getElementType();
	if (kind == IJavaElement.TYPE) {
		flags&= ~Flags.AccSuper;
		final IType type= (IType) member;
		if (!type.isMember())
			flags&= ~Flags.AccPrivate;
		if (Flags.isEnum(flags))
			flags&= ~Flags.AccAbstract;
	}
	if (Flags.isEnum(flags))
		flags&= ~Flags.AccFinal;
	if (kind == IJavaElement.METHOD) {
		flags&= ~Flags.AccVarargs;
		flags&= ~Flags.AccBridge;
	}
	if (flags != 0)
		fBuffer.append(Flags.toString(flags));
}
 
Example 4
Source File: TypeHierarchyViewPart.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Selects an member in the methods list or in the current hierarchy.
 * @param member The member to select
 */
public void selectMember(IMember member) {
	fSelectInEditor= false;
	if (member.getElementType() != IJavaElement.TYPE) {
		Control methodControl= fMethodsViewer.getControl();
		if (methodControl != null && !methodControl.isDisposed()) {
			methodControl.setFocus();
		}

		fMethodsViewer.setSelection(new StructuredSelection(member), true);
	} else {
		Control viewerControl= getCurrentViewer().getControl();
		if (viewerControl != null && !viewerControl.isDisposed()) {
			viewerControl.setFocus();
		}

		if (!member.equals(fSelectedType)) {
			getCurrentViewer().setSelection(new StructuredSelection(member), true);
		}
	}
	fSelectInEditor= true;
}
 
Example 5
Source File: JdtFlags.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean isEnumTypeFinal(IMember member) throws JavaModelException {
	if (!(isEnum(member) && member.getElementType() == IJavaElement.TYPE))
		return false;
	// An enum type is implicitly final unless it contains at least one enum constant that has a class body.
	IJavaElement[] children= member.getChildren();
	for (IJavaElement child : children) {
		if (isEnumConstant((IMember) child) && ((IField) child).getChildren().length != 0) {
			return false;
		}
	}
	return true;
}
 
Example 6
Source File: JavaElementImageProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean isInterfaceOrAnnotationField(IMember element) throws JavaModelException {
	// always show the final symbol on interface fields
	if (element.getElementType() == IJavaElement.FIELD) {
		return JavaModelUtil.isInterfaceOrAnnotation(element.getDeclaringType());
	}
	return false;
}
 
Example 7
Source File: JavaElementUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static String createSignature(IMember member){
	switch (member.getElementType()){
		case IJavaElement.FIELD:
			return createFieldSignature((IField)member);
		case IJavaElement.TYPE:
			return BasicElementLabels.getJavaElementName(((IType)member).getFullyQualifiedName('.'));
		case IJavaElement.INITIALIZER:
			return RefactoringCoreMessages.JavaElementUtil_initializer;
		case IJavaElement.METHOD:
			return createMethodSignature((IMethod)member);
		default:
			Assert.isTrue(false);
			return null;
	}
}
 
Example 8
Source File: MemberFilter.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 parentElement, Object element) {
	try {
		if (element instanceof IMember) {
			IMember member= (IMember) element;
			int memberType= member.getElementType();

			if (hasFilter(FILTER_FIELDS) && memberType == IJavaElement.FIELD) {
				return false;
			}

			if (hasFilter(FILTER_LOCALTYPES) && memberType == IJavaElement.TYPE && isLocalType((IType) member)) {
				return false;
			}

			if (member.getElementName().startsWith("<")) { // filter out <clinit> //$NON-NLS-1$
				return false;
			}
			int flags= member.getFlags();
			if (hasFilter(FILTER_STATIC) && (Flags.isStatic(flags) || isFieldInInterfaceOrAnnotation(member)) && memberType != IJavaElement.TYPE) {
				return false;
			}
			if (hasFilter(FILTER_NONPUBLIC) && !Flags.isPublic(flags) && !isMemberInInterfaceOrAnnotation(member) && !isTopLevelType(member) && !isEnumConstant(member)) {
				return false;
			}
		}
	} catch (JavaModelException e) {
		// ignore
	}
	return true;
}
 
Example 9
Source File: JavaElementUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static String createSignature(IMember member){
	switch (member.getElementType()){
		case IJavaElement.FIELD:
			return createFieldSignature((IField)member);
		case IJavaElement.TYPE:
			return BasicElementLabels.getJavaElementName(((IType)member).getFullyQualifiedName('.'));
		case IJavaElement.INITIALIZER:
			return RefactoringCoreMessages.JavaElementUtil_initializer;
		case IJavaElement.METHOD:
			return createMethodSignature((IMethod)member);
		default:
			Assert.isTrue(false);
			return null;
	}
}
 
Example 10
Source File: GenericRefactoringHandleTransplanter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected ITypeParameter transplantHandle(IMember parent, ITypeParameter element) {
	switch (parent.getElementType()) {
		case IJavaElement.TYPE:
			return ((IType) parent).getTypeParameter(element.getElementName());
		case IJavaElement.METHOD:
			return ((IMethod) parent).getTypeParameter(element.getElementName());
		default:
			throw new IllegalStateException(element.toString());
	}
}
 
Example 11
Source File: RefactoringAvailabilityTester.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean isPullUpAvailable(IMember member) throws JavaModelException {
	if (!member.exists()) {
		return false;
	}
	final int type = member.getElementType();
	if (type != IJavaElement.METHOD && type != IJavaElement.FIELD && type != IJavaElement.TYPE) {
		return false;
	}
	if (JdtFlags.isEnum(member) && type != IJavaElement.TYPE) {
		return false;
	}
	if (!Checks.isAvailable(member)) {
		return false;
	}
	if (member instanceof IType) {
		if (!JdtFlags.isStatic(member) && !JdtFlags.isEnum(member) && !JdtFlags.isAnnotation(member)) {
			return false;
		}
	}
	if (member instanceof IMethod) {
		final IMethod method = (IMethod) member;
		if (method.isConstructor()) {
			return false;
		}
		if (JdtFlags.isNative(method)) {
			return false;
		}
		final IType declaring = method.getDeclaringType();
		if (declaring != null && declaring.isAnnotation()) {
			return false;
		}
	}
	return true;
}
 
Example 12
Source File: RefactoringAvailabilityTester.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean isPushDownAvailable(final IMember member) throws JavaModelException {
	if (!member.exists()) {
		return false;
	}
	final int type = member.getElementType();
	if (type != IJavaElement.METHOD && type != IJavaElement.FIELD) {
		return false;
	}
	if (JdtFlags.isEnum(member)) {
		return false;
	}
	if (!Checks.isAvailable(member)) {
		return false;
	}
	if (JdtFlags.isStatic(member)) {
		return false;
	}
	if (type == IJavaElement.METHOD) {
		final IMethod method = (IMethod) member;
		if (method.isConstructor()) {
			return false;
		}
		if (JdtFlags.isNative(method)) {
			return false;
		}
		final IType declaring = method.getDeclaringType();
		if (declaring != null && declaring.isAnnotation()) {
			return false;
		}
	}
	return true;
}
 
Example 13
Source File: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isMoveStaticAvailable(final IMember member) throws JavaModelException {
	if (!member.exists())
		return false;
	final int type= member.getElementType();
	if (type != IJavaElement.METHOD && type != IJavaElement.FIELD && type != IJavaElement.TYPE)
		return false;
	if (JdtFlags.isEnum(member) && type != IJavaElement.TYPE)
		return false;
	final IType declaring= member.getDeclaringType();
	if (declaring == null)
		return false;
	if (!Checks.isAvailable(member))
		return false;
	if (type == IJavaElement.METHOD && declaring.isInterface()) {
		boolean is18OrHigher= JavaModelUtil.is18OrHigher(member.getJavaProject());
		if (!is18OrHigher || !Flags.isStatic(member.getFlags()))
			return false;
	}
	if (type == IJavaElement.METHOD && !JdtFlags.isStatic(member))
		return false;
	if (type == IJavaElement.METHOD && ((IMethod) member).isConstructor())
		return false;
	if (type == IJavaElement.TYPE && !JdtFlags.isStatic(member))
		return false;
	if (!declaring.isInterface() && !JdtFlags.isStatic(member))
		return false;
	return true;
}
 
Example 14
Source File: JdtFlags.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean isEnumConstant(IMember member) throws JavaModelException {
	return member.getElementType() == IJavaElement.FIELD && isEnum(member);
}
 
Example 15
Source File: CallHierarchyViewPart.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Computes the content description for the call hierarchy computation.
 * 
 * @param includeMask the include mask
 * @return the content description
 * @since 3.7
 */
private String computeContentDescription(int includeMask) {
	// see also HistoryAction.getElementLabel(IMember[])
	String scopeDescription= fSearchScopeActions.getFullDescription(includeMask);

	if (fInputElements.length == 1) {
		IMember element= fInputElements[0];
		String elementName= JavaElementLabels.getElementLabel(element, JavaElementLabels.ALL_DEFAULT);
		String[] args= new String[] { elementName, scopeDescription };
		if (fCurrentCallMode == CALL_MODE_CALLERS) {
			switch (element.getElementType()) {
				case IJavaElement.TYPE:
					return Messages.format(CallHierarchyMessages.CallHierarchyViewPart_callsToConstructors, args);
				case IJavaElement.FIELD:
					switch (this.fCurrentFieldMode) {
			            case IJavaSearchConstants.READ_ACCESSES:
							return Messages.format(CallHierarchyMessages.CallHierarchyViewPart_callsToFieldRead, args);
			            case IJavaSearchConstants.WRITE_ACCESSES:
							return Messages.format(CallHierarchyMessages.CallHierarchyViewPart_callsToFieldWrite, args);
						default: // all references
							return Messages.format(CallHierarchyMessages.CallHierarchyViewPart_callsToField, args);
					}
				case IJavaElement.METHOD:
				case IJavaElement.INITIALIZER:
				default:
					return Messages.format(CallHierarchyMessages.CallHierarchyViewPart_callsToMethod, args);
			}
		} else {
			switch (element.getElementType()) {
				case IJavaElement.TYPE:
					return Messages.format(CallHierarchyMessages.CallHierarchyViewPart_callsFromConstructors, args);
				case IJavaElement.FIELD:
				case IJavaElement.METHOD:
				case IJavaElement.INITIALIZER:
				default:
					return Messages.format(CallHierarchyMessages.CallHierarchyViewPart_callsFromMethod, args);
			}
		}

	} else {
		if (fCurrentCallMode == CALL_MODE_CALLERS) {
			switch (fInputElements.length) {
	        	case 0:
	        		Assert.isTrue(false);
	        		return null;
	        	case 2:
	        		return Messages.format(CallHierarchyMessages.CallHierarchyViewPart_callsToMembers_2,
	        				new String[] { getShortLabel(fInputElements[0]), getShortLabel(fInputElements[1]), scopeDescription });

	        	default:
	        		return Messages.format(CallHierarchyMessages.CallHierarchyViewPart_callsToMembers_more,
	        				new String[] { getShortLabel(fInputElements[0]), getShortLabel(fInputElements[1]), scopeDescription });
			}
		} else {
			switch (fInputElements.length) {
				case 0:
					Assert.isTrue(false);
					return null;
				case 2:
					return Messages.format(CallHierarchyMessages.CallHierarchyViewPart_callsFromMembers_2,
							new String[] { getShortLabel(fInputElements[0]), getShortLabel(fInputElements[1]), scopeDescription });

				default:
					return Messages.format(CallHierarchyMessages.CallHierarchyViewPart_callsFromMembers_more,
							new String[] { getShortLabel(fInputElements[0]), getShortLabel(fInputElements[1]), scopeDescription });
			}
		}
	}
}
 
Example 16
Source File: JdtFlags.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean isInterfaceOrAnnotationMethod(IMember member) throws JavaModelException {
	return member.getElementType() == IJavaElement.METHOD && isInterfaceOrAnnotationMember(member);
}
 
Example 17
Source File: MoveStaticMembersProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private String createNonAccessibleMemberMessage(IMember member, IType accessingType, boolean moved){
	//Non-visibility can have various reasons and always displaying all visibility
	//flags for all enclosing elements would be too heavy. Context reveals exact cause.
	IType declaringType= moved ? getDestinationType() : getDeclaringType();
	String message;
	switch (member.getElementType()){
		case IJavaElement.FIELD: {
			if (moved)
				message= Messages.format(RefactoringCoreMessages.MoveMembersRefactoring_moved_field,
							new String[]{JavaElementUtil.createFieldSignature((IField)member),
						getQualifiedTypeLabel(accessingType),
						getQualifiedTypeLabel(declaringType)});
			else
				message= Messages.format(RefactoringCoreMessages.MoveMembersRefactoring_accessed_field,
							new String[]{JavaElementUtil.createFieldSignature((IField)member),
						getQualifiedTypeLabel(accessingType)});
			return message;
		}
		case IJavaElement.METHOD: {
			if (moved)
				message= Messages.format(RefactoringCoreMessages.MoveMembersRefactoring_moved_method,
							new String[]{JavaElementUtil.createMethodSignature((IMethod)member),
						getQualifiedTypeLabel(accessingType),
						getQualifiedTypeLabel(declaringType)});
			else
				message= Messages.format(RefactoringCoreMessages.MoveMembersRefactoring_accessed_method,
							new String[]{JavaElementUtil.createMethodSignature((IMethod)member),
						getQualifiedTypeLabel(accessingType)});

			return message;
		}
		case IJavaElement.TYPE:{
			if (moved)
				message= Messages.format(RefactoringCoreMessages.MoveMembersRefactoring_moved_type,
							new String[]{getQualifiedTypeLabel((IType)member),
						getQualifiedTypeLabel(accessingType),
						getQualifiedTypeLabel(declaringType)});
			else
				message= Messages.format(RefactoringCoreMessages.MoveMembersRefactoring_accessed_type,
							new String[]{ getQualifiedTypeLabel((IType)member), getQualifiedTypeLabel(accessingType)});
			return message;
		}
		default:
			Assert.isTrue(false);
			return null;
	}
}
 
Example 18
Source File: JdtFlags.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean isAnonymousType(IMember member) throws JavaModelException {
	return member.getElementType() == IJavaElement.TYPE &&
			((IType)member).isAnonymous();
}
 
Example 19
Source File: RefactoringAvailabilityTester.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public static boolean isExtractSupertypeAvailable(IMember member) throws JavaModelException {
	if (!member.exists()) {
		return false;
	}
	final int type = member.getElementType();
	if (type != IJavaElement.METHOD && type != IJavaElement.FIELD && type != IJavaElement.TYPE) {
		return false;
	}
	if (JdtFlags.isEnum(member) && type != IJavaElement.TYPE) {
		return false;
	}
	if (!Checks.isAvailable(member)) {
		return false;
	}
	if (member instanceof IMethod) {
		final IMethod method = (IMethod) member;
		if (method.isConstructor()) {
			return false;
		}
		if (JdtFlags.isNative(method)) {
			return false;
		}
		member = method.getDeclaringType();
	} else if (member instanceof IField) {
		member = member.getDeclaringType();
	}
	if (member instanceof IType) {
		if (JdtFlags.isEnum(member) || JdtFlags.isAnnotation(member)) {
			return false;
		}
		if (member.getDeclaringType() != null && !JdtFlags.isStatic(member)) {
			return false;
		}
		if (((IType) member).isAnonymous()) {
			return false; // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=253727
		}
		if (((IType) member).isLambda()) {
			return false;
		}
	}
	return true;
}
 
Example 20
Source File: JdtFlags.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static boolean isEnumConstant(IMember member) throws JavaModelException {
	return member.getElementType() == IJavaElement.FIELD && isEnum(member);
}