Java Code Examples for org.eclipse.jdt.core.IJavaElement#getAncestor()

The following examples show how to use org.eclipse.jdt.core.IJavaElement#getAncestor() . 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: SearchUtils.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @param match the search match
 * @return the enclosing {@link ICompilationUnit} of the given match, or null iff none
 */
public static ICompilationUnit getCompilationUnit(SearchMatch match) {
	IJavaElement enclosingElement = getEnclosingJavaElement(match);
	if (enclosingElement != null){
		if (enclosingElement instanceof ICompilationUnit)
			return (ICompilationUnit) enclosingElement;
		ICompilationUnit cu= (ICompilationUnit) enclosingElement.getAncestor(IJavaElement.COMPILATION_UNIT);
		if (cu != null)
			return cu;
	}

	IJavaElement jElement= JavaCore.create(match.getResource());
	if (jElement != null && jElement.exists() && jElement.getElementType() == IJavaElement.COMPILATION_UNIT)
		return (ICompilationUnit) jElement;
	return null;
}
 
Example 2
Source File: JavaCodeFormatterImpl.java    From jenerate with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Examines a string and returns the first line delimiter found.
 */
private static String getLineDelimiterUsed(IJavaElement elem) throws JavaModelException {
    ICompilationUnit cu = (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT);
    if (cu != null && cu.exists()) {
        IBuffer buf = cu.getBuffer();
        int length = buf.getLength();
        for (int i = 0; i < length; i++) {
            char ch = buf.getChar(i);
            if (ch == SWT.CR) {
                if (i + 1 < length) {
                    if (buf.getChar(i + 1) == SWT.LF) {
                        return "\r\n";
                    }
                }
                return "\r";
            } else if (ch == SWT.LF) {
                return "\n";
            }
        }
    }
    return System.getProperty("line.separator", "\n");
}
 
Example 3
Source File: JavaSearchScope.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public boolean encloses(IJavaElement element) {
	if (this.elements != null) {
		for (int i = 0, length = this.elements.size(); i < length; i++) {
			IJavaElement scopeElement = (IJavaElement)this.elements.get(i);
			IJavaElement searchedElement = element;
			while (searchedElement != null) {
				if (searchedElement.equals(scopeElement))
					return true;
				searchedElement = searchedElement.getParent();
			}
		}
		return false;
	}
	IPackageFragmentRoot root = (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
	if (root != null && root.isArchive()) {
		// external or internal jar
		IPath rootPath = root.getPath();
		String rootPathToString = rootPath.getDevice() == null ? rootPath.toString() : rootPath.toOSString();
		IPath relativePath = getPath(element, true/*relative path*/);
		return indexOf(rootPathToString, relativePath.toString()) >= 0;
	}
	// resource in workspace
	String fullResourcePathString = getPath(element, false/*full path*/).toString();
	return indexOf(fullResourcePathString) >= 0;
}
 
Example 4
Source File: NavigateToDefinitionHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static Location computeDefinitionNavigation(IJavaElement element, IJavaProject javaProject) throws JavaModelException {
	if (element == null) {
		return null;
	}

	ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
	IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE);
	if (compilationUnit != null || (cf != null && cf.getSourceRange() != null)) {
		return fixLocation(element, JDTUtils.toLocation(element), javaProject);
	}

	if (element instanceof IMember && ((IMember) element).getClassFile() != null) {
		return fixLocation(element, JDTUtils.toLocation(((IMember) element).getClassFile()), javaProject);
	}

	return null;
}
 
Example 5
Source File: AbstractJavaSearchResult.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public IFile getFile(Object element) {
	if (element instanceof IJavaElement) {
		IJavaElement javaElement= (IJavaElement) element;
		ICompilationUnit cu= (ICompilationUnit) javaElement.getAncestor(IJavaElement.COMPILATION_UNIT);
		if (cu != null) {
			return (IFile) cu.getResource();
		} else {
			IClassFile cf= (IClassFile) javaElement.getAncestor(IJavaElement.CLASS_FILE);
			if (cf != null)
				return (IFile) cf.getResource();
		}
		return null;
	}
	if (element instanceof IFile)
		return (IFile) element;
	return null;
}
 
