Search in sources :

Example 6 with PackageFragment

use of org.eclipse.jdt.internal.core.PackageFragment in project che by eclipse.

the class ToolFactory method createDefaultClassFileReader.

/**
     * Create a classfile reader onto a classfile Java element.
     * Create a default classfile reader, able to expose the internal representation of a given classfile
     * according to the decoding flag used to initialize the reader.
     * Answer null if the file named fileName doesn't represent a valid .class file.
     * <p/>
     * The decoding flags are described in IClassFileReader.
     *
     * @param classfile
     *         the classfile element to introspect
     * @param decodingFlag
     *         the flag used to decode the class file reader.
     * @return a default classfile reader
     * @see org.eclipse.jdt.core.util.IClassFileReader
     */
public static IClassFileReader createDefaultClassFileReader(IClassFile classfile, int decodingFlag) {
    IPackageFragmentRoot root = (IPackageFragmentRoot) classfile.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
    if (root != null) {
        try {
            if (root instanceof JarPackageFragmentRoot) {
                String archiveName = null;
                ZipFile jar = null;
                try {
                    jar = ((JarPackageFragmentRoot) root).getJar();
                    archiveName = jar.getName();
                } finally {
                    JavaModelManager.getJavaModelManager().closeZipFile(jar);
                }
                PackageFragment packageFragment = (PackageFragment) classfile.getParent();
                String classFileName = classfile.getElementName();
                String entryName = org.eclipse.jdt.internal.core.util.Util.concatWith(packageFragment.names, classFileName, '/');
                return createDefaultClassFileReader(archiveName, entryName, decodingFlag);
            } else {
                InputStream in = null;
                try {
                    in = ((IFile) ((JavaElement) classfile).resource()).getContents();
                    return createDefaultClassFileReader(in, decodingFlag);
                } finally {
                    if (in != null)
                        try {
                            in.close();
                        } catch (IOException e) {
                        // ignore
                        }
                }
            }
        } catch (CoreException e) {
        // unable to read
        }
    }
    return null;
}
Also used : PackageFragment(org.eclipse.jdt.internal.core.PackageFragment) JavaElement(org.eclipse.jdt.internal.core.JavaElement) IJavaElement(org.eclipse.jdt.core.IJavaElement) ZipFile(java.util.zip.ZipFile) CoreException(org.eclipse.core.runtime.CoreException) JarPackageFragmentRoot(org.eclipse.jdt.internal.core.JarPackageFragmentRoot) InputStream(java.io.InputStream) IOException(java.io.IOException) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Aggregations

PackageFragment (org.eclipse.jdt.internal.core.PackageFragment)6 IJavaElement (org.eclipse.jdt.core.IJavaElement)3 IClassFile (org.eclipse.jdt.core.IClassFile)2 CompilationUnitDeclaration (org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)2 IBinaryType (org.eclipse.jdt.internal.compiler.env.IBinaryType)2 BinaryType (org.eclipse.jdt.internal.core.BinaryType)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Iterator (java.util.Iterator)1 ZipFile (java.util.zip.ZipFile)1 IResource (org.eclipse.core.resources.IResource)1 CoreException (org.eclipse.core.runtime.CoreException)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IImportDeclaration (org.eclipse.jdt.core.IImportDeclaration)1 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 WorkingCopyOwner (org.eclipse.jdt.core.WorkingCopyOwner)1