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

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

Introduction

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

Prototype

IJavaModel getJavaModel();

Source Link

Document

Returns the Java model.

Usage

From source file:net.sf.fjep.fatjar.popup.actions.BuildFatJar.java

License:Open Source License

private void getChildProjects(IJavaProject jproject, Vector projects, boolean exportedOnly) {

    IClasspathEntry[] cpes = jproject.readRawClasspath();
    if (cpes != null) {
        for (int i = 0; i < cpes.length; i++) {
            IClasspathEntry cpe = JavaCore.getResolvedClasspathEntry(cpes[i]);
            if (cpe == null) {
                System.err.println("Error: cpes[" + i + "]=" + cpes[i] + " does not resolve");
                continue;
            }/*from   w  w  w  .j ava 2 s .  com*/
            int kind = cpe.getEntryKind();
            String name = relPath(cpe.getPath());
            if (kind == IClasspathEntry.CPE_CONTAINER) {
                try {
                    IClasspathContainer container = JavaCore.getClasspathContainer(cpe.getPath(), jproject);
                    if ((container.getKind() == IClasspathContainer.K_APPLICATION)
                            || (container.getKind() == IClasspathContainer.K_SYSTEM)) {
                        IClasspathEntry[] cpes2 = container.getClasspathEntries();
                        for (int j = 0; j < cpes2.length; j++) {
                            IClasspathEntry cpe2 = cpes2[j];
                            int kind2 = cpe2.getEntryKind();
                            String name2 = relPath(cpe2.getPath());
                            if (name2 == null) {
                                System.err.println("invalid classpath entry: " + cpe2.toString());
                            } else {
                                if (kind2 == IClasspathEntry.CPE_PROJECT) {
                                    if (!exportedOnly || cpe2.isExported()) {
                                        if (!projects.contains(name2)) {
                                            IJavaProject jChildProject2 = jproject.getJavaModel()
                                                    .getJavaProject(name2);
                                            projects.add(jChildProject2);
                                            getChildProjects(jChildProject2, projects, true);
                                        }
                                    }
                                }
                            }
                        }
                    }
                } catch (JavaModelException e) {
                }
            } else if (kind == IClasspathEntry.CPE_PROJECT) {
                if (name == null) {
                    System.err.println("invalid classpath entry: " + cpe.toString());
                } else {
                    if (!exportedOnly || cpe.isExported()) {
                        if (!projects.contains(name)) {
                            IJavaProject jChildProject = jproject.getJavaModel().getJavaProject(name);
                            projects.add(jChildProject);
                            getChildProjects(jChildProject, projects, true);
                        }
                    }
                }
            }
        }
    }
}

From source file:org.codehaus.groovy.eclipse.launchers.AbstractGroovyLaunchShortcut.java

License:Apache License

/**
 * Need to recursively walk the classpath and visit all dependent projects
 * Not looking at classpath containers yet.
 *
 * @param javaProject//  ww w  .  j av a 2s  . c  o m
 * @param entries
 */
private void addClasspathEntriesForProject(IJavaProject javaProject, SortedSet<String> sourceEntries,
        SortedSet<String> binEntries) {
    List<IJavaProject> dependingProjects = new ArrayList<IJavaProject>();
    try {
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        for (IClasspathEntry entry : entries) {
            int kind = entry.getEntryKind();
            switch (kind) {
            case IClasspathEntry.CPE_LIBRARY:
                IPath libPath = entry.getPath();
                if (!isPathInWorkspace(libPath)) {
                    sourceEntries.add(libPath.toOSString());
                    break;
                }
                //$FALL-THROUGH$
            case IClasspathEntry.CPE_SOURCE:
                IPath srcPath = entry.getPath();
                String sloc = getProjectLocation(srcPath);
                if (srcPath.segmentCount() > 1) {
                    sloc += File.separator + srcPath.removeFirstSegments(1).toOSString();
                }
                sourceEntries.add(sloc);

                IPath outPath = entry.getOutputLocation();
                if (outPath != null) {
                    String bloc = getProjectLocation(outPath);
                    if (outPath.segmentCount() > 1) {
                        bloc += File.separator + outPath.removeFirstSegments(1).toOSString();
                    }
                    binEntries.add(bloc);
                }
                break;

            case IClasspathEntry.CPE_PROJECT:
                dependingProjects.add(javaProject.getJavaModel().getJavaProject(entry.getPath().lastSegment()));
                break;
            }
        }
        IPath defaultOutPath = javaProject.getOutputLocation();
        if (defaultOutPath != null) {
            String bloc = getProjectLocation(javaProject);
            if (defaultOutPath.segmentCount() > 1) {
                bloc += File.separator + defaultOutPath.removeFirstSegments(1).toOSString();
            }
            binEntries.add(bloc);
        }
    } catch (JavaModelException e) {
        GroovyCore.logException("Exception generating classpath for launching groovy script", e);
    }
    // recur through dependent projects
    for (IJavaProject dependingProject : dependingProjects) {
        if (dependingProject.getProject().isAccessible()) {
            addClasspathEntriesForProject(dependingProject, sourceEntries, binEntries);
        }
    }
}

