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

The following examples show how to use org.eclipse.jdt.core.IJavaElement#getParent() . 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: CompilationUnitChangeNode.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private JavaLanguageNode getChangeElement(Map<IJavaElement, JavaLanguageNode> map, IJavaElement element, List<ChildNode> children, TextEditChangeNode cunitChange) {
	JavaLanguageNode result= map.get(element);
	if (result != null)
		return result;
	IJavaElement parent= element.getParent();
	if (parent instanceof ICompilationUnit) {
		result= new JavaLanguageNode(cunitChange, element);
		children.add(result);
		map.put(element, result);
	} else {
		JavaLanguageNode parentChange= getChangeElement(map, parent, children, cunitChange);
		result= new JavaLanguageNode(parentChange, element);
		parentChange.addChild(result);
		map.put(element, result);
	}
	return result;
}
 
Example 2
Source File: Region.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Removes any children of this element that are contained within this
 * region as this parent is about to be added to the region.
 *
 * <p>Children are all children, not just direct children.
 */
protected void removeAllChildren(IJavaElement element) {
	if (element instanceof IParent) {
		ArrayList newRootElements = new ArrayList();
		for (int i = 0, size = this.rootElements.size(); i < size; i++) {
			IJavaElement currentRoot = (IJavaElement)this.rootElements.get(i);
			//walk the current root hierarchy
			IJavaElement parent = currentRoot.getParent();
			boolean isChild= false;
			while (parent != null) {
				if (parent.equals(element)) {
					isChild= true;
					break;
				}
				parent = parent.getParent();
			}
			if (!isChild) {
				newRootElements.add(currentRoot);
			}
		}
		this.rootElements= newRootElements;
	}
}
 
Example 3
Source File: BugInfoView.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
private boolean matchInput(IEditorInput input) {
    if (file != null && (input instanceof IFileEditorInput)) {
        return file.equals(((IFileEditorInput) input).getFile());
    }
    if (javaElt != null && input != null) {
        IJavaElement javaElement = JavaUI.getEditorInputJavaElement(input);
        if (javaElt.equals(javaElement)) {
            return true;
        }
        IJavaElement parent = javaElt.getParent();
        while (parent != null && !parent.equals(javaElement)) {
            parent = parent.getParent();
        }
        if (parent != null && parent.equals(javaElement)) {
            return true;
        }
    }
    return false;
}
 
Example 4
Source File: TraceForTypeRootProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected IPath getSourcePath(final ITypeRoot derivedJavaType) {
	IJavaElement current = derivedJavaType.getParent();
	while (current != null) {
		if (current instanceof IPackageFragmentRoot) {
			IPackageFragmentRoot fragmentRoot = (IPackageFragmentRoot) current;
			try {
				IPath attachmentPath = fragmentRoot.getSourceAttachmentPath();
				if (attachmentPath != null)
					return attachmentPath;
			} catch (JavaModelException e) {
			}
			if (current instanceof JarPackageFragmentRoot)
				return fragmentRoot.getPath();

		}
		current = current.getParent();
	}
	return null;
}
 
Example 5
Source File: JavaEditorBreadcrumb.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the input for the given element. The Java breadcrumb does not show some elements of
 * the model:
 * <ul>
 * 		<li><code>ITypeRoots</li>
 * 		<li><code>IPackageDeclaration</li>
 * 		<li><code>IImportContainer</li>
 * 		<li><code>IImportDeclaration</li>
 * </ul>
 *
 * @param element the potential input element
 * @return the element to use as input
 */
private IJavaElement getInput(IJavaElement element) {
	try {
		if (element instanceof IImportDeclaration)
			element= element.getParent();

		if (element instanceof IImportContainer)
			element= element.getParent();

		if (element instanceof IPackageDeclaration)
			element= element.getParent();

		if (element instanceof ICompilationUnit) {
			IType[] types= ((ICompilationUnit) element).getTypes();
			if (types.length > 0)
				element= types[0];
		}

		if (element instanceof IClassFile)
			element= ((IClassFile) element).getType();

		return element;
	} catch (JavaModelException e) {
		return null;
	}
}
 
