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.eclipse.jst.common.project.facet.core.internal.JavaFacetInstallDelegate.java

License:Open Source License

public void execute(final IProject project, final IProjectFacetVersion fv, final Object cfg,
        final IProgressMonitor monitor)

        throws CoreException

{
    validateEdit(project);//  ww w.j a  v  a2 s  . c om

    final JavaFacetInstallConfig config = castToConfig(cfg);

    // Create the source and the output directories.

    IJavaProject jproject = null;

    if (project.exists()) {
        jproject = JavaCore.create(project);
    }

    if (!jproject.exists()) {
        final List<IClasspathEntry> cp = new ArrayList<IClasspathEntry>();

        for (IPath srcFolderPath : config.getSourceFolders()) {
            final IFolder folder = createFolder(project, srcFolderPath, false);
            cp.add(JavaCore.newSourceEntry(folder.getFullPath()));
        }

        final IPath defOutputPath = config.getDefaultOutputFolder();
        IFolder defOutputFolder = null;

        if (defOutputPath != null) {
            defOutputFolder = createFolder(project, config.getDefaultOutputFolder(), true);
        }

        // Add the java nature. This will automatically add the builder.

        final IProjectDescription desc = project.getDescription();
        final String[] current = desc.getNatureIds();
        final String[] replacement = new String[current.length + 1];
        System.arraycopy(current, 0, replacement, 0, current.length);
        replacement[current.length] = JavaCore.NATURE_ID;
        desc.setNatureIds(replacement);
        project.setDescription(desc, null);

        // Setup the classpath.

        if (defOutputFolder == null) {
            jproject.setRawClasspath(cp.toArray(new IClasspathEntry[cp.size()]), null);
        } else {
            jproject.setRawClasspath(cp.toArray(new IClasspathEntry[cp.size()]), defOutputFolder.getFullPath(),
                    null);
        }

        JavaFacetUtil.resetClasspath(project, null, fv);
        JavaFacetUtil.setCompilerLevel(project, fv);
    } else {
        // Set the compiler compliance level for the project. Ignore whether
        // this might already be set so at the workspace level in case
        // workspace settings change later or the project is included in a
        // different workspace.

        String oldCompilerLevel = JavaFacetUtil.getCompilerLevel(project);
        JavaFacetUtil.setCompilerLevel(project, fv);

        String newCompilerLevel = JavaFacetUtil.getCompilerLevel(project);

        // Schedule a full build of the project if the compiler level changed
        // because we want classes in the project to be recompiled.

        if (newCompilerLevel != null && !newCompilerLevel.equals(oldCompilerLevel)) {
            JavaFacetUtil.scheduleFullBuild(project);
        }
    }
}

From source file:org.eclipse.jst.j2ee.internal.actions.OpenJ2EEResourceAction.java

License:Open Source License

protected void openResourceInEditor(String name, EObject object) {
    IResource resource = WorkbenchResourceHelper.getFile(object);
    if (resource == null)
        return;/*from w  w w  . ja v  a2  s  . c  o  m*/

    IProject project = resource.getProject();
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject.exists()) {
        IType type = null;
        try {
            //if name is null then can't get type
            if (name != null) {
                type = javaProject.findType(name);
            }

            //if type is null then can't open its editor, so open editor for the resource
            if (type != null) {
                if (!type.isBinary()) {
                    ICompilationUnit cu = type.getCompilationUnit();
                    EditorUtility.openInEditor(cu);
                } else {
                    IClassFile classFile = type.getClassFile();
                    EditorUtility.openInEditor(classFile);
                }

            } else {
                if (resource.exists() && resource.getType() == IResource.FILE) {
                    IFile file = (IFile) resource;
                    IContentType contentType = IDE.getContentType(file);
                    currentDescriptor = PlatformUI.getWorkbench().getEditorRegistry()
                            .getDefaultEditor(file.getName(), contentType);
                }
                openAppropriateEditor(resource);
            }
        } catch (JavaModelException e) {
            J2EEUIPlugin.logError(-1, e.getMessage(), e);
        } catch (PartInitException e) {
            J2EEUIPlugin.logError(-1, e.getMessage(), e);
        }

    }
}

From source file:org.eclipse.jst.j2ee.internal.common.classpath.J2EEComponentClasspathContainerUtils.java