Example 6
Source File: ReorgCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void apply(IDocument document) {
	Map<Object, Object> data= null;
	if (fReferencedType != null) {
		IJavaElement elem= fReferencedType.getJavaElement();
		if (elem != null) {
			IPackageFragmentRoot root= (IPackageFragmentRoot) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
			if (root != null) {
				try {
					IClasspathEntry entry= root.getRawClasspathEntry();
					if (entry != null) {
						data= new HashMap<Object, Object>(1);
						data.put(BuildPathsPropertyPage.DATA_REVEAL_ENTRY, entry);
						if (entry.getEntryKind() != IClasspathEntry.CPE_CONTAINER) {
							data.put(BuildPathsPropertyPage.DATA_REVEAL_ATTRIBUTE_KEY, CPListElement.ACCESSRULES);
						}
					}
				} catch (JavaModelException e) {
					// ignore
				}
			}
		}
	}
	PreferencesUtil.createPropertyDialogOn(JavaPlugin.getActiveWorkbenchShell(), fProject, BuildPathsPropertyPage.PROP_ID, null, data).open();
}
 
Example 7
Source File: JdtUtils.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @param javaElement
 * @return first non-anonymous ancestor
 */
private static IJavaElement getFirstNonAnonymous(IJavaElement javaElement, IJavaElement topAncestor) {
    if (javaElement.getElementType() == IJavaElement.TYPE && !isAnonymousType(javaElement)) {
        return javaElement;
    }
    IJavaElement parent = javaElement.getParent();
    if (parent == null) {
        return topAncestor;
    }
    IJavaElement ancestor = parent.getAncestor(IJavaElement.TYPE);
    if (ancestor != null) {
        return getFirstNonAnonymous(ancestor, topAncestor);
    }
    return topAncestor;
}
 
Example 8
Source File: JavaProjectModulesManager.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param dontSearchInit: not applicable for this method (ignored)
 * @return the module that corresponds to the passed name.
 */
