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

The following examples show how to use org.eclipse.jdt.core.IMember#getFlags() . 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: MoveStaticMembersProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private boolean canMoveToInterface(IMember member, boolean is18OrHigher) throws JavaModelException {
	int flags= member.getFlags();
	switch (member.getElementType()) {
		case IJavaElement.FIELD:
			if (!(Flags.isStatic(flags) && Flags.isFinal(flags)))
				return false;
			if (Flags.isEnum(flags))
				return false;
			VariableDeclarationFragment declaration= ASTNodeSearchUtil.getFieldDeclarationFragmentNode((IField) member, fSource.getRoot());
			if (declaration != null)
				return declaration.getInitializer() != null;
			return false;
		case IJavaElement.TYPE: {
			IType type= (IType) member;
			if (type.isInterface() && !Checks.isTopLevel(type))
				return true;
			return Flags.isStatic(flags);
		}
		case IJavaElement.METHOD: {
			return is18OrHigher && Flags.isStatic(flags);
		}
		default:
			return false;
	}
}
 
Example 2
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 3
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 (possible from another package) is visible from
 * elements in a package.
 * @param member The member to test the visibility for
 * @param pack The package in 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 isVisible(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) || (declaringType != null && isInterfaceOrAnnotation(declaringType))) {
		return true;
	} else if (Flags.isPrivate(otherflags)) {
		return false;
	}

	IPackageFragment otherpack= (IPackageFragment) member.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
	return (pack != null && otherpack != null && isSamePackage(pack, otherpack));
}
 
Example 4
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 5
Source File: JdtFlags.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean isAbstract(IMember member) throws JavaModelException{
	int flags= member.getFlags();
	if (!member.isBinary() && isInterfaceOrAnnotationMethod(member)) {
		return !Flags.isPrivate(flags) && !Flags.isStatic(flags) && !Flags.isDefaultMethod(flags);
	}
	return Flags.isAbstract(flags);
}
 
Example 6
Source File: JdtUtils.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static boolean isStatic(IMember firstNonAnon) {
    int topFlags = 0;
    try {
        topFlags = firstNonAnon.getFlags();
    } catch (JavaModelException e) {
        FindbugsPlugin.getDefault().logException(e, "isStatic() failed");
    }
    return Flags.isStatic(topFlags);
}
 
Example 7
Source File: MemberVisibilityAdjustor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Adjusts the visibility of the specified member.
 *
 * @param element the "source" point from which to calculate the visibility
 * @param referencedMovedElement the moved element which may be adjusted in visibility
 * @param monitor the progress monitor to use
 * @throws JavaModelException if the visibility adjustment could not be computed
 */
private void adjustIncomingVisibility(final IJavaElement element, IMember referencedMovedElement, final IProgressMonitor monitor) throws JavaModelException {
	final ModifierKeyword threshold= getVisibilityThreshold(element, referencedMovedElement, monitor);
	int flags= referencedMovedElement.getFlags();
	IType declaring= referencedMovedElement.getDeclaringType();
	if (declaring != null && declaring.isInterface() || referencedMovedElement instanceof IField && Flags.isEnum(referencedMovedElement.getFlags()))
		return;
	if (hasLowerVisibility(flags, threshold == null ? Modifier.NONE : threshold.toFlagValue()) && needsVisibilityAdjustment(referencedMovedElement, threshold))
		fAdjustments.put(referencedMovedElement, new IncomingMemberVisibilityAdjustment(referencedMovedElement, threshold, RefactoringStatus.createStatus(fVisibilitySeverity, Messages.format(getMessage(referencedMovedElement), new String[] { getLabel(referencedMovedElement), getLabel(threshold)}), JavaStatusContext.create(referencedMovedElement), null, RefactoringStatusEntry.NO_CODE, null)));
}
 
Example 8
Source File: JdtFlags.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isAbstract(IMember member) throws JavaModelException{
	int flags= member.getFlags();
	if (!member.isBinary() && isInterfaceOrAnnotationMethod(member)) {
		return !Flags.isStatic(flags) && !Flags.isDefaultMethod(flags);
	}
	return Flags.isAbstract(flags);
}
 
