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

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

Introduction

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

Prototype

IJavaProject getJavaProject();

Source Link

Document

Returns the Java project this element is contained in, or null if this element is not contained in any Java project (for instance, the IJavaModel is not contained in any Java project).

Usage

From source file:com.liferay.ide.portlet.ui.wizard.NewPortletClassWizardPage.java

License:Open Source License

protected void handleClassButtonSelected(Control control, String baseClass, String title, String message) {
    getControl().setCursor(new Cursor(getShell().getDisplay(), SWT.CURSOR_WAIT));

    IPackageFragmentRoot packRoot = (IPackageFragmentRoot) model
            .getProperty(INewJavaClassDataModelProperties.JAVA_PACKAGE_FRAGMENT_ROOT);

    if (packRoot == null) {
        return;/*from ww  w  .j ava  2  s. co m*/
    }

    // this eliminates the non-exported classpath entries
    // final IJavaSearchScope scope =
    // TypeSearchEngine.createJavaSearchScopeForAProject(packRoot.getJavaProject(),
    // true, true);

    IJavaSearchScope scope = null;

    try {
        IType type = packRoot.getJavaProject().findType(baseClass);

        if (type == null) {
            return;
        }

        scope = BasicSearchEngine.createHierarchyScope(type);
    } catch (JavaModelException e) {
        PortletUIPlugin.logError(e);
        return;
    }

    // This includes all entries on the classpath. This behavior is
    // identical
    // to the Super Class Browse Button on the Create new Java Class Wizard
    // final IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new
    // IJavaElement[] {root.getJavaProject()} );
    FilteredTypesSelectionDialog dialog = new FilteredTypesSelectionDialogEx(getShell(), false,
            getWizard().getContainer(), scope, IJavaSearchConstants.CLASS);
    dialog.setTitle(title);
    dialog.setMessage(message);

    if (dialog.open() == Window.OK) {
        IType type = (IType) dialog.getFirstResult();

        String classFullPath = J2EEUIMessages.EMPTY_STRING;

        if (type != null) {
            classFullPath = type.getFullyQualifiedName();
        }

        if (control instanceof Text) {
            ((Text) control).setText(classFullPath);
        } else if (control instanceof Combo) {
            ((Combo) control).setText(classFullPath);
        }

        getControl().setCursor(null);

        return;
    }

    getControl().setCursor(null);
}

From source file:com.motorola.studio.android.model.BuildingBlockModel.java

License:Apache License

/**
 * Configure source folder and package from workbench selection
 * @param selection//from w  w  w . ja  va2  s. com
 */
public void configure(IStructuredSelection selection) {
    try {
        IPackageFragmentRoot srcFolder = extractPackageFragmentRoot(selection);
        setPackageFragmentRoot(srcFolder);
        IJavaProject javaProject = srcFolder == null ? null : srcFolder.getJavaProject();
        setPackageFragment(extractPackageFragment(selection, javaProject));
        if (javaProject != null) {
            apiVersion = AndroidUtils.getApiVersionNumberForProject(javaProject.getProject());
        }

    } catch (Exception e) {
        StudioLogger.error(BuildingBlockModel.class, "Error configuring building block from selection.", e);
    }
}

From source file:com.motorola.studio.android.model.BuildingBlockModel.java

License:Apache License

/**
 * Extract source folder from selection.
 * @param selection/*from  ww  w  .  j a  va  2  s.  c o  m*/
 * @return
 * @throws CoreException
 */
