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:org.eclipse.pde.ui.tests.ee.ExecutionEnvironmentTests.java

License:Open Source License

/**
 * Creates a plug-in project with a J2SE-1.4 execution environment. Validates that
 * compiler compliance settings and build path are correct. Modifies the compliance
 * options and then updates the class path again. Ensures that the enum and assert
 * identifier options do not overwrite existing 'error' severity.
 * // w w  w. j  a  v a2  s . c  o  m
 * @throws Exception
 */
public void testMinimumComplianceNoOverwrite() throws Exception {
    try {
        IExecutionEnvironment env = JavaRuntime.getExecutionEnvironmentsManager().getEnvironment("J2SE-1.4");
        IJavaProject project = ProjectUtils.createPluginProject("j2se14.error", env);
        assertTrue("Project was not created", project.exists());

        validateOption(project, JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_2);
        validateOption(project, JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_3);
        validateOption(project, JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_4);
        validateOption(project, JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.WARNING);
        validateOption(project, JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.WARNING);

        validateSystemLibrary(project, JavaRuntime.newJREContainerPath(env));

        // set to ignore assert/enum options
        project.setOption(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.ERROR);
        project.setOption(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.ERROR);
        validateOption(project, JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.ERROR);
        validateOption(project, JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.ERROR);

        // updating class path should increase severity to warning
        IPluginModelBase model = PluginRegistry.findModel(project.getProject());
        UpdateClasspathJob job = new UpdateClasspathJob(new IPluginModelBase[] { model });
        job.schedule();
        job.join();

        // re-validate options
        validateOption(project, JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_2);
        validateOption(project, JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_3);
        validateOption(project, JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_4);
        validateOption(project, JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.ERROR);
        validateOption(project, JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.ERROR);

    } finally {
        deleteProject("j2se14.error");
    }
}

From source file:org.eclipse.pde.ui.tests.ee.ExportBundleTests.java

License:Open Source License

/**
 * Exports a plug-in project with a custom execution environment and validates class file
 * target level./*from  w  w  w . j  a va  2 s  .  co m*/
 * 
 * @throws Exception
 */
public void testExportCustomEnvironment() throws Exception {
    try {
        IExecutionEnvironment env = JavaRuntime.getExecutionEnvironmentsManager()
                .getEnvironment(EnvironmentAnalyzerDelegate.EE_NO_SOUND);
        IJavaProject project = ProjectUtils.createPluginProject("no.sound.export", env);
        assertTrue("Project was not created", project.exists());

        final FeatureExportInfo info = new FeatureExportInfo();
        info.toDirectory = true;
        info.useJarFormat = true;
        info.exportSource = false;
        info.allowBinaryCycles = false;
        info.useWorkspaceCompiledClasses = false;
        info.destinationDirectory = EXPORT_PATH.toOSString();
        info.zipFileName = null;
        info.items = new Object[] { PluginRegistry.findModel(project.getProject()) };
        info.signingInfo = null;
        info.qualifier = "vXYZ";

        PluginExportOperation job = new PluginExportOperation(info, "Test-Export");
        job.schedule();
        job.join();
        if (job.hasAntErrors()) {
            fail("Export job had ant errors");
        }
        IStatus result = job.getResult();
        assertTrue("Export job had errors", result.isOK());

        // verify exported bundle exists
        IPath path = EXPORT_PATH.append("plugins/no.sound.export_1.0.0.jar");

        // The jar file may not have been copied to the file system yet, see Bug 424597
        if (!path.toFile().exists()) {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
            }
        }

        assertTrue("Missing exported bundle", path.toFile().exists());
        validateTargetLevel(path.toOSString(), "no/sound/export/Activator.class", 47);
    } finally {
        deleteProject("no.sound.export");
        deleteFolder(EXPORT_PATH.toFile());
    }
}

From source file:org.eclipse.pde.ui.tests.ee.ExportBundleTests.java

License:Open Source License

/**
 * Exports a plug-in project with a J2SE-1.4 execution environment and validates class file
 * target level./*  w w  w .ja v  a 2  s.com*/
 * 
 * @throws Exception
 */
