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

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

Introduction

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

Prototype

IPath getPath();

Source Link

Document

Returns the path to the innermost resource enclosing this element.

Usage

From source file:apet.utils.ClasspathUtils.java

License:Open Source License

/**
 * Returns the Eclipse Project class loader using project build path
 * @param javaProject Eclipse Java Project
 * @return A class loader using project build path
 * @throws Exception If the project is no valid
 *//*ww  w  .  ja v  a  2 s .  c o  m*/
public static ClassLoader getProjectClassLoader(IJavaProject javaProject) throws Exception {
    IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
    String wsPath = javaProject.getProject().getLocation().toPortableString();
    String firstEntryLocation = javaProject.getPath().toPortableString();
    int idx = wsPath.indexOf(firstEntryLocation);

    URL[] urls = null;
    int i = 0;

    //System.out.println("ClassLoader " + wsPath);
    //System.out.println("ClassLoader " + firstEntryLocation);

    String output = javaProject.getOutputLocation().toPortableString();
    urls = new URL[entries.length + 1];

    if (idx != -1) {
        wsPath = wsPath.substring(0, idx);
    } else {
        output = output.substring(firstEntryLocation.length());
    }
    urls[i++] = new File(wsPath + output).toURL();

    //System.out.println("ClassLoader " + output);

    String fullPath = null;

    for (IClasspathEntry entry : entries) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            IResource project = ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath());
            String projectPath = JavaCore.create(project.getProject()).getOutputLocation().toPortableString();
            fullPath = wsPath + projectPath;
        } else {
            Object resource = JavaModel.getTarget(entry.getPath(), true);

            if (resource instanceof IResource) {
                IResource iFile = (IResource) resource;
                fullPath = iFile.getLocation().toPortableString();
            } else if (resource instanceof File) {
                File file = (File) resource;
                fullPath = file.getAbsolutePath();
            }
        }

        urls[i++] = new File(fullPath).toURL();
        //System.out.println(fullPath);
    }

    URLClassLoader classLoader = new URLClassLoader(urls, String.class.getClassLoader());

    /*for (int j = 0; j < urls.length; j ++) {
       System.out.println(urls[j]);
    }*/

    return classLoader;
}

From source file:apet.utils.ClasspathUtils.java

License:Open Source License

/**
 * Returns a String with all libraries defined in build path
 * @param javaProject Eclipse Java Project
 * @return a String with all libraries defined in build path
 * @throws Exception If the project is no valid
 *//* ww  w. ja v a  2  s.  c  o m*/
public static String getStringClasspath(IJavaProject javaProject) throws Exception {

    IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
    String wsPath = javaProject.getProject().getLocation().toPortableString();
    String firstEntryLocation = javaProject.getPath().toPortableString();
    int idx = wsPath.indexOf(firstEntryLocation);

    String[] urls = new String[entries.length + 1];
    String fullPath = null;
    int i = 0;
    String output = javaProject.getOutputLocation().toPortableString();

    if (idx != -1) {
        wsPath = wsPath.substring(0, idx);
    } else {
        output = output.substring(firstEntryLocation.length());
    }
    urls[i++] = wsPath + output;

    for (IClasspathEntry entry : entries) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            IResource project = ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath());
            String projectPath = JavaCore.create(project.getProject()).getOutputLocation().toPortableString();
            fullPath = wsPath + projectPath;
        } else {
            Object resource = JavaModel.getTarget(entry.getPath(), true);

            if (resource instanceof IResource) {
                IResource iFile = (IResource) resource;
                fullPath = iFile.getLocation().toPortableString();
            } else if (resource instanceof File) {
                File file = (File) resource;
                fullPath = file.getAbsolutePath();
            }
        }

        urls[i++] = fullPath;
    }

    StringBuffer buffer = new StringBuffer();
    for (String url : urls) {
        buffer.append(url);
        buffer.append(":");
    }

    return buffer.toString().substring(0, buffer.length() - 1);

}

From source file:at.spardat.xma.gui.projectw.XMAProjectWizzardPage.java

License:Open Source License

public void performFinish(IProgressMonitor monitor) throws CoreException, InterruptedException {
    try {/*from w w  w .j  ava  2s .  c  om*/
        monitor.beginTask("create xma specifics", 3); //$NON-NLS-1$

        IJavaProject prj = fJavaPage.getJavaProject();
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        IResource resource = root.findMember(prj.getPath());
        IContainer container = (IContainer) resource;

        IPath location = container.getLocation();

        ProjectTemplateHandler.createProject(location.toString(), webappName.getText(),
                pluginName != null ? pluginName.getText() : "dummyPlugin.war", container.getName());

        container.refreshLocal(IResource.DEPTH_INFINITE, monitor);

    } finally {
        monitor.done();
    }
}