private static IPackageFragmentRoot extractPackageFragmentRoot(IStructuredSelection selection)
        throws CoreException {
    IPackageFragmentRoot pack = null;
    Object selectionElement = selection.getFirstElement();

    if (selectionElement instanceof IPackageFragmentRoot) {
        pack = (IPackageFragmentRoot) selectionElement;
    } else if (selectionElement instanceof IJavaElement) {
        pack = (IPackageFragmentRoot) ((IJavaElement) selectionElement)
                .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
        if (pack == null) {
            IJavaProject element = ((IJavaElement) selectionElement).getJavaProject();

            for (IPackageFragmentRoot root : element.getPackageFragmentRoots()) {
                if (root.getResource() != null) {
                    boolean isSrc = (root.getElementType()
                            & IPackageFragmentRoot.K_SOURCE) == IPackageFragmentRoot.K_SOURCE;
                    boolean isGen = root.getElementName().equals(IAndroidConstants.GEN_SRC_FOLDER)
                            && (root.getParent() instanceof IJavaProject);
                    if (isSrc && !isGen) {
                        pack = root;
                        break;
                    }
                }
            }
        }
    } else if (selectionElement instanceof IResource) {
        IJavaProject element = JavaCore.create(((IResource) selectionElement).getProject());

        if (element.isOpen()) {
            for (IPackageFragmentRoot root : element.getPackageFragmentRoots()) {
                if (root.getResource() != null) {
                    boolean isSrc = (root.getElementType()
                            & IPackageFragmentRoot.K_SOURCE) == IPackageFragmentRoot.K_SOURCE;
                    boolean isGen = root.getElementName().equals(IAndroidConstants.GEN_SRC_FOLDER)
                            && (root.getParent() instanceof IJavaProject);
                    if (isSrc && !isGen) {
                        pack = root;
                        break;
                    }
                }
            }
        }
    } else {
        IJavaProject[] allProjects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot())
                .getJavaProjects();
        if ((allProjects != null) && (allProjects.length > 0)) {
            for (IJavaProject project : allProjects) {
                if (project.getResource().getProject().hasNature(IAndroidConstants.ANDROID_NATURE)) {
                    IPackageFragmentRoot[] roots = project.getPackageFragmentRoots();

                    if ((roots != null) && (roots.length > 0)) {
                        boolean found = false;

                        for (IPackageFragmentRoot root : roots) {
                            boolean isSrc = (root.getElementType()
                                    & IPackageFragmentRoot.K_SOURCE) == IPackageFragmentRoot.K_SOURCE;
                            boolean isGen = root.getElementName().equals(IAndroidConstants.GEN_SRC_FOLDER)
                                    && (root.getParent() instanceof IJavaProject);
                            if (isSrc && !isGen) {
                                found = true;
                                pack = root;
                                break;
                            }
                        }

                        if (found) {
                            break;
                        }
                    }
                }
            }
        }
    }

    if (pack != null) {
        if (!pack.getJavaProject().getProject().hasNature(IAndroidConstants.ANDROID_NATURE)) {
            pack = extractPackageFragmentRoot(new TreeSelection());
        }
    }
    return pack;
}

From source file:com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage.java

License:Apache License

private void updatePackage(IPackageFragmentRoot packageFragmentRoot) {
    if (packageFragmentRoot != null) {
        IJavaProject project = null;/*from  w  w w  .j  a v  a2 s. c o m*/
        IPackageFragment pack = null;

        project = packageFragmentRoot.getJavaProject();
        try {
            pack = EclipseUtils.getDefaultPackageFragment(project);
            getBuildBlock().setPackageFragment(pack);
        } catch (JavaModelException e) {
            StudioLogger.error(NewBuildingBlocksWizardPage.class, "Error getting default package fragment.", e); //$NON-NLS-1$
            // do nothing            
        }
        setPackageFragment(pack, true);
        handleFieldChanged(NewTypeWizardPage.PACKAGE);
    }
}

From source file:com.mountainminds.eclemma.autoMerge.OldFileAnalyzer.java

License:Open Source License

private AnalyzedNodes analyzeInternal(final IPackageFragmentRoot root) throws CoreException {
    IPath location = null;//w ww.j  a  va2  s  .  c  o m
    try {
        location = root.getJavaProject().getOutputLocation();

        if (location == null) {
            TRACER.trace("No class files found for package fragment root {0}", //$NON-NLS-1$
                    root.getPath());
            return AnalyzedNodes.EMPTY;
        }

        AnalyzedNodes nodes = cache.get(getClassfilesLocation(root));
        if (nodes != null) {
            ConsoleMessage.showMessage("cache existed"); //$NON-NLS-1$
            return nodes;
        }

        final CoverageBuilder builder = new CoverageBuilder();
        final Analyzer analyzer = new Analyzer(executiondata, builder);
        // binroot = resource.getProjectRelativePath();
        analyzePackage = new HashSet<String>();
        for (SourceFileInfo fileInfo : oldfiles.values()) {
            String className = fileInfo.getClassName();
            int pos = className.lastIndexOf("/");
            String packageName = className.substring(0, pos);
            if (analyzePackage.contains(packageName))
                continue;
            analyzePackage.add(packageName);
            IFolder packagefolder = root.getJavaProject().getProject().getParent()
                    .getFolder(location.append(packageName));

            for (IResource classresource : packagefolder.members()) {

                if ((classresource instanceof IFile) == false)
                    continue;
                HashSet<String> readMainClass = new HashSet<String>();
                final IFile file = (IFile) classresource;
                if (file.getFileExtension().equals("class") == false)
                    continue;
                // String classNanme = resource.getName();

                String classname = packageName + "/" + file.getName();
                classname = classname.substring(0, classname.lastIndexOf(".class"));
                // TODO GET PROJECT PATH
                // file.getp
                String noInnerPerf = classname;
                if (classname.indexOf("$") > 0)
                    noInnerPerf = classname.substring(0, classname.indexOf("$"));

                if (oldfiles.containsKey(noInnerPerf + ".java") && !sessionClass.contains(classname)) {
                    final InputStream in = file.getContents(true);
                    try {
                        analyzer.analyzeAll(in, classname);
                        System.err.println("Analyse old class..." + classname);
                        // inner class  class
                        if (!classname.equals(noInnerPerf) && readMainClass.contains(noInnerPerf) == false) {
                            String parentclassname = file.getName().substring(0, file.getName().indexOf("$"));
                            System.err.println("Analyse old class..." + packageName + "/" + parentclassname);
                            IFile parentfileFile = (IFile) packagefolder.findMember(parentclassname + ".class");
                            InputStream parentin = parentfileFile.getContents(true);
                            analyzer.analyzeAll(parentin, noInnerPerf);
                            readMainClass.add(noInnerPerf);
                            System.err.println("Analyse old class..." + noInnerPerf);
                        }
                    } finally {
                        in.close();
                    }
                }
            }
        }
        // new OldTreeWalker(analyzer, oldfiles).walk(location);
        nodes = new AnalyzedNodes(builder.getClasses(), builder.getSourceFiles());
        cache.put(getClassfilesLocation(root), nodes);
        return nodes;
    } catch (Exception e) {
        throw new CoreException(
                EclEmmaStatus.BUNDLE_ANALYSIS_ERROR.getStatus(root.getElementName(), location, e));
    }
}

