Example usage for org.eclipse.jdt.core IJavaProject exists

List of usage examples for org.eclipse.jdt.core IJavaProject exists

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaProject exists.

Prototype

boolean exists();

Source Link

Document

Returns whether this Java element exists in the model.

Usage

From source file:org.key_project.util.jdt.JDTUtil.java

License:Open Source License

/**
 * Internal helper method that is used in {@link #getSourceResources(IProject)}
 * to compute the source path. It is required to solve cycles in project dependencies.
 * @param project The given Project.//w w  w  .  j ava 2  s .  c om
 * @param alreadyHandledProjects The already handled {@link IProject} that don't need to be analysed again.
 * @return The found source {@link IResource}s in the workspace.
 * @throws JavaModelException Occurred Exception.
 */
private static List<IResource> getSourceResources(IProject project, Set<IProject> alreadyHandledProjects)
        throws JavaModelException {
    List<IResource> result = new LinkedList<IResource>();
    if (project != null) {
        Assert.isNotNull(alreadyHandledProjects);
        alreadyHandledProjects.add(project);
        IJavaProject javaProject = getJavaProject(project);
        if (javaProject != null && javaProject.exists()) {
            IClasspathEntry[] entries = javaProject.getRawClasspath();
            for (IClasspathEntry entry : entries) {
                if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE) {
                    List<IResource> location = getResourceFor(javaProject, entry, IPackageFragmentRoot.K_SOURCE,
                            alreadyHandledProjects);
                    if (location != null) {
                        result.addAll(location);
                    }
                }
            }
        }
    }
    return result;
}

From source file:org.mobicents.eclipslee.servicecreation.wizards.generic.FilenamePage.java

License:Apache License

/**
 * Tests if the current workbench selection is a suitable
 * container to use./*from  w w w . j a  v  a 2  s.co m*/
 */

private void initialize() {
    if (selection != null && selection.isEmpty() == false && selection instanceof IStructuredSelection) {

        IJavaElement element = getInitialJavaElement((IStructuredSelection) selection);
        IPackageFragmentRoot initialRoot;
        initialRoot = JavaModelUtil.getPackageFragmentRoot(element);
        if (initialRoot == null || initialRoot.isArchive()) {
            IJavaProject javaProject = element.getJavaProject();
            if (javaProject != null) {
                try {
                    initialRoot = null;
                    if (javaProject.exists()) {
                        IPackageFragmentRoot roots[] = javaProject.getPackageFragmentRoots();
                        for (int i = 0; i < roots.length; i++) {
                            if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
                                initialRoot = roots[i];
                                break;
                            }
                        }
                    }
                } catch (JavaModelException e) {
                    ServiceCreationPlugin.log("JavaModelException determining project root.");
                }
                if (initialRoot == null) {
                    initialRoot = javaProject.getPackageFragmentRoot(javaProject.getResource());
                }
            }
        }

        try {
            setSourceContainer((IFolder) initialRoot.getCorrespondingResource());
        } catch (JavaModelException e) {
            ServiceCreationPlugin.log("JavaModelException thrown setting source container on FilenamePage");
        }

        //         // Initialize the maven module dialog
        //         mavenModuleText.setText(mavenModule);

        // Initialize the filename dialog
        fileText.setText("__Replace_Me__" + ends);

        if (element != null && element.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
            IPackageFragment fragment = (IPackageFragment) element;
            setPackage(fragment);
            return;
        }

        if (element != null && element.getElementType() == IJavaElement.COMPILATION_UNIT) {
            ICompilationUnit unit = (ICompilationUnit) element;
            IPackageFragment fragment = (IPackageFragment) unit.getParent();
            setPackage(fragment);
            return;
        }

        setPackage(null);
    }

}

From source file:org.mule.munit.plugin.MunitLaunchConfigurationDelegate.java

License:Open Source License

/**
 * Performs a check on the launch configuration's attributes. If an attribute contains an invalid value, a {@link CoreException}
 * with the error is thrown.//from  w ww  .j  a  v  a2  s  .  c o m
 *
 * @param configuration the launch configuration to verify
 * @param launch the launch to verify
 * @param monitor the progress monitor to use
 * @throws CoreException an exception is thrown when the verification fails
 */
