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

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

Introduction

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

Prototype

IResource getResource();

Source Link

Document

Returns the innermost resource enclosing this element.

Usage

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

License:Open Source License

private void deleteCurrentMarks(IJavaProject javaProject) {
    try {/*  w w  w  . j  a  v  a 2s  . c  om*/
        IMarker[] problems = javaProject.getResource().findMarkers(MARKER_ID, true, IResource.DEPTH_INFINITE);
        for (IMarker iMarker : problems) {
            iMarker.delete();
        }
    } catch (CoreException e) {
        throw new IllegalStateException("Can not delete current marks", e);
    }

}

From source file:at.bestsolution.efxclipse.tooling.ui.wizards.AbstractNewJDTElementWizard.java

License:Open Source License

protected IPackageFragmentRoot getFragmentRoot(IJavaElement elem) {
    IPackageFragmentRoot initRoot = null;
    if (elem != null) {
        initRoot = (IPackageFragmentRoot) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
        try {// w ww .  j av  a  2  s.  c  o  m
            if (initRoot == null || initRoot.getKind() != IPackageFragmentRoot.K_SOURCE) {
                IJavaProject jproject = elem.getJavaProject();
                if (jproject != null) {
                    initRoot = null;
                    if (jproject.exists()) {
                        IPackageFragmentRoot[] roots = jproject.getPackageFragmentRoots();
                        for (int i = 0; i < roots.length; i++) {
                            if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
                                initRoot = roots[i];
                                break;
                            }
                        }
                    }
                    if (initRoot == null) {
                        initRoot = jproject.getPackageFragmentRoot(jproject.getResource());
                    }
                }
            }
        } catch (JavaModelException e) {
            // TODO
            e.printStackTrace();
        }
    }
    return initRoot;
}

From source file:br.ufal.cideei.handlers.DoAnalysisOnClassPath.java

License:Open Source License

/**
 * Configures the classpath, sets up the transformers, load (jimplify) classes and run the packs.
 * /*from  w  w w  .  j  a va2 s . co  m*/
 * @param javaProject
 * @param entry
 * @param libs
 */
