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

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

Introduction

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

Prototype

String getOption(String optionName, boolean inheritJavaCoreOptions);

Source Link

Document

Helper method for returning one option value only.

Usage

From source file:JavaSourceViewerConfiguration.java

License:Open Source License

@Override
public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
    IJavaProject project = getProject();
    final int tabWidth = CodeFormatterUtil.getTabWidth(project);
    final int indentWidth = CodeFormatterUtil.getIndentWidth(project);
    boolean allowTabs = tabWidth <= indentWidth;

    String indentMode;/*  www .j  a v a2s  .co  m*/
    if (project == null)
        indentMode = JavaCore.getOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR);
    else
        indentMode = project.getOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, true);

    boolean useSpaces = JavaCore.SPACE.equals(indentMode)
            || DefaultCodeFormatterConstants.MIXED.equals(indentMode);

    Assert.isLegal(allowTabs || useSpaces);

    if (!allowTabs) {
        char[] spaces = new char[indentWidth];
        Arrays.fill(spaces, ' ');
        return new String[] { new String(spaces), "" }; //$NON-NLS-1$
    } else if (!useSpaces)
        return getIndentPrefixesForTab(tabWidth);
    else
        return getIndentPrefixesForSpaces(tabWidth);
}

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   ww  w .  ja  v  a  2s  .c o 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:ch.acanda.eclipse.pmd.java.resolution.PMDMarkerResolutionGenerator.java

License:Open Source License

private Version getCompilerCompliance(final IMarker marker) {
    final IJavaProject project = JavaCore.create(marker.getResource().getProject());
    final String compilerCompliance = project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
    return new Version(compilerCompliance);
}

From source file:com.android.ide.eclipse.adt.internal.project.ProjectHelper.java

License:Open Source License

/**
 * Checks the project compiler compliance level is supported.
 * @param javaProject The project to check
 * @return A pair with the first integer being an error code, and the second value
 *   being the invalid value found or null. The error code can be: <ul>
 * <li><code>COMPILER_COMPLIANCE_OK</code> if the project is properly configured</li>
 * <li><code>COMPILER_COMPLIANCE_LEVEL</code> for unsupported compiler level</li>
 * <li><code>COMPILER_COMPLIANCE_SOURCE</code> for unsupported source compatibility</li>
 * <li><code>COMPILER_COMPLIANCE_CODEGEN_TARGET</code> for unsupported .class format</li>
 * </ul>//from   w  w w  .  j  a va2  s  .c  o  m
 */
public static final Pair<Integer, String> checkCompilerCompliance(IJavaProject javaProject) {
    // get the project compliance level option
    String compliance = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);

    // check it against a list of valid compliance level strings.
    if (!checkCompliance(javaProject, compliance)) {
        // if we didn't find the proper compliance level, we return an error
        return Pair.of(COMPILER_COMPLIANCE_LEVEL, compliance);
    }

    // otherwise we check source compatibility
    String source = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);

    // check it against a list of valid compliance level strings.
    if (!checkCompliance(javaProject, source)) {
        // if we didn't find the proper compliance level, we return an error
        return Pair.of(COMPILER_COMPLIANCE_SOURCE, source);
    }

    // otherwise check codegen level
    String codeGen = javaProject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true);

    // check it against a list of valid compliance level strings.
    if (!checkCompliance(javaProject, codeGen)) {
        // if we didn't find the proper compliance level, we return an error
        return Pair.of(COMPILER_COMPLIANCE_CODEGEN_TARGET, codeGen);
    }

    return Pair.of(COMPILER_COMPLIANCE_OK, null);
}

From source file:com.android.ide.eclipse.adt.internal.project.ProjectHelper.java

License:Open Source License

/**
 * Makes the given project use JDK 6 (or more specifically,
 * {@link AdtConstants#COMPILER_COMPLIANCE_PREFERRED} as the compilation
 * target, regardless of what the default IDE JDK level is, provided a JRE
 * of the given level is installed.//  w w  w.ja va 2 s.c  o m
 *
 * @param javaProject the Java project
 * @throws CoreException if the IDE throws an exception setting the compiler
 *             level
 */
