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

The following examples show how to use org.eclipse.jdt.core.IType#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: JavaElementUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static boolean isMainType(IType type) throws JavaModelException{
	if (! type.exists()) {
		return false;
	}

	if (type.isBinary()) {
		return false;
	}

	if (type.getCompilationUnit() == null) {
		return false;
	}

	if (type.getDeclaringType() != null) {
		return false;
	}

	return isPrimaryType(type) || isCuOnlyType(type);
}
 
Example 2
Source File: RenameFieldProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private RefactoringStatus checkEnclosingHierarchy() {
	IType current= fField.getDeclaringType();
	if (Checks.isTopLevel(current)) {
		return null;
	}
	RefactoringStatus result= new RefactoringStatus();
	while (current != null){
		IField otherField= current.getField(getNewElementName());
		if (otherField.exists()){
			String msg= Messages.format(RefactoringCoreMessages.RenameFieldRefactoring_hiding2,
			 	new String[]{ BasicElementLabels.getJavaElementName(getNewElementName()), BasicElementLabels.getJavaElementName(current.getFullyQualifiedName('.')), BasicElementLabels.getJavaElementName(otherField.getElementName())});
			result.addWarning(msg, JavaStatusContext.create(otherField));
		}
		current= current.getDeclaringType();
	}
	return result;
}
 
Example 3
Source File: JavaBrowsingContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Object[] getPackageContents(IPackageFragment fragment) throws JavaModelException {
	ISourceReference[] sourceRefs;
	if (fragment.getKind() == IPackageFragmentRoot.K_SOURCE) {
		sourceRefs= fragment.getCompilationUnits();
	}
	else {
		IClassFile[] classFiles= fragment.getClassFiles();
		List<IClassFile> topLevelClassFile= new ArrayList<IClassFile>();
		for (int i= 0; i < classFiles.length; i++) {
			IType type= classFiles[i].getType();
			if (type != null && type.getDeclaringType() == null && !type.isAnonymous() && !type.isLocal())
				topLevelClassFile.add(classFiles[i]);
		}
		sourceRefs= topLevelClassFile.toArray(new ISourceReference[topLevelClassFile.size()]);
	}

	Object[] result= new Object[0];
	for (int i= 0; i < sourceRefs.length; i++)
		result= concatenate(result, removeImportAndPackageDeclarations(getChildren(sourceRefs[i])));
	return concatenate(result, fragment.getNonJavaResources());
}
 
Example 4
Source File: ReorgPolicyFactory.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public IJavaElement[] getActualJavaElementsToReorg() throws JavaModelException {
	List<IJavaElement> result= new ArrayList<>();
	for (int i= 0; i < fJavaElements.length; i++) {
		IJavaElement element= fJavaElements[i];
		if (element == null) {
			continue;
		}
		if (element instanceof IType) {
			IType type= (IType) element;
			ICompilationUnit cu= type.getCompilationUnit();
			if (cu != null && type.getDeclaringType() == null && cu.exists() && cu.getTypes().length == 1 && !result.contains(cu)) {
				result.add(cu);
			} else if (!result.contains(type)) {
				result.add(type);
			}
		} else if (!result.contains(element)) {
			result.add(element);
		}
	}
	return result.toArray(new IJavaElement[result.size()]);
}
 
Example 5
Source File: JavaOutlineInformationControl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Object[] getChildren(Object element) {
	if (fShowOnlyMainType) {
		if (element instanceof ITypeRoot) {
			element= ((ITypeRoot)element).findPrimaryType();
		}

		if (element == null)
			return NO_CHILDREN;
	}

	if (fShowInheritedMembers && element instanceof IType) {
		IType type= (IType)element;
		if (type.getDeclaringType() == null || type.equals(fInitiallySelectedType)) {
			ITypeHierarchy th= getSuperTypeHierarchy(type);
			if (th != null) {
				List<Object> children= new ArrayList<Object>();
				IType[] superClasses= th.getAllSupertypes(type);
				children.addAll(Arrays.asList(super.getChildren(type)));
				for (int i= 0, scLength= superClasses.length; i < scLength; i++)
					children.addAll(Arrays.asList(super.getChildren(superClasses[i])));
				return children.toArray();
			}
		}
	}
	return super.getChildren(element);
}
 
Example 6
Source File: TypesView.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Answers if the given <code>element</code> is a valid
 * element for this part.
 *
 * @param 	element	the object to test
 * @return	<true> if the given element is a valid element
 */
@Override
protected boolean isValidElement(Object element) {
	if (element instanceof ICompilationUnit)
		return super.isValidElement(((ICompilationUnit)element).getParent());
	else if (element instanceof IType) {
		IType type= (IType)element;
		return type.getDeclaringType() == null && isValidElement(type.getCompilationUnit());
	}
	return false;
}
 
Example 7
Source File: MemberCheckUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean typeNameExistsInEnclosingTypeChain(IType type, String typeName){
	IType enclosing= type.getDeclaringType();
	while (enclosing != null){
		if (enclosing.getElementName().equals(typeName))
			return true;
		enclosing= enclosing.getDeclaringType();
	}
	return false;
}
 