License:Open Source License

public static IClasspathEntry getInstalledContainerEntry(IJavaProject jproj, IPath classpathContainerPath) {
    if (jproj.exists()) {
        IJavaProjectLite javaProjectLite = JavaCoreLite.create(jproj);
        IClasspathEntry[] cpes = javaProjectLite.readRawClasspath();
        for (int j = 0; j < cpes.length; j++) {
            final IClasspathEntry cpe = cpes[j];
            if (cpe.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                if (cpe.getPath().equals(classpathContainerPath)) {
                    return cpe; // entry found
                }/*from  ww w .  j  a  v a 2s  .  co m*/
            }
        }
    }
    // entry not found
    return null;
}

From source file:org.eclipse.jst.j2ee.refactor.listeners.J2EEElementChangedListener.java

License:Open Source License

protected static List<IPath> getSourceFolders(IProject project) {

    IJavaProject javaProj = JavaCore.create(project);
    if (javaProj == null)
        return null;
    if (!javaProj.exists())
        return null;

    IClasspathEntry[] cp = null;/*from   w w  w  .ja v  a 2 s . c om*/
    try {
        cp = javaProj.getRawClasspath();
    } catch (JavaModelException ex) {
        J2EEPlugin.logError(ex);
        return null;
    }
    List sourcePaths = new ArrayList();
    for (int i = 0; i < cp.length; i++) {
        if (cp[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            sourcePaths.add(cp[i].getPath().removeFirstSegments(1));
        }
    }
    return sourcePaths;
}

From source file:org.eclipse.jst.javaee.internal.adapter.JavaEEArtifactAdapterFactory.java

License:Open Source License

private IJavaElement getJavaElement(IJavaProject javaProject, String className) {
    if (className != null && javaProject != null && javaProject.exists()) {
        try {/*w  w w. ja va 2 s .  com*/
            return javaProject.findType(className);
        } catch (JavaModelException e) {
            J2EECorePlugin.logError(e);
        }
    }
    return null;
}

From source file:org.eclipse.jst.jee.ui.internal.navigator.OpenJEEResourceAction.java

License:Open Source License

/**
 * The user has invoked this action/* w  ww.  j  a v  a2 s  .  c  o  m*/
 */
@Override
public void run() {
    if (!isEnabled())
        return;

    if (srcObject instanceof J2EEJavaClassProviderHelper) {
        ((J2EEJavaClassProviderHelper) srcObject).openInEditor();
        return;
    }

    //[Bug 240512] deal with if any of these node types ndo not have an associated class
    if (srcObject instanceof SessionBean || srcObject instanceof MessageDrivenBean
            || srcObject instanceof EntityBean || srcObject instanceof Servlet || srcObject instanceof Filter
            || srcObject instanceof Listener) {

        String name = ""; //$NON-NLS-1$
        if (srcObject instanceof SessionBean) {
            SessionBean bean = (SessionBean) srcObject;
            name = bean.getEjbClass();
        } else if (srcObject instanceof MessageDrivenBean) {
            MessageDrivenBean bean = (MessageDrivenBean) srcObject;
            name = bean.getEjbClass();
        } else if (srcObject instanceof EntityBean) {
            EntityBean bean = (EntityBean) srcObject;
            name = bean.getEjbClass();
        } else if (srcObject instanceof Servlet) {
            Servlet servlet = (Servlet) srcObject;
            name = servlet.getServletClass();
        } else if (srcObject instanceof Filter) {
            Filter filter = (Filter) srcObject;
            name = filter.getFilterClass();
        } else if (srcObject instanceof Listener) {
            Listener listener = (Listener) srcObject;
            name = listener.getListenerClass();
        }

        IResource resource = WorkbenchResourceHelper.getFile((EObject) srcObject);
        if (resource == null)
            return;
        IProject project = resource.getProject();
        IJavaProject javaProject = JavaCore.create(project);
        if (javaProject.exists()) {
            IType type = null;
            try {
                //if name is null then can't get type
                if (name != null) {
                    type = javaProject.findType(name);
                }

                //if type is null then can't open its editor, so open editor for the resource
                if (type != null) {
                    ICompilationUnit cu = type.getCompilationUnit();
                    if (cu != null) {
                        EditorUtility.openInEditor(cu);
                    } else {
                        EditorUtility.openInEditor(type);
                    }
                } else {
                    openAppropriateEditor(resource);
                }
            } catch (JavaModelException e) {
                JEEUIPlugin.logError(e.getMessage(), e);
            } catch (PartInitException e) {
                JEEUIPlugin.logError(e.getMessage(), e);
            }
        }
        return;
    }

    if (srcObject instanceof EObject) {
        if (srcObject instanceof EjbLocalRefImpl || srcObject instanceof ResourceRefImpl) {
            IResource resource = WorkbenchResourceHelper.getFile((EObject) srcObject);
            if (resource == null) {
                openEObject((EObject) srcObject);
                return;
            }
            IProject project = resource.getProject();

            EJBJar ejbJar = (EJBJar) ModelProviderManager.getModelProvider(project)
                    .getModelObject(new Path(J2EEConstants.EJBJAR_DD_URI));

            if (srcObject instanceof EjbLocalRefImpl) {
                openEjbLocalRefNode(resource, ejbJar);
            } else if (srcObject instanceof ResourceRefImpl) {
                openResourceRefNode(resource, ejbJar);
            }
        } else {
            openEObject((EObject) srcObject);
        }
    } else if (srcObject instanceof BeanInterfaceNode) {
        openAppropriateEditor(((BeanInterfaceNode) srcObject).get_fqn());
        return;
    } else if (srcObject instanceof BeanNode) {
        openAppropriateEditor(((BeanNode) srcObject).getEjbClassQualifiedName());
        return;
    } else if (srcObject instanceof WebAppProvider) {
        IFile file = ((WebAppProvider) srcObject).getDDFile();
        if (file.isAccessible()) {
            openAppropriateEditor(file);
            return;
        }
    } else if (srcObject instanceof WebArtifactNode) {
        openEObject((EObject) ((WebArtifactNode) srcObject).getJavaEEObject());

    } else if (srcObject instanceof GroupEJBProvider) {
        openEObject((EObject) ((GroupEJBProvider) srcObject).getEjbJar());
    } else if (srcObject instanceof GroupEARProvider) {
        IFile file = ((GroupEARProvider) srcObject).getDDFile();
        if (file.isAccessible()) {
            openAppropriateEditor(file);
            return;
        }
    } else if (srcObject instanceof GroupAppClientProvider) {
        IFile file = ((GroupAppClientProvider) srcObject).getDDFile();
        if (file.isAccessible()) {
            openAppropriateEditor(file);
            return;
        }
    } else if (srcObject instanceof AbstractGroupProvider) {
        openEObject((EObject) ((AbstractGroupProvider) srcObject).getJavaEEObject());
    } else if (srcObject instanceof Resource)
        openAppropriateEditor(WorkbenchResourceHelper.getFile((Resource) srcObject));
}

From source file:org.eclipse.jst.jsf.common.ui.internal.utils.JavaModelUtil.java

License:Open Source License

/**
 * Tests if the given element is on the class path of its containing
 * project. Handles the case that the containing project isn't a Java
 * project.// ww w.  ja  v a2  s .c  om
 * @param element 
 * @return true if element in on the class path?
 */
public static boolean isOnClasspath(IJavaElement element) {
    IJavaProject project = element.getJavaProject();
    if (!project.exists())
        return false;
    return project.isOnClasspath(element);
}

From source file:org.eclipse.jst.jsf.core.JSFVersion.java

License:Open Source License

/**
 * @param project/*from   w  w w . j a va  2s. com*/
 * @return the best guess at what JSF version the project is or null if can't determine.
 */
public static JSFVersion guessJSFVersion(final IProject project) {
    JSFVersion jsfVersion = JSFVersion.valueOfProject(project);
    if (jsfVersion == null) {
        try {
            IJavaProject javaProj = JavaCore.create(project);
            if (javaProj != null && javaProj.exists()) {
                if (javaProj.findType("javax.faces.component.html.HtmlBody") != null) //$NON-NLS-1$
                {
                    // at least 2.0 inside here
                    jsfVersion = JSFVersion.V2_0;
                    if (javaProj.findType("javax.faces.view.facelets.FaceletCacheFactory") != null) //$NON-NLS-1$
                    {
                        // add in 2.1
                        jsfVersion = JSFVersion.V2_1;
                    }
                }
            }
        } catch (JavaModelException jme) {
            JSFCorePlugin.log(jme, "Trying to guess jsf version"); //$NON-NLS-1$
        }
    }
    return jsfVersion;
}

From source file:org.eclipse.jst.jsp.core.internal.java.JSPTranslation.java

License:Open Source License

/**
 * Originally from ReconcileStepForJava.  Creates an ICompilationUnit from the contents of the JSP document.
 * //from   w ww  .j  a v a 2 s.com
 * @return an ICompilationUnit from the contents of the JSP document
 */
private ICompilationUnit createCompilationUnit() throws JavaModelException {

    IJavaProject je = getJavaProject();

    if (je == null || !je.exists())
        return null;

    final String name = getClassname() + ".java";
    IPackageFragmentRoot packageFragmentRoot = null;
    IPackageFragmentRoot[] packageFragmentRoots = je.getPackageFragmentRoots();
    for (int i = 0; i < packageFragmentRoots.length; i++) {
        if (!packageFragmentRoots[i].isArchive() && !packageFragmentRoots[i].isExternal()) {
            packageFragmentRoot = packageFragmentRoots[i];
            break;
        }
    }
    if (packageFragmentRoot == null) {
        if (DEBUG) {
            System.out.println(
                    "** Abort create working copy: cannot create working copy: JSP is not in a Java project with source package fragment root"); //$NON-NLS-1$
        }
        return null;
    }
    final IPackageFragment fragment = packageFragmentRoot
            .getPackageFragment(IPackageFragmentRoot.DEFAULT_PACKAGEROOT_PATH);
    ICompilationUnit cu = fragment.getCompilationUnit(name).getWorkingCopy(getWorkingCopyOwner(),
            getProgressMonitor());
    setContents(cu);

    if (DEBUG) {
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); //$NON-NLS-1$
        System.out.println("(+) JSPTranslation [" + this + "] finished creating CompilationUnit: " + cu); //$NON-NLS-1$ //$NON-NLS-2$
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); //$NON-NLS-1$
    }

    return cu;
}