@SuppressWarnings("restriction") // JDT API for setting compliance options
public static void enforcePreferredCompilerCompliance(@NonNull IJavaProject javaProject) throws CoreException {
    String compliance = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
    if (compliance == null || JavaModelUtil.isVersionLessThan(compliance, COMPILER_COMPLIANCE_PREFERRED)) {
        IVMInstallType[] types = JavaRuntime.getVMInstallTypes();
        for (int i = 0; i < types.length; i++) {
            IVMInstallType type = types[i];
            IVMInstall[] installs = type.getVMInstalls();
            for (int j = 0; j < installs.length; j++) {
                IVMInstall install = installs[j];
                if (install instanceof IVMInstall2) {
                    IVMInstall2 install2 = (IVMInstall2) install;
                    // Java version can be 1.6.0, and preferred is 1.6
                    if (install2.getJavaVersion().startsWith(COMPILER_COMPLIANCE_PREFERRED)) {
                        Map<String, String> options = javaProject.getOptions(false);
                        JavaCore.setComplianceOptions(COMPILER_COMPLIANCE_PREFERRED, options);
                        JavaModelUtil.setDefaultClassfileOptions(options, COMPILER_COMPLIANCE_PREFERRED);
                        javaProject.setOptions(options);
                        return;
                    }
                }
            }
        }
    }
}

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

License:Open Source License

/**
 * Outputs the beginning of an Android task in the build file.
 */// w w  w  . j a v  a2 s  .c  om
private void startAndroidTask(ProjectState projectState) {
    int buildApi = projectState.getTarget().getVersion().getApiLevel();
    String toolsVersion = projectState.getTarget().getBuildToolInfo().getRevision().toString();
    mBuildFile.append("android {\n"); //$NON-NLS-1$
    mBuildFile.append("    compileSdkVersion " + buildApi + "\n"); //$NON-NLS-1$
    mBuildFile.append("    buildToolsVersion \"" + toolsVersion + "\"\n"); //$NON-NLS-1$
    mBuildFile.append("\n"); //$NON-NLS-1$

    try {
        IJavaProject javaProject = BaseProjectHelper.getJavaProject(projectState.getProject());
        // otherwise we check source compatibility
        String source = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
        if (JavaCore.VERSION_1_7.equals(source)) {
            mBuildFile.append("    compileOptions {\n" + //$NON-NLS-1$
                    "        sourceCompatibility JavaVersion.VERSION_1_7\n" + //$NON-NLS-1$
                    "        targetCompatibility JavaVersion.VERSION_1_7\n" + //$NON-NLS-1$
                    "    }\n" + //$NON-NLS-1$
                    "\n"); //$NON-NLS-1$
        }
    } catch (CoreException e) {
        // Ignore compliance level, go with default
    }
}

From source file:com.android.ide.eclipse.adt.project.ProjectHelper.java

License:Open Source License

/**
 * Checks the project compiler compliance level is supported.
 * @param javaProject The project to check
 * @return <ul>//  w ww.  j a  v a 2s .  c om
 * <li><code>COMPILER_COMPLIANCE_OK</code> if the project is properly configured</li>
 * <li><code>COMPILER_COMPLIANCE_LEVEL</code> for unsupported compiler level</li>
 * <li><code>COMPILER_COMPLIANCE_SOURCE</code> for unsupported source compatibility</li>
 * <li><code>COMPILER_COMPLIANCE_CODEGEN_TARGET</code> for unsupported .class format</li>
 * </ul>
 */
public static final int checkCompilerCompliance(IJavaProject javaProject) {
    // get the project compliance level option
    String compliance = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);

    // check it against a list of valid compliance level strings.
    if (checkCompliance(compliance) == false) {
        // if we didn't find the proper compliance level, we return an error
        return COMPILER_COMPLIANCE_LEVEL;
    }

    // otherwise we check source compatibility
    String source = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);

    // check it against a list of valid compliance level strings.
    if (checkCompliance(source) == false) {
        // if we didn't find the proper compliance level, we return an error
        return COMPILER_COMPLIANCE_SOURCE;
    }

    // otherwise check codegen level
    String codeGen = javaProject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true);

    // check it against a list of valid compliance level strings.
    if (checkCompliance(codeGen) == false) {
        // if we didn't find the proper compliance level, we return an error
        return COMPILER_COMPLIANCE_CODEGEN_TARGET;
    }

    return COMPILER_COMPLIANCE_OK;
}

From source file:com.android.ide.eclipse.auidt.internal.project.ProjectHelper.java

License:Open Source License

/**
 * Checks the project compiler compliance level is supported.
 * @param javaProject The project to check
 * @return A pair with the first integer being an error code, and the second value
 *   being the invalid value found or null. The error code can be: <ul>
 * <li><code>COMPILER_COMPLIANCE_OK</code> if the project is properly configured</li>
 * <li><code>COMPILER_COMPLIANCE_LEVEL</code> for unsupported compiler level</li>
 * <li><code>COMPILER_COMPLIANCE_SOURCE</code> for unsupported source compatibility</li>
 * <li><code>COMPILER_COMPLIANCE_CODEGEN_TARGET</code> for unsupported .class format</li>
 * </ul>/*  w w  w.j  a  v  a 2  s.  c om*/
 */
