Example usage for org.eclipse.jdt.internal.core PackageFragment getParent

List of usage examples for org.eclipse.jdt.internal.core PackageFragment getParent

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core PackageFragment getParent.

Prototype

@Override
public IJavaElement getParent() 

Source Link

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.ClassFile.java

License:Open Source License

private IBinaryType getJarBinaryTypeInfo(PackageFragment pkg, boolean fullyInitialize)
        throws CoreException, IOException, ClassFormatException {
    JarPackageFragmentRoot root = (JarPackageFragmentRoot) pkg.getParent();
    ZipFile zip = null;//from   w  w  w .  j av a2 s.c om
    try {
        zip = root.getJar();
        String entryName = Util.concatWith(pkg.names, getElementName(), '/');
        ZipEntry ze = zip.getEntry(entryName);
        if (ze != null) {
            byte contents[] = org.eclipse.jdt.internal.compiler.util.Util.getZipEntryByteContent(ze, zip);
            String fileName = root.getHandleIdentifier() + IDependent.JAR_FILE_ENTRY_SEPARATOR + entryName;
            return new ClassFileReader(contents, fileName.toCharArray(), fullyInitialize);
        }
    } finally {
        manager.closeZipFile(zip);
    }
    return null;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.NameLookup.java

License:Open Source License

public NameLookup(IPackageFragmentRoot[] packageFragmentRoots, HashtableOfArrayToObject packageFragments,
        ICompilationUnit[] workingCopies, Map rootToResolvedEntries, JavaModelManager manager) {
    this.manager = manager;
    long start = -1;
    if (VERBOSE) {
        Util.verbose(" BUILDING NameLoopkup"); //$NON-NLS-1$
        Util.verbose(" -> pkg roots size: " + (packageFragmentRoots == null ? 0 : packageFragmentRoots.length)); //$NON-NLS-1$
        Util.verbose(" -> pkgs size: " + (packageFragments == null ? 0 : packageFragments.size())); //$NON-NLS-1$
        Util.verbose(" -> working copy size: " + (workingCopies == null ? 0 : workingCopies.length)); //$NON-NLS-1$
        start = System.currentTimeMillis();
    }//from   ww  w .  j  a va  2  s  . com
    this.packageFragmentRoots = packageFragmentRoots;
    if (workingCopies == null) {
        this.packageFragments = packageFragments;
    } else {
        // clone tables as we're adding packages from working copies
        try {
            this.packageFragments = (HashtableOfArrayToObject) packageFragments.clone();
        } catch (CloneNotSupportedException e1) {
            // ignore (implementation of HashtableOfArrayToObject supports cloning)
        }
        this.typesInWorkingCopies = new HashMap();
        HashtableOfObjectToInt rootPositions = new HashtableOfObjectToInt();
        for (int i = 0, length = packageFragmentRoots.length; i < length; i++) {
            rootPositions.put(packageFragmentRoots[i], i);
        }
        for (int i = 0, length = workingCopies.length; i < length; i++) {
            ICompilationUnit workingCopy = workingCopies[i];
            PackageFragment pkg = (PackageFragment) workingCopy.getParent();
            IPackageFragmentRoot root = (IPackageFragmentRoot) pkg.getParent();
            int rootPosition = rootPositions.get(root);
            if (rootPosition == -1)
                continue; // working copy is not visible from this project (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=169970)
            HashMap typeMap = (HashMap) this.typesInWorkingCopies.get(pkg);
            if (typeMap == null) {
                typeMap = new HashMap();
                this.typesInWorkingCopies.put(pkg, typeMap);
            }
            try {
                IType[] types = workingCopy.getTypes();
                int typeLength = types.length;
                if (typeLength == 0) {
                    String typeName = Util.getNameWithoutJavaLikeExtension(workingCopy.getElementName());
                    typeMap.put(typeName, NO_TYPES);
                } else {
                    for (int j = 0; j < typeLength; j++) {
                        IType type = types[j];
                        String typeName = type.getElementName();
                        Object existing = typeMap.get(typeName);
                        if (existing == null) {
                            typeMap.put(typeName, type);
                        } else if (existing instanceof IType) {
                            typeMap.put(typeName, new IType[] { (IType) existing, type });
                        } else {
                            IType[] existingTypes = (IType[]) existing;
                            int existingTypeLength = existingTypes.length;
                            System.arraycopy(existingTypes, 0,
                                    existingTypes = new IType[existingTypeLength + 1], 0, existingTypeLength);
                            existingTypes[existingTypeLength] = type;
                            typeMap.put(typeName, existingTypes);
                        }
                    }
                }
            } catch (JavaModelException e) {
                // working copy doesn't exist -> ignore
            }

            // add root of package fragment to cache
            String[] pkgName = pkg.names;
            Object existing = this.packageFragments.get(pkgName);
            if (existing == null || existing == JavaProjectElementInfo.NO_ROOTS) {
                this.packageFragments.put(pkgName, root);
                // ensure super packages (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=119161)
                // are also in the map
                JavaProjectElementInfo.addSuperPackageNames(pkgName, this.packageFragments);
            } else {
                if (existing instanceof PackageFragmentRoot) {
                    int exisitingPosition = rootPositions.get(existing);
                    if (rootPosition != exisitingPosition) { // if not equal
                        this.packageFragments.put(pkgName,
                                exisitingPosition < rootPosition
                                        ? new IPackageFragmentRoot[] { (PackageFragmentRoot) existing, root }
                                        : new IPackageFragmentRoot[] { root, (PackageFragmentRoot) existing });
                    }
                } else {
                    // insert root in the existing list
                    IPackageFragmentRoot[] roots = (IPackageFragmentRoot[]) existing;
                    int rootLength = roots.length;
                    int insertionIndex = 0;
                    for (int j = 0; j < rootLength; j++) {
                        int existingPosition = rootPositions.get(roots[j]);
                        if (rootPosition > existingPosition) {
                            // root is after this index
                            insertionIndex = j;
                        } else if (rootPosition == existingPosition) {
                            // root already in the existing list
                            insertionIndex = -1;
                            break;
                        } else if (rootPosition < existingPosition) {
                            // root is before this index (thus it is at the insertion index)
                            break;
                        }
                    }
                    if (insertionIndex != -1) {
                        IPackageFragmentRoot[] newRoots = new IPackageFragmentRoot[rootLength + 1];
                        System.arraycopy(roots, 0, newRoots, 0, insertionIndex);
                        newRoots[insertionIndex] = root;
                        System.arraycopy(roots, insertionIndex, newRoots, insertionIndex + 1,
                                rootLength - insertionIndex);
                        this.packageFragments.put(pkgName, newRoots);
                    }
                }
            }
        }
    }

    this.rootToResolvedEntries = rootToResolvedEntries;
    if (VERBOSE) {
        Util.verbose(" -> spent: " + (System.currentTimeMillis() - start) + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java

License:Open Source License

public static ClassFileReader classFileReader(IType type) {
    IClassFile classFile = type.getClassFile();
    JavaModelManager manager = JavaModelManager.getJavaModelManager();
    if (classFile.isOpen())
        return (ClassFileReader) manager.getInfo(type);

    PackageFragment pkg = (PackageFragment) type.getPackageFragment();
    IPackageFragmentRoot root = (IPackageFragmentRoot) pkg.getParent();
    try {//from ww  w .  j a v  a2  s.c om
        if (!root.isArchive())
            return Util.newClassFileReader(((JavaElement) type).resource());

        ZipFile zipFile = null;
        try {
            IPath zipPath = root.getPath();
            if (JavaModelManager.ZIP_ACCESS_VERBOSE)
                System.out.println("(" + Thread.currentThread()
                        + ") [MatchLocator.classFileReader()] Creating ZipFile on " + zipPath); //$NON-NLS-1$   //$NON-NLS-2$
            zipFile = manager.getZipFile(zipPath);
            String classFileName = classFile.getElementName();
            String path = Util.concatWith(pkg.names, classFileName, '/');
            return ClassFileReader.read(zipFile, path);
        } finally {
            manager.closeZipFile(zipFile);
        }
    } catch (ClassFormatException e) {
        // invalid class file: return null
    } catch (CoreException e) {
        // cannot read class file: return null
    } catch (IOException e) {
        // cannot read class file: return null
    }
    return null;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java

License:Open Source License

protected IBinaryType getBinaryInfo(ClassFile classFile, IResource resource) throws CoreException {
    BinaryType binaryType = (BinaryType) classFile.getType();
    if (classFile.isOpen())
        return (IBinaryType) binaryType.getElementInfo(); // reuse the info from the java model cache

    // create a temporary info
    IBinaryType info;//from   w ww.  j av a  2 s  .c  om
    try {
        PackageFragment pkg = (PackageFragment) classFile.getParent();
        PackageFragmentRoot root = (PackageFragmentRoot) pkg.getParent();
        if (root.isArchive()) {
            // class file in a jar
            String classFileName = classFile.getElementName();
            String classFilePath = Util.concatWith(pkg.names, classFileName, '/');
            ZipFile zipFile = null;
            try {
                zipFile = ((JarPackageFragmentRoot) root).getJar();
                info = ClassFileReader.read(zipFile, classFilePath);
            } finally {
                JavaModelManager.getJavaModelManager().closeZipFile(zipFile);
            }
        } else {
            // class file in a directory
            info = Util.newClassFileReader(resource);
        }
        if (info == null)
            throw binaryType.newNotPresentException();
        return info;
    } catch (ClassFormatException e) {
        //e.printStackTrace();
        return null;
    } catch (IOException e) {
        throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
    }
}

From source file:org.eclipse.jdt.internal.core.hierarchy.HierarchyBuilder.java

License:Open Source License

/**
* Create a type info from the given class file in a jar and adds it to the given list of infos.
*//*from ww  w . ja v a2s.c o  m*/
protected IBinaryType createInfoFromClassFileInJar(Openable classFile) {
    PackageFragment pkg = (PackageFragment) classFile.getParent();
    String classFilePath = Util.concatWith(pkg.names, classFile.getElementName(), '/');
    IBinaryType info = null;
    java.util.zip.ZipFile zipFile = null;
    try {
        zipFile = ((JarPackageFragmentRoot) pkg.getParent()).getJar();
        info = org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader.read(zipFile, classFilePath);
    } catch (org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException e) {
        if (TypeHierarchy.DEBUG) {
            e.printStackTrace();
        }
        return null;
    } catch (java.io.IOException e) {
        if (TypeHierarchy.DEBUG) {
            e.printStackTrace();
        }
        return null;
    } catch (CoreException e) {
        if (TypeHierarchy.DEBUG) {
            e.printStackTrace();
        }
        return null;
    } finally {
        JavaModelManager.getJavaModelManager().closeZipFile(zipFile);
    }
    this.infoToHandle.put(info, classFile);
    return info;
}

From source file:org.eclipse.jdt.internal.core.search.matching.MatchLocator.java

License:Open Source License

public static ClassFileReader classFileReader(IType type) {
    IClassFile classFile = type.getClassFile();
    JavaModelManager manager = JavaModelManager.getJavaModelManager();
    if (classFile.isOpen())
        return (ClassFileReader) manager.getInfo(type);

    PackageFragment pkg = (PackageFragment) type.getPackageFragment();
    IPackageFragmentRoot root = (IPackageFragmentRoot) pkg.getParent();
    try {//  w  w  w.j  a v  a 2  s  .c  om
        if (!root.isArchive())
            return Util.newClassFileReader(((JavaElement) type).resource());

        ZipFile zipFile = null;
        try {
            IPath zipPath = root.getPath();
            if (JavaModelManager.ZIP_ACCESS_VERBOSE)
                System.out.println("(" + Thread.currentThread() //$NON-NLS-1$
                        + ") [MatchLocator.classFileReader()] Creating ZipFile on " + zipPath); //$NON-NLS-1$
            zipFile = manager.getZipFile(zipPath);
            String classFileName = classFile.getElementName();
            String path = Util.concatWith(pkg.names, classFileName, '/');
            return ClassFileReader.read(zipFile, path);
        } finally {
            manager.closeZipFile(zipFile);
        }
    } catch (ClassFormatException e) {
        // invalid class file: return null
    } catch (CoreException e) {
        // cannot read class file: return null
    } catch (IOException e) {
        // cannot read class file: return null
    }
    return null;
}

From source file:org.eclipse.jdt.internal.core.search.matching.MatchLocator.java

License:Open Source License

protected IBinaryType getBinaryInfo(ClassFile classFile, IResource resource) throws CoreException {
    BinaryType binaryType = (BinaryType) classFile.getType();
    if (classFile.isOpen())
        return (IBinaryType) binaryType.getElementInfo(); // reuse the info from the java model cache

    // create a temporary info
    IBinaryType info;/*  www  . j a v  a  2  s.  c  o m*/
    try {
        PackageFragment pkg = (PackageFragment) classFile.getParent();
        PackageFragmentRoot root = (PackageFragmentRoot) pkg.getParent();
        if (root.isArchive()) {
            // class file in a jar
            String classFileName = classFile.getElementName();
            String classFilePath = Util.concatWith(pkg.names, classFileName, '/');
            ZipFile zipFile = null;
            try {
                zipFile = ((JarPackageFragmentRoot) root).getJar();
                info = ClassFileReader.read(zipFile, classFilePath);
            } finally {
                JavaModelManager.getJavaModelManager().closeZipFile(zipFile);
            }
        } else {
            // class file in a directory
            info = Util.newClassFileReader(resource);
        }
        if (info == null)
            throw binaryType.newNotPresentException();
        return info;
    } catch (ClassFormatException e) {
        //e.printStackTrace();
        return null;
    } catch (java.io.IOException e) {
        throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
    }
}

From source file:org.springframework.ide.eclipse.beans.core.internal.model.BeansJavaConfig.java

License:Open Source License

public BeansJavaConfig(IBeansProject project, IType configClass, String configClassName, Type type) {
    super(project, BeansConfigFactory.JAVA_CONFIG_TYPE + configClassName, type);

    this.configClass = configClass;
    this.configClassName = configClassName;

    modificationTimestamp = IResource.NULL_STAMP;

    if (this.configClass != null) {
        IResource resource = this.configClass.getResource();
        if (resource != null && resource instanceof IFile) {
            file = (IFile) resource;/*from  www  . j ava 2 s .c om*/
        } else {
            IClassFile classFile = configClass.getClassFile();

            PackageFragment pkg = (PackageFragment) configClass.getPackageFragment();
            IPackageFragmentRoot root = (IPackageFragmentRoot) pkg.getParent();

            if (root.isArchive()) {
                IPath zipPath = root.getPath();

                String classFileName = classFile.getElementName();
                String path = Util.concatWith(pkg.names, classFileName, '/');
                file = new ExternalFile(zipPath.toFile(), path, project.getProject());
            }
        }
    }

    if (file == null || !file.exists()) {
        modificationTimestamp = IResource.NULL_STAMP;
        String msg = "Beans Java config class '" + configClassName + "' not accessible";
        problems = new CopyOnWriteArraySet<ValidationProblem>();
        problems.add(new ValidationProblem(IMarker.SEVERITY_ERROR, msg, file, -1));
    } else {
        modificationTimestamp = file.getModificationStamp();
        try {
            file.setSessionProperty(IBeansConfig.CONFIG_FILE_TAG, IBeansConfig.CONFIG_FILE_TAG_VALUE);
        } catch (CoreException e) {
            BeansCorePlugin.log(new Status(IStatus.WARNING, BeansCorePlugin.PLUGIN_ID,
                    String.format("Error occured while tagging config file '%s'", file.getFullPath()), e));
        }
    }

}