From source file:org.eclipse.jst.jsp.core.internal.java.JSPTranslator.java

License:Open Source License

/**
 * @param type/*from   ww  w  .  j a va2  s  . co m*/
 * @return
 */
private boolean isTypeFound(String rawTypeValue, List errorTypeNames) {
    // If the translation is being loaded from disk, the model and structured document may not have been intiailized yet
    IFile file = getStructuredDocument() != null ? getFile()
            : (fSavedModelPath != null ? ResourcesPlugin.getWorkspace().getRoot().getFile(fSavedModelPath)
                    : null);
    if (file == null)
        return true;

    IProject project = file.getProject();
    IJavaProject p = JavaCore.create(project);
    if (p.exists()) {
        List types = new ArrayList();
        if (rawTypeValue.indexOf('<') > 0) {
            // JSR 14 : Generics are being used, parse them out
            try {
                StringTokenizer toker = new StringTokenizer(rawTypeValue);
                String token = toker.nextToken("<,>/\""); //$NON-NLS-1$
                while (token != null) {
                    types.add(token);
                    token = toker.nextToken("<,>/\""); //$NON-NLS-1$
                }
            } catch (NoSuchElementException e) {
                // StringTokenizer failure with unsupported syntax
                return true;
            }
        } else {
            types.add(rawTypeValue);
        }

        for (int i = 0; i < types.size(); i++) {
            String typeName = types.get(i).toString();
            // remove any array suffixes
            if (typeName.indexOf('[') > 0 && typeName.indexOf(']') > typeName.indexOf('[')) {
                typeName = typeName.substring(0, typeName.indexOf('['));
            }
            // remove any "extends" prefixes (JSR 14)
            if (typeName.indexOf("extends") > 0) { //$NON-NLS-1$
                typeName = StringUtils.strip(typeName.substring(typeName.indexOf("extends"))); //$NON-NLS-1$
            }

            addNameToListIfTypeNotFound(p, typeName, errorTypeNames);
        }
    }
    return errorTypeNames.isEmpty();
}