From source file:com.mountainminds.eclemma.autoMerge.OldFileAnalyzer.java

License:Open Source License

private IResource getClassfilesLocation(IPackageFragmentRoot root) throws CoreException {

    // For binary roots the underlying resource directly points to class files:
    if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
        return root.getResource();
    }/* www  .jav a2 s. c  om*/

    // For source roots we need to find the corresponding output folder:
    IPath path = root.getRawClasspathEntry().getOutputLocation();
    if (path == null) {
        path = root.getJavaProject().getOutputLocation();
    }
    return root.getResource().getWorkspace().getRoot().findMember(path);
}

From source file:com.mountainminds.eclemma.core.ScopeUtils.java

License:Open Source License

/**
 * Remove all JRE runtime entries from the given set
 * //from w ww  .ja va2  s .c om
 * @param scope
 *          set to filter
 * @return filtered set without JRE runtime entries
 */
public static Set<IPackageFragmentRoot> filterJREEntries(Collection<IPackageFragmentRoot> scope)
        throws JavaModelException {
    final Set<IPackageFragmentRoot> filtered = new HashSet<IPackageFragmentRoot>();
    for (final IPackageFragmentRoot root : scope) {
        final IClasspathEntry entry = root.getRawClasspathEntry();
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_SOURCE:
        case IClasspathEntry.CPE_LIBRARY:
        case IClasspathEntry.CPE_VARIABLE:
            filtered.add(root);
            break;
        case IClasspathEntry.CPE_CONTAINER:
            IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(),
                    root.getJavaProject());
            if (container != null && container.getKind() == IClasspathContainer.K_APPLICATION) {
                filtered.add(root);
            }
            break;
        }
    }
    return filtered;
}

From source file:com.mountainminds.eclemma.internal.core.analysis.JavaModelCoverage.java

License:Open Source License

public void putFragmentRoot(IPackageFragmentRoot fragmentroot, IBundleCoverage coverage) {
    coveragemap.put(fragmentroot, coverage);
    fragmentroots.add(fragmentroot);/*from   w  w  w  . j  ava  2  s  .  com*/
    getProjectCoverage(fragmentroot.getJavaProject()).increment(coverage);
}

From source file:com.mountainminds.eclemma.internal.core.analysis.TypeTraverser.java

License:Open Source License

/**
 * This methods checks whether the given package fragment root is still on the
 * classpath. This check is required as the user might change the classpath
 * and old coverage sessions afterwards (SF #1836551).
 * /*from  w w  w  .  j a va2s.com*/
 * @param root
 *          package fragment root
 * @return true, if the classpath entry still exists
 * @throws JavaModelException
 */
private boolean isOnClasspath(IPackageFragmentRoot root) throws JavaModelException {
    IPath path = root.getPath();
    IJavaProject project = root.getJavaProject();
    return project.findPackageFragmentRoot(path) != null;
}

From source file:com.mountainminds.eclemma.internal.core.instr.ClassFilesStore.java

License:Open Source License

private static IPath getClassFileLocation(IPackageFragmentRoot root) throws JavaModelException {
    IPath path;/*from   ww w  . ja v  a2  s  .com*/
    if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
        IClasspathEntry entry = root.getRawClasspathEntry();
        path = entry.getOutputLocation();
        if (path == null) {
            path = root.getJavaProject().getOutputLocation();
        }
    } else {
        path = root.getPath();
    }
    return path;
}