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

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

Introduction

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

Prototype

boolean exists();

Source Link

Document

Returns whether this Java element exists in the model.

Usage

From source file:net.sf.j2s.ui.launching.J2SClasspathOptionTab.java

License:Open Source License

public void initializeFrom(ILaunchConfiguration configuration) {
    setDirty(false);// ww w  . ja  v a2  s .c o  m
    this.fLaunchConfiguration = configuration;
    try {
        IJavaModel javaModel = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
        String projectName = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
                (String) null);
        if ((projectName == null) || (projectName.trim().length() < 1)) {
            return;
        }
        IJavaProject javaProject = javaModel.getJavaProject(projectName);
        if ((javaProject == null) || !javaProject.exists()) {
            return;
        }
        IProject project = javaProject.getProject();
        String prjFolder = project.getLocation().toOSString();
        File workingDir = new File(prjFolder);
        String path = javaProject.getOutputLocation().toString();
        int idx = path.indexOf('/', 2);
        String relativePath = null;
        if (idx != -1) {
            relativePath = path.substring(idx + 1);
        }
        File j2sFile = new File(workingDir, ".j2s");
        String classpath = configuration.getAttribute(IJ2SLauchingConfiguration.J2S_CLASS_PATH, (String) null);
        String abandonClasspath = configuration.getAttribute(IJ2SLauchingConfiguration.J2S_ABANDON_CLASS_PATH,
                (String) null);
        if ((classpath == null || classpath.trim().length() == 0)
                && (abandonClasspath == null || abandonClasspath.trim().length() == 0)) {
            configPage.initConfigPage(j2sFile);
        } else {
            if (relativePath == null) {
                relativePath = "";
            }
            String propStr = "j2s.compiler.status=enable\r\nj2s.output.path=" + relativePath
                    + "\r\nj2s.resources.list=" + classpath + "\r\nj2s.abandoned.resources.list="
                    + abandonClasspath;
            configPage.initConfigPage(j2sFile, new ByteArrayInputStream(propStr.getBytes()));
        }
    } catch (CoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:net.sf.j2s.ui.launching.J2SClasspathOptionTab.java

License:Open Source License

public void performApply(ILaunchConfigurationWorkingCopy configuration) {
    try {//from w w  w  . j  av a  2  s .  c  o  m
        IJavaModel javaModel = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
        String projectName = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
                (String) null);
        if ((projectName == null) || (projectName.trim().length() < 1)) {
            return;
        }
        IJavaProject javaProject = javaModel.getJavaProject(projectName);
        if ((javaProject == null) || !javaProject.exists()) {
            return;
        }
        IProject project = javaProject.getProject();
        String prjFolder = project.getLocation().toOSString();
        File workingDir = new File(prjFolder);
        boolean useDefault = true;
        try {
            useDefault = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH,
                    true);
        } catch (CoreException e) {
            //JDIDebugUIPlugin.log(e);
        }
        String path = javaProject.getOutputLocation().toString();
        int idx = path.indexOf('/', 2);
        String relativePath = null;
        if (idx != -1) {
            relativePath = path.substring(idx + 1);
        }
        File j2sFile = new File(workingDir, ".j2s");
        String classpath = configuration.getAttribute(IJ2SLauchingConfiguration.J2S_CLASS_PATH, (String) null);
        String abandonClasspath = configuration.getAttribute(IJ2SLauchingConfiguration.J2S_ABANDON_CLASS_PATH,
                (String) null);
        if (relativePath == null) {
            relativePath = "";
        }
        String propStr = "j2s.output.path=" + relativePath + "\r\nj2s.resources.list=" + classpath
                + "\r\nj2s.abandoned.resources.list==" + abandonClasspath;
        InputStream is = new ByteArrayInputStream(propStr.getBytes());
        Properties prop = configPage.getUpdatedProperties(is, j2sFile);
        String resList = prop.getProperty("j2s.resources.list");
        if (!resList.equals(classpath)) {
            configuration.setAttribute(IJ2SLauchingConfiguration.J2S_CLASS_PATH, resList);
        }
        resList = prop.getProperty("j2s.abandoned.resources.list");
        if (!resList.equals(classpath)) {
            configuration.setAttribute(IJ2SLauchingConfiguration.J2S_ABANDON_CLASS_PATH, resList);
        }
    } catch (CoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:net.sf.jasperreports.eclipse.classpath.JavaProjectClassLoader.java

License:Open Source License

protected void init(IJavaProject project) {
    if (project == null || !project.exists() || !project.getResource().isAccessible())
        throw new IllegalArgumentException("Invalid javaProject");
    this.javaProject = project;
    getURLClassloader();// ww  w. j  ava 2s  .  co m
    listener = new IElementChangedListener() {

        public void elementChanged(final ElementChangedEvent event) {
            if (ignoreClasspathChanges(event))
                return;
            System.out.println("CLASSPATH CHANGED:" + event);
            // FIXME should release this classloader
            // what happend with current objects? we have 1 loader per
            // project, maybe se can filter some events? to have less
            // updates
            curlLoader = null;
            getURLClassloader();
            if (events != null)
                events.firePropertyChange("classpath", false, true);
        }
    };
    JavaCore.addElementChangedListener(listener, ElementChangedEvent.POST_CHANGE);
}

From source file:net.sf.jasperreports.eclipse.classpath.OutputFolderClassLoader.java

License:Open Source License

private byte[] loadClassData(String className) throws Exception {
    String[] requiredProjectNames = javaProject.getRequiredProjectNames();
    for (String reqPrjName : requiredProjectNames) {
        IJavaProject prj = javaProject.getJavaModel().getJavaProject(reqPrjName);
        if (prj.exists()) {
            byte[] classBytes = loadClassData(className, prj);
            if (classBytes != null) {
                return classBytes;
            }/* w w w .j  a v  a  2s .  c om*/
        }
    }
    return null;
}

From source file:net.sf.spindle.core.builder.EclipseBuildInfrastructure.java

License:Mozilla Public License

private boolean doesProjectSupportJavaAnnotations(IProject project) {
    if (true)/*from  w w  w . ja v  a 2s .c o  m*/
        return false; // FIXME get rid of this when we figure out how to handle annotations!

    IJavaProject jproject = JavaCore.create(project);

    if (jproject == null || !jproject.exists() || !project.isAccessible())
        return false;

    Map options = jproject.getOptions(true);

    String target = (String) options.get(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM);

    if (target == null)
        return false;

    float version = Float.parseFloat(target);

    if (version < 1.5)
        return false;

    System.out.println("target: " + target);

    String sourceCompatibility = (String) options.get(JavaCore.COMPILER_SOURCE);

    if (sourceCompatibility == null)
        return false;

    float compat = Float.parseFloat(sourceCompatibility);

    if (compat < 1.5)
        return false;

    System.out.println("sourceCompatibility: " + sourceCompatibility);

    return true;

}

From source file:org.apache.felix.sigil.eclipse.SigilCore.java

License:Apache License

public static void makeSigilProject(IProject project, IProgressMonitor monitor) throws CoreException {
    IProjectDescription description = project.getDescription();

    String[] natures = description.getNatureIds();
    String[] newNatures = new String[natures.length + 1];
    System.arraycopy(natures, 0, newNatures, 0, natures.length);
    newNatures[natures.length] = SigilCore.NATURE_ID;
    description.setNatureIds(newNatures);

    ICommand sigilBuild = description.newCommand();
    sigilBuild.setBuilderName(SigilCore.BUILDER_ID);

    ICommand javaBuild = description.newCommand();
    javaBuild.setBuilderName(JavaCore.BUILDER_ID);

    description.setBuildSpec(new ICommand[] { javaBuild, sigilBuild });

    project.setDescription(description, new SubProgressMonitor(monitor, 2));

    IJavaProject java = JavaCore.create(project);
    if (java.exists()) {
        IClasspathEntry[] cp = java.getRawClasspath();
        // check if sigil container is already on classpath - if not add it
        if (!isSigilOnClasspath(cp)) {
            ArrayList<IClasspathEntry> entries = new ArrayList<IClasspathEntry>(Arrays.asList(cp));
            entries.add(JavaCore.newContainerEntry(new Path(SigilCore.CLASSPATH_CONTAINER_PATH)));
            java.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), monitor);
        }//from w  w w .  j a v  a2s. c o  m
    }
}