Example 9
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 10
Source File: Jdt2Ecore.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies if the target feature is visible from the type.
 *
 * <p>The type finder could be obtained with {@link #toTypeFinder(IJavaProject)}.
 *
 * @param typeFinder the type finder to be used for finding the type definitions.
 * @param fromType the type from which the feature visibility is tested.
 * @param target the feature to test for the visibility.
 * @return <code>true</code> if the given type can see the target feature.
 * @throws JavaModelException if the Java model is invalid.
 * @see #toTypeFinder(IJavaProject)
 */
public boolean isVisible(TypeFinder typeFinder, IType fromType, IMember target) throws JavaModelException {
	final int flags = target.getFlags();
	if (Flags.isPublic(flags)) {
		return true;
	}
	final String fromTypeName = fromType.getFullyQualifiedName();
	final String memberType = target.getDeclaringType().getFullyQualifiedName();
	if (Flags.isPrivate(flags)) {
		return target.getDeclaringType().getFullyQualifiedName().equals(fromTypeName);
	}
	if (Flags.isProtected(flags)) {
		IType t = fromType;
		while (t != null) {
			if (memberType.equals(t.getFullyQualifiedName())) {
				return true;
			}
			final String typeName = t.getSuperclassName();
			if (Strings.isNullOrEmpty(typeName)) {
				t = null;
			} else {
				t = typeFinder.findType(typeName);
			}
		}
	}
	final IPackageFragment f1 = target.getDeclaringType().getPackageFragment();
	final IPackageFragment f2 = fromType.getPackageFragment();
	if (f1.isDefaultPackage()) {
		return f2.isDefaultPackage();
	}
	return f1.getElementName().equals(f2.getElementName());
}
 
Example 11
Source File: StubCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected void appendMembers(final IType type, final IProgressMonitor monitor) throws JavaModelException {
	try {
		monitor.beginTask(RefactoringCoreMessages.StubCreationOperation_creating_type_stubs, 1);
		final IJavaElement[] children= type.getChildren();
		for (int index= 0; index < children.length; index++) {
			final IMember child= (IMember) children[index];
			final int flags= child.getFlags();
			final boolean isPrivate= Flags.isPrivate(flags);
			final boolean isDefault= !Flags.isPublic(flags) && !Flags.isProtected(flags) && !isPrivate;
			final boolean stub= fStubInvisible || (!isPrivate && !isDefault);
			if (child instanceof IType) {
				if (stub)
					appendTypeDeclaration((IType) child, new SubProgressMonitor(monitor, 1));
			} else if (child instanceof IField) {
				if (stub && !Flags.isEnum(flags) && !Flags.isSynthetic(flags))
					appendFieldDeclaration((IField) child);
			} else if (child instanceof IMethod) {
				final IMethod method= (IMethod) child;
				final String name= method.getElementName();
				if (method.getDeclaringType().isEnum()) {
					final int count= method.getNumberOfParameters();
					if (count == 0 && "values".equals(name)) //$NON-NLS-1$
						continue;
					if (count == 1 && "valueOf".equals(name) && "Ljava.lang.String;".equals(method.getParameterTypes()[0])) //$NON-NLS-1$ //$NON-NLS-2$
						continue;
					if (method.isConstructor())
						continue;
				}
				boolean skip= !stub || name.equals("<clinit>"); //$NON-NLS-1$
				if (method.isConstructor())
					skip= false;
				skip= skip || Flags.isSynthetic(flags) || Flags.isBridge(flags);
				if (!skip)
					appendMethodDeclaration(method);
			}
			fBuffer.append("\n"); //$NON-NLS-1$
		}
	} finally {
		monitor.done();
	}
}
 
Example 12
Source File: JavaElementImageProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private int computeJavaAdornmentFlags(IJavaElement element, int renderFlags) {
	int flags= 0;
	if (showOverlayIcons(renderFlags)) {
		try {
			if (element instanceof IMember) {
				IMember member= (IMember)element;

				int modifiers= member.getFlags();
				if (confirmAbstract(member) && JdtFlags.isAbstract(member))
					flags|= JavaElementImageDescriptor.ABSTRACT;
				if (Flags.isFinal(modifiers) || isInterfaceOrAnnotationField(member) || isEnumConstant(member, modifiers))
					flags|= JavaElementImageDescriptor.FINAL;
				if (Flags.isStatic(modifiers) || isInterfaceOrAnnotationFieldOrType(member) || isEnumConstant(member, modifiers))
					flags|= JavaElementImageDescriptor.STATIC;

				if (Flags.isDeprecated(modifiers))
					flags|= JavaElementImageDescriptor.DEPRECATED;

				int elementType= element.getElementType();
				if (elementType == IJavaElement.METHOD) {
					if (((IMethod)element).isConstructor())
						flags|= JavaElementImageDescriptor.CONSTRUCTOR;
					if (Flags.isSynchronized(modifiers)) // collides with 'super' flag
						flags|= JavaElementImageDescriptor.SYNCHRONIZED;
					if (Flags.isNative(modifiers))
						flags|= JavaElementImageDescriptor.NATIVE;
					if (Flags.isDefaultMethod(modifiers))
						flags|= JavaElementImageDescriptor.DEFAULT_METHOD;
					if (Flags.isAnnnotationDefault(modifiers))
						flags|= JavaElementImageDescriptor.ANNOTATION_DEFAULT;
				}

				if (member.getElementType() == IJavaElement.TYPE) {
					if (JavaModelUtil.hasMainMethod((IType)member)) {
						flags|= JavaElementImageDescriptor.RUNNABLE;
					}
				}

				if (member.getElementType() == IJavaElement.FIELD) {
					if (Flags.isVolatile(modifiers))
						flags|= JavaElementImageDescriptor.VOLATILE;
					if (Flags.isTransient(modifiers))
						flags|= JavaElementImageDescriptor.TRANSIENT;
				}
			} else if (element instanceof ILocalVariable && Flags.isFinal(((ILocalVariable)element).getFlags())) {
				flags|= JavaElementImageDescriptor.FINAL;
			}
		} catch (JavaModelException e) {
			// do nothing. Can't compute runnable adornment or get flags
		}
	}
	return flags;
}