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

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

Introduction

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

Prototype

boolean isOnClasspath(IResource resource);

Source Link

Document

Returns whether the given resource is on the classpath of this project, that is, referenced from a classpath entry and not explicitly excluded using an exclusion pattern.

Usage

From source file:com.redhat.ceylon.eclipse.core.model.JDTModule.java

License:Open Source License

public synchronized List<IPackageFragmentRoot> getPackageFragmentRoots() {
    if (packageFragmentRoots.isEmpty() && !moduleManager.isExternalModuleLoadedFromSource(getNameAsString())) {
        IJavaProject javaProject = moduleManager.getJavaProject();
        if (javaProject != null) {
            if (this.equals(getLanguageModule())) {
                IClasspathEntry runtimeClasspathEntry = null;

                try {
                    for (IClasspathEntry entry : javaProject.getRawClasspath()) {
                        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER && entry.getPath().segment(0)
                                .equals(CeylonLanguageModuleContainer.CONTAINER_ID)) {
                            runtimeClasspathEntry = entry;
                            break;
                        }//from  w w  w .  jav a  2s .  c o  m
                    }

                    if (runtimeClasspathEntry != null) {
                        for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
                            if (root.exists() && javaProject.isOnClasspath(root)
                                    && root.getRawClasspathEntry().equals(runtimeClasspathEntry)) {
                                packageFragmentRoots.add(root);
                            }
                        }
                    }
                } catch (JavaModelException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                File jarToSearch = null;
                try {
                    jarToSearch = returnCarFile();
                    if (jarToSearch == null) {
                        RepositoryManager repoMgr = CeylonBuilder
                                .getProjectRepositoryManager(javaProject.getProject());
                        if (repoMgr != null) {
                            jarToSearch = CeylonProjectModulesContainer.getModuleArtifact(repoMgr, this);
                        }
                    }

                    if (jarToSearch != null) {
                        IPackageFragmentRoot root = moduleManager.getJavaProject()
                                .getPackageFragmentRoot(jarToSearch.toString());
                        if (root instanceof JarPackageFragmentRoot) {
                            JarPackageFragmentRoot jarRoot = (JarPackageFragmentRoot) root;
                            if (jarRoot.getJar().getName().equals(jarToSearch.getPath())) {
                                packageFragmentRoots.add(root);
                            }
                        }
                    }
                } catch (CoreException e) {
                    if (jarToSearch != null) {
                        System.err.println("Exception trying to get Jar file '" + jarToSearch + "' :");
                    }
                    e.printStackTrace();
                }
            }
        }
    }
    return packageFragmentRoots;
}

From source file:com.wuetherich.osgi.ds.annotations.internal.builder.DsAnnotationBuildVisitor.java

License:Open Source License

/**
 * <p>/*from   ww w.j  ava2 s.  c o m*/
 * </p>
 * 
 * @param resource
 * @throws CoreException
 */
private void handle(IResource resource) throws CoreException {

    //
    if (!resource.getName().endsWith(".java")) {
        return;
    }

    //
    IJavaElement element = JavaCore.create(resource);
    IJavaProject javaProject = JavaCore.create(resource.getProject());
    if (!javaProject.isOnClasspath(element) || !element.isStructureKnown()) {
        return;
    }

    if (JavaCore.create(resource) == null) {
        return;
    }

    try {
        resource.deleteMarkers(Constants.DS_ANNOTATION_PROBLEM_MARKER, true, IResource.DEPTH_ZERO);
    } catch (CoreException e1) {
        e1.printStackTrace();
    }

    //
    IJavaElement compilationUnit = JavaCore.create(resource);

    if (compilationUnit != null && compilationUnit.isStructureKnown()) {
        parse((ICompilationUnit) compilationUnit, resource);
    }
}

From source file:de.loskutov.bco.ui.JdtUtils.java

License:Open Source License

private static boolean isOnClasspath(IJavaElement javaElement) {
    IJavaProject project = javaElement.getJavaProject();
    if (project != null) {
        boolean result = project.isOnClasspath(javaElement);
        return result;
    }//from   ww w.j  a  va 2s.c o  m
    return false;
}

From source file:lang.java.jdt.internal.JDT.java

License:Open Source License

public IValue isOnBuildPath(ISourceLocation loc) {
    IResource r = getResource(loc);/*from   w w  w.  j a v a 2s. c  o  m*/
    IJavaProject jp = JavaCore.create(r.getProject());

    if (!jp.exists()) {
        throw new Throw(VF.string("Location is not in a Java project: " + loc), (ISourceLocation) null, null);
    }

    return VF.bool(jp.isOnClasspath(r));
}

From source file:net.rim.ejde.internal.util.ResourceBuilderUtils.java

License:Open Source License

/**
 * Creates the resource output root folder if it is not there.
 *
 * @param project//from  ww  w.  j a  v a2 s.co m
 * @param monitor
 * @throws CoreException
 *
 */