Example 6
Source File: EditorUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static IEditorInput getEditorInput(IJavaElement element) {
	while (element != null) {
		if (element instanceof ICompilationUnit) {
			ICompilationUnit unit= ((ICompilationUnit) element).getPrimary();
				IResource resource= unit.getResource();
				if (resource instanceof IFile)
					return new FileEditorInput((IFile) resource);
		}

		if (element instanceof IClassFile)
			return new InternalClassFileEditorInput((IClassFile) element);

		element= element.getParent();
	}

	return null;
}
 
Example 7
Source File: JDTUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static ISourceRange getNameRange(IJavaElement element) throws JavaModelException {
	ISourceRange nameRange = null;
	if (element instanceof IMember) {
		IMember member = (IMember) element;
		nameRange = member.getNameRange();
		if ((!SourceRange.isAvailable(nameRange))) {
			nameRange = member.getSourceRange();
		}
	} else if (element instanceof ITypeParameter || element instanceof ILocalVariable) {
		nameRange = ((ISourceReference) element).getNameRange();
	} else if (element instanceof ISourceReference) {
		nameRange = ((ISourceReference) element).getSourceRange();
	}
	if (!SourceRange.isAvailable(nameRange) && element.getParent() != null) {
		nameRange = getNameRange(element.getParent());
	}
	return nameRange;
}
 
Example 8
Source File: SourceMapper.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the type with the given <code>typeName</code>.  Returns inner classes
 * as well.
 */
protected IType getType(String typeName) {
	if (typeName.length() == 0) {
		IJavaElement classFile = this.binaryType.getParent();
		String classFileName = classFile.getElementName();
		StringBuffer newClassFileName = new StringBuffer();
		int lastDollar = classFileName.lastIndexOf('$');
		for (int i = 0; i <= lastDollar; i++)
			newClassFileName.append(classFileName.charAt(i));
		newClassFileName.append(Integer.toString(this.anonymousCounter));
		PackageFragment pkg = (PackageFragment) classFile.getParent();
		return new BinaryType(new ClassFile(pkg, newClassFileName.toString()), typeName);
	} else if (this.binaryType.getElementName().equals(typeName))
		return this.binaryType;
	else
		return this.binaryType.getType(typeName);
}
 
Example 9
Source File: JavaEditorBreadcrumb.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Tells whether the given element is an ancestor of the given input.
 *
 * @param element the element which might be a parent
 * @param input the element to resolve the parent chain for
 * @return <code>true</code> if <code>element</code> is a parent of <code>input</code>
 */
private boolean isAncestor(IJavaElement element, IJavaElement input) {
	while (input != null && !input.equals(element)) {
		input= input.getParent();
	}

	return input != null;
}
 
Example 10
Source File: JavaStructureCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected String[] getPath(Object element, Object input) {
	if (element instanceof IJavaElement) {
		IJavaElement je = (IJavaElement) element;
		// build a path starting at the given Java element and walk
		// up the parent chain until we reach a IWorkingCopy or ICompilationUnit
		List<String> args= new ArrayList<String>();
		while (je != null) {
			// each path component has a name that uses the same
			// conventions as a JavaNode name
			String name= JavaCompareUtilities.getJavaElementID(je);
			if (name == null)
				return null;
			args.add(name);
			if (je instanceof ICompilationUnit)
				break;
			je= je.getParent();
		}

		// revert the path
		int n= args.size();
		String[] path= new String[n];
		for (int i= 0; i < n; i++)
			path[i]= args.get(n-1-i);

		return path;
	}
	return null;
}
 
Example 11
Source File: ReorgUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean containsElementOrParent(Set<IAdaptable> elements, IJavaElement element) {
	IJavaElement curr= element;
	do {
		if (elements.contains(curr)) {
			return true;
		}
		curr= curr.getParent();
	} while (curr != null);
	return false;
}
 
