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

The following examples show how to use org.eclipse.jdt.core.IMember#getDeclaringType() . 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: MemberVisibilityAdjustor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void adjustOutgoingVisibilityChain(final IMember member, final IProgressMonitor monitor) throws JavaModelException {

		if (!Modifier.isPublic(member.getFlags())) {
			final ModifierKeyword threshold= computeOutgoingVisibilityThreshold(fReferencing, member, monitor);
			if (member instanceof IMethod) {
				adjustOutgoingVisibility(member, threshold, RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility_method_warning);
			} else if (member instanceof IField) {
				adjustOutgoingVisibility((IField) member, threshold);
			} else if (member instanceof IType) {
				adjustOutgoingVisibility(member, threshold, RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility_type_warning);
			}
		}

		if (member.getDeclaringType() != null)
			adjustOutgoingVisibilityChain(member.getDeclaringType(), monitor);
	}
 
Example 2
Source File: Implementors.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Searches for interfaces which are implemented by the declaring classes of the
 * specified Java elements. Currently, only IMethod instances are searched for.
 * Also, only the first element of the elements parameter is taken into
 * consideration.
 *
 * @param elements
 *
 * @return An array of found interfaces implemented by the declaring classes of the
 *         specified Java elements (currently only IMethod instances)
 */
public IJavaElement[] searchForInterfaces(IJavaElement[] elements,
    IProgressMonitor progressMonitor) {
    if ((elements != null) && (elements.length > 0)) {
        IJavaElement element = elements[0];

        if (element instanceof IMember) {
            IMember member = (IMember) element;
            IType type = member.getDeclaringType();

            IType[] implementingTypes = findInterfaces(type, progressMonitor);

            if (!progressMonitor.isCanceled()) {
                if (member.getElementType() == IJavaElement.METHOD) {
                    return findMethods((IMethod)member, implementingTypes, progressMonitor);
                } else {
                    return implementingTypes;
                }
            }
        }
    }

    return null;
}
 
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: RenameMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private IType[] searchForOuterTypesOfReferences(IMethod[] newNameMethods, IProgressMonitor pm) throws CoreException {
	final Set<IType> outerTypesOfReferences= new HashSet<IType>();
	SearchPattern pattern= RefactoringSearchEngine.createOrPattern(newNameMethods, IJavaSearchConstants.REFERENCES);
	IJavaSearchScope scope= createRefactoringScope(getMethod());
	SearchRequestor requestor= new SearchRequestor() {
		@Override
		public void acceptSearchMatch(SearchMatch match) throws CoreException {
			Object element= match.getElement();
			if (!(element instanceof IMember))
				return; // e.g. an IImportDeclaration for a static method import
			IMember member= (IMember) element;
			IType declaring= member.getDeclaringType();
			if (declaring == null)
				return;
			IType outer= declaring.getDeclaringType();
			if (outer != null)
				outerTypesOfReferences.add(declaring);
		}
	};
	new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(),
			scope, requestor, pm);
	return outerTypesOfReferences.toArray(new IType[outerTypesOfReferences.size()]);
}
 
Example 6
Source File: JdtFlags.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isStatic(IMember member) throws JavaModelException {
	if (isNestedInterfaceOrAnnotation(member))
		return true;
	if (member.getElementType() != IJavaElement.METHOD
			&& isInterfaceOrAnnotationMember(member))
		return true;
	if (isEnum(member) && (member.getElementType() == IJavaElement.FIELD || member.getDeclaringType() != null))
		return true;
	return Flags.isStatic(member.getFlags());
}
 
Example 7
Source File: JavaElementImageProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean isInterfaceOrAnnotationFieldOrType(IMember element) throws JavaModelException {
	// always show the static symbol on interface fields and types
	if (element.getElementType() == IJavaElement.FIELD) {
		return JavaModelUtil.isInterfaceOrAnnotation(element.getDeclaringType());
	} else if (element.getElementType() == IJavaElement.TYPE && element.getDeclaringType() != null) {
		return JavaModelUtil.isInterfaceOrAnnotation(element.getDeclaringType());
	}
	return false;
}
 