public void testExport14Environment() throws Exception {
    try {
        IExecutionEnvironment env = JavaRuntime.getExecutionEnvironmentsManager().getEnvironment("J2SE-1.4");
        IJavaProject project = ProjectUtils.createPluginProject("j2se14.export", env);
        assertTrue("Project was not created", project.exists());

        final FeatureExportInfo info = new FeatureExportInfo();
        info.toDirectory = true;
        info.useJarFormat = true;
        info.exportSource = false;
        info.allowBinaryCycles = false;
        info.useWorkspaceCompiledClasses = false;
        info.destinationDirectory = EXPORT_PATH.toOSString();
        info.zipFileName = null;
        info.items = new Object[] { PluginRegistry.findModel(project.getProject()) };
        info.signingInfo = null;
        info.qualifier = "vXYZ";

        long l1 = System.currentTimeMillis();
        PluginExportOperation job = new PluginExportOperation(info, "Test-Export");
        long l2 = System.currentTimeMillis();
        job.schedule();
        long l3 = System.currentTimeMillis();
        job.join();
        long l4 = System.currentTimeMillis();
        if (job.hasAntErrors()) {
            fail("Export job had ant errors");
        }
        long l5 = System.currentTimeMillis();
        IStatus result = job.getResult();
        assertTrue("Export job had errors", result.isOK());
        long l6 = System.currentTimeMillis();

        // veriry exported bundle exists
        IPath path = EXPORT_PATH.append("plugins/j2se14.export_1.0.0.jar");
        long l7 = System.currentTimeMillis();

        boolean didPathExistBeforeSleep = path.toFile().exists();
        /*      give a 30 second delay when the path doesn't exist
              ( JUST IN CASE - unlikely to work but worth trying)*/
        if (!path.toFile().exists()) {
            try {
                Thread.sleep(30000);
            } catch (InterruptedException e) {
            }
        }
        boolean didPathExistAfterSleep = path.toFile().exists();

        long l8 = System.currentTimeMillis();

        /*    print out the time taken and see if there is a pattern when this test breaks
             see Bug 424597. Further debug statement may be required in future  */
        if (true) { //print information everytime - in PASS OR FAIL
            System.out.println("BUG 424597\n================================");
            System.out.println("Constructor of PluginExportOperation time: " + (l2 - l1));
            System.out.println("Schedule                             time: " + (l3 - l2));
            System.out.println("Job join                             time: " + (l4 - l3));
            System.out.println("Ant Error                            time: " + (l5 - l4));
            System.out.println("Job result                           time: " + (l6 - l5));
            System.out.println("Append                               time: " + (l7 - l6));
            System.out.println("Thread sleep time if file not pr     time: " + (l8 - l7));

            System.out.println("Did file exist before sleep: " + didPathExistBeforeSleep);
            System.out.println("Did file exist after  sleep: " + didPathExistAfterSleep);

            System.out.println("================================\nEnd of BUG 424597");
        }

        if (!path.toFile().exists()) {
            System.out.println("BUG 424597\n================================");
            File exportContents = EXPORT_PATH.toFile();
            if (exportContents.isDirectory()) {
                // Should only have plugin/feature folders
                File[] children = exportContents.listFiles();
                for (int i = 0; i < children.length; i++) {
                    if (children[i].isDirectory()) {
                        System.out.println("Directory: " + children[i].getName());
                        File[] subChildren = children[i].listFiles();
                        for (int j = 0; j < subChildren.length; j++) {
                            if (subChildren[j].isDirectory()) {
                                System.out.println("   Directory: " + subChildren[j].getName());
                            } else {
                                System.out.println("   File: " + subChildren[j].getName());
                            }
                        }
                    } else {
                        System.out.println("File: " + children[i].getName());
                    }
                }
            }
            System.out.println("================================\nEnd of BUG 424597");
        }

        assertTrue("Missing exported bundle", path.toFile().exists());
        validateTargetLevel(path.toOSString(), "j2se14/export/Activator.class", 46);
    } finally {
        deleteProject("j2se14.export");
        deleteFolder(EXPORT_PATH.toFile());
    }
}

From source file:org.eclipse.recommenders.completion.rcp.processable.IntelligentCompletionProposalComputer.java

License:Open Source License

private boolean isTriggeredInJavaProject() {
    if (jdtContext == null) {
        return false;
    }//from  w w  w  . ja  v a 2 s  .co  m

    IEditorPart editor = editorProvider.get();
    if (editor == null) {
        return false;
    }

    if (!JAVA_EDITOR_WHITELIST.contains(editor.getClass().getName())) {
        return false;
    }

    IJavaProject project = jdtContext.getProject();
    if (project == null) {
        return false;
    }
    return project.exists();
}

From source file:org.eclipse.sapphire.java.jdt.internal.ResourceStoreToIJavaProjectConversionService.java

License:Open Source License

@Override
public IJavaProject convert(final ResourceStore store) {
    final IProject project = store.adapt(IProject.class);

    if (project != null) {
        final IJavaProject jproject = JavaCore.create(project);

        if (jproject.exists()) {
            return jproject;
        }/*from www .j  a  v  a2  s  .  co m*/
    }

    return null;
}

From source file:org.eclipse.sapphire.java.jdt.ui.internal.JavaTypeJumpActionHandler.java

License:Open Source License

