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:com.android.ide.eclipse.adt.internal.project.AndroidClasspathContainerInitializer.java

License:Open Source License

@Override
public void requestClasspathContainerUpdate(IPath containerPath, IJavaProject project,
        IClasspathContainer containerSuggestion) throws CoreException {
    AdtPlugin plugin = AdtPlugin.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  ava 2 s .co 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:com.android.ide.eclipse.adt.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//from   www  . j  av a  2  s .c o  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(AdtConstants.NATURE_DEFAULT)) {
                continue;
            }
        } catch (CoreException e) {
            continue;
        }

        projectList.add(androidJavaProject);
    }

    return projectList;
}

From source file:com.android.ide.eclipse.adt.project.ProjectHelper.java

License:Open Source License

/**
 * Find the list of projects on which this JavaProject is dependent on at the compilation level.
 * /*from  ww w  .  j a  v a2 s  . c o m*/
 * @param javaProject Java project that we are looking for the dependencies.
 * @return A list of Java projects for which javaProject depend on.
 * @throws JavaModelException
 */
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(AndroidConstants.NATURE)) {
                continue;
            }
        } catch (CoreException e) {
            continue;
        }

        projectList.add(androidJavaProject);
    }

    return projectList;
}

From source file:com.android.ide.eclipse.auidt.internal.project.AndroidClasspathContainerInitializer.java

License:Open Source License

@Override
public void requestClasspathContainerUpdate(IPath containerPath, IJavaProject project,
        IClasspathContainer containerSuggestion) throws CoreException {
    AdtPlugin plugin = AdtPlugin.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 ava  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:com.iw.plugins.spindle.util.lookup.TapestryLookup.java

License:Mozilla Public License

public void configure(IJavaProject project) throws JavaModelException {
    seekExtensions = TapestryPlugin.getManagedExtensions();
    this.project = project;
    workspace = project.getJavaModel().getWorkspace();
    fPackageFragmentRoots = project.getAllPackageFragmentRoots();
    fPackageFragments = new HashMap();
    IPackageFragment[] frags = getPackageFragmentsInRoots(fPackageFragmentRoots, project);
    for (int i = 0; i < frags.length; i++) {
        IPackageFragment fragment = frags[i];
        IPackageFragment[] entry = (IPackageFragment[]) fPackageFragments.get(fragment.getElementName());
        if (entry == null) {
            entry = new IPackageFragment[1];
            entry[0] = fragment;//from w  w w.  j  a v  a 2s . co  m
            fPackageFragments.put(fragment.getElementName(), entry);
        } else {
            IPackageFragment[] copy = new IPackageFragment[entry.length + 1];
            System.arraycopy(entry, 0, copy, 0, entry.length);
            copy[entry.length] = fragment;
            fPackageFragments.put(fragment.getElementName(), copy);
        }
    }
    initialized = true;
}

From source file:com.photon.phresco.util.Utility.java

License:Apache License

private static String getEclipseHome() {
    IPath location = null;/*from  www  .  j  a v  a2s .  c  o  m*/
    String workingPath = "";
    ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();
    if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
        Object[] selectedObjects = ((IStructuredSelection) selection).toArray();
        for (Object object : selectedObjects) {
            if (object instanceof IProject) {
                IProject iProject = (IProject) object;
                location = iProject.getLocation();
            }
            if (object instanceof IJavaProject) {
                IJavaProject project = (IJavaProject) object;
                project.getJavaModel().getPath();
                location = project.getProject().getLocation();
            }
        }
        String dir = location.toOSString();
        workingPath = StringUtils.removeEnd(dir, location.lastSegment());
    } else {
        String workingDir = ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString()
                + File.separatorChar + "projects";
        File filePath = new File(workingDir);
        if (!filePath.isDirectory()) {
            filePath.mkdir();
        }
        workingPath = filePath.getPath() + File.separatorChar;
    }
    return workingPath;
}

From source file:com.technophobia.substeps.junit.action.OpenEditorAction.java

License:Open Source License

private IType internalFindType(final IJavaProject project, final String testClassName,
        final Set<IJavaProject> visitedProjects, final IProgressMonitor monitor) throws JavaModelException {
    try {/*from  w  w w . ja  v  a2s  .  c  om*/
        if (visitedProjects.contains(project))
            return null;
        monitor.beginTask("", 2); //$NON-NLS-1$
        IType type = project.findType(testClassName, new SubProgressMonitor(monitor, 1));
        if (type != null)
            return type;
        // fix for bug 87492: visit required projects explicitly to also
        // find not exported types
        visitedProjects.add(project);
        final IJavaModel javaModel = project.getJavaModel();
        final String[] requiredProjectNames = project.getRequiredProjectNames();
        final IProgressMonitor reqMonitor = new SubProgressMonitor(monitor, 1);
        reqMonitor.beginTask("", requiredProjectNames.length); //$NON-NLS-1$
        for (final String requiredProjectName : requiredProjectNames) {
            final IJavaProject requiredProject = javaModel.getJavaProject(requiredProjectName);
            if (requiredProject.exists()) {
                type = internalFindType(requiredProject, testClassName, visitedProjects,
                        new SubProgressMonitor(reqMonitor, 1));
                if (type != null)
                    return type;
            }
        }
        return null;
    } finally {
        monitor.done();
    }
}

