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.xtext.xbase.ui.validation.XbaseValidationConfigurationBlock.java

License:Open Source License

protected String javaValue(final String javaIssueCode) {
    String delegatedValue;//  w  w w  .j a  v a 2s.  c o m
    String decodedDelegateKey = XbaseSeverityConverter.decodeDelegationKey(javaIssueCode).getFirst();
    IJavaProject javaProject = JavaCore.create(getProject());
    if (javaProject != null && javaProject.exists() && javaProject.getProject().isAccessible()) {
        delegatedValue = javaProject.getOption(decodedDelegateKey, true);
    } else {
        delegatedValue = JavaCore.getOption(decodedDelegateKey);
    }
    return delegatedValue;
}

From source file:org.eclipse.xtext.xtext.ecoreInference.ProjectAwareXtendXtext2EcorePostProcessor.java

License:Open Source License

@Override
protected synchronized ResourceLoader getResourceLoader(GeneratedMetamodel metamodel) {
    if (resourceLoader != null)
        return resourceLoader;

    URI uri = metamodel.eResource().getURI();
    if (ClasspathUriUtil.isClasspathUri(uri)) {
        ResourceSet resourceSet = metamodel.eResource().getResourceSet();
        uri = resourceSet.getURIConverter().normalize(uri);
    }//from w ww  .  j  a v a  2 s  .com
    IFile grammarFile = getWorkspace().getRoot().getFile(new Path(uri.toPlatformString(true)));
    IJavaProject javaProject = JavaCore.create(grammarFile.getProject());
    try {
        if (javaProject.exists()) {
            ClassLoader classLoader = createClassLoader(javaProject);
            resourceLoader = new ResourceLoaderImpl(classLoader);
            return resourceLoader;
        }
    } catch (CoreException e) {
        logger.error("Error creating execution context for java project '" + grammarFile.getProject().getName()
                + "'", e);
    }
    return super.getResourceLoader(metamodel);
}

From source file:org.eclipseguru.gwt.core.launch.GwtLaunchUtil.java

License:Open Source License

/**
 * Adds the source folder from the specified project to the classpath list.
 * // w  w  w.ja  va2 s .c o m
 * @param gwtProject
 * @param classpath
 * @throws JavaModelException
 */
private static void addSourceFolderToClasspath(final IJavaProject project, final List<String> classpath)
        throws JavaModelException {
    if ((null == project) || !project.exists()) {
        return;
    }

    final IPackageFragmentRoot[] packageFragmentRoots = project.getPackageFragmentRoots();
    for (final IPackageFragmentRoot packageFragmentRoot : packageFragmentRoots) {
        if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
            classpath.add(packageFragmentRoot.getResource().getLocation().toOSString());
        }
    }
}

From source file:org.eclipselabs.spray.runtime.xtext.ui.builder.JdtToBeBuiltComputerExt.java

License:Open Source License

