List of usage examples for org.eclipse.jdt.core IPackageFragment getCompilationUnit
ICompilationUnit getCompilationUnit(String name);
"Object.java"
). From source file:ar.com.tadp.xml.rinzo.jdt.contentassist.processors.ClassNameProcessor.java
License:Open Source License
private ICompilationUnit getTemporaryCompilationUnit(IJavaProject project) throws JavaModelException { IPackageFragment root = project.getPackageFragments()[0]; ICompilationUnit unit = root.getCompilationUnit(TEMPORAL_CLASS_FILE_NAME) .getWorkingCopy(new NullProgressMonitor()); return unit;//from www .j a v a2 s .c om }
From source file:at.bestsolution.efxclipse.tooling.jdt.ui.internal.wizard.clazz.AbstractNewClassWizard.java
License:Open Source License
@Override protected IFile createFile() { IPackageFragment fragment = getDomainClass().getPackageFragment(); if (fragment != null) { String cuName = getDomainClass().getName() + ".java"; ICompilationUnit unit = fragment.getCompilationUnit(cuName); IResource resource = unit.getResource(); return (IFile) resource; } else {/*from w w w. j av a2 s . c om*/ String cuName = getDomainClass().getName() + ".java"; IFolder p = (IFolder) getDomainClass().getFragmentRoot().getResource(); return p.getFile(cuName); } }
From source file:at.bestsolution.fxide.jdt.text.javadoc.JavadocContentAccess2.java
License:Open Source License
/** * Returns the Javadoc for a package which could be present in package.html, package-info.java * or from an attached Javadoc./* w ww . j av a 2s. c om*/ * * @param packageFragment the package which is requesting for the document * @return the document content in HTML format or <code>null</code> if there is no associated * Javadoc * @throws CoreException if the Java element does not exists or an exception occurs while * accessing the file containing the package Javadoc * @since 3.9 */ public static String getHTMLContent(IPackageFragment packageFragment) throws CoreException { IPackageFragmentRoot root = (IPackageFragmentRoot) packageFragment .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); //1==> Handle the case when the documentation is present in package-info.java or package-info.class file ITypeRoot packageInfo; boolean isBinary = root.getKind() == IPackageFragmentRoot.K_BINARY; if (isBinary) { packageInfo = packageFragment.getClassFile(JavaModelUtil.PACKAGE_INFO_CLASS); } else { packageInfo = packageFragment.getCompilationUnit(JavaModelUtil.PACKAGE_INFO_JAVA); } if (packageInfo != null && packageInfo.exists()) { String cuSource = packageInfo.getSource(); //the source can be null for some of the class files if (cuSource != null) { Javadoc packageJavadocNode = getPackageJavadocNode(packageFragment, cuSource); if (packageJavadocNode != null) { IJavaElement element; if (isBinary) { element = ((IClassFile) packageInfo).getType(); } else { element = packageInfo.getParent(); // parent is the IPackageFragment } return new JavadocContentAccess2(element, packageJavadocNode, cuSource).toHTML(); } } } // 2==> Handle the case when the documentation is done in package.html file. The file can be either in normal source folder or coming from a jar file else { Object[] nonJavaResources = packageFragment.getNonJavaResources(); // 2.1 ==>If the package.html file is present in the source or directly in the binary jar for (Object nonJavaResource : nonJavaResources) { if (nonJavaResource instanceof IFile) { IFile iFile = (IFile) nonJavaResource; if (iFile.exists() && JavaModelUtil.PACKAGE_HTML.equals(iFile.getName())) { return getIFileContent(iFile); } } } // 2.2==>The file is present in a binary container if (isBinary) { for (Object nonJavaResource : nonJavaResources) { // The content is from an external binary class folder if (nonJavaResource instanceof IJarEntryResource) { IJarEntryResource jarEntryResource = (IJarEntryResource) nonJavaResource; String encoding = getSourceAttachmentEncoding(root); if (JavaModelUtil.PACKAGE_HTML.equals(jarEntryResource.getName()) && jarEntryResource.isFile()) { return getHTMLContent(jarEntryResource, encoding); } } } //2.3 ==>The file is present in the source attachment path. String contents = getHTMLContentFromAttachedSource(root, packageFragment); if (contents != null) return contents; } } //3==> Handle the case when the documentation is coming from the attached Javadoc if ((root.isArchive() || root.isExternal())) { return packageFragment.getAttachedJavadoc(null); } return null; }
From source file:at.bestsolution.javafx.ide.jdt.internal.JavaClassResourceService.java
License:Open Source License
IResource handleElementCreation(IContainer container, String packageName, String className) { // FIXME This is temporary if (!container.getProject().isOpen()) { try {//from ww w . j a v a2 s .c o m container.getProject().open(new NullProgressMonitor()); } catch (CoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } } IProject pr = container.getProject(); IJavaElement jElement = getInitialJavaElement(container); IPackageFragmentRoot jRoot = getFragmentRoot(jElement); try { IJavaProject jProject = JavaCore.create(pr); jProject.open(new NullProgressMonitor()); jRoot.open(new NullProgressMonitor()); IPackageFragment fragment = jRoot.getPackageFragment(packageName); if (!fragment.exists()) { ((IFolder) fragment.getResource()).create(true, true, null); } ICompilationUnit u = fragment.getCompilationUnit(className + ".java"); IFile f = (IFile) u.getResource(); ByteArrayInputStream in = new ByteArrayInputStream( new String("public class " + className + " {\n}").getBytes()); f.create(in, IFile.FORCE | IFile.KEEP_HISTORY, new NullProgressMonitor()); in.close(); // pr.build(IncrementalProjectBuilder.FULL_BUILD, new // NullProgressMonitor()); return f; } catch (JavaModelException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (CoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.err.println(jRoot); return null; }
From source file:ch.mlutz.plugins.t4e.tapestry.TapestryModule.java
License:Open Source License
public static ICompilationUnit findCompilationUnit(IProject project, String fullyQualifiedName) { IPackageFragment fragment; ICompilationUnit compilationUnit;//from w w w . jav a 2 s .co m IPackageFragmentRoot[] fragmentRoots; try { fragmentRoots = getPackageFragmentRoots(project); } catch (JavaModelException e) { log.warn("Could not get packageFragmentRoots of project " + project.getName(), e); return null; } catch (CoreException e) { log.warn("Could not get packageFragmentRoots of project " + project.getName(), e); return null; } // split fullyQualifiedName into package and simple name // int lastDotIndex= fullyQualifiedName.lastIndexOf('.'); String packageName; String compilationUnitName; Pattern pattern = Pattern.compile("(?:(.*)\\.)?([^.]+\\.[^.]+)"); Matcher matcher = pattern.matcher(fullyQualifiedName); if (matcher.matches()) { packageName = matcher.group(1); if (packageName == null) { packageName = ""; // default package } compilationUnitName = matcher.group(2); } else { return null; } /* if (lastDotIndex != -1) { packageName= fullyQualifiedName.substring(0, lastDotIndex); compilationUnitName= fullyQualifiedName.substring(lastDotIndex + 1, fullyQualifiedName.length()); } else { // no dot found ==> try to retrieve class in default package packageName= ""; compilationUnitName= fullyQualifiedName; } */ for (IPackageFragmentRoot root : fragmentRoots) { try { // only take into account source roots if (root.getKind() != IPackageFragmentRoot.K_SOURCE) { continue; } } catch (JavaModelException e) { log.warn("Could not get kind of packageFragmentRoot " + project.getName(), e); continue; } fragment = root.getPackageFragment(packageName); if (fragment == null) { continue; } /* log.info("Trying package fragment: " + fragment.getPath()); */ compilationUnit = fragment.getCompilationUnit(compilationUnitName); if (compilationUnit != null && compilationUnit.exists()) { return compilationUnit; } } return null; }
From source file:ch.mlutz.plugins.t4e.tapestry.TapestryModule.java
License:Open Source License
public static ICompilationUnit findCompilationUnitInClassPackages(IProject project, Iterable<String> classPackages, String packageSuffix, String resourceName) { IPackageFragment fragment; ICompilationUnit compilationUnit;//from w w w. j a v a 2s. co m IPackageFragmentRoot[] fragmentRoots; try { fragmentRoots = getPackageFragmentRoots(project); } catch (JavaModelException e) { log.warn("Could not get packageFragmentRoots of project " + project.getName(), e); return null; } catch (CoreException e) { log.warn("Could not get packageFragmentRoots of project " + project.getName(), e); return null; } if (classPackages == null) { // add empty string as classPackage if none supplied classPackages = Arrays.asList(new String[] { "" }); } // loop through class package names for (String packageName : classPackages) { for (IPackageFragmentRoot root : fragmentRoots) { try { // only take into account source roots if (root.getKind() != IPackageFragmentRoot.K_SOURCE) { continue; } } catch (JavaModelException e) { log.warn("Could not get kind of packageFragmentRoot " + project.getName(), e); continue; } fragment = root.getPackageFragment(packageName + packageSuffix); if (fragment == null) { continue; } /* log.info("Trying package fragment: " + fragment.getPath()); */ compilationUnit = fragment.getCompilationUnit(resourceName); if (compilationUnit != null && compilationUnit.exists()) { return compilationUnit; } } } return null; }
From source file:com.amashchenko.eclipse.strutsclipse.java.JavaClassCompletion.java
License:Apache License
private static ICompilationUnit createSourceCompilationUnit(IDocument document) throws JavaModelException { ICompilationUnit unit = null;/*from www . j a v a 2 s . c o m*/ IJavaProject javaProject = ProjectUtil.getCurrentJavaProject(document); if (javaProject != null) { IPackageFragment packageFragment = javaProject.getPackageFragments()[0]; if (packageFragment != null) { unit = packageFragment.getCompilationUnit(CLASS_NAME + ".java").getWorkingCopy(null); } } return unit; }
From source file:com.codenvy.ide.ext.java.server.internal.core.DeltaProcessor.java
License:Open Source License
private Openable createElement(File resource, int elementType, RootInfo rootInfo) { if (resource == null) return null; IPath path = new Path(resource.getPath()); IJavaElement element = null;//from ww w . j av a 2s . c o m switch (elementType) { // // case IJavaElement.JAVA_PROJECT: // // // note that non-java resources rooted at the project level will also enter this code with // // an elementType JAVA_PROJECT (see #elementType(...)). // if (resource instanceof IProject){ // // popUntilPrefixOf(path); // // if (this.currentElement != null // && this.currentElement.getElementType() == IJavaElement.JAVA_PROJECT // && ((IJavaProject)this.currentElement).getProject().equals(resource)) { // return this.currentElement; // } // if (rootInfo != null && rootInfo.project.getProject().equals(resource)){ // element = rootInfo.project; // break; // } // IProject proj = (IProject)resource; // if (JavaProject.hasJavaNature(proj)) { // element = JavaCore.create(proj); // } else { // // java project may have been been closed or removed (look for // // element amongst old java project s list). // element = this.state.findJavaProject(proj.getName()); // } // } // break; case IJavaElement.PACKAGE_FRAGMENT_ROOT: element = rootInfo == null ? JavaModelManager.create(resource, manager.getJavaProject()) : rootInfo.getPackageFragmentRoot(resource); break; case IJavaElement.PACKAGE_FRAGMENT: if (rootInfo != null) { if (rootInfo.project.contains(resource)) { PackageFragmentRoot root = (PackageFragmentRoot) rootInfo.getPackageFragmentRoot(null); // create package handle IPath pkgPath = path.removeFirstSegments(root.resource().toPath().getNameCount()); String[] pkgName = pkgPath.segments(); element = root.getPackageFragment(pkgName); } } else { // find the element that encloses the resource popUntilPrefixOf(path); if (this.currentElement == null) { element = JavaCore.create(resource); } else { // find the root PackageFragmentRoot root = this.currentElement.getPackageFragmentRoot(); if (root == null) { element = JavaCore.create(resource); } else if (((JavaProject) root.getJavaProject()).contains(resource)) { // create package handle IPath pkgPath = path.removeFirstSegments(root.getPath().segmentCount()); String[] pkgName = pkgPath.segments(); element = root.getPackageFragment(pkgName); } } } break; case IJavaElement.COMPILATION_UNIT: case IJavaElement.CLASS_FILE: // find the element that encloses the resource popUntilPrefixOf(path); if (this.currentElement == null) { element = rootInfo == null ? JavaModelManager.create(resource, manager.getJavaProject()) : JavaModelManager.create(resource, rootInfo.project); } else { // find the package IPackageFragment pkgFragment = null; switch (this.currentElement.getElementType()) { case IJavaElement.PACKAGE_FRAGMENT_ROOT: PackageFragmentRoot root = (PackageFragmentRoot) this.currentElement; IPath rootPath = root.getPath(); IPath pkgPath = path.removeLastSegments(1); String[] pkgName = pkgPath.removeFirstSegments(rootPath.segmentCount()).segments(); pkgFragment = root.getPackageFragment(pkgName); break; case IJavaElement.PACKAGE_FRAGMENT: Openable pkg = this.currentElement; if (pkg.getPath().equals(path.removeLastSegments(1))) { pkgFragment = (IPackageFragment) pkg; } // else case of package x which is a prefix of x.y break; case IJavaElement.COMPILATION_UNIT: case IJavaElement.CLASS_FILE: pkgFragment = (IPackageFragment) this.currentElement.getParent(); break; } if (pkgFragment == null) { element = rootInfo == null ? JavaCore.create(resource) : JavaModelManager.create(resource, rootInfo.project); } else { if (elementType == IJavaElement.COMPILATION_UNIT) { // create compilation unit handle // fileName validation has been done in elementType(IResourceDelta, int, boolean) String fileName = path.lastSegment(); element = pkgFragment.getCompilationUnit(fileName); } else { // create class file handle // fileName validation has been done in elementType(IResourceDelta, int, boolean) String fileName = path.lastSegment(); element = pkgFragment.getClassFile(fileName); } } } break; } if (element == null) return null; this.currentElement = (Openable) element; return this.currentElement; }
From source file:com.codenvy.ide.ext.java.server.internal.core.DeltaProcessor.java
License:Open Source License
private void updateIndex(Openable element, IResourceDelta delta) { IndexManager indexManager = this.manager.indexManager; if (indexManager == null) return;/*w ww . ja v a2 s. c om*/ switch (element.getElementType()) { case IJavaElement.JAVA_PROJECT: switch (delta.getKind()) { case IResourceDelta.ADDED: indexManager.indexAll((JavaProject) element.getJavaProject()); break; case IResourceDelta.REMOVED: indexManager.removeIndexFamily(element.getJavaProject().getProject().getFullPath()); // NB: Discarding index jobs belonging to this project was done during PRE_DELETE break; // NB: Update of index if project is opened, closed, or its java nature is added or removed // is done in updateCurrentDeltaAndIndex } break; case IJavaElement.PACKAGE_FRAGMENT_ROOT: if (element instanceof JarPackageFragmentRoot) { JarPackageFragmentRoot root = (JarPackageFragmentRoot) element; // index jar file only once (if the root is in its declaring project) IPath jarPath = root.getPath(); switch (delta.getKind()) { case IResourceDelta.ADDED: // index the new jar indexManager.indexLibrary(jarPath, root.getIndexPath()); break; case IResourceDelta.CHANGED: // first remove the index so that it is forced to be re-indexed indexManager.removeIndex(jarPath); // then index the jar indexManager.indexLibrary(jarPath, root.getIndexPath()); break; case IResourceDelta.REMOVED: // the jar was physically removed: remove the index indexManager.discardJobs(jarPath.toString()); indexManager.removeIndex(jarPath); break; } break; } int kind = delta.getKind(); if (kind == IResourceDelta.ADDED || kind == IResourceDelta.REMOVED || (kind == IResourceDelta.CHANGED && (delta.getFlags() & IResourceDelta.LOCAL_CHANGED) != 0)) { PackageFragmentRoot root = (PackageFragmentRoot) element; updateRootIndex(root, CharOperation.NO_STRINGS, delta); break; } // don't break as packages of the package fragment root can be indexed below // $FALL-THROUGH$ case IJavaElement.PACKAGE_FRAGMENT: switch (delta.getKind()) { case IResourceDelta.CHANGED: if ((delta.getFlags() & IResourceDelta.LOCAL_CHANGED) == 0) break; // $FALL-THROUGH$ case IResourceDelta.ADDED: case IResourceDelta.REMOVED: IPackageFragment pkg = null; if (element instanceof IPackageFragmentRoot) { PackageFragmentRoot root = (PackageFragmentRoot) element; pkg = root.getPackageFragment(CharOperation.NO_STRINGS); } else { pkg = (IPackageFragment) element; } RootInfo rootInfo = rootInfo(pkg.getParent().getPath(), delta.getKind()); boolean isSource = rootInfo == null // if null, defaults to source || rootInfo.entryKind == IClasspathEntry.CPE_SOURCE; IResourceDelta[] children = (IResourceDelta[]) delta.getAffectedChildren(); for (int i = 0, length = children.length; i < length; i++) { IResourceDelta child = children[i]; IResource resource = child.getResource(); // TODO (philippe) Why do this? Every child is added anyway as the delta is walked if (resource instanceof IFile) { String name = resource.getName(); if (isSource) { if (org.eclipse.jdt.internal.core.util.Util.isJavaLikeFileName(name)) { Openable cu = (Openable) pkg.getCompilationUnit(name); updateIndex(cu, child); } } else if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(name)) { Openable classFile = (Openable) pkg.getClassFile(name); updateIndex(classFile, child); } } } break; } break; case IJavaElement.CLASS_FILE: // IFile file = (IFile) delta.getResource(); // IJavaProject project = element.getJavaProject(); // PackageFragmentRoot root = element.getPackageFragmentRoot(); // IPath binaryFolderPath = root.isExternal() && !root.isArchive() ? root.resource().getFullPath() : root.getPath(); // // if the class file is part of the binary output, it has been created by // // the java builder -> ignore // try { // if (binaryFolderPath.equals(project.getOutputLocation())) { // break; // } // } catch (JavaModelException e) { // // project doesn't exist: ignore // } // switch (delta.getKind()) { // case IResourceDelta.CHANGED : // // no need to index if the content has not changed // int flags = delta.getFlags(); // if ((flags & IResourceDelta.CONTENT) == 0 && (flags & IResourceDelta.ENCODING) == 0) // break; // // $FALL-THROUGH$ // case IResourceDelta.ADDED : // indexManager.addBinary(file, binaryFolderPath); // break; // case IResourceDelta.REMOVED : // String containerRelativePath = Util.relativePath(file.getFullPath(), binaryFolderPath.segmentCount()); // indexManager.remove(containerRelativePath, binaryFolderPath); // break; // } break; case IJavaElement.COMPILATION_UNIT: File file = delta.getFile(); switch (delta.getKind()) { case IResourceDelta.CHANGED: // no need to index if the content has not changed int flags = delta.getFlags(); if ((flags & IResourceDelta.CONTENT) == 0 && (flags & IResourceDelta.ENCODING) == 0) break; // $FALL-THROUGH$ case IResourceDelta.ADDED: indexManager.addSource(file.toPath(), element.getJavaProject().getPath(), getSourceElementParser(element)); // Clean file from secondary types cache but do not update indexing secondary type cache as it will be updated through indexing itself this.manager.secondaryTypesRemoving(file, false); break; case IResourceDelta.REMOVED: indexManager.remove( file.getPath().substring(element.getJavaProject().getPath().toOSString().length()), element.getJavaProject().getPath()); // Clean file from secondary types cache and update indexing secondary type cache as indexing cannot remove secondary types from cache this.manager.secondaryTypesRemoving(file, true); break; } } }
From source file:com.codenvy.ide.ext.java.server.internal.core.NameLookup.java
License:Open Source License
protected boolean seekTypesInWorkingCopies(String name, IPackageFragment pkg, int firstDot, boolean partialMatch, String topLevelTypeName, int acceptFlags, IJavaElementRequestor requestor, boolean considerSecondaryTypes) { if (!partialMatch) { HashMap typeMap = (HashMap) (this.typesInWorkingCopies == null ? null : this.typesInWorkingCopies.get(pkg)); if (typeMap != null) { Object object = typeMap.get(topLevelTypeName); if (object instanceof IType) { IType type = getMemberType((IType) object, name, firstDot); if (!considerSecondaryTypes && !isPrimaryType(name, (IType) object, false)) return false; if (acceptType(type, acceptFlags, true/*a source type*/)) { requestor.acceptType(type); return true; // don't continue with compilation unit }//ww w . ja v a2 s.com } else if (object instanceof IType[]) { if (object == NO_TYPES) { // all types where deleted -> type is hidden, OR it is the fake type package-info String packageInfoName = String.valueOf(TypeConstants.PACKAGE_INFO_NAME); if (packageInfoName.equals(name)) requestor.acceptType(pkg.getCompilationUnit(packageInfoName.concat(SUFFIX_STRING_java)) .getType(name)); return true; } IType[] topLevelTypes = (IType[]) object; for (int i = 0, length = topLevelTypes.length; i < length; i++) { if (requestor.isCanceled()) return false; IType type = getMemberType(topLevelTypes[i], name, firstDot); if (acceptType(type, acceptFlags, true/*a source type*/)) { requestor.acceptType(type); return true; // return the first one } } } } } else { HashMap typeMap = (HashMap) (this.typesInWorkingCopies == null ? null : this.typesInWorkingCopies.get(pkg)); if (typeMap != null) { Iterator iterator = typeMap.values().iterator(); while (iterator.hasNext()) { if (requestor.isCanceled()) return false; Object object = iterator.next(); if (object instanceof IType) { if (!considerSecondaryTypes && !isPrimaryType(name, (IType) object, true)) continue; seekTypesInTopLevelType(name, firstDot, (IType) object, requestor, acceptFlags); } else if (object instanceof IType[]) { IType[] topLevelTypes = (IType[]) object; for (int i = 0, length = topLevelTypes.length; i < length; i++) seekTypesInTopLevelType(name, firstDot, topLevelTypes[i], requestor, acceptFlags); } } } } return false; }