Example usage for org.eclipse.jdt.core IPackageFragmentRoot getElementName

List of usage examples for org.eclipse.jdt.core IPackageFragmentRoot getElementName

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IPackageFragmentRoot getElementName.

Prototype

String getElementName();

Source Link

Document

Returns the name of this element.

Usage

From source file:org.org.eclipse.dws.ui.internal.wizards.pages.LookupJavadocAndSourcesForLibrariesInClasspathPage.java

License:Open Source License

/**
 * Gets the libraries with missing javadoc or sources wrappers.
 * //from w w w.  j ava 2  s.c om
 * @param javaProject
 *            the java project
 * 
 * @return the libraries with missing javadoc or sources wrappers
 * 
 * @throws JavaModelException
 *             the java model exception
 */
private Set<LibraryWithMissingJavadocOrSourcesWrapper> getLibrariesWithMissingJavadocOrSourcesWrappers(
        IJavaProject javaProject) throws JavaModelException {
    Set<IPackageFragmentRoot> packageFragmentRoots = new TreeSet<IPackageFragmentRoot>(
            new Comparator<IPackageFragmentRoot>() {
                public int compare(IPackageFragmentRoot o1, IPackageFragmentRoot o2) {
                    return o1.getElementName().compareTo(o2.getElementName());
                }

            });
    for (IPackageFragmentRoot packageFragmentRoot : javaProject.getPackageFragmentRoots()) {
        if (isLibraryWithMissingJavadocOrSource(packageFragmentRoot)) {
            packageFragmentRoots.add(packageFragmentRoot);
        }
    }
    Set<LibraryWithMissingJavadocOrSourcesWrapper> librariesWithMissingJavadocOrSources = new TreeSet<LibraryWithMissingJavadocOrSourcesWrapper>(
            new Comparator<LibraryWithMissingJavadocOrSourcesWrapper>() {

                public int compare(LibraryWithMissingJavadocOrSourcesWrapper o1,
                        LibraryWithMissingJavadocOrSourcesWrapper o2) {
                    return o1.getLibraryId().compareTo(o2.getLibraryId());
                }

            });
    for (IPackageFragmentRoot packageFragmentRoot : packageFragmentRoots) {
        String libraryPath = packageFragmentRoot.getPath().toOSString();
        final String libraryId = packageFragmentRoot.getElementName();
        Set<ArtifactVersion> possibleDependencies = findPossibleDependencies(libraryId);
        if (possibleDependencies.size() > 0) {
            Set<ArtifactVersionWrapper> artifactVersionWrappers = new TreeSet<ArtifactVersionWrapper>(
                    new ArtifactVersionWrappersComparator(libraryId));
            wrapArtifactVersions(possibleDependencies, artifactVersionWrappers, libraryId);
            boolean hasExactMatch = lookForExactMatch(artifactVersionWrappers);
            if (hasExactMatch) {
                librariesWithMissingJavadocOrSources.add(new LibraryWithMissingJavadocOrSourcesWrapper(
                        libraryPath, libraryId, artifactVersionWrappers, missesJavadoc(packageFragmentRoot),
                        missesSources(packageFragmentRoot), packageFragmentRoot));
            }
        }
    }
    librariesWithMissingJavadocOrSources = Collections.unmodifiableSet(librariesWithMissingJavadocOrSources);
    return librariesWithMissingJavadocOrSources;
}

From source file:org.seasar.s2daoplugin.cache.deployment.deployer.JarComponentAutoRegisterDeployer.java

License:Apache License

private boolean isAppliedJar(IPackageFragmentRoot jar) {
    if (jarFilePatterns == null) {
        return true;
    }/*from   w  w w. ja  va2s. c  o m*/
    for (int i = 0; i < jarFilePatterns.length; i++) {
        String jarWithoutExt = removeExtension(jar.getElementName());
        if (jarFilePatterns[i] != null && jarFilePatterns[i].matcher(jarWithoutExt).matches()) {
            return true;
        }
    }
    return false;
}

From source file:org.sf.feeling.decompiler.actions.ExportSourceAction.java