protected void preLaunchCheck(ILaunchConfiguration configuration, ILaunch launch, IProgressMonitor monitor)
        throws CoreException {
    try {
        IJavaProject javaProject = getJavaProject(configuration);
        if ((javaProject == null) || !javaProject.exists()) {
            abort(JUnitMessages.JUnitLaunchConfigurationDelegate_error_invalidproject, null,
                    IJavaLaunchConfigurationConstants.ERR_NOT_A_JAVA_PROJECT);
        }
        if (!CoreTestSearchEngine.hasTestCaseType(javaProject)) {
            abort(JUnitMessages.JUnitLaunchConfigurationDelegate_error_junitnotonpath, null,
                    IJUnitStatusConstants.ERR_JUNIT_NOT_ON_PATH);
        }

        ITestKind testKind = getTestRunnerKind(configuration);
        boolean isJUnit4Configuration = TestKindRegistry.JUNIT4_TEST_KIND_ID.equals(testKind.getId());
        if (isJUnit4Configuration && !CoreTestSearchEngine.hasTestAnnotation(javaProject)) {
            abort(JUnitMessages.JUnitLaunchConfigurationDelegate_error_junit4notonpath, null,
                    IJUnitStatusConstants.ERR_JUNIT_NOT_ON_PATH);
        }
    } finally {
        monitor.done();
    }
}

From source file:org.neuro4j.studio.debug.core.model.FlowLineBreakpoint.java

License:Apache License

