Java Code Examples for org.eclipse.jdt.core.IPackageFragment#getClassFile()

The following examples show how to use org.eclipse.jdt.core.IPackageFragment#getClassFile() . 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: JavadocContentAccess.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Gets a reader for an package fragment's Javadoc comment content from the source attachment.
 * The content does contain only the text from the comment without the Javadoc leading star characters.
 * Returns <code>null</code> if the package fragment does not contain a Javadoc comment or if no source is available.
 * @param fragment The package fragment to get the Javadoc of.
 * @return Returns a reader for the Javadoc comment content or <code>null</code> if the member
 * does not contain a Javadoc comment or if no source is available
 * @throws JavaModelException is thrown when the package fragment's javadoc can not be accessed
 * @since 3.4
 */
private static Reader internalGetContentReader(IPackageFragment fragment) throws JavaModelException {
	IPackageFragmentRoot root= (IPackageFragmentRoot) fragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);

	//1==> Handle the case when the documentation is present in package-info.java or package-info.class file
	boolean isBinary= root.getKind() == IPackageFragmentRoot.K_BINARY;
	ITypeRoot packageInfo;
	if (isBinary) {
		packageInfo= fragment.getClassFile(PACKAGE_INFO_CLASS);
	} else {
		packageInfo= fragment.getCompilationUnit(PACKAGE_INFO_JAVA);
	}
	if (packageInfo != null && packageInfo.exists()) {
		String source = packageInfo.getSource();
		//the source can be null for some of the class files
		if (source != null) {
			Javadoc javadocNode = getPackageJavadocNode(fragment, source);
			if (javadocNode != null) {
				int start = javadocNode.getStartPosition();
				int length = javadocNode.getLength();
				return new JavaDocCommentReader(source, start, start + length - 1);
			}
		}
	}
	return null;
}
 
Example 2
Source File: JavadocHover.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static IEditorPart openDeclaration(IJavaElement element) throws PartInitException, JavaModelException {
	if (!(element instanceof IPackageFragment)) {
		return JavaUI.openInEditor(element);
	}
	
	IPackageFragment packageFragment= (IPackageFragment) element;
	ITypeRoot typeRoot;
	IPackageFragmentRoot root= (IPackageFragmentRoot) packageFragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
	if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
		typeRoot= packageFragment.getClassFile(JavaModelUtil.PACKAGE_INFO_CLASS);
	} else {
		typeRoot= packageFragment.getCompilationUnit(JavaModelUtil.PACKAGE_INFO_JAVA);
	}

	// open the package-info file in editor if one exists
	if (typeRoot.exists())
		return JavaUI.openInEditor(typeRoot);

	// open the package.html file in editor if one exists
	Object[] nonJavaResources= packageFragment.getNonJavaResources();
	for (Object nonJavaResource : nonJavaResources) {
		if (nonJavaResource instanceof IFile) {
			IFile file= (IFile) nonJavaResource;
			if (file.exists() && JavaModelUtil.PACKAGE_HTML.equals(file.getName())) {
				return EditorUtility.openInEditor(file, true);
			}
		}
	}

	// select the package in the Package Explorer if there is no associated package Javadoc file
	PackageExplorerPart view= (PackageExplorerPart) JavaPlugin.getActivePage().showView(JavaUI.ID_PACKAGES);
	view.tryToReveal(packageFragment);
	return null;
}
 
Example 3
Source File: GenericRefactoringHandleTransplanter.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
protected IClassFile transplantHandle(IPackageFragment parent, IClassFile element) {
	return parent.getClassFile(element.getElementName());
}
 
Example 4
Source File: ClasspathResourceUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Returns the given file or JAR entry if it is on the given package fragment. The file can be a Java file, class
 * file, or a non-Java resource.
 * <p>
 * This method returns null for .java files or .class files inside JARs.
 *
 * @param fileName
 *          the file name
 * @param pckgFragment
 *          the package fragment to search
 * @return the file as an IResource or IJarEntryResource, or null
 * @throws JavaModelException
 */
public static IStorage resolveFileOnPackageFragment(String fileName, IPackageFragment pckgFragment)
    throws JavaModelException {

  boolean isJavaFile = JavaCore.isJavaLikeFileName(fileName);
  boolean isClassFile = ResourceUtils.endsWith(fileName, ".class");

  // Check the non-Java resources first
  Object[] nonJavaResources = pckgFragment.getNonJavaResources();
  for (Object nonJavaResource : nonJavaResources) {
    if (nonJavaResource instanceof IFile) {
      IFile file = (IFile) nonJavaResource;
      String resFileName = file.getName();

      if (ResourceUtils.areFilenamesEqual(resFileName, fileName)) {
        // Java source files that have been excluded from the build path
        // show up as non-Java resources, but we'll ignore them since
        // they're not available on the classpath.
        if (!JavaCore.isJavaLikeFileName(resFileName)) {
          return file;
        } else {
          return null;
        }
      }
    }

    // JAR resources are not IResource's, so we need to handle them
    // differently
    if (nonJavaResource instanceof IJarEntryResource) {
      IJarEntryResource jarEntry = (IJarEntryResource) nonJavaResource;
      if (jarEntry.isFile() && ResourceUtils.areFilenamesEqual(jarEntry.getName(), fileName)) {
        return jarEntry;
      }
    }
  }

  // If we're looking for a .java or .class file, we can use the regular
  // Java Model methods.

  if (isJavaFile) {
    ICompilationUnit cu = pckgFragment.getCompilationUnit(fileName);
    if (cu.exists()) {
      return (IFile) cu.getCorrespondingResource();
    }
  }

  if (isClassFile) {
    IClassFile cf = pckgFragment.getClassFile(fileName);
    if (cf.exists()) {
      return (IFile) cf.getCorrespondingResource();
    }
  }

  return null;
}
 
Example 5
Source File: GenericRefactoringHandleTransplanter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected IClassFile transplantHandle(IPackageFragment parent, IClassFile element) {
	return parent.getClassFile(element.getElementName());
}