Example 8
Source File: MembersView.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Answers if the given <code>element</code> is a valid
 * input for this part.
 *
 * @param 	element	the object to test
 * @return	<true> if the given element is a valid input
 */
@Override
protected boolean isValidInput(Object element) {
	if (element instanceof IType) {
		IType type= (IType)element;
		return type.isBinary() || type.getDeclaringType() == null;
	}
	return false;
}
 
Example 9
Source File: MoveStaticMembersProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean isVisibleFrom(IType newMemberDeclaringType, IType accessingType) throws JavaModelException {
	int memberVisibility= JdtFlags.getVisibilityCode(newMemberDeclaringType);

	IType declaringType= newMemberDeclaringType.getDeclaringType();
	while (declaringType != null) { //get lowest visibility in all parent types of newMemberDeclaringType
		memberVisibility= JdtFlags.getLowerVisibility(
				memberVisibility, JdtFlags.getVisibilityCode(declaringType));
		declaringType= declaringType.getDeclaringType();
	}

	switch (memberVisibility) {
		case Modifier.PRIVATE :
			return isEqualOrEnclosedType(accessingType, newMemberDeclaringType);

		case Modifier.NONE :
			return JavaModelUtil.isSamePackage(accessingType.getPackageFragment(), newMemberDeclaringType.getPackageFragment());

		case Modifier.PROTECTED :
			return JavaModelUtil.isSamePackage(accessingType.getPackageFragment(), newMemberDeclaringType.getPackageFragment())
					|| accessingType.newSupertypeHierarchy(null).contains(newMemberDeclaringType);

		case Modifier.PUBLIC :
			return true;

		default:
			Assert.isTrue(false);
			return false;
	}
}
 
Example 10
Source File: MatchLocator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public MethodBinding getMethodBinding(MethodPattern methodPattern) {
    MethodBinding methodBinding = getMethodBinding0(methodPattern);
    if (methodBinding != null)
    	return methodBinding; // known to be valid.
    // special handling for methods of anonymous/local types. Since these cannot be looked up in the environment the usual way ...
    if (methodPattern.focus instanceof SourceMethod) {
    	char[] typeName = PatternLocator.qualifiedPattern(methodPattern.declaringSimpleName, methodPattern.declaringQualification);
    	if (typeName != null && CharOperation.indexOf(IIndexConstants.ONE_STAR, typeName, true) >= 0) { // See org.eclipse.jdt.core.search.SearchPattern.enclosingTypeNames(IType)
    		IType type = methodPattern.declaringType;
    		IType enclosingType = type.getDeclaringType();
    		while (enclosingType != null) {
    			type = enclosingType;
    			enclosingType = type.getDeclaringType();
    		}
    		typeName = type.getFullyQualifiedName().toCharArray();
    		TypeBinding declaringTypeBinding = getType(typeName, typeName);
    		if (declaringTypeBinding instanceof SourceTypeBinding) {
    			SourceTypeBinding sourceTypeBinding = ((SourceTypeBinding) declaringTypeBinding);
    			ClassScope skope = sourceTypeBinding.scope;
    			if (skope != null) {
    				CompilationUnitDeclaration unit = skope.referenceCompilationUnit();
    				if (unit != null) {
    					AbstractMethodDeclaration amd = new ASTNodeFinder(unit).findMethod((IMethod) methodPattern.focus);
    					if (amd != null && amd.binding != null && amd.binding.isValidBinding()) {
    						this.bindings.put(methodPattern, amd.binding);
    						return amd.binding;
    					}
    				}
    			}
    		}
    	}
    }
	return null;
}
 
Example 11
Source File: MoveInnerToTopRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static List<IType> getDeclaringTypes(IType type) {
	IType declaringType= type.getDeclaringType();
	if (declaringType == null)
		return new ArrayList<IType>(0);
	List<IType> result= getDeclaringTypes(declaringType);
	result.add(declaringType);
	return result;
}
 
Example 12
Source File: RenameTypeProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static IType findEnclosingType(IType type, String newName) {
	IType enclosing= type.getDeclaringType();
	while (enclosing != null){
		if (newName.equals(enclosing.getElementName()))
			return enclosing;
		else
			enclosing= enclosing.getDeclaringType();
	}
	return null;
}
 
Example 13
Source File: UiBinderUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Gets the owner type for the given UiBinder subtype.
 *
 * @return the owner type
 * @throws UiBinderException if the given UiBinder subtype is not enclosed by
 *           an owner type
 */
public static IType getOwnerType(IType uiBinderSubtype)
    throws UiBinderException {
  IType ownerType = uiBinderSubtype.getDeclaringType();
  if (ownerType == null) {
    throw new UiBinderException(
        "The UiBinder subtype is not enclosed by an owner class.");
  }

  return ownerType;
}
 
