Example usage for org.eclipse.jdt.core JavaCore create

List of usage examples for org.eclipse.jdt.core JavaCore create

Introduction

In this page you can find the example usage for org.eclipse.jdt.core JavaCore create.

Prototype

public static IJavaModel create(IWorkspaceRoot root) 

Source Link

Document

Returns the Java model.

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
 *///w w w.j a  va2s .  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
 *///from www .  j  a v a 2s  .co  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:apet.utils.SourceUtils.java

License:Open Source License

private static IJavaProject obtainJavaProjectFromResource(IResource jresource) {
    IProject project = jresource.getProject();
    return JavaCore.create(project);
}

From source file:ar.com.fluxit.jqa.JQAEclipseRunner.java

License:Open Source License

private void run(IResource rulesContextFile, IProject targetProject) throws RulesContextFactoryException,
        IntrospectionException, FileNotFoundException, TypeFormatException, IOException {
    final IJavaProject javaProject = JavaCore.create(targetProject);
    final RulesContextFactory rulesContextFactory = RulesContextFactoryLocator.getRulesContextFactory();
    final RulesContext rulesContext = rulesContextFactory
            .getRulesContext(rulesContextFile.getRawLocation().toOSString());
    try {/*from w  w  w.ja v a2 s .co m*/
        Collection<File> classPath = getClassPath(javaProject);
        Collection<File> classFiles = getClassFiles(javaProject);
        String sourceJavaVersion = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
        File[] sourceDir = Utils.getSourcesDirs(javaProject);
        final CheckingResult checkResult = RulesContextChecker.INSTANCE.check(targetProject.getName(),
                classFiles, classPath, rulesContext, sourceDir, sourceJavaVersion, LOGGER);
        JQAEclipseMarker.INSTANCE.mark(javaProject, checkResult);
    } catch (JavaModelException e) {
        throw new IllegalStateException("Can not parse Java project: " + javaProject.getElementName(), e);
    }
}

From source file:ar.com.fluxit.jqa.utils.JdtUtils.java

License:Open Source License

private static Collection<IPackageFragment> getPackages(String pkg, IProject[] targetProjects) {
    try {/*from  w ww.j a v  a 2  s  . c  om*/
        Collection<IPackageFragment> result = new ArrayList<IPackageFragment>();
        for (IProject project : targetProjects) {
            IJavaProject javaProject = JavaCore.create(project);
            for (IPackageFragment packageFragment : javaProject.getPackageFragments()) {
                if (packageFragment.getElementName().equals(pkg) && JdtUtils.isSourcePackage(packageFragment)) {
                    result.add(packageFragment);
                }
            }
        }
        return result;
    } catch (JavaModelException e) {
        throw new IllegalStateException("An error occured while collecting packages", e);
    }
}

From source file:ar.com.fluxit.jqa.wizard.page.LayersDefinitionWizardPage.java

License:Open Source License

private List<IJavaElement> collectNonEmptyPackages() {
    try {//from w w w . j  a  v a 2  s  . c  o  m
        List<IJavaElement> result = new ArrayList<IJavaElement>();
        for (IProject project : getWizard().getTargetProjects()) {
            final IJavaProject javaProject = JavaCore.create(project);
            for (IPackageFragment packageFragment : javaProject.getPackageFragments()) {
                if (packageFragment.containsJavaResources()
                        && packageFragment.getKind() == IPackageFragmentRoot.K_SOURCE) {
                    result.add(packageFragment);
                }
            }
        }
        return result;
    } catch (JavaModelException e) {
        throw new IllegalStateException("An error has occurred while collection Java packages", e);
    }
}

From source file:ar.com.tadp.xml.rinzo.jdt.JDTUtils.java

License:Open Source License

/**
 * @return All projects contained in the workspace.
 *///from   w w  w.  j  a  v  a  2s  .c  om