@Override
public ToBeBuilt updateProject(final IProject project, IProgressMonitor monitor) throws CoreException {
    SubMonitor progress = SubMonitor.convert(monitor, 2);
    final ToBeBuilt toBeBuilt = super.updateProject(project, progress.newChild(1));
    if (!project.isAccessible() || progress.isCanceled())
        return toBeBuilt;
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject.exists()) {
        IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
        progress.setWorkRemaining(roots.length);
        final Set<String> previouslyBuilt = Sets.newHashSet();
        final Set<String> subsequentlyBuilt = Sets.newHashSet();
        final JarEntryLocator locator = new JarEntryLocator();
        for (final IPackageFragmentRoot root : roots) {
            if (progress.isCanceled())
                return toBeBuilt;
            if (shouldHandle(root)) {
                try {
                    final Map<String, Long> updated = Maps.newHashMap();
                    new PackageFragmentRootWalker<Boolean>() {
                        @Override
                        protected Boolean handle(IJarEntryResource jarEntry, TraversalState state) {
                            URI uri = locator.getURI(root, jarEntry, state);
                            if (isValid(uri, jarEntry)) {
                                if (wasFragmentRootAlreadyProcessed(uri))
                                    return Boolean.TRUE; // abort traversal
                                if (log.isDebugEnabled())
                                    log.debug("Scheduling: " + project.getName() + " - " + uri);
                                toBeBuilt.getToBeDeleted().add(uri);
                                toBeBuilt.getToBeUpdated().add(uri);
                            }/*ww  w .  j  a va  2  s .  com*/
                            return null;
                        }

                        protected boolean wasFragmentRootAlreadyProcessed(URI uri) {
                            Iterable<Pair<IStorage, IProject>> storages = getMapper().getStorages(uri);
                            for (Pair<IStorage, IProject> pair : storages) {
                                IProject otherProject = pair.getSecond();
                                // Spray issue#127 workaround
                                if (".org.eclipse.jdt.core.external.folders".equals(otherProject.getName())
                                        && uri.lastSegment().endsWith(".ecore"))
                                    return false;
                                if (!pair.getSecond().equals(project)) {
                                    if (previouslyBuilt.contains(otherProject.getName()))
                                        return true;
                                    if (!subsequentlyBuilt.contains(otherProject.getName())) {
                                        boolean process = XtextProjectHelper.hasNature(otherProject);
                                        String otherName = otherProject.getName();
                                        if (!process) {
                                            process = otherProject.isAccessible() && otherProject.isHidden();
                                            if (process) {
                                                Long previousStamp = modificationStampCache.projectToModificationStamp
                                                        .get(otherName);
                                                if (previousStamp == null || otherProject
                                                        .getModificationStamp() > previousStamp.longValue()) {
                                                    process = false;
                                                    updated.put(otherName, otherProject.getModificationStamp());
                                                }
                                            }
                                        }
                                        if (process) {
                                            ProjectOrder projectOrder = project.getWorkspace()
                                                    .computeProjectOrder(
                                                            new IProject[] { project, otherProject });
                                            if (!projectOrder.hasCycles) {
                                                if (otherProject.equals(projectOrder.projects[0])) {
                                                    previouslyBuilt.add(otherName);
                                                    return true;
                                                } else {
                                                    subsequentlyBuilt.add(otherName);
                                                }
                                            } else {
                                                subsequentlyBuilt.add(otherName);
                                            }
                                        }
                                    }
                                }
                            }
                            return false;
                        }
                    }.traverse(root, true);
                    synchronized (modificationStampCache) {
                        modificationStampCache.projectToModificationStamp.putAll(updated);
                    }
                } catch (JavaModelException ex) {
                    if (!ex.isDoesNotExist())
                        log.error(ex.getMessage(), ex);
                }
            }
            progress.worked(1);
        }
    }
    return toBeBuilt;
}

From source file:org.emftext.commons.jdt.resolve.JDTJavaClassifierUtil.java

License:Open Source License

/**
 * Returns the {@link IType} that corresponds to the given classifier.
 *//*from   w ww  .j a v  a 2s .  c  o  m*/
public IType getIType(JDTJavaClassifier classifier) {

    String projectName = classifier.getProjectName();
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot root = workspace.getRoot();
    IProject project = root.getProject(projectName);
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject == null) {
        return null;
    }

    if (!javaProject.exists()) {
        return null;
    }

    String qualifiedName = classifier.getQualifiedName();
    try {
        IType type = javaProject.findType(qualifiedName);
        return type;
    } catch (JavaModelException e) {
        handleException(e);
        return null;
    }
}

From source file:org.entirej.ide.core.project.EJPluginEntireJClassLoader.java

License:Apache License

private static Collection<URL> getClasspathEntries(IJavaProject javaProject, boolean ignoreSource) {
    if (javaProject == null) {
        throw new NullPointerException(
                "Trying to get hte projects class path, but the project passed was null");
    }//  w w w. j a  v a2s  .com
    try {
        if (!javaProject.exists()) {
            return new ArrayList<URL>();
        }
        // IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();

        IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);

        List<URL> urlList = toURLS(javaProject, entries, ignoreSource);

        //add Project output path
        IPath outputLocation = null;
        try {
            outputLocation = javaProject.getOutputLocation();

            IPath path = javaProject.getProject().getLocation();

            outputLocation = path.append(outputLocation.removeFirstSegments(1));
        } catch (JavaModelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (outputLocation != null) {
            URL url = new URL("file", null, outputLocation.toString() + "/");
            urlList.add(url);
        }
        return urlList;
    } catch (JavaModelException e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.entirej.ide.ui.utils.JavaAccessUtils.java

License:Apache License

public static IPackageFragment[] choosePackage(Shell shell, IJavaProject javaProject, String filter,
        Boolean multipleSelection, IPackageFragmentFilter packageFragmentFilter) {
    List<IPackageFragment> elements = new ArrayList<IPackageFragment>();
    if (javaProject.exists()) {
        try {/* w w w .  j  a v a2  s . co  m*/
            IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
            for (int i = 0; i < roots.length; i++) {
                if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
                    IPackageFragmentRoot sourceRoot = roots[i];
                    if (sourceRoot != null && sourceRoot.exists()) {
                        IJavaElement[] children = sourceRoot.getChildren();
                        for (IJavaElement element : children) {
                            if (element instanceof IPackageFragment && (packageFragmentFilter == null
                                    || packageFragmentFilter.acccept((IPackageFragment) element)))
                                elements.add((IPackageFragment) element);
                        }
                    }
                }
            }
        } catch (JavaModelException e) {
            EJCoreLog.logException(e);
        }
    }

    ElementListSelectionDialog dialog = new ElementListSelectionDialog(shell,
            new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT));
    dialog.setIgnoreCase(false);
    dialog.setTitle("Package Selection");
    dialog.setMessage("Choose a folder");
    dialog.setFilter(filter);
    dialog.setMultipleSelection(multipleSelection);
    dialog.setEmptyListMessage("");
    dialog.setElements(elements.toArray(new IPackageFragment[0]));
    dialog.setHelpAvailable(false);

    if (dialog.open() == Window.OK) {
        Object[] result = dialog.getResult();
        IPackageFragment[] fragments = new IPackageFragment[result.length];
        for (int i = 0; i < result.length; i++) {
            fragments[i] = (IPackageFragment) result[i];
        }
        return fragments;

    }
    return new IPackageFragment[0];
}