public static IFolder createResourcesOutputRoot(IProject project, IProgressMonitor monitor)
        throws CoreException {

    IContainer sourceContainer = project
            .getFolder(ImportUtils.getImportPref(ResourceBuilder.LOCALE_INTERFACES_FOLDER_NAME));
    if (!sourceContainer.exists()) {
        // if the folder does not exist, create it
        ((IFolder) sourceContainer).create(IResource.DERIVED, true, monitor);
    }
    IJavaProject javaProject = JavaCore.create(project);
    // check if folder is on classpath already (can occur if project is on classpath
    if (!javaProject.isOnClasspath(sourceContainer)) {
        IClasspathEntry tmpSourceRootEntry = JavaCore.newSourceEntry(sourceContainer.getFullPath());
        // Set raw classpath
        IClasspathEntry[] classpathEntries = javaProject.getRawClasspath();
        IClasspathEntry[] newClasspathEntries = new IClasspathEntry[classpathEntries.length + 1];
        System.arraycopy(classpathEntries, 0, newClasspathEntries, 0, classpathEntries.length);
        newClasspathEntries[classpathEntries.length] = tmpSourceRootEntry;
        javaProject.setRawClasspath(newClasspathEntries, monitor);
    }
    return (IFolder) sourceContainer;
}

From source file:net.sf.refactorit.eclipse.vfs.EclipseSourcePath.java

License:Open Source License

public List getAllSources() {
    ArrayList result = new ArrayList(200);
    if (project == null || !project.isOpen()) { // just in case
        return result;
    }// w ww .j a  v  a  2s  .com

    final IJavaProject javaProject = getJavaProject();

    Source[] rootSources = getRootSources();
    SourceFilter filter = new SourceFilter() {
        private PathItem[] ignoredItems = projectOptions.getIgnoredSourcePath().getItems();

        public boolean accept(Source source) {
            IResource resource = ((EclipseSource) source).getResource();
            if (resource.getType() != IResource.FILE) {
                // do not stop early on top level folders because
                // something might be included on deeper level
                return !isIgnored(source);
            }

            return javaProject.isOnClasspath(resource) && !isIgnored(source);
        }

        /**
         * @param source
         * @return true if it is in ignored path
         */
        private boolean isIgnored(final Source source) {
            if (!projectOptions.isAutoDetect()) {
                for (int i = 0; i < ignoredItems.length; i++) {
                    String path = source.getAbsolutePath();
                    String ignored = ignoredItems[i].getAbsolutePath();
                    if (path.indexOf(ignored) == 0) {
                        return true;
                    }
                }
            }

            return false;
        }
    };

    for (int i = 0; i < rootSources.length; i++) {
        iterateDirectory(rootSources[i], result, filter);
    }

    return result;
}

From source file:org.codehaus.jdt.groovy.model.GroovyCompilationUnit.java

License:Open Source License

public boolean isOnBuildPath() {
    // fix for bug http://dev.eclipse.org/bugs/show_bug.cgi?id=20051
    IJavaProject project = this.getJavaProject();
    if (!project.isOnClasspath(this)) {
        return false;
    }/*  w  w w  .j  a v  a 2  s  .  c  om*/
    IProject resourceProject = project.getProject();
    if (resourceProject == null || !resourceProject.isAccessible()
            || !GroovyNature.hasGroovyNature(resourceProject)) {
        return false;
    }

    return true;
}

From source file:org.drools.eclipse.view.rules.RulesView.java

License:Apache License

