List of usage examples for org.eclipse.jdt.internal.core PackageFragmentRoot getPackageFragment
public PackageFragment getPackageFragment(String[] pkgName)
From source file:com.codenvy.ide.ext.java.server.internal.core.NameLookup.java
License:Open Source License
private ICompilationUnit findCompilationUnit(String[] pkgName, String cuName, PackageFragmentRoot root) { if (!root.isArchive()) { IPackageFragment pkg = root.getPackageFragment(pkgName); try {//from ww w .j a v a 2s . co m ICompilationUnit[] cus = pkg.getCompilationUnits(); for (int j = 0, length = cus.length; j < length; j++) { ICompilationUnit cu = cus[j]; if (Util.equalsIgnoreJavaLikeExtension(cu.getElementName(), cuName)) return cu; } } catch (JavaModelException e) { // pkg does not exist // -> try next package } } return null; }
From source file:com.codenvy.ide.ext.java.server.internal.core.NameLookup.java
License:Open Source License
/** * Returns the package fragments whose name matches the given * (qualified) name or pattern, or <code>null</code> if none exist. * <p/>//from w w w . jav a2 s .com * The name can be: * <ul> * <li>empty: ""</li> * <li>qualified: "pack.pack1.pack2"</li> * <li>a pattern: "pack.*.util"</li> * </ul> * * @param partialMatch * partial name matches qualify when <code>true</code>, * @param patternMatch * <code>true</code> when the given name might be a pattern, * <code>false</code> otherwise. */ public IPackageFragment[] findPackageFragments(String name, boolean partialMatch, boolean patternMatch) { boolean isStarPattern = name.equals("*"); //$NON-NLS-1$ boolean hasPatternChars = isStarPattern || (patternMatch && (name.indexOf('*') >= 0 || name.indexOf('?') >= 0)); if (partialMatch || hasPatternChars) { String[] splittedName = Util.splitOn('.', name, 0, name.length()); IPackageFragment[] oneFragment = null; ArrayList pkgs = null; char[] lowercaseName = hasPatternChars && !isStarPattern ? name.toLowerCase().toCharArray() : null; Object[][] keys = this.packageFragments.keyTable; for (int i = 0, length = keys.length; i < length; i++) { String[] pkgName = (String[]) keys[i]; if (pkgName != null) { boolean match = isStarPattern || (hasPatternChars ? CharOperation.match(lowercaseName, Util.concatCompoundNameToCharArray(pkgName), false) : Util.startsWithIgnoreCase(pkgName, splittedName, partialMatch)); if (match) { Object value = this.packageFragments.valueTable[i]; if (value instanceof PackageFragmentRoot) { IPackageFragment pkg = ((PackageFragmentRoot) value).getPackageFragment(pkgName); if (oneFragment == null) { oneFragment = new IPackageFragment[] { pkg }; } else { if (pkgs == null) { pkgs = new ArrayList(); pkgs.add(oneFragment[0]); } pkgs.add(pkg); } } else { IPackageFragmentRoot[] roots = (IPackageFragmentRoot[]) value; for (int j = 0, length2 = roots.length; j < length2; j++) { PackageFragmentRoot root = (PackageFragmentRoot) roots[j]; IPackageFragment pkg = root.getPackageFragment(pkgName); if (oneFragment == null) { oneFragment = new IPackageFragment[] { pkg }; } else { if (pkgs == null) { pkgs = new ArrayList(); pkgs.add(oneFragment[0]); } pkgs.add(pkg); } } } } } } if (pkgs == null) return oneFragment; int resultLength = pkgs.size(); IPackageFragment[] result = new IPackageFragment[resultLength]; pkgs.toArray(result); return result; } else { String[] splittedName = Util.splitOn('.', name, 0, name.length()); int pkgIndex = this.packageFragments.getIndex(splittedName); if (pkgIndex == -1) return null; Object value = this.packageFragments.valueTable[pkgIndex]; // reuse existing String[] String[] pkgName = (String[]) this.packageFragments.keyTable[pkgIndex]; if (value instanceof PackageFragmentRoot) { return new IPackageFragment[] { ((PackageFragmentRoot) value).getPackageFragment(pkgName) }; } else { IPackageFragmentRoot[] roots = (IPackageFragmentRoot[]) value; IPackageFragment[] result = new IPackageFragment[roots.length]; for (int i = 0; i < roots.length; i++) { result[i] = ((PackageFragmentRoot) roots[i]).getPackageFragment(pkgName); } return result; } } }
From source file:com.codenvy.ide.ext.java.server.internal.core.NameLookup.java
License:Open Source License
/** * Notifies the given requestor of all package fragments with the * given name. Checks the requestor at regular intervals to see if the * requestor has canceled. The domain of * the search is bounded by the <code>IJavaProject</code> * this <code>NameLookup</code> was obtained from. * * @param partialMatch//from w w w .j a v a 2 s.c o m * partial name matches qualify when <code>true</code>; * only exact name matches qualify when <code>false</code> */ public void seekPackageFragments(String name, boolean partialMatch, IJavaElementRequestor requestor) { /* if (VERBOSE) { Util.verbose(" SEEKING PACKAGE FRAGMENTS"); //$NON-NLS-1$ Util.verbose(" -> name: " + name); //$NON-NLS-1$ Util.verbose(" -> partial match:" + partialMatch); //$NON-NLS-1$ } */ if (partialMatch) { String[] splittedName = Util.splitOn('.', name, 0, name.length()); Object[][] keys = this.packageFragments.keyTable; for (int i = 0, length = keys.length; i < length; i++) { if (requestor.isCanceled()) return; String[] pkgName = (String[]) keys[i]; if (pkgName != null && Util.startsWithIgnoreCase(pkgName, splittedName, partialMatch)) { Object value = this.packageFragments.valueTable[i]; if (value instanceof PackageFragmentRoot) { PackageFragmentRoot root = (PackageFragmentRoot) value; requestor.acceptPackageFragment(root.getPackageFragment(pkgName)); } else { IPackageFragmentRoot[] roots = (IPackageFragmentRoot[]) value; for (int j = 0, length2 = roots.length; j < length2; j++) { if (requestor.isCanceled()) return; PackageFragmentRoot root = (PackageFragmentRoot) roots[j]; requestor.acceptPackageFragment(root.getPackageFragment(pkgName)); } } } } } else { String[] splittedName = Util.splitOn('.', name, 0, name.length()); int pkgIndex = this.packageFragments.getIndex(splittedName); if (pkgIndex != -1) { Object value = this.packageFragments.valueTable[pkgIndex]; // reuse existing String[] String[] pkgName = (String[]) this.packageFragments.keyTable[pkgIndex]; if (value instanceof PackageFragmentRoot) { requestor.acceptPackageFragment(((PackageFragmentRoot) value).getPackageFragment(pkgName)); } else { IPackageFragmentRoot[] roots = (IPackageFragmentRoot[]) value; if (roots != null) { for (int i = 0, length = roots.length; i < length; i++) { if (requestor.isCanceled()) return; PackageFragmentRoot root = (PackageFragmentRoot) roots[i]; requestor.acceptPackageFragment(root.getPackageFragment(pkgName)); } } } } } }
From source file:org.eclipse.che.jdt.internal.core.JavaModelManager.java
License:Open Source License
/** * Creates and returns a compilation unit element for the given <code>.java</code> * file, its project being the given project. Returns <code>null</code> if unable * to recognize the compilation unit.//from www. j a va 2 s. c o m */ public static ICompilationUnit createCompilationUnitFrom(File file, IJavaProject project) { if (file == null) return null; // if (project == null) { // project = JavaCore.create(file.getProject()); // } IPackageFragment pkg = (IPackageFragment) determineIfOnClasspath(file, (JavaProject) project); if (pkg == null) { // not on classpath - make the root its folder, and a default package PackageFragmentRoot root = (PackageFragmentRoot) project.getPackageFragmentRoot(file.getParent()); pkg = root.getPackageFragment(CharOperation.NO_STRINGS); if (VERBOSE) { System.out.println("WARNING : creating unit element outside classpath (" + Thread.currentThread() + "): " + file.getAbsolutePath()); //$NON-NLS-1$//$NON-NLS-2$ } } return pkg.getCompilationUnit(file.getName()); }
From source file:org.eclipse.che.jdt.internal.core.JavaModelManager.java
License:Open Source License
/** * Returns the package fragment root represented by the resource, or * the package fragment the given resource is located in, or <code>null</code> * if the given resource is not on the classpath of the given project. *//*from www.j a v a 2 s . co m*/ public static IJavaElement determineIfOnClasspath(File resource, JavaProject project) { IPath resourcePath = new Path(resource.getAbsolutePath()); boolean isExternal = false; //ExternalFoldersManager.isInternalPathForExternalFolder(resourcePath); // if (isExternal) // resourcePath = resource.getLocation(); try { JavaProjectElementInfo projectInfo = (JavaProjectElementInfo) ((JavaProject) project).manager .getInfo(project); JavaProjectElementInfo.ProjectCache projectCache = projectInfo == null ? null : projectInfo.projectCache; HashtableOfArrayToObject allPkgFragmentsCache = projectCache == null ? null : projectCache.allPkgFragmentsCache; boolean isJavaLike = Util.isJavaLikeFileName(resourcePath.lastSegment()); IClasspathEntry[] entries = isJavaLike ? project.getRawClasspath() // JAVA file can only live inside SRC folder (on the raw path) : ((JavaProject) project).getResolvedClasspath(); int length = entries.length; if (length > 0) { String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true); String complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true); for (int i = 0; i < length; i++) { IClasspathEntry entry = entries[i]; if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) continue; IPath rootPath = entry.getPath(); if (rootPath.equals(resourcePath)) { if (isJavaLike) return null; return project.getPackageFragmentRoot(resource); } else if (rootPath.isPrefixOf(resourcePath)) { // allow creation of package fragment if it contains a .java file that is included if (!Util.isExcluded(resourcePath, ((ClasspathEntry) entry).fullInclusionPatternChars(), ((ClasspathEntry) entry).fullExclusionPatternChars(), true)) { // given we have a resource child of the root, it cannot be a JAR pkg root PackageFragmentRoot root = // isExternal ? // new ExternalPackageFragmentRoot(rootPath, (JavaProject) project) : (PackageFragmentRoot) ((JavaProject) project) .getFolderPackageFragmentRoot(rootPath); if (root == null) return null; IPath pkgPath = resourcePath.removeFirstSegments(rootPath.segmentCount()); if (resource.isFile()) { // if the resource is a file, then remove the last segment which // is the file name in the package pkgPath = pkgPath.removeLastSegments(1); } String[] pkgName = pkgPath.segments(); // if package name is in the cache, then it has already been validated // (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=133141) if (allPkgFragmentsCache != null && allPkgFragmentsCache.containsKey(pkgName)) return root.getPackageFragment(pkgName); if (pkgName.length != 0 && JavaConventions .validatePackageName(Util.packageName(pkgPath, sourceLevel, complianceLevel), sourceLevel, complianceLevel) .getSeverity() == IStatus.ERROR) { return null; } return root.getPackageFragment(pkgName); } } } } } catch (JavaModelException npe) { return null; } return null; }
From source file:org.eclipse.che.jdt.internal.core.JavaModelManager.java
License:Open Source License
/** * Creates and returns a class file element for the given <code>.class</code> file, * its project being the given project. Returns <code>null</code> if unable * to recognize the class file.// ww w . j a v a 2 s . c om */ public static IClassFile createClassFileFrom(File file, IJavaProject project) { if (file == null) { return null; } // if (project == null) { // project = JavaCore.create(file.getProject()); // } IPackageFragment pkg = (IPackageFragment) determineIfOnClasspath(file, (JavaProject) project); if (pkg == null) { // fix for 1FVS7WE // not on classpath - make the root its folder, and a default package PackageFragmentRoot root = (PackageFragmentRoot) project.getPackageFragmentRoot(file.getParent()); pkg = root.getPackageFragment(CharOperation.NO_STRINGS); } return pkg.getClassFile(file.getName()); }
From source file:org.eclipse.jdt.internal.core.CopyResourceElementsOperation.java
License:Open Source License
/** * Creates any destination package fragment(s) which do not exists yet. * Return true if a read-only package fragment has been found among package fragments, false otherwise *//* w w w . j av a 2s.c om*/ private boolean createNeededPackageFragments(IContainer sourceFolder, PackageFragmentRoot root, String[] newFragName, boolean moveFolder) throws JavaModelException { boolean containsReadOnlyPackageFragment = false; IContainer parentFolder = (IContainer) root.resource(); JavaElementDelta projectDelta = null; String[] sideEffectPackageName = null; char[][] inclusionPatterns = root.fullInclusionPatternChars(); char[][] exclusionPatterns = root.fullExclusionPatternChars(); for (int i = 0; i < newFragName.length; i++) { String subFolderName = newFragName[i]; sideEffectPackageName = Util.arrayConcat(sideEffectPackageName, subFolderName); IResource subFolder = parentFolder.findMember(subFolderName); if (subFolder == null) { // create deepest folder only if not a move (folder will be moved in processPackageFragmentResource) if (!(moveFolder && i == newFragName.length - 1)) { createFolder(parentFolder, subFolderName, this.force); } parentFolder = parentFolder.getFolder(new Path(subFolderName)); sourceFolder = sourceFolder.getFolder(new Path(subFolderName)); if (Util.isReadOnly(sourceFolder)) { containsReadOnlyPackageFragment = true; } IPackageFragment sideEffectPackage = root.getPackageFragment(sideEffectPackageName); if (i < newFragName.length - 1 // all but the last one are side effect packages && !Util.isExcluded(parentFolder, inclusionPatterns, exclusionPatterns)) { if (projectDelta == null) { projectDelta = getDeltaFor(root.getJavaProject()); } projectDelta.added(sideEffectPackage); } this.createdElements.add(sideEffectPackage); } else { parentFolder = (IContainer) subFolder; } } return containsReadOnlyPackageFragment; }
From source file:org.eclipse.jdt.internal.core.CopyResourceElementsOperation.java
License:Open Source License
/** * Copies/moves a package fragment with the name <code>newName</code> * to the destination package.<br> * * @exception JavaModelException if the operation is unable to * complete//ww w . j a v a2 s . c om */ private void processPackageFragmentResource(PackageFragment source, PackageFragmentRoot root, String newName) throws JavaModelException { try { String[] newFragName = (newName == null) ? source.names : Util.getTrimmedSimpleNames(newName); PackageFragment newFrag = root.getPackageFragment(newFragName); IResource[] resources = collectResourcesOfInterest(source); // if isMove() can we move the folder itself ? (see http://bugs.eclipse.org/bugs/show_bug.cgi?id=22458) boolean shouldMoveFolder = isMove() && !newFrag.resource().exists(); // if new pkg fragment exists, it is an override IFolder srcFolder = (IFolder) source.resource(); IPath destPath = newFrag.getPath(); if (shouldMoveFolder) { // check if destination is not included in source if (srcFolder.getFullPath().isPrefixOf(destPath)) { shouldMoveFolder = false; } else { // check if there are no sub-packages IResource[] members = srcFolder.members(); for (int i = 0; i < members.length; i++) { if (members[i] instanceof IFolder) { shouldMoveFolder = false; break; } } } } boolean containsReadOnlySubPackageFragments = createNeededPackageFragments( (IContainer) source.parent.resource(), root, newFragName, shouldMoveFolder); boolean sourceIsReadOnly = Util.isReadOnly(srcFolder); // Process resources if (shouldMoveFolder) { // move underlying resource // TODO Revisit once bug 43044 is fixed if (sourceIsReadOnly) { Util.setReadOnly(srcFolder, false); } srcFolder.move(destPath, this.force, true /* keep history */, getSubProgressMonitor(1)); if (sourceIsReadOnly) { Util.setReadOnly(srcFolder, true); } setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE); } else { // process the leaf resources if (resources.length > 0) { if (isRename()) { if (!destPath.equals(source.getPath())) { moveResources(resources, destPath); } } else if (isMove()) { // we need to delete this resource if this operation wants to override existing resources for (int i = 0, max = resources.length; i < max; i++) { IResource destinationResource = ResourcesPlugin.getWorkspace().getRoot() .findMember(destPath.append(resources[i].getName())); if (destinationResource != null) { if (this.force) { deleteResource(destinationResource, IResource.KEEP_HISTORY); } else { throw new JavaModelException( new JavaModelStatus(IJavaModelStatusConstants.NAME_COLLISION, Messages.bind(Messages.status_nameCollision, destinationResource.getFullPath().toString()))); } } } moveResources(resources, destPath); } else { // we need to delete this resource if this operation wants to override existing resources for (int i = 0, max = resources.length; i < max; i++) { IResource destinationResource = ResourcesPlugin.getWorkspace().getRoot() .findMember(destPath.append(resources[i].getName())); if (destinationResource != null) { if (this.force) { // we need to delete this resource if this operation wants to override existing resources deleteResource(destinationResource, IResource.KEEP_HISTORY); } else { throw new JavaModelException( new JavaModelStatus(IJavaModelStatusConstants.NAME_COLLISION, Messages.bind(Messages.status_nameCollision, destinationResource.getFullPath().toString()))); } } } copyResources(resources, destPath); } } } // Update package statement in compilation unit if needed if (!Util.equalArraysOrNull(newFragName, source.names)) { // if package has been renamed, update the compilation units char[][] inclusionPatterns = root.fullInclusionPatternChars(); char[][] exclusionPatterns = root.fullExclusionPatternChars(); for (int i = 0; i < resources.length; i++) { String resourceName = resources[i].getName(); if (Util.isJavaLikeFileName(resourceName)) { // we only consider potential compilation units ICompilationUnit cu = newFrag.getCompilationUnit(resourceName); if (Util.isExcluded(cu.getPath(), inclusionPatterns, exclusionPatterns, false/*not a folder*/)) continue; this.parser.setSource(cu); CompilationUnit astCU = (CompilationUnit) this.parser.createAST(this.progressMonitor); AST ast = astCU.getAST(); ASTRewrite rewrite = ASTRewrite.create(ast); updatePackageStatement(astCU, newFragName, rewrite, cu); TextEdit edits = rewrite.rewriteAST(); applyTextEdit(cu, edits); cu.save(null, false); } } } // Discard empty old package (if still empty after the rename) boolean isEmpty = true; if (isMove()) { // delete remaining files in this package (.class file in the case where Proj=src=bin) // in case of a copy updateReadOnlyPackageFragmentsForMove((IContainer) source.parent.resource(), root, newFragName, sourceIsReadOnly); if (srcFolder.exists()) { IResource[] remaining = srcFolder.members(); for (int i = 0, length = remaining.length; i < length; i++) { IResource file = remaining[i]; if (file instanceof IFile) { if (Util.isReadOnly(file)) { Util.setReadOnly(file, false); } deleteResource(file, IResource.FORCE | IResource.KEEP_HISTORY); } else { isEmpty = false; } } } if (isEmpty) { IResource rootResource; // check if source is included in destination if (destPath.isPrefixOf(srcFolder.getFullPath())) { rootResource = newFrag.resource(); } else { rootResource = source.parent.resource(); } // delete recursively empty folders deleteEmptyPackageFragment(source, false, rootResource); } } else if (containsReadOnlySubPackageFragments) { // in case of a copy updateReadOnlyPackageFragmentsForCopy((IContainer) source.parent.resource(), root, newFragName); } // workaround for bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=24505 if (isEmpty && isMove() && !(Util.isExcluded(source) || Util.isExcluded(newFrag))) { IJavaProject sourceProject = source.getJavaProject(); getDeltaFor(sourceProject).movedFrom(source, newFrag); IJavaProject destProject = newFrag.getJavaProject(); getDeltaFor(destProject).movedTo(newFrag, source); } } catch (JavaModelException e) { throw e; } catch (CoreException ce) { throw new JavaModelException(ce); } }
From source file:org.eclipse.jdt.internal.core.JavaModelManager.java
License:Open Source License
/** * Creates and returns a class file element for the given <code>.class</code> file, * its project being the given project. Returns <code>null</code> if unable * to recognize the class file.//from www . ja va 2 s .co m */ public static IClassFile createClassFileFrom(IFile file, IJavaProject project) { if (file == null) { return null; } if (project == null) { project = JavaCore.create(file.getProject()); } IPackageFragment pkg = (IPackageFragment) determineIfOnClasspath(file, project); if (pkg == null) { // fix for 1FVS7WE // not on classpath - make the root its folder, and a default package PackageFragmentRoot root = (PackageFragmentRoot) project.getPackageFragmentRoot(file.getParent()); pkg = root.getPackageFragment(CharOperation.NO_STRINGS); } return pkg.getClassFile(file.getName()); }
From source file:org.eclipse.jdt.internal.core.JavaModelManager.java
License:Open Source License
/** * Creates and returns a compilation unit element for the given <code>.java</code> * file, its project being the given project. Returns <code>null</code> if unable * to recognize the compilation unit.//from w w w . jav a 2 s. com */ public static ICompilationUnit createCompilationUnitFrom(IFile file, IJavaProject project) { if (file == null) return null; if (project == null) { project = JavaCore.create(file.getProject()); } IPackageFragment pkg = (IPackageFragment) determineIfOnClasspath(file, project); if (pkg == null) { // not on classpath - make the root its folder, and a default package PackageFragmentRoot root = (PackageFragmentRoot) project.getPackageFragmentRoot(file.getParent()); pkg = root.getPackageFragment(CharOperation.NO_STRINGS); if (VERBOSE) { System.out.println("WARNING : creating unit element outside classpath (" + Thread.currentThread() //$NON-NLS-1$ + "): " + file.getFullPath()); //$NON-NLS-1$ } } return pkg.getCompilationUnit(file.getName()); }