Example 12
Source File: MavenBuildSupport.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void discoverSource(IClassFile classFile, IProgressMonitor monitor) throws CoreException {
	if (classFile == null) {
		return;
	}
	IJavaElement element = classFile;
	while (element.getParent() != null) {
		element = element.getParent();
		if (element instanceof IPackageFragmentRoot) {
			final IPackageFragmentRoot fragment = (IPackageFragmentRoot) element;
			IPath attachmentPath = fragment.getSourceAttachmentPath();
			if (attachmentPath != null && !attachmentPath.isEmpty() && attachmentPath.toFile().exists()) {
				break;
			}
			if (fragment.isArchive()) {
				IPath path = fragment.getPath();
				Boolean downloaded = downloadRequestsCache.getIfPresent(path.toString());
				if (downloaded == null) {
					downloadRequestsCache.put(path.toString(), true);
					IClasspathManager buildpathManager = MavenJdtPlugin.getDefault().getBuildpathManager();
					buildpathManager.scheduleDownload(fragment, true, true);
					JobHelpers.waitForDownloadSourcesJobs(MAX_TIME_MILLIS);
				}
				break;
			}
		}
	}
}
 
Example 13
Source File: JavadocContentAccess2.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private JavadocContentAccess2(IJavaElement element, Javadoc javadoc, String source, JavadocLookup lookup) {
	Assert.isNotNull(element);
	Assert.isTrue(element instanceof IMethod || element instanceof ILocalVariable || element instanceof ITypeParameter);
	fElement = element;
	fMethod = (IMethod) ((element instanceof ILocalVariable || element instanceof ITypeParameter) ? element.getParent() : element);
	fJavadoc = javadoc;
	fSource = source;
	fJavadocLookup = lookup;
}
 
Example 14
Source File: JavaOutlinePage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Checks if element has an enclosing parent among other selected elements.
 * 
 * @param element the element to be checked for overlap against all elements
 * @param allElements the list of all elements
 * @return <code>true</code> if the element has a parent in the list of all elements
 */
private boolean isElementOverlapping(IJavaElement element, List<?> allElements) {
	element= element.getParent();
	while (element != null) {
		if (element instanceof ISourceReference) {
			if (allElements.contains(element))
				return true;
		} else {
			return false;
		}
		element= element.getParent();
	}
	return false;
}
 
Example 15
Source File: JdtUtils.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @param javaElement
 * @return null, if javaElement is top level class
 */
private static IType getFirstAncestor(IJavaElement javaElement) {
    IJavaElement parent = javaElement;
    if (javaElement.getElementType() == IJavaElement.TYPE) {
        parent = javaElement.getParent();
    }
    if (parent != null) {
        return (IType) parent.getAncestor(IJavaElement.TYPE);
    }
    return null;
}
 
Example 16
Source File: SourceMapper.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public int getFlags(IJavaElement element) {
	switch(element.getElementType()) {
		case IJavaElement.LOCAL_VARIABLE :
			LocalVariableElementKey key = new LocalVariableElementKey(element.getParent(), element.getElementName());
			if (this.finalParameters != null && this.finalParameters.contains(key)) {
				return Flags.AccFinal;
			}
	}
	return 0;
}
 
Example 17
Source File: MembersView.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Finds the closest Java element which can be used as input for
 * this part and has the given Java element as child.
 *
 * @param 	je 	the Java element for which to search the closest input
 * @return	the closest Java element used as input for this part, or <code>null</code>
 */
@Override
protected IJavaElement findInputForJavaElement(IJavaElement je) {
	if (je == null || !je.exists() || (je.getJavaProject() != null && !je.getJavaProject().isOnClasspath(je)))
		return null;

	switch (je.getElementType()) {
		case IJavaElement.TYPE:
			IType type= ((IType)je).getDeclaringType();
			if (type == null)
				return je;
			else
				return findInputForJavaElement(type);
		case IJavaElement.COMPILATION_UNIT:
			return getTypeForCU((ICompilationUnit)je);
		case IJavaElement.CLASS_FILE:
			return findInputForJavaElement(((IClassFile)je).getType());
		case IJavaElement.IMPORT_DECLARATION:
			return findInputForJavaElement(je.getParent());
		case IJavaElement.PACKAGE_DECLARATION:
		case IJavaElement.IMPORT_CONTAINER:
			IJavaElement parent= je.getParent();
			if (parent instanceof ICompilationUnit) {
				return getTypeForCU((ICompilationUnit)parent);
			}
			else if (parent instanceof IClassFile)
				return findInputForJavaElement(parent);
			return null;
		default:
			if (je instanceof IMember)
				return findInputForJavaElement(((IMember)je).getDeclaringType());
	}
	return null;
}
 