private boolean updateResource(IResource resource) {
    IProject project = resource.getProject();
    if (project != null) {
        IJavaProject javaProject = JavaCore.create(project);
        if (!javaProject.exists()) {
            return false;
        }//from  ww  w .ja v  a2  s  .  c  om
        if (resource instanceof IFile && javaProject.isOnClasspath(resource)) {
            IFile file = (IFile) resource;
            if ("drl".equals(resource.getFileExtension()) || "dslr".equals(resource.getFileExtension())) {
                try {
                    DRLInfo drlInfo = DroolsEclipsePlugin.getDefault().parseResource(resource, false);
                    String packageName = drlInfo.getPackageName();
                    Package pkg = ruleSet.getPackage(packageName);
                    if (pkg == null) {
                        pkg = DroolsModelBuilder.addPackage(ruleSet, packageName, 0, 0);
                    }
                    if (drlInfo.getBuilderErrors().length > 0 || drlInfo.getPackageDescr() == null) {
                        return false;
                    }
                    // add rules
                    List rules = drlInfo.getPackageDescr().getRules();
                    for (Iterator iterator = rules.iterator(); iterator.hasNext();) {
                        RuleDescr ruleDescr = (RuleDescr) iterator.next();
                        boolean isQuery = ruleDescr instanceof QueryDescr;
                        String ruleName = ruleDescr.getName();
                        if (!isQuery) {
                            Rule rule = DroolsModelBuilder.addRule(pkg, ruleName, file,
                                    ruleDescr.getStartCharacter(),
                                    ruleDescr.getEndCharacter() - ruleDescr.getStartCharacter() + 1, null);
                            // create link between resource and created rule nodes
                            List droolsElements = (List) resourcesMap.get(file);
                            if (droolsElements == null) {
                                droolsElements = new ArrayList();
                                resourcesMap.put(file, droolsElements);
                            }
                            droolsElements.add(rule);
                        } else {
                            Query query = DroolsModelBuilder.addQuery(pkg, ruleName, file,
                                    ruleDescr.getStartCharacter(),
                                    ruleDescr.getEndCharacter() - ruleDescr.getStartCharacter() + 1);
                            // create link between resource and created rule nodes
                            List droolsElements = (List) resourcesMap.get(file);
                            if (droolsElements == null) {
                                droolsElements = new ArrayList();
                                resourcesMap.put(file, droolsElements);
                            }
                            droolsElements.add(query);
                        }
                    }
                    // add globals
                    List globals = drlInfo.getPackageDescr().getGlobals();
                    for (Iterator iterator = globals.iterator(); iterator.hasNext();) {
                        GlobalDescr globalDescr = (GlobalDescr) iterator.next();
                        Global global = DroolsModelBuilder.addGlobal(pkg, globalDescr.getIdentifier(), file,
                                globalDescr.getStartCharacter(),
                                globalDescr.getEndCharacter() - globalDescr.getStartCharacter() + 1);
                        // create link between resource and created rule nodes
                        List droolsElements = (List) resourcesMap.get(file);
                        if (droolsElements == null) {
                            droolsElements = new ArrayList();
                            resourcesMap.put(file, droolsElements);
                        }
                        droolsElements.add(global);
                    }
                    // add functions
                    List functions = drlInfo.getPackageDescr().getFunctions();
                    for (Iterator iterator = functions.iterator(); iterator.hasNext();) {
                        FunctionDescr functionDescr = (FunctionDescr) iterator.next();
                        String functionName = functionDescr.getName();
                        Function function = DroolsModelBuilder.addFunction(pkg, functionName, file,
                                functionDescr.getStartCharacter(),
                                functionDescr.getEndCharacter() - functionDescr.getStartCharacter() + 1);
                        // create link between resource and created rule nodes
                        List droolsElements = (List) resourcesMap.get(file);
                        if (droolsElements == null) {
                            droolsElements = new ArrayList();
                            resourcesMap.put(file, droolsElements);
                        }
                        droolsElements.add(function);
                    }
                } catch (Throwable t) {
                    DroolsEclipsePlugin.log(t);
                }
                return false;
            } else if ("rf".equals(resource.getFileExtension())) {
                try {
                    String processString = convertToString(file.getContents());
                    ProcessInfo processInfo = DroolsEclipsePlugin.getDefault().parseProcess(processString,
                            resource);
                    if (processInfo != null && processInfo.getProcess() != null) {
                        String packageName = processInfo.getProcess().getPackageName();
                        Package pkg = ruleSet.getPackage(packageName);
                        if (pkg == null) {
                            pkg = DroolsModelBuilder.addPackage(ruleSet, packageName, 0, 0);
                        }
                        Process process = DroolsModelBuilder.addProcess(pkg, processInfo.getProcess().getId(),
                                file);
                        List droolsElements = (List) resourcesMap.get(file);
                        if (droolsElements == null) {
                            droolsElements = new ArrayList();
                            resourcesMap.put(file, droolsElements);
                        }
                        droolsElements.add(process);
                    }
                } catch (Throwable t) {
                    DroolsEclipsePlugin.log(t);
                }
                return false;
            }
        }
    }
    return true;
}

From source file:org.eclipse.ajdt.core.BuildConfig.java

License:Open Source License

/**
 * Find out whether a file is included.  This does NOT use the cached version,
 * so if you are calling it a lot and don't need the most up-to date information
 * it would be better to use getIncludedSourceFiles(file.getProject()).contains(file) instead.
 * @param file//w  ww .java2s  .  c o m
 * @return
 */
public static boolean isIncluded(IResource file) {
    IJavaProject jp = JavaCore.create(file.getProject());
    return jp.isOnClasspath(file);
}

From source file:org.eclipse.ajdt.internal.ui.editor.actions.AJOrganizeImportsAction.java

License:Open Source License

private boolean testOnBuildPath(ICompilationUnit cu, MultiStatus status) {
    IJavaProject project = cu.getJavaProject();
    if (!project.isOnClasspath(cu)) {
        String cuLocation = cu.getPath().makeRelative().toString();
        String message = Messages.format("{0}: Compilation unit not on build path. No changes applied.",
                cuLocation);/*from ww  w. ja  va  2  s.com*/
        status.add(new Status(IStatus.INFO, JavaUI.ID_PLUGIN, IStatus.ERROR, message, null));
        return false;
    }
    return true;
}