From source file:org.compiere.mfg_scm.eclipse.db.DbfBootstrap.java

License:Apache License

private void getClassPathEntries(IJavaProject prj, ArrayList data, List selectedPaths,
        ArrayList visitedProjects) {
    IClasspathEntry[] entries = null;//from  www  . ja v a 2  s .co m

    IPath outputPath = null;
    try {
        outputPath = prj.getOutputLocation();
        if (selectedPaths.contains(outputPath.toFile().toString().replace('\\', '/'))) {
            add(data, prj.getProject().getWorkspace().getRoot().findMember(outputPath));
        }
        entries = prj.getRawClasspath();
    } catch (JavaModelException e) {
        DbfLauncherPlugin.log(e);
    }
    if (entries == null)
        return;
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        IPath path = entry.getPath();
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            path = entry.getOutputLocation();
            if (path == null)
                continue;
        }
        if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            String prjName = entry.getPath().lastSegment();
            if (!visitedProjects.contains(prjName)) {
                visitedProjects.add(prjName);
                getClassPathEntries(prj.getJavaModel().getJavaProject(prjName), data, selectedPaths,
                        visitedProjects);
            }
            continue;
        } else if (!selectedPaths.contains(path.toFile().toString().replace('\\', '/')))
            continue;

        IClasspathEntry[] tmpEntry = null;
        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            try {
                tmpEntry = JavaCore.getClasspathContainer(path, prj).getClasspathEntries();
            } catch (JavaModelException e1) {
                DbfLauncherPlugin.log(e1);
                continue;
            }
        } else {
            tmpEntry = new IClasspathEntry[1];
            tmpEntry[0] = JavaCore.getResolvedClasspathEntry(entry);
        }

        for (int j = 0; j < tmpEntry.length; j++) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                IResource res = prj.getProject().getWorkspace().getRoot().findMember(tmpEntry[j].getPath());
                if (res != null)
                    add(data, res);
                else
                    add(data, tmpEntry[j].getPath());
            } else if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath srcPath = entry.getOutputLocation();
                if (srcPath != null && !srcPath.equals(outputPath)) {
                    add(data, prj.getProject().getWorkspace().getRoot().findMember(srcPath));
                }
            } else {
                add(data, tmpEntry[j].getPath());
            }
        }
    }
}

From source file:org.eclipse.andmore.internal.project.AndroidClasspathContainerInitializer.java

License:Open Source License

@Override
public void requestClasspathContainerUpdate(IPath containerPath, IJavaProject project,
        IClasspathContainer containerSuggestion) throws CoreException {
    AndmoreAndroidPlugin plugin = AndmoreAndroidPlugin.getDefault();

    synchronized (Sdk.getLock()) {
        boolean sdkIsLoaded = plugin.getSdkLoadStatus() == LoadStatus.LOADED;

        // check if the project has a valid target.
        IAndroidTarget target = null;//from   w  w  w . j  av  a  2 s. c  o  m
        if (sdkIsLoaded) {
            target = Sdk.getCurrent().getTarget(project.getProject());
        }
        if (sdkIsLoaded && target != null) {
            String[] paths = getTargetPaths(target);
            IPath android_lib = new Path(paths[CACHE_INDEX_JAR]);
            IClasspathEntry[] entries = containerSuggestion.getClasspathEntries();
            for (int i = 0; i < entries.length; i++) {
                IClasspathEntry entry = entries[i];
                if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                    IPath entryPath = entry.getPath();

                    if (entryPath != null) {
                        if (entryPath.equals(android_lib)) {
                            IPath entrySrcPath = entry.getSourceAttachmentPath();
                            IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
                            if (entrySrcPath != null) {
                                ProjectHelper.saveStringProperty(root, getAndroidSourceProperty(target),
                                        entrySrcPath.toString());
                            } else {
                                ProjectHelper.saveStringProperty(root, getAndroidSourceProperty(target), null);
                            }
                            IClasspathAttribute[] extraAttributtes = entry.getExtraAttributes();
                            if (extraAttributtes.length == 0) {
                                ProjectHelper.saveStringProperty(root, PROPERTY_ANDROID_API, NULL_API_URL);
                            }
                            for (int j = 0; j < extraAttributtes.length; j++) {
                                IClasspathAttribute extraAttribute = extraAttributtes[j];
                                String value = extraAttribute.getValue();
                                if ((value == null || value.trim().length() == 0)
                                        && IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME
                                                .equals(extraAttribute.getName())) {
                                    value = NULL_API_URL;
                                }
                                if (IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME
                                        .equals(extraAttribute.getName())) {
                                    ProjectHelper.saveStringProperty(root, PROPERTY_ANDROID_API, value);

                                }
                            }
                        }
                    }
                }
            }
            rebindClasspathEntries(project.getJavaModel(), containerPath);
        }
    }
}

