Example usage for org.eclipse.jdt.core.util IClassFileReader getSourceFileAttribute

List of usage examples for org.eclipse.jdt.core.util IClassFileReader getSourceFileAttribute

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.util IClassFileReader getSourceFileAttribute.

Prototype

ISourceAttribute getSourceFileAttribute();

Source Link

Document

Answer the source file attribute, if it exists, null otherwise.

Usage

From source file:com.redhat.ceylon.eclipse.code.wizard.ClassPathDetector.java

License:Open Source License

private IPath detectOutputFolder() throws CoreException {
    HashSet<IPath> classFolders = new HashSet<IPath>();

    for (Iterator<IResource> iter = fClassFiles.iterator(); iter.hasNext();) {
        IFile file = (IFile) iter.next();
        IClassFileReader reader = null;
        InputStream content = null;
        try {/*from  ww w .  j a v a2  s .  co m*/
            content = file.getContents();
            reader = ToolFactory.createDefaultClassFileReader(content, IClassFileReader.CLASSFILE_ATTRIBUTES);
        } finally {
            try {
                if (content != null)
                    content.close();
            } catch (IOException e) {
                throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.ERROR,
                        Messages.format("error closing file",
                                BasicElementLabels.getPathLabel(file.getFullPath(), false)),
                        e));
            }
        }
        if (reader == null) {
            continue; // problematic class file
        }
        char[] className = reader.getClassName();
        ISourceAttribute sourceAttribute = reader.getSourceFileAttribute();
        if (className != null && sourceAttribute != null && sourceAttribute.getSourceFileName() != null) {
            IPath packPath = file.getParent().getFullPath();
            int idx = CharOperation.lastIndexOf('/', className) + 1;
            IPath relPath = new Path(new String(className, 0, idx));
            IPath cuPath = relPath.append(new String(sourceAttribute.getSourceFileName()));

            IPath resPath = null;
            if (idx == 0) {
                resPath = packPath;
            } else {
                IPath folderPath = getFolderPath(packPath, relPath);
                if (folderPath != null) {
                    resPath = folderPath;
                }
            }
            if (resPath != null) {
                IPath path = findInSourceFolders(cuPath);
                if (path != null) {
                    return resPath;
                } else {
                    classFolders.add(resPath);
                }
            }
        }
    }
    IPath projPath = fProject.getFullPath();
    if (fSourceFolders.size() == 1 && classFolders.isEmpty() && fSourceFolders.get(projPath) != null) {
        return projPath;
    } else {
        IPath path = projPath
                .append(PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.SRCBIN_BINNAME));
        while (classFolders.contains(path)) {
            path = new Path(path.toString() + '1');
        }
        return path;
    }
}

From source file:org.eclipse.ajdt.internal.ui.wizards.exports.AJJarFileExportOperation.java

License:Open Source License

private Map<String, ArrayList<IFile>> buildJavaToClassMap(IContainer container) throws CoreException {
    if (container == null || !container.isAccessible())
        return new HashMap<String, ArrayList<IFile>>(0);
    /*/*  ww w.  j a v  a2 s .  c o m*/
     * XXX: Bug 6584: Need a way to get class files for a java file (or CU)
     */
    IClassFileReader cfReader = null;
    IResource[] members = container.members();
    Map<String, ArrayList<IFile>> map = new HashMap<String, ArrayList<IFile>>(members.length);
    for (int i = 0; i < members.length; i++) {
        if (isClassFile(members[i])) {
            IFile classFile = (IFile) members[i];
            IPath location = classFile.getLocation();
            if (location != null) {
                File file = location.toFile();
                cfReader = ToolFactory.createDefaultClassFileReader(location.toOSString(),
                        IClassFileReader.CLASSFILE_ATTRIBUTES);
                if (cfReader != null) {
                    ISourceAttribute sourceAttribute = cfReader.getSourceFileAttribute();
                    if (sourceAttribute == null) {
                        /*
                         * Can't fully build the map because one or more
                         * class file does not contain the name of its 
                         * source file.
                         */
                        addWarning(Messages.format(
                                JarPackagerMessages.JarFileExportOperation_classFileWithoutSourceFileAttribute,
                                file), null);
                        return null;
                    }
                    String javaName = new String(sourceAttribute.getSourceFileName());
                    ArrayList<IFile> classFiles = map.get(javaName);
                    if (classFiles == null) {
                        classFiles = new ArrayList<IFile>(3);
                        map.put(javaName, classFiles);
                    }
                    classFiles.add(classFile);
                }
            }
        }
    }
    return map;
}