Example 8
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 referenced body declaration.
 *
 * @param member the member where to adjust the visibility
 * @param threshold the visibility keyword representing the required visibility, or <code>null</code> for default visibility
 * @param template the message template to use
 * @throws JavaModelException if an error occurs
 */
private void adjustOutgoingVisibility(final IMember member, final ModifierKeyword threshold, final String template) throws JavaModelException {
	Assert.isTrue(!member.isBinary() && !member.isReadOnly());
	boolean adjust= true;
	final IType declaring= member.getDeclaringType();
	if (declaring != null && (JavaModelUtil.isInterfaceOrAnnotation(declaring)
			|| (member instanceof IField) && Flags.isEnum(member.getFlags()) 
			|| declaring.equals(fReferenced)))
		adjust= false;
	if (adjust && hasLowerVisibility(member.getFlags(), keywordToVisibility(threshold)) && needsVisibilityAdjustment(member, threshold))
		fAdjustments.put(member, new OutgoingMemberVisibilityAdjustment(member, threshold, RefactoringStatus.createStatus(fVisibilitySeverity, Messages.format(template, new String[] { JavaElementLabels.getTextLabel(member, JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.ALL_FULLY_QUALIFIED), getLabel(threshold)}), JavaStatusContext.create(member), null, RefactoringStatusEntry.NO_CODE, null)));
}
 
Example 9
Source File: TypeHierarchyContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public Object getParent(Object element) {
	if (element instanceof IMember) {
		IMember member= (IMember) element;
		if (member.getElementType() == IJavaElement.TYPE) {
			return getParentType((IType)member);
		}
		return member.getDeclaringType();
	}
	return null;
}
 
Example 10
Source File: PullUpRefactoringProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected boolean canBeAccessedFrom(final IMember member, final IType target, final ITypeHierarchy hierarchy) throws JavaModelException {
	if (super.canBeAccessedFrom(member, target, hierarchy)) {
		if (target.isInterface())
			return true;
		if (target.equals(member.getDeclaringType()))
			return true;
		if (target.equals(member))
			return true;
		if (member instanceof IMethod) {
			final IMethod method= (IMethod) member;
			final IMethod stub= target.getMethod(method.getElementName(), method.getParameterTypes());
			if (stub.exists())
				return true;
		}
		if (member.getDeclaringType() == null) {
			if (!(member instanceof IType))
				return false;
			if (JdtFlags.isPublic(member))
				return true;
			if (!JdtFlags.isPackageVisible(member))
				return false;
			if (JavaModelUtil.isSamePackage(((IType) member).getPackageFragment(), target.getPackageFragment()))
				return true;
			final IType type= member.getDeclaringType();
			if (type != null)
				return hierarchy.contains(type);
			return false;
		}
		final IType declaringType= member.getDeclaringType();
		if (!canBeAccessedFrom(declaringType, target, hierarchy))
			return false;
		if (declaringType.equals(getDeclaringType()))
			return false;
		return true;
	}
	return false;
}
 
Example 11
Source File: RenameMethodProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private IType[] searchForOuterTypesOfReferences(IMethod[] newNameMethods, IProgressMonitor pm) throws CoreException {
	final Set<IType> outerTypesOfReferences= new HashSet<>();
	SearchPattern pattern= RefactoringSearchEngine.createOrPattern(newNameMethods, IJavaSearchConstants.REFERENCES);
	IJavaSearchScope scope= createRefactoringScope(getMethod());
	SearchRequestor requestor= new SearchRequestor() {
		@Override
		public void acceptSearchMatch(SearchMatch match) throws CoreException {
			Object element= match.getElement();
			if (!(element instanceof IMember))
			 {
				return; // e.g. an IImportDeclaration for a static method import
			}
			IMember member= (IMember) element;
			IType declaring= member.getDeclaringType();
			if (declaring == null) {
				return;
			}
			IType outer= declaring.getDeclaringType();
			if (outer != null) {
				outerTypesOfReferences.add(declaring);
			}
		}
	};
	new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(),
			scope, requestor, pm);
	return outerTypesOfReferences.toArray(new IType[outerTypesOfReferences.size()]);
}
 