From source file:jp.co.dgic.eclipse.jdt.internal.junit.launcher.DJUnitLaunchConfiguration.java

License:Open Source License

private void findSourceDirs(IJavaProject javaProject, Set<IPath> sourceDirs, Set<String> checkedProjects)
        throws CoreException {

    // check cycle reference
    if (checkedProjects.contains(javaProject.getProject().getName()))
        return;/*from w w w .  java2 s . c o  m*/
    checkedProjects.add(javaProject.getProject().getName());

    findSourceDirFrom(javaProject, sourceDirs);
    try {
        String[] names = javaProject.getRequiredProjectNames();
        for (int i = 0; i < names.length; i++) {
            IJavaProject requiredProject = javaProject.getJavaModel().getJavaProject(names[i]);
            findSourceDirs(requiredProject, sourceDirs, checkedProjects);
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
}

From source file:net.sf.eclipse.tomcat.TomcatBootstrap.java

License:Open Source License

private void getClassPathEntries(IClasspathEntry[] entries, IJavaProject prj, ArrayList data,
        List selectedPaths, ArrayList visitedProjects, IPath outputPath) {
    for (IClasspathEntry entrie : entries) {
        IClasspathEntry entry = entrie;/*from   ww  w  . ja v  a2s.c o m*/
        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('\\', '/'))) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER
                    && !entry.getPath().toString().equals("org.eclipse.jdt.launching.JRE_CONTAINER")) {

                // entires in container are only processed individually
                // if container itself is not selected

                IClasspathContainer container;
                try {
                    container = JavaCore.getClasspathContainer(path, prj);
                } catch (JavaModelException e1) {
                    TomcatLauncherPlugin.log(e1);
                    container = null;
                }

                if (container != null) {
                    getClassPathEntries(container.getClasspathEntries(), prj, data, selectedPaths,
                            visitedProjects, outputPath);
                }
            }
            continue;
        }

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

        for (IClasspathEntry element : tmpEntry) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                IResource res = prj.getProject().getWorkspace().getRoot().findMember(element.getPath());
                if (res != null) {
                    add(data, res);
                } else {
                    add(data, element.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 {
                TomcatLauncherPlugin.log(">>> " + element);
                if (element.getPath() != null) {
                    add(data, element.getPath());
                }
            }
        }
    }
}

From source file:net.sf.eclipse.tomcat.TomcatBootstrap.java

License:Open Source License

private void collectMavenDependencies(IClasspathEntry[] entries, IJavaProject prj, List data,
        List visitedProjects) {/* w  ww . ja  va  2 s . c om*/
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        IPath path = entry.getPath();
        if ((entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER)
                && entry.getPath().toString().endsWith("MAVEN2_CLASSPATH_CONTAINER")) {
            IClasspathEntry[] tmpEntry = null;
            try {
                tmpEntry = JavaCore.getClasspathContainer(path, prj).getClasspathEntries();
            } catch (JavaModelException e1) {
                TomcatLauncherPlugin.log(e1);
                continue;
            }
            for (int j = 0; j < tmpEntry.length; j++) {
                if (tmpEntry[j].getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                    if (tmpEntry[j].getPath().lastSegment().matches(".*servlet-api[^\\/]{0,10}\\.jar$")) {
                        continue;
                    }
                    if (tmpEntry[j].getPath().lastSegment().matches(".*jasper[^\\/]{0,10}\\.jar$")) {
                        continue;
                    }
                    if (tmpEntry[j].getPath().lastSegment().matches(".*annotations-api[^\\/]{0,10}.\\.jar$")) {
                        continue;
                    }
                    if (tmpEntry[j].getPath().lastSegment().matches(".*el-api[^\\/]{0,10}\\.jar$")) {
                        continue;
                    }
                    if (tmpEntry[j].getPath().lastSegment().matches(".*jsp-api[^\\/]{0,10}\\.jar$")) {
                        continue;
                    }
                    IResource res = prj.getProject().getWorkspace().getRoot().findMember(tmpEntry[j].getPath());
                    if (res != null) {
                        add(data, res);
                    } else {
                        add(data, tmpEntry[j].getPath());
                    }
                } else if (tmpEntry[j].getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                    String prjName = tmpEntry[j].getPath().lastSegment();
                    IJavaProject subPrj = prj.getJavaModel().getJavaProject(prjName);

                    try {
                        add(data, prj.getProject().getWorkspace().getRoot()
                                .findMember(subPrj.getOutputLocation()));
                    } catch (JavaModelException e1) {
                        TomcatLauncherPlugin.log(e1);
                        continue;
                    }
                    if (!visitedProjects.contains(prjName)) {
                        visitedProjects.add(prjName);
                        collectMavenDependencies(subPrj, data, visitedProjects);
                    }
                    continue;
                } else {
                    TomcatLauncherPlugin.log(">>> " + tmpEntry[j]);
                    if (tmpEntry[j].getPath() != null) {
                        add(data, tmpEntry[j].getPath());
                    }
                }
            }
        }
    }
}