private IJavaProject computeJavaProject(IJavaStackFrame stackFrame) {
    ILaunch launch = stackFrame.getLaunch();
    if (launch == null) {
        return null;
    }//from  ww  w  .  j a  v a  2 s.c  o m
    try {
        String projectName = launch.getLaunchConfiguration()
                .getAttribute("org.eclipse.jdt.launching.PROJECT_ATTR", "");
        if (projectName != null) {
            IJavaProject jProject = ClassloaderHelper.getJavaProject(projectName);
            if (jProject != null) {
                return jProject;
            }
        }

    } catch (CoreException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    ISourceLocator locator = launch.getSourceLocator();
    if (locator == null)
        return null;

    Object sourceElement = null;
    try {
        if (locator instanceof ISourceLookupDirector && !stackFrame.isStatic()) {
            IJavaType thisType = stackFrame.getThis().getJavaType();
            if (thisType instanceof IJavaReferenceType) {
                String[] sourcePaths = ((IJavaReferenceType) thisType).getSourcePaths(null);
                if (sourcePaths != null && sourcePaths.length > 0) {
                    sourceElement = ((ISourceLookupDirector) locator).getSourceElement(sourcePaths[0]);
                }
            }
        }
    } catch (DebugException e) {
        DebugPlugin.log(e);
    }
    if (sourceElement == null) {
        sourceElement = locator.getSourceElement(stackFrame);
    }
    if (!(sourceElement instanceof IJavaElement) && sourceElement instanceof IAdaptable) {
        Object element = ((IAdaptable) sourceElement).getAdapter(IJavaElement.class);
        if (element != null) {
            sourceElement = element;
        }
    }
    if (sourceElement instanceof IJavaElement) {
        return ((IJavaElement) sourceElement).getJavaProject();
    } else if (sourceElement instanceof IResource) {
        IJavaProject project = JavaCore.create(((IResource) sourceElement).getProject());
        if (project.exists()) {
            return project;
        }
    }

    return null;
}

From source file:org.nuxeo.ide.sdk.comp.contentassist.contextualproposal.AbstractNodeContextualProposalComputer.java

License:Open Source License

protected IJavaProject getJavaProjectForDocument(IDocument document) {
    IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
    if (model == null) {
        return null;
    }/*w w  w  .j a v a 2  s.  c  om*/
    Path path = new Path(model.getBaseLocation());
    IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
    if (!file.exists()) {
        return null;
    }

    IProject project = file.getProject();
    if (project != null) {
        IJavaProject javaProject = JavaCore.create(project);
        if (javaProject != null && !javaProject.exists()) {
            return null;
        }
        return javaProject;
    }
    return null;

}

From source file:org.ofbiz.plugin.analysis.Analysis.java

License:Apache License

public Analysis(IJavaProject javaProject, List<Service> services, Project p) {
    assert javaProject != null;
    assert javaProject.exists();
    assert services != null;
    this.javaProject = javaProject;
    this.contexts = new AnalysisContext[services.size()];
    for (int i = 0; i < services.size(); i++) {
        assert services.get(i) != null;
        this.contexts[i] = new AnalysisContext();
        this.contexts[i].javaProject = javaProject;
        this.contexts[i].service = services.get(i);
    }//from   ww  w . jav a  2 s  . com
}

From source file:org.org.eclipse.dws.core.internal.bridges.ProjectInteractionHelper.java

License:Open Source License

/**
 * Gets the classpath entries.//ww  w  .  j  ava  2 s .  co  m
 * 
 * @param javaProject the java project
 * 
 * @return the classpath entries
 */
public static Set<DWSClasspathEntryDescriptor> getClasspathEntries(IJavaProject javaProject) {
    Set<DWSClasspathEntryDescriptor> classpathEntryDescriptors = new LinkedHashSet<DWSClasspathEntryDescriptor>();
    try {
        if (javaProject.exists()) {
            for (IClasspathEntry classpathEntry : javaProject.getRawClasspath()) {
                DWSClasspathEntryDescriptor classpathEntryDescriptor = new DWSClasspathEntryDescriptor();
                classpathEntryDescriptor
                        .setEncodedClasspathEntry(javaProject.encodeClasspathEntry(classpathEntry));
                classpathEntryDescriptor.setPath(classpathEntry.getPath().toPortableString());
                classpathEntryDescriptor.setProjectName(javaProject.getElementName());
                IJavaModelStatus javaModelStatus = JavaConventions.validateClasspathEntry(javaProject,
                        classpathEntry, false);
                classpathEntryDescriptor.setValid(classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY
                        || classpathEntry.getEntryKind() == IClasspathEntry.CPE_VARIABLE
                                && javaModelStatus.isOK());
                classpathEntryDescriptors.add(classpathEntryDescriptor);
            }
        }
    } catch (JavaModelException e) {
        throw new JavaProjectInteractionException(
                "An error occured while scanning for classpath entries in project:" + javaProject, e);
    }
    return classpathEntryDescriptors;
}

From source file:org.parallelj.designer.extension.tools.ExecutableTools.java

License:Open Source License

/**
 * Returns the fully qualified name of Class created. It is used as to open
 * the New Class Wizard dialog. It uses the OpenNewClassWizardAction to open
 * the wizard and returns the Class name if Java Element is created.
 * //from  w  ww .  ja va  2  s  . c  o m
 * @param eObject
 * @return name of created Java class
 */
public static String getExecutableValueFromClassWizard(EObject eObject) {
    String newExecutableValue = null;
    NewClassWizardPage newClassWizardPage = new NewClassWizardPage();
    newClassWizardPage.addSuperInterface("java.lang.Runnable");
    OpenNewClassWizardAction openNewClassWizardAction = new OpenNewClassWizardAction();
    IPackageFragmentRoot initRoot = null;
    IJavaProject jproject = ResourceSelectorTools.getJavaProjectFromEObject(eObject).getJavaProject();
    try {
        if (jproject.exists()) {
            IPackageFragmentRoot[] roots = jproject.getPackageFragmentRoots();
            for (int i = 0; i < roots.length; i++) {
                if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
                    initRoot = roots[i];
                    break;
                }
            }
        }
    } catch (JavaModelException e) {
        Activator.logError(e.getMessage());
    }
    newClassWizardPage.setPackageFragmentRoot(initRoot, true);
    newClassWizardPage.setMethodStubSelection(false, false, true, true);
    openNewClassWizardAction.setConfiguredWizardPage(newClassWizardPage);
    openNewClassWizardAction.run();
    if (openNewClassWizardAction.getCreatedElement() != null
            && openNewClassWizardAction.getCreatedElement().getElementName() != null) {
        if (newClassWizardPage.getPackageText() != "") {
            newExecutableValue = newClassWizardPage.getPackageText() + "." + newClassWizardPage.getTypeName();
        } else {
            newExecutableValue = newClassWizardPage.getTypeName();
        }
    }
    return newExecutableValue;
}