@Override
public IModule getModuleInDirectManager(String name, IPythonNature nature, boolean dontSearchInit) {
    if (DEBUG_GET_MODULE) {
        System.out.println("Trying to get module in java project modules manager: " + name);
    }
    if (name.startsWith(".")) { //this happens when looking for a relative import
        return null;
    }
    try {
        IJavaElement javaElement = this.javaProject.findType(name);
        if (javaElement == null) {
            javaElement = this.javaProject.findElement(new Path(name.replace('.', '/')));
        }

        if (DEBUG_GET_MODULE) {
            System.out.println("Found: " + javaElement);
        }

        if (javaElement != null) {

            //now, there's a catch here, we'll find any class in the project classpath, even if it's in the
            //global classpath (e.g.: rt.jar), and this shouldn't be treated in this project modules manager
            //(that's treated in the Jython system manager)
            IJavaElement ancestor = javaElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
            if (ancestor instanceof IPackageFragmentRoot) {
                IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) ancestor;
                IClasspathEntry rawClasspathEntry = packageFragmentRoot.getRawClasspathEntry();
                if (rawClasspathEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                    return null;
                }
            }
            return new JavaModuleInProject(name, this.javaProject);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return null;
}
 
Example 9
Source File: JavaElementLinks.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public JavaElementLinkedLabelComposer(IJavaElement member, StringBuffer buf) {
	super(buf);
	if (member instanceof IPackageDeclaration) {
		fElement= member.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
	} else {
		fElement= member;
	}
}
 
Example 10
Source File: ReorgPolicyFactory.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static IType getEnclosingType(IJavaElement element) {
	if (element instanceof IType) {
		return (IType) element;
	}

	return (IType) element.getAncestor(IJavaElement.TYPE);
}
 
Example 11
Source File: ReorgPolicyFactory.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
protected static final ICompilationUnit getEnclosingCompilationUnit(IJavaElement element) {
	if (element instanceof ICompilationUnit) {
		return (ICompilationUnit) element;
	}

	return (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
}
 
Example 12
Source File: PasteAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static IJavaElement getAsTypeOrCu(IJavaElement element) {
	//try to get type first
	if (element.getElementType() == IJavaElement.COMPILATION_UNIT || element.getElementType() == IJavaElement.TYPE)
		return element;
	IJavaElement ancestorType= element.getAncestor(IJavaElement.TYPE);
	if (ancestorType != null)
		return ancestorType;
	return ReorgUtils.getCompilationUnit(element);
}
 
Example 13
Source File: SelectionConverter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static IType getTypeAtOffset(JavaEditor editor) throws JavaModelException {
	IJavaElement element= SelectionConverter.getElementAtOffset(editor);
	IType type= (IType)element.getAncestor(IJavaElement.TYPE);
	if (type == null) {
		ICompilationUnit unit= SelectionConverter.getInputAsCompilationUnit(editor);
		if (unit != null)
			type= unit.findPrimaryType();
	}
	return type;
}
 
Example 14
Source File: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static IType getDeclaringType(IJavaElement element) {
	if (element == null)
		return null;
	if (!(element instanceof IType))
		element= element.getAncestor(IJavaElement.TYPE);
	return (IType) element;
}
 
Example 15
Source File: JavaTaskListAdapter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public IResource getAffectedResource(IAdaptable element) {
IJavaElement java = (IJavaElement) element;
IResource resource= java.getResource();
if (resource != null)
	return resource;

ICompilationUnit cu= (ICompilationUnit) java.getAncestor(IJavaElement.COMPILATION_UNIT);
if (cu != null) {
	return cu.getPrimary().getResource();
}
return null;
}
 
Example 16
Source File: JavaModelUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static void addAllCus(HashSet<ICompilationUnit> collector, IJavaElement javaElement) throws JavaModelException {
	switch (javaElement.getElementType()) {
		case IJavaElement.JAVA_PROJECT:
			IJavaProject javaProject= (IJavaProject) javaElement;
			IPackageFragmentRoot[] packageFragmentRoots= javaProject.getPackageFragmentRoots();
			for (int i= 0; i < packageFragmentRoots.length; i++)
				addAllCus(collector, packageFragmentRoots[i]);
			return;

		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			IPackageFragmentRoot packageFragmentRoot= (IPackageFragmentRoot) javaElement;
			if (packageFragmentRoot.getKind() != IPackageFragmentRoot.K_SOURCE)
				return;
			IJavaElement[] packageFragments= packageFragmentRoot.getChildren();
			for (int j= 0; j < packageFragments.length; j++)
				addAllCus(collector, packageFragments[j]);
			return;

		case IJavaElement.PACKAGE_FRAGMENT:
			IPackageFragment packageFragment= (IPackageFragment) javaElement;
			collector.addAll(Arrays.asList(packageFragment.getCompilationUnits()));
			return;

		case IJavaElement.COMPILATION_UNIT:
			collector.add((ICompilationUnit) javaElement);
			return;

		default:
			IJavaElement cu= javaElement.getAncestor(IJavaElement.COMPILATION_UNIT);
			if (cu != null)
				collector.add((ICompilationUnit) cu);
	}
}
 
Example 17
Source File: RefactoringAvailabilityTester.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static IType getDeclaringType(IJavaElement element) {
	if (element == null) {
		return null;
	}
	if (!(element instanceof IType)) {
		element = element.getAncestor(IJavaElement.TYPE);
	}
	return (IType) element;
}
 
Example 18
Source File: RefactoringActions.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static IType convertToEnclosingOrPrimaryType(IJavaElement element) throws JavaModelException {
	if (element instanceof IType)
		return (IType)element;
	IType result= (IType)element.getAncestor(IJavaElement.TYPE);
	if (result != null)
		return result;
	if (element instanceof ICompilationUnit)
		return ((ICompilationUnit)element).findPrimaryType();
	if (element instanceof IClassFile)
		return ((IClassFile)element).getType();
	return null;
}
 
Example 19
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected static final ICompilationUnit getEnclosingCompilationUnit(IJavaElement element) {
	if (element instanceof ICompilationUnit)
		return (ICompilationUnit) element;

	return (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
}
 
Example 20
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static IType getEnclosingType(IJavaElement element) {
	if (element instanceof IType)
		return (IType) element;

	return (IType) element.getAncestor(IJavaElement.TYPE);
}