public static IJavaProject[] getWorkspaceProjects() {
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IJavaModel javaModel = JavaCore.create(workspace.getRoot());
    IJavaProject[] projects = null;
    try {
        projects = javaModel.getJavaProjects();
    } catch (JavaModelException jme) {
        projects = new IJavaProject[0];
    }
    return projects;
}

From source file:ar.com.tadp.xml.rinzo.jdt.JDTUtils.java

License:Open Source License

public static IJavaProject getActiveJavaProject() {
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    IJavaProject project = null;//from  ww  w.j  a  v a2  s.  co  m
    if (window != null) {
        IWorkbenchPage page = window.getActivePage();
        if (page != null) {
            IFile input = ResourceUtil.getFile(page.getActiveEditor().getEditorInput());
            if (input != null) {
                project = JavaCore.create(input.getProject());
            }
        }
    }
    return project;
}

From source file:ariba.ideplugin.eclipse.wizards.CreateProject.java

License:Apache License

public void execute(IProgressMonitor monitor) throws Exception {
    monitor.beginTask("Creating AribaWeb Project", 4);
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot wroot = workspace.getRoot();

    IProject proj = wroot.getProject(_projectName);
    if (!proj.exists()) {
        File ploc = new File(wroot.getLocation().toFile(), _projectName);
        ploc.mkdirs();//from   www.java 2 s . c  o  m
        _paramMap.put("ProjectName", _projectName);

        _connector.createProject(_templateLoc, ploc, _paramMap);

        monitor.worked(1);

        File locationAsFile = new File(wroot.getLocation().toString(), _projectName);
        if (!locationAsFile.exists() && !locationAsFile.mkdir()) {
            // String msg = "Could not create " + locationAsFile;
            // log error
            return;
        }
        File f = new File(locationAsFile, ".project");
        if (f.exists()) {
            f.delete();
        }
        f = new File(locationAsFile, ".classpath");
        if (f.exists()) {
            f.delete();
        }
        IProjectDescription pid = workspace.newProjectDescription(_projectName);
        String natures[] = { "org.eclipse.jdt.core.javanature" };
        pid.setNatureIds(natures);
        pid.setLocation(null);

        proj.create(pid, monitor);
        final IJavaProject javaProj = JavaCore.create(proj);
        if (!proj.isOpen()) {
            proj.open(monitor);
        }

        monitor.worked(1);
        updateClasspath(javaProj, locationAsFile, _projectName, monitor);
        monitor.worked(1);
    } else {
        // this should not happen
    }
}

From source file:at.bestsolution.efxclipse.robovm.RobovmSetupHandler.java

License:Open Source License

private void resolveDataProject(IJavaProject project, Set<IPath> listProjectSourceDirs,
        Set<IPath> listRefProjectSourceDirs, Set<IPath> listRefLibraries) {
    try {//w ww  .  j a  v a2  s.  c  o  m
        IClasspathEntry[] entries = project.getRawClasspath();
        for (IClasspathEntry e : entries) {
            if (e.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(e.getPath().lastSegment());
                if (p.exists()) {
                    resolveDataProject(JavaCore.create(p), listRefProjectSourceDirs, listRefProjectSourceDirs,
                            listRefLibraries);
                }
            } else if (e.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                listRefLibraries.add(e.getPath());
            } else if (e.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                listProjectSourceDirs.add(e.getPath());
            } else if (e.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                String start = e.getPath().segment(0);
                // TODO remove hard coded strings
                if (!"org.eclipse.jdt.launching.JRE_CONTAINER".equals(start)
                        && !"org.eclipse.fx.ide.jdt.core.JAVAFX_CONTAINER".equals(start)) {
                    IClasspathContainer cpe = JavaCore.getClasspathContainer(e.getPath(), project);
                    IClasspathEntry[] cpEntries = cpe.getClasspathEntries();
                    for (IClasspathEntry tmp : cpEntries) {
                        if (tmp.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                            listRefLibraries.add(tmp.getPath());
                        }
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}