private void addPacks(IJavaProject javaProject, IClasspathEntry entry, String libs) {
    /*
     * if the classpath entry is "", then JDT will complain about it.
     */
    String classPath;
    if (entry.getPath().toOSString().equals(File.separator + javaProject.getElementName())) {
        classPath = javaProject.getResource().getLocation().toFile().getAbsolutePath();
    } else {
        classPath = ResourcesPlugin.getWorkspace().getRoot().getFolder(entry.getPath()).getLocation()
                .toOSString();
    }

    SootManager.configure(classPath + File.pathSeparator + libs);

    System.out.println(classPath);

    IFeatureExtracter extracter = CIDEFeatureExtracterFactory.getInstance().getExtracter();
    IPackageFragmentRoot[] packageFragmentRoots = javaProject.findPackageFragmentRoots(entry);
    for (IPackageFragmentRoot packageFragmentRoot : packageFragmentRoots) {
        IJavaElement[] children = null;
        try {
            children = packageFragmentRoot.getChildren();
        } catch (JavaModelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (IJavaElement child : children) {
            IPackageFragment packageFragment = (IPackageFragment) child;
            ICompilationUnit[] compilationUnits = null;
            try {
                compilationUnits = packageFragment.getCompilationUnits();
            } catch (JavaModelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            for (ICompilationUnit compilationUnit : compilationUnits) {
                String fragmentName = packageFragment.getElementName();
                String compilationName = compilationUnit.getElementName();
                StringBuilder qualifiedNameStrBuilder = new StringBuilder(fragmentName);
                // If it's the default package:
                if (qualifiedNameStrBuilder.length() == 0) {
                    // Remove ".java" suffix
                    qualifiedNameStrBuilder.append(compilationName.substring(0, compilationName.length() - 5));
                } else {
                    // Remove ".java" suffix
                    qualifiedNameStrBuilder.append(".")
                            .append(compilationName.substring(0, compilationName.length() - 5));
                }

                // This goes into Soot loadAndSupport
                SootManager.loadAndSupport(qualifiedNameStrBuilder.toString());
            }
        }
    }

    Scene.v().loadNecessaryClasses();

    AlloyConfigurationCheck alloyConfigurationCheck = null;
    // #ifdef FEATUREMODEL
    //@      try {
    //@         String absolutePath = javaProject.getResource().getLocation().toFile().getAbsolutePath();
    //@         alloyConfigurationCheck = new AlloyConfigurationCheck(absolutePath + File.separator  + "fm.als");
    //@      } catch (CannotReadAlloyFileException e) {
    //@         e.printStackTrace();
    //@         return;
    //@      }
    // #endif

    addPacks(classPath, extracter, alloyConfigurationCheck);

    SootManager.runPacks(extracter);
}

From source file:br.ufal.cideei.handlers.DoFeatureObliviousAnalysisOnClassPath.java

License:Open Source License

private void addPacks(IJavaProject javaProject, IClasspathEntry entry, String libs) {
    /*// w w  w .j av  a 2 s.co m
     * if the classpath entry is "", then JDT will complain about it.
     */
    String classPath;
    if (entry.getPath().toOSString().equals(File.separator + javaProject.getElementName())) {
        classPath = javaProject.getResource().getLocation().toFile().getAbsolutePath();
    } else {
        classPath = ResourcesPlugin.getWorkspace().getRoot().getFolder(entry.getPath()).getLocation()
                .toOSString();
    }

    SootManager.configure(classPath + File.pathSeparator + libs);

    IFeatureExtracter extracter = CIDEFeatureExtracterFactory.getInstance().getExtracter();
    IPackageFragmentRoot[] packageFragmentRoots = javaProject.findPackageFragmentRoots(entry);
    for (IPackageFragmentRoot packageFragmentRoot : packageFragmentRoots) {
        IJavaElement[] children = null;
        try {
            children = packageFragmentRoot.getChildren();
        } catch (JavaModelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (IJavaElement child : children) {
            IPackageFragment packageFragment = (IPackageFragment) child;
            ICompilationUnit[] compilationUnits = null;
            try {
                compilationUnits = packageFragment.getCompilationUnits();
            } catch (JavaModelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            for (ICompilationUnit compilationUnit : compilationUnits) {
                String fragmentName = packageFragment.getElementName();
                String compilationName = compilationUnit.getElementName();
                StringBuilder qualifiedNameStrBuilder = new StringBuilder(fragmentName);
                // If it's the default package:
                if (qualifiedNameStrBuilder.length() == 0) {
                    // Remove ".java" suffix
                    qualifiedNameStrBuilder.append(compilationName.substring(0, compilationName.length() - 5));
                } else {
                    // Remove ".java" suffix
                    qualifiedNameStrBuilder.append(".")
                            .append(compilationName.substring(0, compilationName.length() - 5));
                }

                // This goes into Soot loadAndSupport
                SootManager.loadAndSupport(qualifiedNameStrBuilder.toString());
            }
        }
    }
    Scene.v().loadNecessaryClasses();

    AlloyConfigurationCheck alloyConfigurationCheck = null;
    // #ifdef FEATUREMODEL
    //@      try {
    //@         String absolutePath = javaProject.getResource().getLocation().toFile().getAbsolutePath();
    //@         alloyConfigurationCheck = new AlloyConfigurationCheck(absolutePath + File.separator  + "fm.als");
    //@      } catch (CannotReadAlloyFileException e) {
    //@         e.printStackTrace();
    //@         return;
    //@      }
    // #endif

    addPacks(classPath, extracter, alloyConfigurationCheck);

    SootManager.runPacks(extracter);
}

From source file:com.android.ide.eclipse.adt.internal.wizards.exportgradle.ExportGradleTest.java

License:Open Source License

public void testSimpleAndroidApp() throws Throwable {
    IProject project = getProject("simple-app");
    final IJavaProject javaProject = BaseProjectHelper.getJavaProject(project);

    final ProjectSetupBuilder builder = new ProjectSetupBuilder();
    builder.setProject(Collections.singletonList(javaProject));

    Job job = new Job("Validate project") {
        @Override// ww  w.jav  a 2 s.  c  o  m
        protected IStatus run(IProgressMonitor monitor) {
            try {
                BuildFileCreator.createBuildFiles(builder, null, monitor);
                File buildfile = new File(javaProject.getResource().getLocation().toString(),
                        BuildFileCreator.BUILD_FILE);
                assertTrue(buildfile.exists());
                String contents = Files.toString(buildfile, Charsets.UTF_8);
                String expectedContents = "buildscript {\n" + "    repositories {\n" + "        "
                        + BuildFileCreator.MAVEN_REPOSITORY + "\n" + "    }\n" + "    dependencies {\n"
                        + "        " + BuildFileCreator.PLUGIN_CLASSPATH + "\n" + "    }\n" + "}\n"
                        + "apply plugin: 'android'\n" + "\n" + "dependencies {\n" + "}\n" + "\n" + "android {\n"
                        + "    compileSdkVersion 16\n" + "    buildToolsVersion \"16\"\n" + "\n"
                        + "    defaultConfig {\n" + "        minSdkVersion 1\n" + "        targetSdkVersion 1\n"
                        + "    }\n" + "    sourceSets {\n" + "        main {\n"
                        + "            manifest.srcFile 'AndroidManifest.xml'\n"
                        + "            java.srcDirs = ['src']\n" + "            resources.srcDirs = ['src']\n"
                        + "            aidl.srcDirs = ['src']\n"
                        + "            renderscript.srcDirs = ['src']\n" + "            res.srcDirs = ['res']\n"
                        + "            assets.srcDirs = ['assets']\n" + "        }\n"
                        + "        instrumentTest.setRoot('tests')\n" + "    }\n" + "}";

                assertEqualsWhitespaceInsensitive(expectedContents, contents);
            } catch (Throwable t) {
                mLastThrown = t;
            }
            return null;
        }
    };
    job.schedule(1000);
    job.join();
    Object property = job.getProperty(ERROR_KEY);
    assertNull(property);
    if (mLastThrown != null) {
        throw mLastThrown;
    }
}

From source file:com.android.ide.eclipse.adt.internal.wizards.exportgradle.ExportGradleTest.java

License:Open Source License

public void testSimpleAndroidLib() throws Throwable {
    final IProject project = getProject("simple-library");
    ProjectState projectState = Sdk.getProjectState(project.getProject());
    ProjectPropertiesWorkingCopy propertiesWorkingCopy = projectState.getProperties().makeWorkingCopy();
    propertiesWorkingCopy.setProperty(PROPERTY_LIBRARY, "true");
    propertiesWorkingCopy.save();/*from  w  w  w . java  2  s  . c om*/
    IResource projectProp = project.findMember(SdkConstants.FN_PROJECT_PROPERTIES);
    if (projectProp != null) {
        projectProp.refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor());
    }

    Job job = new Job("Validate project") {
        @Override
        protected IStatus run(IProgressMonitor monitor) {
            try {
                IJavaProject javaProject = BaseProjectHelper.getJavaProject(project);

                final ProjectSetupBuilder builder = new ProjectSetupBuilder();
                builder.setProject(Collections.singletonList(javaProject));

                BuildFileCreator.createBuildFiles(builder, null, monitor);
                File buildfile = new File(javaProject.getResource().getLocation().toString(),
                        BuildFileCreator.BUILD_FILE);
                assertTrue(buildfile.exists());
                String contents = Files.toString(buildfile, Charsets.UTF_8);
                String expectedContents = "buildscript {\n" + "    repositories {\n" + "        "
                        + BuildFileCreator.MAVEN_REPOSITORY + "\n" + "    }\n" + "    dependencies {\n"
                        + "        " + BuildFileCreator.PLUGIN_CLASSPATH + "\n" + "    }\n" + "}\n"
                        + "apply plugin: 'android-library'\n" + "\n" + "dependencies {\n" + "}\n" + "\n"
                        + "android {\n" + "    compileSdkVersion 16\n" + "    buildToolsVersion \"16\"\n" + "\n"
                        + "    defaultConfig {\n" + "        minSdkVersion 1\n" + "        targetSdkVersion 1\n"
                        + "    }\n" + "    sourceSets {\n" + "        main {\n"
                        + "            manifest.srcFile 'AndroidManifest.xml'\n"
                        + "            java.srcDirs = ['src']\n" + "            resources.srcDirs = ['src']\n"
                        + "            aidl.srcDirs = ['src']\n"
                        + "            renderscript.srcDirs = ['src']\n" + "            res.srcDirs = ['res']\n"
                        + "            assets.srcDirs = ['assets']\n" + "        }\n"
                        + "        instrumentTest.setRoot('tests')\n" + "    }\n" + "}";

                assertEqualsWhitespaceInsensitive(expectedContents, contents);
            } catch (Throwable t) {
                mLastThrown = t;
            }
            return null;
        }
    };
    job.schedule(1000);
    job.join();
    Object property = job.getProperty(ERROR_KEY);
    assertNull(property);
    if (mLastThrown != null) {
        throw mLastThrown;
    }
}

From source file:com.android.ide.eclipse.adt.internal.wizards.exportgradle.ExportGradleTest.java

License:Open Source License

public void testPlainJavaProject() throws Throwable {
    IProject project = getJavaProject("simple-java");
    final IJavaProject javaProject = BaseProjectHelper.getJavaProject(project);

    final ProjectSetupBuilder builder = new ProjectSetupBuilder();
    builder.setProject(Collections.singletonList(javaProject));

    BuildFileCreator.createBuildFiles(builder, null, null);
    Job job = new Job("Validate project") {
        @Override/* ww w . j a  va 2  s.c  om*/
        protected IStatus run(IProgressMonitor monitor) {
            try {
                File buildfile = new File(javaProject.getResource().getLocation().toString(), "build.gradle");
                assertTrue(buildfile.exists());
                String contents = Files.toString(buildfile, Charsets.UTF_8);
                String expectedContents = "apply plugin: 'java'\n" + "sourceSets {\n"
                        + "    main.java.srcDirs = ['src']\n" + "}";

                assertEqualsWhitespaceInsensitive(expectedContents, contents);
            } catch (Throwable t) {
                mLastThrown = t;
            }
            return null;
        }
    };
    job.schedule(1000);
    job.join();
    Object property = job.getProperty(ERROR_KEY);
    assertNull(property);
    if (mLastThrown != null) {
        throw mLastThrown;
    }
}

From source file:com.centurylink.mdw.plugin.WizardPage.java

License:Apache License

public void determinePackageFragmentRoot(WorkflowProject workflowProject) {
    IPackageFragmentRoot oldPackageFragmentRoot = getPackageFragmentRoot();

    if (workflowProject != null && workflowProject.isLocalJavaSupported()) {
        try {//from   ww w  .jav a 2  s.c om
            IPackageFragmentRoot tempRoot = null;
            IPackageFragmentRoot srcRoot = null;
            IJavaProject javaProject = workflowProject == null ? null : workflowProject.getSourceJavaProject();
            if (javaProject != null) {
                for (IPackageFragmentRoot pfr : javaProject.getPackageFragmentRoots()) {
                    if (pfr.getKind() == IPackageFragmentRoot.K_SOURCE) {
                        if (pfr.getElementName().equals(MdwPlugin.getSettings().getTempResourceLocation())) {
                            tempRoot = pfr;
                        } else {
                            srcRoot = pfr;
                            break;
                        }
                    }
                }
                if (srcRoot == null && tempRoot == null)
                    srcRoot = javaProject.getPackageFragmentRoot(javaProject.getResource());
                setPackageFragmentRoot(srcRoot == null ? tempRoot : srcRoot, true);
            }
        } catch (JavaModelException ex) {
            PluginMessages.log(ex);
        }
    } else {
        setPackageFragmentRoot(getPackageFragmentRoot(), true);
    }
    if (oldPackageFragmentRoot == null || !oldPackageFragmentRoot.equals(getPackageFragmentRoot()))
        setPackageFragment(null, true);
}

From source file:com.centurylink.mdw.plugin.WizardPage.java

License:Apache License

/**
 * Override to prefer non-temp package root.
 *//*from   ww  w  . ja va  2 s . com*/
@SuppressWarnings("restriction")
@Override
protected void initContainerPage(IJavaElement elem) {
    IPackageFragmentRoot tempRoot = null; // only as fallback
    IPackageFragmentRoot initRoot = null;
    if (elem != null) {
        initRoot = org.eclipse.jdt.internal.corext.util.JavaModelUtil.getPackageFragmentRoot(elem);
        try {
            if (initRoot == null || initRoot.getKind() != IPackageFragmentRoot.K_SOURCE) {
                IJavaProject jproject = elem.getJavaProject();
                if (jproject != null) {
                    initRoot = null;
                    if (jproject.exists()) {
                        IPackageFragmentRoot[] roots = jproject.getPackageFragmentRoots();
                        for (int i = 0; i < roots.length; i++) {
                            if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
                                if (roots[i].getElementName()
                                        .equals(MdwPlugin.getSettings().getTempResourceLocation())) {
                                    tempRoot = roots[i];
                                } else {
                                    initRoot = roots[i];
                                    break;
                                }
                            }
                        }
                    }
                    if (initRoot == null && tempRoot == null) {
                        initRoot = jproject.getPackageFragmentRoot(jproject.getResource());
                    }
                }
            }
        } catch (JavaModelException e) {
            org.eclipse.jdt.internal.ui.JavaPlugin.log(e);
        }
    }
    setPackageFragmentRoot(initRoot == null ? tempRoot : initRoot, true);
}

From source file:com.google.gwt.eclipse.core.wizards.NewModuleWizardPage.java

License:Open Source License

private void addCommonGWTModulesAsDefaultInherits() {

    IJavaProject javaProject = getJavaProject();

    assert (JavaProjectUtilities.isJavaProjectNonNullAndExists(javaProject)
            && GWTNature.isGWTProject(javaProject.getProject()));

    // Look for com.google.gwt.user.User module, and add it as a
    // default in the inherits list if available.

    /*/*w  w  w  . j a  v a 2 s . co  m*/
     * TODO: If the project has many jars, this lookup could be slow, thus
     * causing a delay when the dialog is rendered the first time. Look into the
     * possibility of being able to ask a project's GWT Runtime about its
     * available modules, as opposed to calling ModuleUtils.findModule, which
     * iterates through all of the project's package fragment roots.
     */
    if (javaProject.getResource().isAccessible()) {
        IModule gwtUserModule = ModuleUtils.findModule(javaProject, "com.google.gwt.user.User", true);
        if (gwtUserModule != null) {
            addModuleIfNotAlreadyInList(gwtUserModule);
        }
    }
}