From source file:org.apache.ivyde.eclipse.cp.IvyClasspathContainerHelper.java

License:Apache License

/**
 * Search the Ivy classpath containers within the specified Java project
 * //w ww . j a v  a 2 s  .  co m
 * @param javaProject
 *            the project to search into
 * @return the Ivy classpath container if found
 */
public static List<IvyClasspathContainer> getContainers(IJavaProject javaProject) {
    List<IvyClasspathContainer> containers = new ArrayList<IvyClasspathContainer>();
    if (javaProject == null || !javaProject.exists()) {
        return containers;
    }
    try {
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                IPath path = entry.getPath();
                if (isIvyClasspathContainer(path)) {
                    IClasspathContainer cp = JavaCore.getClasspathContainer(path, javaProject);
                    if (cp instanceof IvyClasspathContainer) {
                        containers.add((IvyClasspathContainer) cp);
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        // unless there are issues with the JDT, this should never happen
        IvyPlugin.log(e);
    }
    return containers;
}

From source file:org.apache.ivyde.eclipse.cp.IvyClasspathContainerHelper.java

License:Apache License

public static List<IvyClasspathContainer> getContainersFromIvyFile(IFile ivyfile) {
    IJavaProject javaProject = JavaCore.create(ivyfile.getProject());
    List<IvyClasspathContainer> containers = new ArrayList<IvyClasspathContainer>();
    if (javaProject == null || !javaProject.exists()) {
        return containers;
    }/*from w  ww .jav a 2  s  . c o m*/
    try {
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                IPath path = entry.getPath();
                if (isIvyClasspathContainer(path)) {
                    IClasspathContainer cp = JavaCore.getClasspathContainer(path, javaProject);
                    if (cp instanceof IvyClasspathContainerImpl) {
                        IvyClasspathContainerImpl ivycp = (IvyClasspathContainerImpl) cp;
                        if (ivycp.getConf().getIvyXmlPath()
                                .equals(ivyfile.getProjectRelativePath().toString())) {
                            containers.add(ivycp);
                        }
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        // unless there are issues with the JDT, this should never happen
        IvyPlugin.log(e);
    }
    return containers;
}

From source file:org.apache.ivyde.eclipse.cp.IvyClasspathContainerHelper.java

License:Apache License

public static List<IvyClasspathContainer> getContainersFromIvySettings(IFile ivySettings) {
    IJavaProject javaProject = JavaCore.create(ivySettings.getProject());
    List<IvyClasspathContainer> containers = new ArrayList<IvyClasspathContainer>();
    if (javaProject == null || !javaProject.exists()) {
        return containers;
    }//from  ww  w .j av  a2s.c om
    try {
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                IPath path = entry.getPath();
                if (isIvyClasspathContainer(path)) {
                    IClasspathContainer cp = JavaCore.getClasspathContainer(path, javaProject);
                    if (cp instanceof IvyClasspathContainerImpl) {
                        IvyClasspathContainerImpl ivycp = (IvyClasspathContainerImpl) cp;
                        ResolvedPath settingsPath;
                        try {
                            settingsPath = ivycp.getConf().getInheritedSettingsSetup()
                                    .getResolvedIvySettingsPath(ivycp.getConf().getProject());
                        } catch (IvyDEException e) {
                            // cannot resolve the ivy settings so just ignore
                            continue;
                        }
                        if (settingsPath.getResolvedPath()
                                .equals(ivySettings.getProjectRelativePath().toString())) {
                            containers.add(ivycp);
                        }
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        // unless there are issues with the JDT, this should never happen
        IvyPlugin.log(e);
    }
    return containers;
}

From source file:org.apache.ivyde.eclipse.cp.IvyClasspathContainerHelper.java

License:Apache License

/**
 * Search the Ivy classpath entry within the specified Java project with the specific path
 * //from   w w  w.java2  s.  c om
 * @param containerPath
 *            the path of the container
 * @param javaProject
 *            the project to search into
 * @return the Ivy classpath container if found, otherwise return <code>null</code>
 */
public static IClasspathEntry getEntry(IPath containerPath, IJavaProject javaProject) {
    if (javaProject == null || !javaProject.exists()) {
        return null;
    }
    try {
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                if (containerPath.equals(entry.getPath())) {
                    return entry;
                }
            }
        }
    } catch (JavaModelException e) {
        // unless there are issues with the JDT, this should never happen
        IvyPlugin.log(e);
    }
    return null;
}