From source file:org.entirej.ide.ui.utils.PackageAssistProvider.java

License:Apache License

public IContentProposal[] getProposals(String currentContent, int position) {
    final List<TypeProposal> proposals = new ArrayList<TypeProposal>();

    if (currentContent.length() > position) {
        currentContent = currentContent.substring(0, position);
    }/* w  w  w. j a v  a 2  s.  c  o  m*/
    IJavaProject javaProject = projectProvider.getJavaProject();
    if (javaProject == null)
        return new IContentProposal[0];

    if (javaProject.exists()) {
        try {
            IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
            for (int i = 0; i < roots.length; i++) {
                if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
                    IPackageFragmentRoot sourceRoot = roots[i];
                    if (sourceRoot != null && sourceRoot.exists()) {
                        IJavaElement[] children = sourceRoot.getChildren();
                        for (IJavaElement element : children) {
                            if (element instanceof IPackageFragment) {

                                if (filterPkg(currentContent, position, element.getElementName())) {
                                    IPackageFragment fragment = (IPackageFragment) element;
                                    proposals.add(new TypeProposal(new Type(element.getElementName(),
                                            fragment.hasChildren() ? PKG_IMG : PKG_IMG_EMPTY), null));
                                }
                            }
                        }
                    }
                }
            }
        } catch (JavaModelException e) {
            EJCoreLog.logException(e);
        }
    }

    Collections.sort(proposals, new Comparator<TypeProposal>() {

        public int compare(TypeProposal o1, TypeProposal o2) {

            return o1.type.pkg.compareTo(o2.type.pkg);
        }
    });
    return proposals.toArray(new IContentProposal[0]);
}

From source file:org.fastcode.util.SourceUtil.java

License:Open Source License

/**
 *
 * @param name//from  ww  w  .  j  a  va 2s.com
 * @return
 * @throws Exception
 */
public static IResource getResourceFromWorkspace(final String name) throws Exception {
    final IProject[] projectArr = ResourcesPlugin.getWorkspace().getRoot().getProjects();

    for (final IProject project : projectArr) {
        if (project == null || !project.exists() || !project.isOpen()) {
            continue;
        }
        IResource resource = project.findMember(name);
        if (resource != null && resource.exists()) {
            return resource;
        }

        final IJavaProject javaProject = JavaCore.create(project);

        if (javaProject != null && javaProject.exists()) {
            resource = (IResource) getElementFromProject(javaProject, name, ".jar");
        }

        if (resource != null && resource.exists()) {
            return resource;
        }
    }
    return null;
}

From source file:org.fastcode.util.SourceUtil.java

License:Open Source License

/**
 *
 * @param paramType/*  ww  w  .  j  a v  a 2 s. co m*/
 * @param filter
 * @return
 * @throws Exception
 */
public static IType getTypeFromWorkspace(final String paramType, final String filter) throws Exception {

    final IWorkspace workspace = ResourcesPlugin.getWorkspace();
    final IJavaModel javaModel = JavaCore.create(workspace.getRoot());
    final IJavaProject projects[] = javaModel.getJavaProjects();

    // First try to find from the projects directly
    for (final IJavaProject project : projects) {
        if (project == null || !project.exists() || !project.isOpen()) {
            continue;
        }
        final IType type = project.findType(paramType);
        if (type != null && type.exists()) {
            return type;
        }
    }

    // Is still cannot find it, look through all projects.
    /*for (final IJavaProject project : projects) {
       if (project == null || !project.exists()) {
    continue;
       }
       final IType type = getTypeFromProject(project, paramType, filter);
       if (type != null && type.exists()) {
    return type;
       }
    }*/
    return null;
}