License:Open Source License

public void run() {
    if (selection == null || selection.isEmpty())
        return;/*w w w.j  av a  2s.  co m*/

    IPreferenceStore prefs = JavaDecompilerPlugin.getDefault().getPreferenceStore();
    final String decompilerType = prefs.getString(JavaDecompilerPlugin.DECOMPILER_TYPE);
    final boolean reuseBuf = prefs.getBoolean(JavaDecompilerPlugin.REUSE_BUFFER);
    final boolean always = prefs.getBoolean(JavaDecompilerPlugin.IGNORE_EXISTING);

    Object firstElement = selection.get(0);
    if (selection.size() == 1 && firstElement instanceof IClassFile) {
        IClassFile cf = (IClassFile) firstElement;
        exportClass(decompilerType, reuseBuf, always, cf);
    } else if (selection.size() == 1 && firstElement instanceof IPackageFragmentRoot) {
        IPackageFragmentRoot root = (IPackageFragmentRoot) firstElement;
        FileDialog dialog = new FileDialog(Display.getDefault().getActiveShell(), SWT.SAVE | SWT.SHEET);
        String fileName = root.getElementName();
        int index = fileName.lastIndexOf('.');
        if (index != -1) {
            fileName = fileName.substring(0, index);
        }
        dialog.setFileName(fileName + "-src"); //$NON-NLS-1$
        dialog.setFilterExtensions(new String[] { "*.zip" //$NON-NLS-1$
        });
        String file = dialog.open();
        if (file != null && file.trim().length() > 0) {
            final String projectFile = file.trim();
            try {
                final IJavaElement[] children = root.getChildren();
                exportPackagesSource(decompilerType, reuseBuf, always, projectFile, children);
            } catch (CoreException e) {
                ExceptionHandler.handle(e, Messages.getString("ExportSourceAction.ErrorDialog.Title"), //$NON-NLS-1$
                        Messages.getString("ExportSourceAction.ErrorDialog.Message.CollectClassInfo")); //$NON-NLS-1$
            }
        } else {
            return;
        }
    } else {
        IPackageFragmentRoot root = null;
        if (firstElement instanceof IClassFile) {
            root = (IPackageFragmentRoot) ((IClassFile) firstElement).getParent().getParent();
        } else if (firstElement instanceof IPackageFragment) {
            root = (IPackageFragmentRoot) ((IPackageFragment) firstElement).getParent();
        }
        if (root == null)
            return;
        FileDialog dialog = new FileDialog(Display.getDefault().getActiveShell(), SWT.SAVE | SWT.SHEET);
        String fileName = root.getElementName();
        int index = fileName.lastIndexOf('.');
        if (index != -1) {
            fileName = fileName.substring(0, index);
        }
        dialog.setFileName(fileName + "-src"); //$NON-NLS-1$
        dialog.setFilterExtensions(new String[] { "*.zip" //$NON-NLS-1$
        });
        String file = dialog.open();
        if (file != null && file.trim().length() > 0) {
            final String projectFile = file.trim();

            exportPackagesSource(decompilerType, reuseBuf, always, projectFile,
                    (IJavaElement[]) selection.toArray(new IJavaElement[0]));
        } else {
            return;
        }
    }
}

From source file:org.springframework.ide.eclipse.internal.uaa.monitor.LibraryUsageMonitor.java

License:Open Source License

