List of usage examples for org.eclipse.jdt.core IJavaElement getParent
IJavaElement getParent();
null
if this element has no parent. From source file:org.eclipse.jem.internal.beaninfo.adapters.BeaninfoModelSynchronizer.java
License:Open Source License
protected String getFullNameFromElement(IJavaElement element) { String name = element.getElementName(); if (!(element instanceof ICompilationUnit || element instanceof IClassFile)) return name; // Shouldn't be here // remove extension. int periodNdx = name.lastIndexOf('.'); if (periodNdx == -1) return name; // Shouldn't be here. There should be an extension String typeName = null;/*from w ww.j a v a 2 s. c o m*/ String parentName = element.getParent().getElementName(); if (parentName == null || parentName.length() == 0) typeName = name.substring(0, periodNdx); // In default package else typeName = parentName + "." + name.substring(0, periodNdx); //$NON-NLS-1$ return typeName; }
From source file:org.eclipse.jpt.jaxb.ui.internal.wizards.schemagen.SchemaGeneratorWizard.java
License:Open Source License
private String[] buildSourceClassNames(Object[] checkedElements) { ArrayList<String> classNames = new ArrayList<String>(); for (Object element : checkedElements) { IJavaElement javaElement = (IJavaElement) element; String packageName = javaElement.getParent().getElementName(); String elementName = javaElement.getElementName(); String className = FileTools.stripExtension(elementName); if (StringTools.isBlank(packageName)) { classNames.add(className);//from w ww . j av a 2s .c o m } else { classNames.add(packageName + '.' + className); } } return ArrayTools.array(classNames, new String[0]); }
From source file:org.eclipse.jst.common.jdt.internal.integration.ui.WTPUIWorkingCopyManager.java
License:Open Source License
protected IEditorInput getEditorInput(IJavaElement element) throws JavaModelException { IJavaElement localElement = element; while (localElement != null) { switch (localElement.getElementType()) { case IJavaElement.COMPILATION_UNIT: { ICompilationUnit cu = (ICompilationUnit) localElement; if (cu.isWorkingCopy()) cu = cu.getPrimary();/* w ww .j av a 2 s . c o m*/ IResource resource = cu.getUnderlyingResource(); if (resource.getType() == IResource.FILE) return new FileEditorInput((IFile) resource); break; } case IJavaElement.CLASS_FILE: return new InternalClassFileEditorInput((IClassFile) localElement); } localElement = localElement.getParent(); } return null; }
From source file:org.eclipse.jst.common.jdt.internal.integration.WTPWorkingCopyManager.java
License:Open Source License
/** * Return the IPackageFragment for the given ICompilationUnit. *//*from w ww . j a v a 2 s. c om*/ protected IPackageFragment getPackageFragment(ICompilationUnit cu) { if (cu == null) return null; IJavaElement parent = cu; int elementType = cu.getElementType(); while (parent != null && elementType != IJavaElement.PACKAGE_FRAGMENT) { parent = parent.getParent(); if (parent != null) elementType = parent.getElementType(); else elementType = -1; } return (IPackageFragment) parent; }
From source file:org.eclipse.jst.j2ee.internal.dialogs.TypeJavaSearchScope.java
License:Open Source License
public void add(IJavaElement element) throws JavaModelException { IPackageFragmentRoot root = null;/*from w ww. j a v a 2 s .c om*/ switch (element.getElementType()) { case IJavaElement.JAVA_MODEL: // a workspace sope should be used break; case IJavaElement.JAVA_PROJECT: this.add((IJavaProject) element, true, new HashSet(2)); break; case IJavaElement.PACKAGE_FRAGMENT_ROOT: root = (IPackageFragmentRoot) element; this.add(root.getPath(), true); break; case IJavaElement.PACKAGE_FRAGMENT: root = (IPackageFragmentRoot) element.getParent(); if (root.isArchive()) { this.add(root.getPath().append(new Path(element.getElementName().replace('.', '/'))), false); } else { IResource resource = element.getUnderlyingResource(); if (resource != null && resource.isAccessible()) { this.add(resource.getFullPath(), false); } } break; default: // remember sub-cu (or sub-class file) java elements if (element instanceof IMember) { if (this.elements == null) { this.elements = new ArrayList(); } this.elements.add(element); } this.add(this.fullPath(element), true); // find package fragment root including this java element IJavaElement parent = element.getParent(); while (parent != null && !(parent instanceof IPackageFragmentRoot)) { parent = parent.getParent(); } if (parent instanceof IPackageFragmentRoot) { root = (IPackageFragmentRoot) parent; } } if (root != null) { if (root.getKind() == IPackageFragmentRoot.K_BINARY) { this.addEnclosingProjectOrJar(root.getPath()); } else { this.addEnclosingProjectOrJar(root.getJavaProject().getProject().getFullPath()); } } }
From source file:org.eclipse.jst.j2ee.internal.dialogs.TypeJavaSearchScope.java
License:Open Source License
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; }/*from ww w . j a va2s . co m*/ searchedElement = searchedElement.getParent(); } } return false; } return this.encloses(this.fullPath(element)); }
From source file:org.eclipse.jst.j2ee.internal.dialogs.TypeJavaSearchScope.java
License:Open Source License
private IPath fullPath(IJavaElement element) { if (element instanceof IPackageFragmentRoot) { return ((IPackageFragmentRoot) element).getPath(); }/* w ww .j a v a 2 s . c o m*/ IJavaElement parent = element.getParent(); IPath parentPath = parent == null ? null : this.fullPath(parent); IPath childPath; if (element instanceof IPackageFragment) { childPath = new Path(element.getElementName().replace('.', '/')); } else if (element instanceof IOpenable) { childPath = new Path(element.getElementName()); } else { return parentPath; } return parentPath == null ? childPath : parentPath.append(childPath); }
From source file:org.eclipse.jst.jsf.common.ui.internal.utils.JavaModelUtil.java
License:Open Source License
/** * Returns the parent of the supplied java element that conforms to the * given parent type or <code>null</code>, if such a parent doesn't exit. * //from w w w . jav a 2 s.c o m * @deprecated Use element.getParent().getAncestor(kind); */ private static IJavaElement findParentOfKind(IJavaElement element, int kind) { if (element != null && element.getParent() != null) { return element.getParent().getAncestor(kind); } return null; }
From source file:org.eclipse.linuxtools.changelog.parsers.java.JavaParser.java
License:Open Source License
/** * @see IParserChangeLogContrib#parseCurrentFunction(IEditorPart) *///from w w w.ja va2s.c o m public String parseCurrentFunction(IEditorInput input, int offset) throws CoreException { String currentElementName; int elementType; // Get the working copy and connect to input. IWorkingCopyManager manager = JavaUI.getWorkingCopyManager(); manager.connect(input); // Retrieve the Java Element in question. // The following internal access is done because the getWorkingCopy() method // for the WorkingCopyManager returns null for StorageEditorInput, however, // there is a working copy available through the ICompilationUnitDocumentProvider. // ICompilationUnit workingCopy = manager.getWorkingCopy(input); ICompilationUnitDocumentProvider x = (ICompilationUnitDocumentProvider) JavaUI.getDocumentProvider(); // Retrieve the Java Element in question. ICompilationUnit workingCopy = x.getWorkingCopy(input); if (workingCopy == null) return ""; IJavaElement method = workingCopy.getElementAt(offset); manager.disconnect(input); // no element selected if (method == null) return ""; // Get the current element name, to test it. currentElementName = method.getElementName(); // Element doesn't have a name. Can go no further. if (currentElementName == null) return ""; // Get the Element Type to test. elementType = method.getElementType(); switch (elementType) { case IJavaElement.METHOD: case IJavaElement.FIELD: break; case IJavaElement.COMPILATION_UNIT: return ""; case IJavaElement.INITIALIZER: return STATIC_INITIALIZER_NAME; // So it's not a method, field, type, or static initializer. Where are we? default: IJavaElement tmpMethodType; if (((tmpMethodType = method.getAncestor(IJavaElement.METHOD)) == null) && ((tmpMethodType = method.getAncestor(IJavaElement.TYPE)) == null)) { return ""; } else { // In a class, but not in a method. Return class name instead. method = tmpMethodType; currentElementName = method.getElementName(); } } // Build all ancestor classes. // Append all ancestor class names to string IJavaElement tmpParent = method.getParent(); boolean firstLoop = true; while (tmpParent != null) { IJavaElement tmpParentClass = tmpParent.getAncestor(IJavaElement.TYPE); if (tmpParentClass != null) { String tmpParentClassName = tmpParentClass.getElementName(); if (tmpParentClassName == null) return ""; currentElementName = tmpParentClassName + "." + currentElementName; } else { // cut root class name int rootClassPos = currentElementName.indexOf("."); if (rootClassPos >= 0) currentElementName = currentElementName.substring(rootClassPos + 1); if (firstLoop) return ""; else return currentElementName; } tmpParent = tmpParentClass.getParent(); firstLoop = false; } return ""; }
From source file:org.eclipse.linuxtools.internal.changelog.parsers.java.JavaParser.java
License:Open Source License
@Override public String parseCurrentFunction(IEditorInput input, int offset) throws CoreException { String currentElementName;/*from w w w .j a v a 2 s.c o m*/ int elementType; // Get the working copy and connect to input. IWorkingCopyManager manager = JavaUI.getWorkingCopyManager(); manager.connect(input); // Retrieve the Java Element in question. // The following internal access is done because the getWorkingCopy() // method // for the WorkingCopyManager returns null for StorageEditorInput, // however, // there is a working copy available through the // ICompilationUnitDocumentProvider. ICompilationUnitDocumentProvider x = (ICompilationUnitDocumentProvider) JavaUI.getDocumentProvider(); // Retrieve the Java Element in question. ICompilationUnit workingCopy = x.getWorkingCopy(input); if (workingCopy == null) { return ""; } IJavaElement method = workingCopy.getElementAt(offset); manager.disconnect(input); // no element selected if (method == null) { return ""; } // Get the current element name, to test it. currentElementName = method.getElementName(); // Element doesn't have a name. Can go no further. if (currentElementName == null) { return ""; } // Get the Element Type to test. elementType = method.getElementType(); switch (elementType) { case IJavaElement.METHOD: case IJavaElement.FIELD: break; case IJavaElement.COMPILATION_UNIT: return ""; case IJavaElement.INITIALIZER: return STATIC_INITIALIZER_NAME; // So it's not a method, field, type, or static initializer. Where // are we? default: IJavaElement tmpMethodType; if (((tmpMethodType = method.getAncestor(IJavaElement.METHOD)) == null) && ((tmpMethodType = method.getAncestor(IJavaElement.TYPE)) == null)) { return ""; } else { // In a class, but not in a method. Return class name instead. method = tmpMethodType; currentElementName = method.getElementName(); } } // Build all ancestor classes. // Append all ancestor class names to string IJavaElement tmpParent = method.getParent(); boolean firstLoop = true; while (tmpParent != null) { IJavaElement tmpParentClass = tmpParent.getAncestor(IJavaElement.TYPE); if (tmpParentClass != null) { String tmpParentClassName = tmpParentClass.getElementName(); if (tmpParentClassName == null) { return ""; } currentElementName = tmpParentClassName + "." + currentElementName; } else { // cut root class name int rootClassPos = currentElementName.indexOf('.'); if (rootClassPos >= 0) { currentElementName = currentElementName.substring(rootClassPos + 1); } if (firstLoop) { return ""; } else { return currentElementName; } } tmpParent = tmpParentClass.getParent(); firstLoop = false; } return ""; }