From source file:org.eclipse.andmore.internal.project.ProjectHelper.java

License:Open Source License

/**
 * Find the list of projects on which this JavaProject is dependent on at the compilation level.
 *
 * @param javaProject Java project that we are looking for the dependencies.
 * @return A list of Java projects for which javaProject depend on.
 * @throws JavaModelException/*  w w w .j  a  v a  2s.co  m*/
 */
public static List<IJavaProject> getAndroidProjectDependencies(IJavaProject javaProject)
        throws JavaModelException {
    String[] requiredProjectNames = javaProject.getRequiredProjectNames();

    // Go from java project name to JavaProject name
    IJavaModel javaModel = javaProject.getJavaModel();

    // loop through all dependent projects and keep only those that are Android projects
    List<IJavaProject> projectList = new ArrayList<IJavaProject>(requiredProjectNames.length);
    for (String javaProjectName : requiredProjectNames) {
        IJavaProject androidJavaProject = javaModel.getJavaProject(javaProjectName);

        //Verify that the project has also the Android Nature
        try {
            if (!androidJavaProject.getProject().hasNature(AndmoreAndroidConstants.NATURE_DEFAULT)) {
                continue;
            }
        } catch (CoreException e) {
            continue;
        }

        projectList.add(androidJavaProject);
    }

    return projectList;
}

From source file:org.eclipse.jst.j2ee.internal.dialogs.TypeJavaSearchScope.java

License:Open Source License

/**
 * Method addProject. This method adds all the classpath entries for the
 * current project to the search scope./*from  w  w  w .  j  a v a2  s  .  c o m*/
 * 
 * @param javaProject
 * @param includesPrereqProjects
 * @param visitedProjects
 * @throws JavaModelException
 */
public void addProject(IJavaProject javaProject, boolean includesPrereqProjects, HashSet visitedProjects)
        throws JavaModelException {
    IProject project = javaProject.getProject();
    if (!project.isAccessible() || !visitedProjects.add(project))
        return;

    this.addEnclosingProjectOrJar(project.getFullPath());

    IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
    IJavaModel model = javaProject.getJavaModel();
    for (int i = 0, length = entries.length; i < length; i++) {
        IClasspathEntry entry = entries[i];
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
            IPath path = entry.getPath();
            this.add(path, true);
            this.addEnclosingProjectOrJar(path);
            break;
        case IClasspathEntry.CPE_PROJECT:
            if (includesPrereqProjects) {
                this.add(model.getJavaProject(entry.getPath().lastSegment()), true, visitedProjects);
            }
            break;
        case IClasspathEntry.CPE_SOURCE:
            this.add(entry.getPath(), true);
            break;
        }
    }
}

From source file:org.eclipse.jst.j2ee.internal.dialogs.TypeJavaSearchScope.java

License:Open Source License

/**
 * Method add. This method filters out all the classpath entries of the
 * project which are not exported./*from   w w w.ja va 2 s  .c  o m*/
 * 
 * @param javaProject
 * @param includesPrereqProjects
 * @param visitedProjects
 * @throws JavaModelException
 */
public void add(IJavaProject javaProject, boolean includesPrereqProjects, HashSet visitedProjects)
        throws JavaModelException {
    IProject project = javaProject.getProject();
    if (!project.isAccessible() || !visitedProjects.add(project))
        return;

    this.addEnclosingProjectOrJar(project.getFullPath());

    IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
    IJavaModel model = javaProject.getJavaModel();
    for (int i = 0, length = entries.length; i < length; i++) {
        IClasspathEntry entry = entries[i];
        if (includeExportedClassPathEntriesOnly()) {
            if (!entry.isExported() && entry.getEntryKind() != IClasspathEntry.CPE_SOURCE)
                continue;
        }
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
            IPath path = entry.getPath();
            this.add(path, true);
            this.addEnclosingProjectOrJar(path);
            break;
        case IClasspathEntry.CPE_PROJECT:
            if (includesPrereqProjects) {
                this.add(model.getJavaProject(entry.getPath().lastSegment()), true, visitedProjects);
            }
            break;
        case IClasspathEntry.CPE_SOURCE:
            this.add(entry.getPath(), true);
            break;
        }
    }
}