@SuppressWarnings("deprecation")
private ProductMatch readPackageFragmentRoot(IJavaProject source, IPackageFragmentRoot entry)
        throws JavaModelException {
    // Quick assertion that we only check JARs that exist
    if (!entry.exists() || !entry.isArchive()) {
        return null;
    }//  w w  w . j  a va 2 s  . c o  m

    // Check for previous matches or misses
    if (matches.containsKey(entry.getElementName())) {
        ProductMatch match = matches.get(entry.getElementName());
        return match;
    } else if (noMatches.contains(entry.getElementName())) {
        return null;
    }

    String groupId = null;
    String artifactId = null;
    String version = null;
    ProductInfo productInfo = null;

    JarEntryDirectory metaInfDirectory = getJarEntryDirectory(entry);

    // Step 1: Check META-INF/maven
    if (metaInfDirectory != null) {
        for (IJarEntryResource jarEntryResource : metaInfDirectory.getChildren()) {
            if ("maven".equals(jarEntryResource.getName())) {
                Product product = readMaven(jarEntryResource, entry);
                if (product != null) {
                    groupId = product.getGroupId();
                    artifactId = product.getArtifactId();
                    version = product.getVersion();
                    break;
                }
            }
        }
    }

    // Step 2: Check path if it follows the Maven convention of groupId/artifactId/version
    if (artifactId == null || groupId == null) {
        IPath jarPath = entry.getPath();

        IPath m2RepoPath = JavaCore.getClasspathVariable(M2_REPO);
        if (m2RepoPath == null) {
            m2RepoPath = ResourcesPlugin.getWorkspace().getPathVariableManager().getValue(M2_REPO);
        }
        if (m2RepoPath != null && m2RepoPath.isPrefixOf(jarPath)) {
            jarPath = jarPath.removeFirstSegments(m2RepoPath.segmentCount());
            int segments = jarPath.segmentCount();
            for (int i = 0; i < segments - 1; i++) {
                if (i == 0) {
                    groupId = jarPath.segment(i);
                } else if (i > 0 && i < segments - 3) {
                    groupId += "." + jarPath.segment(i);
                } else if (i == segments - 3) {
                    artifactId = jarPath.segment(i);
                } else if (i == segments - 2) {
                    version = jarPath.segment(i);
                }
            }
        }
    }

    // Step 3: Check for typeName match
    if (artifactId == null || groupId == null) {
        for (ProductInfo info : getProducts()) {
            String typeName = info.getTypeName();
            String packageName = "";
            if (typeName != null) {
                int i = typeName.lastIndexOf('.');
                if (i > 0) {
                    packageName = typeName.substring(0, i);
                    typeName = typeName.substring(i + 1);
                }
                IPackageFragment packageFragment = entry.getPackageFragment(packageName);
                if (packageFragment.exists()) {
                    if (packageFragment.getClassFile(typeName + ClassUtils.CLASS_FILE_SUFFIX).exists()) {
                        artifactId = info.getArtifactId();
                        groupId = info.getGroupId();
                        productInfo = info;
                        break;
                    }
                }
            }
        }
    }

    // Step 4: Obtain version from MANIFEST.MF
    if (groupId != null && artifactId != null && metaInfDirectory != null && version == null) {
        for (IJarEntryResource jarEntryResource : metaInfDirectory.getChildren()) {
            if (MANIFEST_FILE_NAME.equals(jarEntryResource.getName())) {
                version = readManifest(jarEntryResource, entry);
            }
        }
    }

    // Step 5: Obtain version from file name
    if (groupId != null && artifactId != null && version == null && entry.getPath() != null) {
        String fileName = entry.getPath().lastSegment();

        // Use regular expression to match any version number
        Matcher matcher = VERSION_PATTERN.matcher(fileName);
        if (matcher.matches()) {
            StringBuilder builder = new StringBuilder();
            for (int i = 1; i <= matcher.groupCount(); i++) {
                builder.append(matcher.group(i));
            }
            version = builder.toString();
        }
    }

    // Step 6: Construct the ProductMatch
    ProductMatch productMatch = null;
    if (productInfo == null) {
        for (ProductInfo info : getProducts()) {
            if (info.getArtifactId().equals(artifactId) && info.getGroupId().equals(groupId)) {
                productInfo = info;
                productMatch = new ProductMatch(info, version);
                break;
            }
        }
    } else {
        productMatch = new ProductMatch(productInfo, version);
    }

    // Step 7: Store ProductMatch for faster access in subsequent runs
    if (productMatch != null) {
        matches.put(entry.getElementName(), productMatch);
        return productMatch;
    } else {
        noMatches.add(entry.getElementName());
        return null;
    }
}