public static final Pair<Integer, String> checkCompilerCompliance(IJavaProject javaProject) {
    // get the project compliance level option
    String compliance = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);

    // check it against a list of valid compliance level strings.
    if (checkCompliance(compliance) == false) {
        // if we didn't find the proper compliance level, we return an error
        return Pair.of(COMPILER_COMPLIANCE_LEVEL, compliance);
    }

    // otherwise we check source compatibility
    String source = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);

    // check it against a list of valid compliance level strings.
    if (checkCompliance(source) == false) {
        // if we didn't find the proper compliance level, we return an error
        return Pair.of(COMPILER_COMPLIANCE_SOURCE, source);
    }

    // otherwise check codegen level
    String codeGen = javaProject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true);

    // check it against a list of valid compliance level strings.
    if (checkCompliance(codeGen) == false) {
        // if we didn't find the proper compliance level, we return an error
        return Pair.of(COMPILER_COMPLIANCE_CODEGEN_TARGET, codeGen);
    }

    return Pair.of(COMPILER_COMPLIANCE_OK, null);
}

From source file:com.android.ide.eclipse.mock.Mocks.java

License:Open Source License

public static IJavaProject createProject(IClasspathEntry[] entries, IPath outputLocation) throws Exception {
    IJavaProject javaProject = createMock(IJavaProject.class);
    final Capture<IClasspathEntry[]> capturedEntries = new Capture<IClasspathEntry[]>();
    Capture<IPath> capturedOutput = new Capture<IPath>();
    capturedEntries.setValue(entries);//from   www . j a  v  a  2  s  .c o m
    capturedOutput.setValue(outputLocation);

    IProject project = createProject();
    expect(javaProject.getProject()).andReturn(project).anyTimes();
    expect(javaProject.getOutputLocation()).andReturn(capturedOutput.getValue()).anyTimes();

    expect(javaProject.getRawClasspath()).andAnswer(new IAnswer<IClasspathEntry[]>() {
        @Override
        public IClasspathEntry[] answer() throws Throwable {
            return capturedEntries.getValue();
        }
    }).anyTimes();

    javaProject.setRawClasspath(capture(capturedEntries), isA(IProgressMonitor.class));
    expectLastCall().anyTimes();

    javaProject.setRawClasspath(capture(capturedEntries), capture(capturedOutput), isA(IProgressMonitor.class));
    expectLastCall().anyTimes();

    final Capture<String> capturedCompliance = new Capture<String>();
    capturedCompliance.setValue("1.4");
    final Capture<String> capturedSource = new Capture<String>();
    capturedSource.setValue("1.4");
    final Capture<String> capturedTarget = new Capture<String>();
    capturedTarget.setValue("1.4");

    expect(javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true)).andAnswer(new IAnswer<String>() {
        @Override
        public String answer() throws Throwable {
            return capturedCompliance.getValue();
        }
    });
    expect(javaProject.getOption(JavaCore.COMPILER_SOURCE, true)).andAnswer(new IAnswer<String>() {
        @Override
        public String answer() throws Throwable {
            return capturedSource.getValue();
        }
    });
    expect(javaProject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true))
            .andAnswer(new IAnswer<String>() {
                @Override
                public String answer() throws Throwable {
                    return capturedTarget.getValue();
                }
            });

    javaProject.setOption(eq(JavaCore.COMPILER_COMPLIANCE), capture(capturedCompliance));
    expectLastCall().anyTimes();
    javaProject.setOption(eq(JavaCore.COMPILER_SOURCE), capture(capturedSource));
    expectLastCall().anyTimes();
    javaProject.setOption(eq(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM), capture(capturedTarget));
    expectLastCall().anyTimes();

    replay(javaProject);

    return javaProject;
}

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

License:Open Source License

protected IStatus validateCompilationUnit(File resource) {
    IPackageFragmentRoot root = getPackageFragmentRoot();
    // root never null as validation is not done for working copies
    try {// ww w .ja va2  s  . com
        if (root.getKind() != IPackageFragmentRoot.K_SOURCE)
            return new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, root);
    } catch (JavaModelException e) {
        return e.getJavaModelStatus();
    }
    if (resource != null) {
        char[][] inclusionPatterns = ((PackageFragmentRoot) root).fullInclusionPatternChars();
        char[][] exclusionPatterns = ((PackageFragmentRoot) root).fullExclusionPatternChars();
        if (Util.isExcluded(new Path(resource.getPath()), inclusionPatterns, exclusionPatterns, false))
            return new JavaModelStatus(IJavaModelStatusConstants.ELEMENT_NOT_ON_CLASSPATH, this);
        if (!resource.exists())
            return new JavaModelStatus(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, this);
    }
    IJavaProject project = getJavaProject();
    return JavaConventions.validateCompilationUnitName(getElementName(),
            project.getOption(JavaCore.COMPILER_SOURCE, true),
            project.getOption(JavaCore.COMPILER_COMPLIANCE, true));
}