From source file:org.eclipse.jst.j2ee.internal.validation.DependencyUtil.java

License:Open Source License

/**
 * Return an array of open IJavaProjects which depend on the given IJavaProject parameter.
 *///w w w. j  av  a 2 s  .  co  m
public static IJavaProject[] getDependentJavaProjects(IJavaProject javaproject) {
    if (javaproject == null) {
        return EMPTY_JAVAPROJECTS;
    }

    // calculate the dependencies now.
    try {
        IJavaProject[] allProjects = javaproject.getJavaModel().getJavaProjects();
        Set tempSet = getTempSet();
        for (int i = 0; i < allProjects.length; i++) {
            IJavaProject p = allProjects[i];
            IJavaProject[] requires = getRequiredJavaProjects(p);
            for (int j = 0; j < requires.length; j++) {
                IJavaProject r = requires[j];
                if (javaproject.equals(r)) {
                    tempSet.add(p);
                    break;
                }
            }
        }
        IJavaProject[] dependency = new IJavaProject[tempSet.size()];
        tempSet.toArray(dependency);
        return dependency;
    } catch (JavaModelException exc) {
        J2EEPlugin.log(J2EEPlugin.createErrorStatus(0, javaproject.getProject().getName(), exc));

        return EMPTY_JAVAPROJECTS;
    }
}

From source file:org.eclipse.jst.j2ee.internal.validation.DependencyUtil.java

License:Open Source License

/**
 * Return an array of open IJavaProjects which the given IJavaProject parameter depends on.
 *//*  w  ww  .j  a  v a 2s  . c o  m*/
public static IJavaProject[] getRequiredJavaProjects(IJavaProject javaproject) {
    if (javaproject == null) {
        return EMPTY_JAVAPROJECTS;
    }

    try {
        IJavaModel jm = javaproject.getJavaModel();
        if (jm == null) {
            J2EEPlugin.log(J2EEPlugin.createErrorStatus(0, "DependencyCache::getRequiredJavaProjects(" //$NON-NLS-1$
                    + javaproject.getProject().getName() + ") IJavaModel == null", null)); //$NON-NLS-1$

            return EMPTY_JAVAPROJECTS;
        }

        String[] requiredProjects = javaproject.getRequiredProjectNames();
        if ((requiredProjects == null) || (requiredProjects.length == 0)) {
            return EMPTY_JAVAPROJECTS;
        }

        IJavaProject[] temp = new IJavaProject[requiredProjects.length];
        int count = 0;
        for (int i = 0; i < requiredProjects.length; i++) {
            String projectName = requiredProjects[i];
            IJavaProject jp = jm.getJavaProject(projectName);
            try {
                if ((jp == null) || (!jp.getProject().exists())) {
                    continue;
                }
                if (!jp.getProject().isAccessible()) {
                    continue;
                }

                if (!jp.getProject().hasNature(JavaCore.NATURE_ID)) {
                    continue;
                }

                temp[count++] = jp;
            } catch (CoreException exc) {
                J2EEPlugin.log(J2EEPlugin.createErrorStatus(0, javaproject.getProject().getName(), exc));

                continue;
            }
        }

        if (count == 0) {
            return EMPTY_JAVAPROJECTS;
        }

        if (count == temp.length) {
            return temp;
        }

        IJavaProject[] result = new IJavaProject[count];
        System.arraycopy(temp, 0, result, 0, count);
        return result;
    } catch (JavaModelException exc) {
        J2EEPlugin.log(J2EEPlugin.createErrorStatus(0, javaproject.getProject().getName(), exc));

        return EMPTY_JAVAPROJECTS;
    }
}

From source file:org.eclipse.modisco.java.discoverer.AbstractDiscoverJavaModelFromProject.java

License:Open Source License

public static Set<IJavaProject> computeRequiredProjects(final IJavaProject project) throws JavaModelException {
    Set<IJavaProject> projects = new LinkedHashSet<IJavaProject>();
    if (project == null) {
        return projects;
    }/*from  w w  w.  j a va  2  s . c om*/
    // we keep package fragments which are binaries
    projects.add(project);
    for (String projectName : project.getRequiredProjectNames()) {
        IJavaProject requiredProject = project.getJavaModel().getJavaProject(projectName);
        if (requiredProject.getProject().isAccessible()) {
            projects.add(requiredProject);
        }
    }
    return projects;
}