List of usage examples for org.eclipse.jdt.core IJavaElement getAncestor
IJavaElement getAncestor(int ancestorType);
From source file:com.codenvy.ide.ext.java.server.internal.core.search.JavaSearchScope.java
License:Open Source License
/** * Add an element to the java search scope. * * @param element//ww w . j a v a 2s .c om * The element we want to add to current java search scope * @throws org.eclipse.jdt.core.JavaModelException * May happen if some Java Model info are not available */ public void add(IJavaElement element) throws JavaModelException { IPath containerPath = null; String containerPathToString = null; PackageFragmentRoot root = null; int includeMask = SOURCES | APPLICATION_LIBRARIES | SYSTEM_LIBRARIES; switch (element.getElementType()) { case IJavaElement.JAVA_MODEL: // a workspace sope should be used break; case IJavaElement.JAVA_PROJECT: add((JavaProject) element, null, includeMask, new HashSet(2), new HashSet(2), null); break; case IJavaElement.PACKAGE_FRAGMENT_ROOT: root = (PackageFragmentRoot) element; IPath rootPath = root.internalPath(); containerPath = root.getKind() == IPackageFragmentRoot.K_SOURCE ? root.getParent().getPath() : rootPath; containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString(); IResource rootResource = root.resource(); String projectPath = root.getJavaProject().getPath().toString(); if (rootResource != null && rootResource.isAccessible()) { String relativePath = Util.relativePath(rootResource.getFullPath(), containerPath.segmentCount()); add(projectPath, relativePath, containerPathToString, false/*not a package*/, null); } else { add(projectPath, "", containerPathToString, false/*not a package*/, null); //$NON-NLS-1$ } break; case IJavaElement.PACKAGE_FRAGMENT: root = (PackageFragmentRoot) element.getParent(); projectPath = root.getJavaProject().getPath().toString(); if (root.isArchive()) { String relativePath = Util.concatWith(((PackageFragment) element).names, '/'); containerPath = root.getPath(); containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString(); add(projectPath, relativePath, containerPathToString, true/*package*/, null); } else { IResource resource = ((JavaElement) element).resource(); if (resource != null) { if (resource.isAccessible()) { containerPath = root.getKind() == IPackageFragmentRoot.K_SOURCE ? root.getParent().getPath() : root.internalPath(); } else { // for working copies, get resource container full path containerPath = resource.getParent().getFullPath(); } containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString(); String relativePath = Util.relativePath(resource.getFullPath(), containerPath.segmentCount()); add(projectPath, relativePath, containerPathToString, true/*package*/, null); } } 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); } root = (PackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); projectPath = root.getJavaProject().getPath().toString(); String relativePath; if (root.getKind() == IPackageFragmentRoot.K_SOURCE) { containerPath = root.getParent().getPath(); relativePath = Util.relativePath(getPath(element, false/*full path*/), 1/*remove project segment*/); } else { containerPath = root.internalPath(); relativePath = getPath(element, true/*relative path*/).toString(); } containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString(); add(projectPath, relativePath, containerPathToString, false/*not a package*/, null); } if (root != null) addEnclosingProjectOrJar( root.getKind() == IPackageFragmentRoot.K_SOURCE ? root.getParent().getPath() : root.getPath()); }
From source file:com.codenvy.ide.ext.java.server.internal.core.search.JavaSearchScope.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; searchedElement = searchedElement.getParent(); }//from w ww. j ava2s .c o m } 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; }
From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java
License:Open Source License
private boolean filterEnum(SearchMatch match) { // filter org.apache.commons.lang.enum package for projects above 1.5 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=317264 IJavaElement element = (IJavaElement) match.getElement(); PackageFragment pkg = (PackageFragment) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT); if (pkg != null) { // enum was found in org.apache.commons.lang.enum at index 5 if (pkg.names.length == 5 && pkg.names[4].equals("enum")) { //$NON-NLS-1$ if (this.options == null) { IJavaProject proj = (IJavaProject) pkg.getAncestor(IJavaElement.JAVA_PROJECT); String complianceStr = proj.getOption(CompilerOptions.OPTION_Source, true); if (CompilerOptions.versionToJdkLevel(complianceStr) >= ClassFileConstants.JDK1_5) return true; } else if (this.options.sourceLevel >= ClassFileConstants.JDK1_5) { return true; }//from w w w. j a v a2 s . c o m } } return false; }
From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java
License:Open Source License
/** * Locates the package declarations corresponding to the search pattern. */// w w w . j a v a 2 s .c om protected void locatePackageDeclarations(SearchPattern searchPattern, SearchParticipant participant, IJavaProject[] projects) throws CoreException { if (this.progressMonitor != null && this.progressMonitor.isCanceled()) { throw new OperationCanceledException(); } if (searchPattern instanceof OrPattern) { SearchPattern[] patterns = ((OrPattern) searchPattern).patterns; for (int i = 0, length = patterns.length; i < length; i++) { locatePackageDeclarations(patterns[i], participant, projects); } } else if (searchPattern instanceof PackageDeclarationPattern) { IJavaElement focus = searchPattern.focus; if (focus != null) { if (encloses(focus)) { SearchMatch match = new PackageDeclarationMatch( focus.getAncestor(IJavaElement.PACKAGE_FRAGMENT), SearchMatch.A_ACCURATE, -1, -1, participant, focus.getResource()); report(match); } return; } PackageDeclarationPattern pkgPattern = (PackageDeclarationPattern) searchPattern; boolean isWorkspaceScope = this.scope == JavaModelManager.getJavaModelManager().getWorkspaceScope(); IPath[] scopeProjectsAndJars = isWorkspaceScope ? null : this.scope.enclosingProjectsAndJars(); int scopeLength = isWorkspaceScope ? 0 : scopeProjectsAndJars.length; SimpleSet packages = new SimpleSet(); for (int i = 0, length = projects.length; i < length; i++) { IJavaProject javaProject = projects[i]; if (this.progressMonitor != null) { if (this.progressMonitor.isCanceled()) throw new OperationCanceledException(); this.progressWorked++; if ((this.progressWorked % this.progressStep) == 0) this.progressMonitor.worked(this.progressStep); } // Verify that project belongs to the scope if (!isWorkspaceScope) { boolean found = false; for (int j = 0; j < scopeLength; j++) { if (javaProject.getPath().equals(scopeProjectsAndJars[j])) { found = true; break; } } if (!found) continue; } // Get all project package fragment names this.nameLookup = ((JavaProject) projects[i]).newNameLookup(this.workingCopies); IPackageFragment[] packageFragments = this.nameLookup .findPackageFragments(new String(pkgPattern.pkgName), false, true); int pLength = packageFragments == null ? 0 : packageFragments.length; // Report matches avoiding duplicate names for (int p = 0; p < pLength; p++) { IPackageFragment fragment = packageFragments[p]; if (packages.addIfNotIncluded(fragment) == null) continue; if (encloses(fragment)) { IResource resource = fragment.getResource(); if (resource == null) // case of a file in an external jar resource = javaProject.getProject(); try { if (encloses(fragment)) { SearchMatch match = new PackageDeclarationMatch(fragment, SearchMatch.A_ACCURATE, -1, -1, participant, resource); report(match); } } catch (JavaModelException e) { throw e; } catch (CoreException e) { throw new JavaModelException(e); } } } } } }
From source file:com.codenvy.ide.ext.java.server.internal.core.util.Util.java
License:Open Source License
public static final boolean isExcluded(IJavaElement element) { int elementType = element.getElementType(); switch (elementType) { case IJavaElement.JAVA_MODEL: case IJavaElement.JAVA_PROJECT: case IJavaElement.PACKAGE_FRAGMENT_ROOT: return false; case IJavaElement.PACKAGE_FRAGMENT: PackageFragmentRoot root = (PackageFragmentRoot) element .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); File resource = ((PackageFragment) element).resource(); return resource != null && isExcluded(resource, root.fullInclusionPatternChars(), root.fullExclusionPatternChars()); case IJavaElement.COMPILATION_UNIT: root = (PackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); resource = ((CompilationUnit) element).resource(); if (resource == null) return false; if (isExcluded(resource, root.fullInclusionPatternChars(), root.fullExclusionPatternChars())) return true; return isExcluded(element.getParent()); default://from www .ja va 2 s. c o m IJavaElement cu = element.getAncestor(IJavaElement.COMPILATION_UNIT); return cu != null && isExcluded(cu); } }
From source file:com.drgarbage.bytecodevisualizer.compare.ClassFileMergeViewer.java
License:Apache License
public static InputStream createStream(IJavaElement javaElement) throws CoreException { IClassFile classFile = (IClassFile) javaElement.getAncestor(IJavaElement.CLASS_FILE); InputStream stream = null;/*w ww . ja v a2 s . c o m*/ if (classFile != null) { stream = new ByteArrayInputStream(classFile.getBytes()); } else { if (javaElement.getParent().getElementType() == IJavaElement.COMPILATION_UNIT) { IType t = (IType) javaElement; IJavaProject javaProject = t.getJavaProject(); String fullyQualifiedName = t.getFullyQualifiedName(); String className = JavaSourceUtils.getSimpleName(fullyQualifiedName); String packageName = JavaSourceUtils.getPackage(fullyQualifiedName); String classPath[] = JavaLangUtils.computeRuntimeClassPath(javaProject); try { stream = JavaLangUtils.findResource(classPath, packageName, className); } catch (IOException e) { throw new CoreException( new Status(IStatus.ERROR, BytecodeVisualizerPlugin.PLUGIN_ID, e.getMessage(), e)); } } else { return null; } } return stream; }
From source file:com.drgarbage.bytecodevisualizer.compare.CompareElement.java
License:Apache License
/** * Returns the class name for the given java element. * @param javaElement/*w w w . j av a 2 s . c o m*/ * @return class name */ private static String getClassName(IJavaElement javaElement) { String name; IClassFile classFile = (IClassFile) javaElement.getAncestor(IJavaElement.CLASS_FILE); if (classFile != null) { name = classFile.getPath().toOSString(); } else { if (javaElement.getPath() != null) { name = javaElement.getPath().toOSString(); } else { name = javaElement.getElementName(); } } return name; }
From source file:com.google.appengine.eclipse.core.validators.java.GaeChecker.java
License:Open Source License
private static IPackageFragmentRoot getPackageFragmentRoot(IJavaElement element) { return (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); }
From source file:com.google.gdt.eclipse.designer.common.GwtPropertyTester.java
License:Open Source License
/** * Tests property, can throw {@link Exception}. *///from w ww.jav a2s . com private static boolean testEx(Object receiver, String property) throws Exception { // prepare IResource final IResource resource; if (receiver instanceof IAdaptable) { resource = (IResource) ((IAdaptable) receiver).getAdapter(IResource.class); } else { resource = null; } // prepare IJavaElement final IJavaElement element; if (receiver instanceof IAdaptable) { element = (IJavaElement) ((IAdaptable) receiver).getAdapter(IJavaElement.class); } else { element = null; } // resources tests { if (PROPERTY_IS_GWT_MODULE_ELEMENT.equals(property)) { if (resource != null) { return Utils.getSingleModule(resource) != null; } return false; } if (PROPERTY_IS_RESOURCE.equals(property)) { return resource != null; } } // project tests { if (PROPERTY_IS_GWT_PROJECT_ELEMENT.equals(property)) { // resource selected if (resource != null && resource.exists()) { IProject project = resource.getProject(); IJavaProject javaProject = JavaCore.create(project); return Utils.isGWTProject(javaProject); } // Java element selected if (element != null && element.exists()) { IJavaProject javaProject = (IJavaProject) element.getAncestor(IJavaElement.JAVA_PROJECT); return Utils.isGWTProject(javaProject); } // bad selection return false; } } // Java tests { // prepare java element if (element == null || !element.exists()) { return false; } // do tests if (PROPERTY_IS_CLIENT_PACKAGE.equals(property)) { IPackageFragment packageFragment = (IPackageFragment) element .getAncestor(IJavaElement.PACKAGE_FRAGMENT); if (packageFragment != null) { return Utils.isModuleSourcePackage(packageFragment); } return false; } if (PROPERTY_IS_ENTRY_POINT.equals(property)) { return Utils.isEntryPoint(element); } if (PROPERTY_IS_REMOTE_SERVICE.equals(property)) { return Utils.isRemoteService(element); } if (PROPERTY_IS_REMOTE_SERVICE_IMPL.equals(property)) { return Utils.isRemoteServiceImpl(element); } } // unknown property throw new IllegalArgumentException( MessageFormat.format("Illegal property '{0}' for '{1}'.", property, receiver)); }
From source file:com.google.gdt.eclipse.designer.gxt.actions.GxtPropertyTester.java
License:Open Source License
/** * Tests property, can throw {@link Exception}. *///from w w w .j av a 2s. c om private static boolean testEx(Object receiver, String property) throws Exception { // prepare IJavaElement final IJavaElement element; if (receiver instanceof IAdaptable) { element = (IJavaElement) ((IAdaptable) receiver).getAdapter(IJavaElement.class); } else { element = null; } // Java tests { // prepare java element if (element == null || !element.exists()) { return false; } // do tests if (PROPERTY_IS_CONFIGURED.equals(property)) { IJavaProject javaProject = element.getJavaProject(); boolean hasJar = javaProject.findType("com.extjs.gxt.ui.client.widget.Component") != null; if (hasJar) { IJavaElement pkg = element.getAncestor(IJavaElement.PACKAGE_FRAGMENT); if (pkg != null) { IResource resource = pkg.getUnderlyingResource(); ModuleDescription module = Utils.getSingleModule(resource); return module != null && Utils.inheritsModule(module, "com.extjs.gxt.ui.GXT"); } } return false; } } // unknown property throw new IllegalArgumentException( MessageFormat.format("Illegal property '{0}' for '{1}'.", property, receiver)); }