Example 12
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 13
Source File: PullUpMethodPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static Map<IType, HashSet<IMember>> createTypeToMemberSetMapping(final IMember[] members) {
	final Map<IType, HashSet<IMember>> typeToMemberSet= new HashMap<IType, HashSet<IMember>>();
	for (int i= 0; i < members.length; i++) {
		final IMember member= members[i];
		final IType type= member.getDeclaringType();
		if (!typeToMemberSet.containsKey(type))
			typeToMemberSet.put(type, new HashSet<IMember>());
		typeToMemberSet.get(type).add(member);
	}
	return typeToMemberSet;
}
 
Example 14
Source File: GWTRenameParticipant.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected boolean initialize(Object element) {
  if (element instanceof IType || element instanceof IMethod
      || element instanceof IField) {
    GWTRefactoringSupport support = getRefactoringSupport();

    IMember oldMember = (IMember) element;
    support.setOldElement(oldMember);

    try {
      /*
       * We can't trust our RenameArgument's getNewName() to always return the
       * correct new name. When the user sets the new name in the Rename
       * refactoring dialog, clicks Next to go to the Preview page, then
       * clicks Back and changes the new name, getNewName() returns the
       * original name, not the updated one. This appears to be a bug in the
       * JDT, which affects other built-in rename participants such as
       * BreakpointRenameTypeParticipant. It does not affect the core JDT
       * refactorings, though, since they are executed directly from
       * JavaRenameRefactoring and not as loaded rename participants.
       * 
       * The workaround here is to instead go directly to the refactoring
       * processor and query it for the new element, which will have the right
       * name.
       * 
       * TODO: file this as a JDT bug?
       */
      JavaRenameProcessor processor = getRenameProcessor();
      IJavaElement newElement = (IJavaElement) processor.getNewElement();
      IType declaringType = oldMember.getDeclaringType();
      String newElementName = newElement.getElementName();
      /*
       * Compute the new method by using the declaring type of the old method.
       * Otherwise when a RenameVirtualMethodProcessor instance is passed in,
       * we will end up looking at the topmost declaration of the method
       * instead of the one we actually want because
       * RenameVirtualMethodProcessor.getNewElement() actually points to the
       * topmost declaration.
       */
      if (element instanceof IField) {
        newElement = declaringType.getField(newElementName);
      } else if (element instanceof IMethod) {
        IMethod method = (IMethod) newElement;
        newElement = declaringType.getMethod(newElementName,
            method.getParameterTypes());
      } else {
        assert (element instanceof IType);
      }

      support.setNewElement(newElement);
      support.setUpdateReferences(getArguments().getUpdateReferences());

      return true;
    } catch (CoreException e) {
      GWTPluginLog.logError(e);
    }
  }

  return false;
}
 
Example 15
Source File: MemberFilter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private boolean isTopLevelType(IMember member) {
	IType parent= member.getDeclaringType();
	return parent == null;
}
 
Example 16
Source File: MemberFilter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private boolean isMemberInInterfaceOrAnnotation(IMember member) throws JavaModelException {
	IType parent= member.getDeclaringType();
	return parent != null && JavaModelUtil.isInterfaceOrAnnotation(parent);
}
 
Example 17
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 18
Source File: JdtFlags.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static boolean isNestedInterfaceOrAnnotation(IMember member) throws JavaModelException{
	return member.getElementType() == IJavaElement.TYPE &&
			member.getDeclaringType() != null &&
			((IType) member).isInterface();
}
 
Example 19
Source File: JdtFlags.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean isInterfaceOrAnnotationMember(IMember member) throws JavaModelException {
	return member.getDeclaringType() != null && JavaModelUtil.isInterfaceOrAnnotation(member.getDeclaringType());
}
 
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 isInterfaceOrAnnotationMember(IMember member) throws JavaModelException {
	return member.getDeclaringType() != null && member.getDeclaringType().isInterface();
}