From source file:cn.dockerfoundry.ide.eclipse.server.core.internal.debug.DebugProvider.java

License:Open Source License

/**
 * Returns either test sources, or non-test sources, based on a flag
 * setting. If nothing is found, returns empty list.
 *//*w w  w.j  av a  2  s.c om*/
protected boolean containsDebugFiles(IJavaProject project) {
    try {

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

        if (entries != null) {
            for (IClasspathEntry entry : entries) {
                if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    IPath projectPath = project.getPath();
                    IPath relativePath = entry.getPath().makeRelativeTo(projectPath);
                    IFolder folder = project.getProject().getFolder(relativePath);
                    if (containsResource(folder, ".profile.d")) {//$NON-NLS-1$
                        return true;
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        DockerFoundryPlugin.logError(e);
    } catch (CoreException ce) {
        DockerFoundryPlugin.logError(ce);
    }
    return false;
}

From source file:com.aliyun.odps.eclipse.utils.NewWizardUtils.java

License:Apache License

public static void createClass(IJavaProject myjavapro, String className, String classPath,
        IProgressMonitor monitor, String content) throws JavaModelException {
    IPath mypath = myjavapro.getPath();
    mypath = mypath.append("src");
    mypath.append(classPath);//from w w  w  .j a  v  a  2 s.  com
    IPackageFragmentRoot fragmentRoot = myjavapro.findPackageFragmentRoot(mypath);
    IPackageFragment fragment = fragmentRoot.createPackageFragment(classPath, true, monitor);
    fragment.createCompilationUnit(className + ".java", content, true, monitor);
}

From source file:com.aliyun.odps.eclipse.utils.NewWizardUtils.java

License:Apache License

public static void createTestBase(IJavaProject myjavapro, String classPath, IProgressMonitor monitor,
        String content, String testBaseName) throws JavaModelException {
    IPath mypath = myjavapro.getPath();
    mypath = mypath.append("src");
    mypath.append(classPath);/*from www. ja va2 s .  c  om*/

    IPackageFragmentRoot fragmentRoot = myjavapro.findPackageFragmentRoot(mypath);
    IPackageFragment fragment = fragmentRoot.createPackageFragment(classPath, true, monitor);
    IJavaElement[] iJavaElement = fragment.getChildren();
    boolean exit = false;
    for (int i = 0; i < iJavaElement.length; i++) {
        if (iJavaElement[i].getElementName().equals(testBaseName)) {
            exit = true;
            break;
        }
    }
    if (!exit) {
        fragment.createCompilationUnit(testBaseName, content, false, monitor);
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.HierarchyScope.java

License:Open Source License

private void computeDependents(IJavaProject project, HashSet set, HashSet visited) {
    if (visited.contains(project))
        return;//from  ww w  .  j  av a2 s .c  om
    visited.add(project);
    IProject[] dependents = project.getProject().getReferencingProjects();
    for (int i = 0; i < dependents.length; i++) {
        try {
            IJavaProject dependent = JavaCore.create(dependents[i]);
            IPackageFragmentRoot[] roots = dependent.getPackageFragmentRoots();
            set.add(dependent.getPath());
            for (int j = 0; j < roots.length; j++) {
                IPackageFragmentRoot pkgFragmentRoot = roots[j];
                if (pkgFragmentRoot.isArchive()) {
                    set.add(pkgFragmentRoot.getPath());
                }
            }
            computeDependents(dependent, set, visited);
        } catch (JavaModelException e) {
            // project is not a java project
        }
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java

License:Open Source License

/**
 * Locates the package declarations corresponding to the search pattern.
 *///from   ww w  .  j  a  v  a2  s . c  o  m
protected void locatePackageDeclarations(SearchPattern searchPattern, SearchParticipant participant,
        IJavaProject[] projects) throws CoreException {
    if (this.progressMonitor != null && this.progressMonitor.isCanceled()) {
        throw new OperationCanceledException();
    }
    if (searchPattern instanceof OrPattern) {
        SearchPattern[] patterns = ((OrPattern) searchPattern).patterns;
        for (int i = 0, length = patterns.length; i < length; i++) {
            locatePackageDeclarations(patterns[i], participant, projects);
        }
    } else if (searchPattern instanceof PackageDeclarationPattern) {
        IJavaElement focus = searchPattern.focus;
        if (focus != null) {
            if (encloses(focus)) {
                SearchMatch match = new PackageDeclarationMatch(
                        focus.getAncestor(IJavaElement.PACKAGE_FRAGMENT), SearchMatch.A_ACCURATE, -1, -1,
                        participant, focus.getResource());
                report(match);
            }
            return;
        }
        PackageDeclarationPattern pkgPattern = (PackageDeclarationPattern) searchPattern;
        boolean isWorkspaceScope = this.scope == JavaModelManager.getJavaModelManager().getWorkspaceScope();
        IPath[] scopeProjectsAndJars = isWorkspaceScope ? null : this.scope.enclosingProjectsAndJars();
        int scopeLength = isWorkspaceScope ? 0 : scopeProjectsAndJars.length;
        SimpleSet packages = new SimpleSet();
        for (int i = 0, length = projects.length; i < length; i++) {
            IJavaProject javaProject = projects[i];
            if (this.progressMonitor != null) {
                if (this.progressMonitor.isCanceled())
                    throw new OperationCanceledException();
                this.progressWorked++;
                if ((this.progressWorked % this.progressStep) == 0)
                    this.progressMonitor.worked(this.progressStep);
            }
            // Verify that project belongs to the scope
            if (!isWorkspaceScope) {
                boolean found = false;
                for (int j = 0; j < scopeLength; j++) {
                    if (javaProject.getPath().equals(scopeProjectsAndJars[j])) {
                        found = true;
                        break;
                    }
                }
                if (!found)
                    continue;
            }
            // Get all project package fragment names
            this.nameLookup = ((JavaProject) projects[i]).newNameLookup(this.workingCopies);
            IPackageFragment[] packageFragments = this.nameLookup
                    .findPackageFragments(new String(pkgPattern.pkgName), false, true);
            int pLength = packageFragments == null ? 0 : packageFragments.length;
            // Report matches avoiding duplicate names
            for (int p = 0; p < pLength; p++) {
                IPackageFragment fragment = packageFragments[p];
                if (packages.addIfNotIncluded(fragment) == null)
                    continue;
                if (encloses(fragment)) {
                    IResource resource = fragment.getResource();
                    if (resource == null) // case of a file in an external jar
                        resource = javaProject.getProject();
                    try {
                        if (encloses(fragment)) {
                            SearchMatch match = new PackageDeclarationMatch(fragment, SearchMatch.A_ACCURATE,
                                    -1, -1, participant, resource);
                            report(match);
                        }
                    } catch (JavaModelException e) {
                        throw e;
                    } catch (CoreException e) {
                        throw new JavaModelException(e);
                    }
                }
            }
        }
    }
}

From source file:com.crispico.flower.mp.codesync.code.java.JavaElementChangedListener.java

License:Open Source License

private void processDelta(IJavaElementDelta deltaToProcess) {
    for (IJavaElementDelta delta : deltaToProcess.getAffectedChildren()) {
        processDelta(delta);/* w ww  .  j  a va2 s  . com*/
    }

    IJavaProject project = deltaToProcess.getElement().getJavaProject();
    if (project != null) {
        String path = project.getPath().toString();
        EditorStatefulService service = (EditorStatefulService) CommunicationPlugin.getInstance()
                .getServiceRegistry().getService(CodeSyncEditorStatefulService.SERVICE_ID);
        EditableResource editableResource = service.getEditableResource(path);
        if (editableResource != null) {
            for (EditableResourceClient client : editableResource.getClients()) {
                //               try {
                //                  CodeSyncPlugin.getInstance().getDragOnDiagramHandler().handleDragOnDiagram(Arrays.asList(deltaToProcess.getElement().getCorrespondingResource()), null, null, null, client.getCommunicationChannel());
                //               } catch (JavaModelException e) {
                //                  e.printStackTrace();
                //               }
            }
        }
    }
}

From source file:com.google.gdt.eclipse.core.JavaProjectTestUtilities.java

License:Open Source License

/**
 * Creates an {@link ICompilationUnit} with the given fully qualified name and
 * code in the <code>javaProject</code>.
 * //from ww  w  . ja  v  a2  s . c om
 * @param javaProject java project to host the new class
 * @param fullyQualifiedClassName fully qualified name for the class
 * @param source code for the classs
 * @return newly created {@link ICompilationUnit}
 * @throws JavaModelException
 */
public static ICompilationUnit createCompilationUnit(IJavaProject javaProject, String fullyQualifiedClassName,
        String source) throws JavaModelException {
    IPackageFragmentRoot root = javaProject.findPackageFragmentRoot(javaProject.getPath());
    if (root == null) {
        addRawClassPathEntry(javaProject, JavaCore.newSourceEntry(javaProject.getPath()));
        root = javaProject.findPackageFragmentRoot(javaProject.getPath());
    }

    String qualifier = Signature.getQualifier(fullyQualifiedClassName);
    IProgressMonitor monitor = new NullProgressMonitor();
    IPackageFragment packageFragment = root.createPackageFragment(qualifier, true, monitor);
    String name = Signature.getSimpleName(fullyQualifiedClassName);
    return packageFragment.createCompilationUnit(name + ".java", source, false, monitor);
}