Example 18
Source File: JdtUtils.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @param elt
 * @return true, if given element is inner class from initializer block or
 *         method body
 */
private static boolean isAnyParentLocal(IJavaElement elt, IJavaElement topParent) {
    if (isLocal(elt)) {
        return true;
    }
    IJavaElement parent = elt.getParent();
    while (parent != null && parent != topParent) {
        if (isLocal(parent)) {
            return true;
        }
        parent = parent.getParent();
    }
    return false;
}
 
Example 19
Source File: JavaElementAdapterFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IResource getResource(IJavaElement element) {
// can't use IJavaElement.getResource directly as we are interested in the
// corresponding resource
switch (element.getElementType()) {
	case IJavaElement.TYPE:
		// top level types behave like the CU
		IJavaElement parent= element.getParent();
		if (parent instanceof ICompilationUnit) {
			return ((ICompilationUnit) parent).getPrimary().getResource();
		}
		return null;
	case IJavaElement.COMPILATION_UNIT:
		return ((ICompilationUnit) element).getPrimary().getResource();
	case IJavaElement.CLASS_FILE:
	case IJavaElement.PACKAGE_FRAGMENT:
		// test if in a archive
		IPackageFragmentRoot root= (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
		if (!root.isArchive() && !root.isExternal()) {
			return element.getResource();
		}
		return null;
	case IJavaElement.PACKAGE_FRAGMENT_ROOT:
	case IJavaElement.JAVA_PROJECT:
	case IJavaElement.JAVA_MODEL:
		return element.getResource();
	default:
		return null;
}
  }
 
Example 20
Source File: JDClassFileEditor.java    From jd-eclipse with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("rawtypes")
protected void setupSourceMapper(IClassFile classFile) {
	try {
		// Search package fragment root and classPath
		IJavaElement packageFragment = classFile.getParent();
		IJavaElement packageFragmentRoot = packageFragment.getParent();

		if (packageFragmentRoot instanceof PackageFragmentRoot) {
			// Setup a new source mapper.
			PackageFragmentRoot root = (PackageFragmentRoot)packageFragmentRoot;				
				
			// Location of the archive file containing classes.
			IPath basePath = root.getPath();
			File baseFile = basePath.makeAbsolute().toFile();	
			
			if (!baseFile.exists()) {
				IResource resource = root.getCorrespondingResource();
				basePath = resource.getLocation();
				baseFile = basePath.makeAbsolute().toFile();
			}
			
			// Class path
			String classPath = classFile.getElementName();
			String packageName = packageFragment.getElementName();
			if ((packageName != null) && (packageName.length() > 0)) {
				classPath = packageName.replace('.', '/') + '/' + classPath;
			}
			
			// Location of the archive file containing source.
			IPath sourcePath = root.getSourceAttachmentPath();
			if (sourcePath == null) {
				sourcePath = basePath;
			}
			
			// Location of the package fragment root within the zip 
			// (empty specifies the default root).
			IPath sourceAttachmentRootPath = root.getSourceAttachmentRootPath();
			String sourceRootPath;
			
			if (sourceAttachmentRootPath == null) {
				sourceRootPath = null;
			} else {
				sourceRootPath = sourceAttachmentRootPath.toString();
				if ((sourceRootPath != null) && (sourceRootPath.length() == 0))
					sourceRootPath = null;
			}
			
			// Options
			Map options = root.getJavaProject().getOptions(true);
			
			root.setSourceMapper(new JDSourceMapper(baseFile, sourcePath, sourceRootPath, options));				
		}		
	} catch (CoreException e) {
		JavaDecompilerPlugin.getDefault().getLog().log(new Status(
			Status.ERROR, JavaDecompilerPlugin.PLUGIN_ID, 
			0, e.getMessage(), e));
	}
}