Example 14
Source File: PatchFixesHider.java    From EasyMPermission with MIT License 5 votes vote down vote up
public static org.eclipse.jdt.core.dom.MethodDeclaration getRealMethodDeclarationNode(org.eclipse.jdt.core.IMethod sourceMethod, org.eclipse.jdt.core.dom.CompilationUnit cuUnit) throws JavaModelException {
	MethodDeclaration methodDeclarationNode = ASTNodeSearchUtil.getMethodDeclarationNode(sourceMethod, cuUnit);
	if (isGenerated(methodDeclarationNode)) {
		IType declaringType = sourceMethod.getDeclaringType();
		Stack<IType> typeStack = new Stack<IType>();
		while (declaringType != null) {
			typeStack.push(declaringType);
			declaringType = declaringType.getDeclaringType();
		}
		
		IType rootType = typeStack.pop();
		org.eclipse.jdt.core.dom.AbstractTypeDeclaration typeDeclaration = findTypeDeclaration(rootType, cuUnit.types());
		while (!typeStack.isEmpty() && typeDeclaration != null) {
			typeDeclaration = findTypeDeclaration(typeStack.pop(), typeDeclaration.bodyDeclarations());
		}
		
		if (typeStack.isEmpty() && typeDeclaration != null) {
			String methodName = sourceMethod.getElementName();
			for (Object declaration : typeDeclaration.bodyDeclarations()) {
				if (declaration instanceof org.eclipse.jdt.core.dom.MethodDeclaration) {
					org.eclipse.jdt.core.dom.MethodDeclaration methodDeclaration = (org.eclipse.jdt.core.dom.MethodDeclaration) declaration;
					if (methodDeclaration.getName().toString().equals(methodName)) {
						return methodDeclaration;
					}
				}
			}
		}
	}
	return methodDeclarationNode;
}
 
Example 15
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 16
Source File: JsniTypeReferenceChangeHelper.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
public boolean hasCompilationUnitPathChanged(ICompilationUnit cu) {
  IType oldType = jsniTypeReferenceChange.getRefactoringSupport().getOldType();
  return (oldType.getDeclaringType() == null && oldType.getCompilationUnit().getPath().equals(
      cu.getPath()));
}
 
Example 17
Source File: Checks.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static boolean isTopLevel(IType type){
	return type.getDeclaringType() == null;
}
 
Example 18
Source File: HierarchyLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private ImageDescriptor getTypeImageDescriptor(IType type) {
	ITypeHierarchy hierarchy= fHierarchy.getHierarchy();
	if (hierarchy == null) {
		return new JavaElementImageDescriptor(JavaPluginImages.DESC_OBJS_CLASS, 0, JavaElementImageProvider.BIG_SIZE);
	}

	int flags= hierarchy.getCachedFlags(type);
	if (flags == -1) {
		return new JavaElementImageDescriptor(JavaPluginImages.DESC_OBJS_CLASS, 0, JavaElementImageProvider.BIG_SIZE);
	}

	boolean isInterface= Flags.isInterface(flags);
	IType declaringType= type.getDeclaringType();
	boolean isInner= declaringType != null;
	boolean isInInterfaceOrAnnotation= false;
	if (isInner) {
		int declaringTypeFlags= hierarchy.getCachedFlags(declaringType);
		if (declaringTypeFlags != -1) {
			isInInterfaceOrAnnotation= Flags.isInterface(declaringTypeFlags);
		} else {
			// declaring type is not in hierarchy, so we have to pay the price for resolving here
			try {
				isInInterfaceOrAnnotation= declaringType.isInterface();
			} catch (JavaModelException e) {
			}
		}
	}

	ImageDescriptor desc= JavaElementImageProvider.getTypeImageDescriptor(isInner, isInInterfaceOrAnnotation, flags, isInDifferentHierarchyScope(type));

	int adornmentFlags= 0;
	if (Flags.isFinal(flags)) {
		adornmentFlags |= JavaElementImageDescriptor.FINAL;
	}
	if (Flags.isAbstract(flags) && !isInterface) {
		adornmentFlags |= JavaElementImageDescriptor.ABSTRACT;
	}
	if (Flags.isStatic(flags)) {
		adornmentFlags |= JavaElementImageDescriptor.STATIC;
	}
	if (Flags.isDeprecated(flags)) {
		adornmentFlags |= JavaElementImageDescriptor.DEPRECATED;
	}

	return new JavaElementImageDescriptor(desc, adornmentFlags, JavaElementImageProvider.BIG_SIZE);
}
 
Example 19
Source File: ChangeUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static boolean isPrimaryType(IType type) {
	String cuName = type.getCompilationUnit().getElementName();
	String typeName = type.getElementName();
	return type.getDeclaringType() == null && JavaCore.removeJavaLikeExtension(cuName).equals(typeName);
}
 
Example 20
Source File: DefaultJavaFoldingStructureProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Returns <code>true</code> if <code>type</code> is not a top-level type, <code>false</code> if it is.
 *
 * @param type the type to test
 * @return <code>true</code> if <code>type</code> is an inner type
 */
private boolean isInnerType(IType type) {
	return type.getDeclaringType() != null;
}