private static IType getType(String fullyQualifiedType, IProject project) {
    assert fullyQualifiedType != null : "Fully qualified type should not be null."; //$NON-NLS-1$

    // the JDT returns a non-null anonymous class IType 
    // for empty string and package names that end with a dot
    // if the type starts with a dot, the JDT helpfully removes it 
    // and returns the type referenced without the dot
    // short circuit here for perf and so validation results make sense
    // e.g. if the valid type is "Thing", then ".Thing" and "Thing." should not be valid
    if (fullyQualifiedType.trim().length() == 0 || fullyQualifiedType.startsWith(".") //$NON-NLS-1$
            || fullyQualifiedType.endsWith(".")) //$NON-NLS-1$
        return null;

    IJavaProject javaProject = JavaCore.create(project);

    if ((javaProject == null) || (!javaProject.exists())) {
        return null;
    }//from   w  ww. ja v a 2  s . co m

    final String name = fullyQualifiedType.replace('$', '.');

    IType type = null;

    try {
        type = javaProject.findType(name);

        if (type != null && (!type.exists() || type.isAnonymous())) {
            type = null;
        }
    } catch (JavaModelException e) {
        SapphireUiFrameworkPlugin.log(e);
    }

    return type;
}

From source file:org.eclipse.sapphire.java.jdt.ui.JavaTypeJumpActionHandler.java

License:Open Source License

private static IType getType(String fullyQualifiedType, IProject project) {
    assert fullyQualifiedType != null : "Fully qualified type should not be null."; //$NON-NLS-1$

    // the JDT returns a non-null anonymous class IType 
    // for empty string and package names that end with a dot
    // if the type starts with a dot, the JDT helpfully removes it 
    // and returns the type referenced without the dot
    // short circuit here for perf and so validation results make sense
    // e.g. if the valid type is "Thing", then ".Thing" and "Thing." should not be valid
    if (fullyQualifiedType.trim().length() == 0 || fullyQualifiedType.startsWith(".") //$NON-NLS-1$
            || fullyQualifiedType.endsWith(".")) //$NON-NLS-1$
        return null;

    IJavaProject javaProject = JavaCore.create(project);

    if ((javaProject == null) || (!javaProject.exists())) {
        return null;
    }/*from  w w w.  ja  va  2 s. c  o  m*/

    final String name = fullyQualifiedType.replace('$', '.');

    IType type = null;

    try {
        type = javaProject.findType(name);

        if (type != null && (!type.exists() || type.isAnonymous())) {
            type = null;
        }
    } catch (JavaModelException e) {
        Sapphire.service(LoggingService.class).log(e);
    }

    return type;
}

From source file:org.eclipse.sirius.common.xtext.internal.ResourceSetClasspathConfigurator.java

License:Open Source License

/**
 * Configures an Xtext ResourceSet for Java integration if we are inside a
 * Java project.//  www  .ja  v a  2  s  .c om
 */
public ResourceSet configure(XtextResourceSet set, IProject project) {
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject != null && javaProject.exists()) {
        set.getURIConverter().getURIMap().putAll(computePlatformURIMap(javaProject));
        set.setClasspathURIContext(javaProject);
        set.setClasspathUriResolver(new JdtClasspathUriResolver());
    }
    return set;
}

From source file:org.eclipse.sirius.common.xtext.internal.ResourceSetClasspathConfigurator.java

License:Open Source License

private Map<URI, URI> computePlatformURIMap(IJavaProject javaProject) {
    HashMap<URI, URI> hashMap = newHashMap(EcorePlugin.computePlatformURIMap());
    try {/* w  w w  .ja  v  a2  s . c om*/
        if (!javaProject.exists())
            return hashMap;
        IClasspathEntry[] classpath = javaProject.getResolvedClasspath(true);
        for (IClasspathEntry classPathEntry : classpath) {
            IPath path = classPathEntry.getPath();
            if (path != null && "jar".equals(path.getFileExtension())) { //$NON-NLS-1$
                try {
                    final File file = path.toFile();
                    if (file != null && file.exists()) {
                        try (JarFile jarFile = new JarFile(file)) {
                            Manifest manifest = jarFile.getManifest();
                            if (manifest != null) {
                                handleManifest(hashMap, file, manifest);
                            }
                        }
                    }
                } catch (IOException e) {
                    DslCommonPlugin.getDefault().error(e.getMessage(), e);
                }
            }
        }
    } catch (JavaModelException e) {
        DslCommonPlugin.getDefault().error(e.getMessage(), e);
    }
    return hashMap;
}

From source file:org.eclipse.sirius.common.xtext.internal.XTextResourceSetFactory.java

License:Open Source License

private ResourceSet configure(XtextResourceSet set, IProject project) {
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject != null && javaProject.exists()) {
        set.getURIConverter().getURIMap().putAll(computePlatformURIMap(javaProject));
        set.setClasspathURIContext(javaProject);
        set.setClasspathUriResolver(new JdtClasspathUriResolver());
    }//from   w w w.  ja  v  a2s .c  o  m
    return set;
}