From source file:org.parallelj.designer.launching.ParalleljMainTab.java

License:Open Source License

@Override
protected void handleSearchButtonSelected() {
    IJavaProject project = getJavaProject();
    IJavaElement[] elements = null;/* w  w w .  j  av  a  2 s  .  c o  m*/
    if ((project == null) || !project.exists()) {
        IJavaModel model = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
        if (model != null) {
            try {
                elements = model.getJavaProjects();
            } catch (JavaModelException e) {
                JDIDebugUIPlugin.log(e);
            }
        }
    } else {
        elements = new IJavaElement[] { project };
    }
    if (elements == null) {
        elements = new IJavaElement[] {};
    }
    int constraints = IJavaSearchScope.SOURCES;
    constraints |= IJavaSearchScope.APPLICATION_LIBRARIES;

    IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(elements, constraints);
    ProgramSearchEngine engine = new ProgramSearchEngine();
    IType[] types = null;
    types = engine.searchTypeWithProgramAnnotation(getLaunchConfigurationDialog(), searchScope);

    DebugTypeSelectionDialog mmsd = new DebugTypeSelectionDialog(getShell(), types,
            LauncherMessages.JavaMainTab_Choose_Main_Type_11);
    if (mmsd.open() == Window.CANCEL) {
        return;
    }
    Object[] results = mmsd.getResult();
    IType type = (IType) results[0];
    if (type != null) {
        fMainText.setText(type.getFullyQualifiedName());
        fProjText.setText(type.getJavaProject().getElementName());
    }
}

From source file:org.raspinloop.fmi.plugin.launcher.RilMainTab.java

License:Open Source License

/**
 * Show a dialog that lists all main types
 *///from w  w  w  .  j  a  va 2  s  . c  o m
@Override
protected void handleSearchButtonSelected() {
    IJavaProject project = getJavaProject();
    IJavaElement[] elements = null;
    if ((project == null) || !project.exists()) {
        IJavaModel model = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
        if (model != null) {
            try {
                elements = model.getJavaProjects();
            } catch (JavaModelException e) {
                Activator.getDefault().log("", e);
                ;
            }
        }
    } else {
        elements = new IJavaElement[] { project };
    }
    if (elements == null) {
        elements = new IJavaElement[] {};
    }
    int constraints = IJavaSearchScope.SOURCES;
    constraints |= IJavaSearchScope.APPLICATION_LIBRARIES;
    if (fSearchExternalJarsCheckButton.getSelection()) {
        constraints |= IJavaSearchScope.SYSTEM_LIBRARIES;
    }
    IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(elements, constraints);
    MainMethodSearchEngine engine = new MainMethodSearchEngine();
    IType[] types = null;
    try {
        types = engine.searchMainMethods(getLaunchConfigurationDialog(), searchScope,
                fConsiderInheritedMainButton.getSelection());
    } catch (InvocationTargetException e) {
        setErrorMessage(e.getMessage());
        return;
    } catch (InterruptedException e) {
        setErrorMessage(e.getMessage());
        return;
    }
    DebugTypeSelectionDialog mmsd = new DebugTypeSelectionDialog(getShell(), types,
            LauncherMessages.JavaMainTab_Choose_Main_Type_11);
    if (mmsd.open() == Window.CANCEL) {
        return;
    }
    Object[] results = mmsd.getResult();
    IType type = (IType) results[0];
    if (type != null) {
        fMainText.setText(type.getFullyQualifiedName());
        fProjText.setText(type.getJavaProject().getElementName());
    }
}