Java Code Examples for org.eclipse.jdt.core.IJavaProject#getOptions()

The following examples show how to use org.eclipse.jdt.core.IJavaProject#getOptions() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RefactoringASTParser.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the compiler options used for creating the refactoring AST.
 * <p>
 * Turns all errors and warnings into ignore and disables task tags. The customizable set of
 * compiler options only contains additional Eclipse options. The standard JDK compiler options
 * can't be changed anyway.
 * 
 * @param element an element (not the Java model)
 * @return compiler options
 */
public static Map<String, String> getCompilerOptions(IJavaElement element) {
	IJavaProject project= element.getJavaProject();
	Map<String, String> options= project.getOptions(true);
	for (Iterator<String> iter= options.keySet().iterator(); iter.hasNext();) {
		String key= iter.next();
		String value= options.get(key);
		if (JavaCore.ERROR.equals(value) || JavaCore.WARNING.equals(value)) {
			// System.out.println("Ignoring - " + key);
			options.put(key, JavaCore.IGNORE);
		}
	}
	options.put(JavaCore.COMPILER_PB_MAX_PER_UNIT, "0"); //$NON-NLS-1$
	options.put(JavaCore.COMPILER_TASK_TAGS, ""); //$NON-NLS-1$
	return options;
}
 
Example 2
Source File: XbaseHoverDocumentationProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public Javadoc getJavaDoc() {
	if (context == null || context.eResource() == null || context.eResource().getResourceSet() == null)
		return null;
	Object classpathURIContext = ((XtextResourceSet) context.eResource().getResourceSet()).getClasspathURIContext();
	if (classpathURIContext instanceof IJavaProject) {
		IJavaProject javaProject = (IJavaProject) classpathURIContext;
		@SuppressWarnings("all")
		ASTParser parser = ASTParser.newParser(AST.JLS3);
		parser.setProject(javaProject);
		Map<String, String> options = javaProject.getOptions(true);
		options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED); // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=212207
		parser.setCompilerOptions(options);
		String source = rawJavaDoc + "class C{}"; //$NON-NLS-1$
		parser.setSource(source.toCharArray());
		CompilationUnit root = (CompilationUnit) parser.createAST(null);
		if (root == null)
			return null;
		List<AbstractTypeDeclaration> types = root.types();
		if (types.size() != 1)
			return null;
		AbstractTypeDeclaration type = types.get(0);
		return type.getJavadoc();
	}
	return null;
}
 
Example 3
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private IBinding resolveBindings(IType jdtType, IJavaProject javaProject) {
	ThreadLocal<Boolean> abortOnMissingSource = JavaModelManager.getJavaModelManager().abortOnMissingSource;
	Boolean wasAbortOnMissingSource = abortOnMissingSource.get();
	try {
		abortOnMissingSource.set(Boolean.TRUE);
		resolveBinding.start();

		parser.setWorkingCopyOwner(workingCopyOwner);
		parser.setIgnoreMethodBodies(true);
		
		parser.setProject(javaProject);
		
		Map<String, String> options = javaProject.getOptions(true);
		
		options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.DISABLED);
		parser.setCompilerOptions(options);

		IBinding[] bindings = parser.createBindings(new IJavaElement[] { jdtType }, null);
		resolveBinding.stop();
		return bindings[0];
	} finally {
		abortOnMissingSource.set(wasAbortOnMissingSource);
	}
}
 
Example 4
Source File: JavadocContentAccess2.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static CompilationUnit createAST(IJavaElement element, String cuSource) {
	Assert.isNotNull(element);
	ASTParser parser = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL);

	IJavaProject javaProject = element.getJavaProject();
	parser.setProject(javaProject);
	Map<String, String> options = javaProject.getOptions(true);
	options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED); // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=212207
	parser.setCompilerOptions(options);

	parser.setSource(cuSource.toCharArray());
	return (CompilationUnit) parser.createAST(null);
}
 
Example 5
Source File: JavadocContentAccess2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static CompilationUnit createAST(IJavaElement element, String cuSource) {
	Assert.isNotNull(element);
	ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
	
	IJavaProject javaProject= element.getJavaProject();
	parser.setProject(javaProject);
	Map<String, String> options= javaProject.getOptions(true);
	options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED); // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=212207
	parser.setCompilerOptions(options);
	
	parser.setSource(cuSource.toCharArray());
	return (CompilationUnit) parser.createAST(null);
}
 
Example 6
Source File: JsniFormattingUtil.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Same as format(IDocument, Map, String[]), except the formatting options
 * are taken from the given project.
 * 
 */
public static TextEdit format(IDocument document, IJavaProject project,
    String[] originalJsniMethods) {
  @SuppressWarnings("unchecked")
  // safe by IJavaScriptProject.getOptions spec
  Map<String, String> jsOptions = JavaScriptCore.create(project.getProject()).getOptions(true);
  @SuppressWarnings("unchecked")
  // safe by IJavaScriptProject.getOptions spec
  Map<String, String> jOptions = project.getOptions(true);
  return format(document, jOptions, jsOptions, originalJsniMethods);
}
 
Example 7
Source File: PerformanceTestProjectSetup.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public static void makeJava8Compliant(IJavaProject javaProject) {
	Map<String, String> options= javaProject.getOptions(false);
	options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_8);
	options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8);
	options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8);
	options.put(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.ERROR);
	options.put(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.ERROR);
	options.put(JavaCore.COMPILER_CODEGEN_INLINE_JSR_BYTECODE, JavaCore.ENABLED);
	options.put(JavaCore.COMPILER_LOCAL_VARIABLE_ATTR, JavaCore.GENERATE);
	options.put(JavaCore.COMPILER_LINE_NUMBER_ATTR, JavaCore.GENERATE);
	options.put(JavaCore.COMPILER_SOURCE_FILE_ATTR, JavaCore.GENERATE);
	options.put(JavaCore.COMPILER_CODEGEN_UNUSED_LOCAL, JavaCore.PRESERVE);
	javaProject.setOptions(options);
}
 
Example 8
Source File: EclipseASTParserFactory.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public void tweakOptions(final ASTParser parser, final IJavaProject project) {
  if ((project != null)) {
    final Map<String, String> options = project.getOptions(true);
    options.remove(JavaCore.COMPILER_TASK_TAGS);
    options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED);
    parser.setCompilerOptions(options);
  }
}
 
Example 9
Source File: WorkbenchTestHelper.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public static void makeCompliantFor(IJavaProject javaProject, JavaVersion javaVersion) {
	Map<String, String> options= javaProject.getOptions(false);
	String jreLevel = javaVersion.getQualifier();
	options.put(JavaCore.COMPILER_COMPLIANCE, jreLevel);
	options.put(JavaCore.COMPILER_SOURCE, jreLevel);
	options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, jreLevel);
	options.put(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.ERROR);
	options.put(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.ERROR);
	options.put(JavaCore.COMPILER_CODEGEN_INLINE_JSR_BYTECODE, JavaCore.ENABLED);
	options.put(JavaCore.COMPILER_LOCAL_VARIABLE_ATTR, JavaCore.GENERATE);
	options.put(JavaCore.COMPILER_LINE_NUMBER_ATTR, JavaCore.GENERATE);
	options.put(JavaCore.COMPILER_SOURCE_FILE_ATTR, JavaCore.GENERATE);
	options.put(JavaCore.COMPILER_CODEGEN_UNUSED_LOCAL, JavaCore.PRESERVE);
	javaProject.setOptions(options);
}
 
Example 10
Source File: ProjectUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static Map<String, String> getJavaOptions(IProject project) {
	if (!isJavaProject(project)) {
		return null;
	}
	IJavaProject javaProject = JavaCore.create(project);
	return javaProject.getOptions(true);
}
 
Example 11
Source File: ProjectSettingsChecker.java    From java-debug with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Check whether the project settings are all matched as the expected.
 * @param params - The checker parameters
 * @return true if all settings is matched
 */
public static boolean check(ProjectSettingsCheckerParams params) {
    Map<String, String> options = new HashMap<>();
    IJavaProject javaProject = null;
    if (StringUtils.isNotBlank(params.projectName)) {
        javaProject = JdtUtils.getJavaProject(params.projectName);
    } else if (StringUtils.isNotBlank(params.className)) {
        try {
            List<IJavaProject> projects = ResolveClasspathsHandler.getJavaProjectFromType(params.className);
            if (!projects.isEmpty()) {
                javaProject = projects.get(0);
            }
        } catch (CoreException e) {
            // do nothing
        }
    }

    if (javaProject != null) {
        options = javaProject.getOptions(params.inheritedOptions);
    }

    for (Entry<String, String> expected : params.expectedOptions.entrySet()) {
        if (!Objects.equals(options.get(expected.getKey()), expected.getValue())) {
            return false;
        }
    }

    return true;
}
 
Example 12
Source File: JavadocContentAccess.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static CompilationUnit createAST(IJavaElement element, String cuSource) {
	ASTParser parser = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL);

	IJavaProject javaProject= element.getJavaProject();
	parser.setProject(javaProject);
	Map<String, String> options= javaProject.getOptions(true);
	options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED); // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=212207
	parser.setCompilerOptions(options);

	parser.setSource(cuSource.toCharArray());
	return (CompilationUnit) parser.createAST(null);
}
 
Example 13
Source File: AnonymousTypeCompletionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public String updateReplacementString(IDocument document, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException {
	// Construct empty body for performance concern
	// See https://github.com/microsoft/language-server-protocol/issues/1032#issuecomment-648748013
	String newBody = fSnippetSupport ? "{\n\t${0}\n}" : "{\n\n}";

	StringBuilder buf = new StringBuilder("new A()"); //$NON-NLS-1$
	buf.append(newBody);
	// use the code formatter
	String lineDelim = TextUtilities.getDefaultLineDelimiter(document);
	final IJavaProject project = fCompilationUnit.getJavaProject();
	IRegion lineInfo = document.getLineInformationOfOffset(fReplacementOffset);
	Map<String, String> options = project != null ? project.getOptions(true) : JavaCore.getOptions();
	String replacementString = CodeFormatterUtil.format(CodeFormatter.K_EXPRESSION, buf.toString(), 0, lineDelim, options);
	int lineEndOffset = lineInfo.getOffset() + lineInfo.getLength();
	int p = offset;
	if (p < document.getLength()) {
		char ch = document.getChar(p);
		while (p < lineEndOffset) {
			if (ch == '(' || ch == ')' || ch == ';' || ch == ',') {
				break;
			}
			ch = document.getChar(++p);
		}
		if (ch != ';' && ch != ',' && ch != ')') {
			replacementString = replacementString + ';';
		}
	}
	int beginIndex = replacementString.indexOf('(');
	replacementString = replacementString.substring(beginIndex);
	return replacementString;
}
 
Example 14
Source File: WorkbenchTestHelper.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Make the given project compliant for the given version.
 *
 * @param javaProject the project.
 * @param javaVersion the Java version.
 */
public static void makeCompliantFor(IJavaProject javaProject, JavaVersion javaVersion) {
	Map<String, String> options = javaProject.getOptions(false);
	String jreLevel;
	switch (javaVersion) {
	case JAVA8:
		jreLevel = JavaCore.VERSION_1_8;
		break;
	case JAVA6:
		jreLevel = JavaCore.VERSION_1_6;
		break;
	case JAVA5:
		jreLevel = JavaCore.VERSION_1_5;
		break;
	case JAVA7:
	default:
		jreLevel = JavaCore.VERSION_1_7;
	}
	options.put(JavaCore.COMPILER_COMPLIANCE, jreLevel);
	options.put(JavaCore.COMPILER_SOURCE, jreLevel);
	options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, jreLevel);
	options.put(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.ERROR);
	options.put(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.ERROR);
	options.put(JavaCore.COMPILER_CODEGEN_INLINE_JSR_BYTECODE, JavaCore.ENABLED);
	options.put(JavaCore.COMPILER_LOCAL_VARIABLE_ATTR, JavaCore.GENERATE);
	options.put(JavaCore.COMPILER_LINE_NUMBER_ATTR, JavaCore.GENERATE);
	options.put(JavaCore.COMPILER_SOURCE_FILE_ATTR, JavaCore.GENERATE);
	options.put(JavaCore.COMPILER_CODEGEN_UNUSED_LOCAL, JavaCore.PRESERVE);
	javaProject.setOptions(options);
}
 
Example 15
Source File: JavaProjectSetupUtil.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public static void makeJava5Compliant(IJavaProject javaProject) {
	Map<String, String> options= javaProject.getOptions(false);
	options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5);
	options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5);
	options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_5);
	options.put(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.ERROR);
	options.put(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.ERROR);
	options.put(JavaCore.COMPILER_CODEGEN_INLINE_JSR_BYTECODE, JavaCore.ENABLED);
	options.put(JavaCore.COMPILER_LOCAL_VARIABLE_ATTR, JavaCore.GENERATE);
	options.put(JavaCore.COMPILER_LINE_NUMBER_ATTR, JavaCore.GENERATE);
	options.put(JavaCore.COMPILER_SOURCE_FILE_ATTR, JavaCore.GENERATE);
	options.put(JavaCore.COMPILER_CODEGEN_UNUSED_LOCAL, JavaCore.PRESERVE);
	javaProject.setOptions(options);
}
 
Example 16
Source File: JavaStructureDiffViewer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private Map<String, String> getCompilerOptions(ITypedElement input) {
	IJavaElement element= findJavaElement(input);
	if (element != null) {
		IJavaProject javaProject= element.getJavaProject();
		if (javaProject != null)
			return javaProject.getOptions(true);
	}
	return null;
}
 
Example 17
Source File: JavaReplaceWithEditionActionImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void performReplace(IMember input, IFile file,
		ITextFileBuffer textFileBuffer, IDocument document, ITypedElement ti)
		throws CoreException, JavaModelException,
		InvocationTargetException, InterruptedException {

	if (ti instanceof IStreamContentAccessor) {

		boolean inEditor= beingEdited(file);

		String content= JavaCompareUtilities.readString((IStreamContentAccessor)ti);
		String newContent= trimTextBlock(content, TextUtilities.getDefaultLineDelimiter(document), input.getJavaProject());
		if (newContent == null) {
			showError();
			return;
		}

		ICompilationUnit compilationUnit= input.getCompilationUnit();
		CompilationUnit root= parsePartialCompilationUnit(compilationUnit);


		ISourceRange nameRange= input.getNameRange();
		if (nameRange == null)
			nameRange= input.getSourceRange();
		// workaround for bug in getNameRange(): for AnnotationMembers length is negative
		int length= nameRange.getLength();
		if (length < 0)
			length= 1;
		ASTNode node2= NodeFinder.perform(root, new SourceRange(nameRange.getOffset(), length));
		ASTNode node;
		if (node2.getNodeType() == ASTNode.INITIALIZER)
			node= node2;
		else
			node= ASTNodes.getParent(node2, BodyDeclaration.class);
		if (node == null)
			node= ASTNodes.getParent(node2, AnnotationTypeDeclaration.class);
		if (node == null)
			node= ASTNodes.getParent(node2, EnumDeclaration.class);

		//ASTNode node= getBodyContainer(root, input);
		if (node == null) {
			showError();
			return;
		}

		ASTRewrite rewriter= ASTRewrite.create(root.getAST());
		rewriter.replace(node, rewriter.createStringPlaceholder(newContent, node.getNodeType()), null);

		if (inEditor) {
			JavaEditor je= getEditor(file);
			if (je != null)
				je.setFocus();
		}

		Map<String, String> options= null;
		IJavaProject javaProject= compilationUnit.getJavaProject();
		if (javaProject != null)
			options= javaProject.getOptions(true);
		applyChanges(rewriter, document, textFileBuffer, getShell(), inEditor, options);

	}
}
 
Example 18
Source File: ASTParser.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Sets the Java project used when resolving bindings.
 * 
 * <p>This method automatically sets the compiler
 * options based on the given project:</p>
 * <pre>
 * setCompilerOptions(project.getOptions(true));
 * </pre>
 * <p>See {@link #setCompilerOptions(Map)} for a discussion of
 * the pros and cons of using these options vs specifying
 * compiler options explicitly.</p>
 * <p>This setting is used in conjunction with {@link #setSource(char[])}.
 * For the purposes of resolving bindings, types declared in the
 * source string will hide types by the same name available
 * through the classpath of the given project.</p>
 * <p>Defaults to none (<code>null</code>).</p>
 *
 * @param project the Java project used to resolve names, or
 *    <code>null</code> if none
 */
public void setProject(IJavaProject project) {
	this.project = project;
	if (project != null) {
		Map options = project.getOptions(true);
		options.remove(JavaCore.COMPILER_TASK_TAGS); // no need to parse task tags
		this.compilerOptions = options;
	}
}
 
Example 19
Source File: CodeFormatterUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Old API. Consider to use format2 (TextEdit)
 *
 * @param kind
 *        Use to specify the kind of the code snippet to format.
 *        It can be any of the kind constants defined in {@link CodeFormatter}
 * @param source
 *        The source to format
 * @param indentationLevel
 *        The initial indentation level, used to shift left/right the entire source fragment.
 *        An initial indentation level of zero or below has no effect.
 * @param lineSeparator
 *        The line separator to use in formatted source,
 *        if set to <code>null</code>, then the platform default one will be used.
 * @param project
 *        The project from which to retrieve the formatter options from
 *        If set to <code>null</code>, then use the current settings from {@link JavaCore#getOptions()}.
 * @return the formatted source string
 */
public static String format(int kind, String source, int indentationLevel, String lineSeparator, IJavaProject project) {
	Map<String, String> options= project != null ? project.getOptions(true) : null;
	return format(kind, source, indentationLevel, lineSeparator, options);
}
 
Example 20
Source File: CodeFormatterUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Creates a string that represents the given number of indentation units.
 * The returned string can contain tabs and/or spaces depending on the core formatter preferences.
 *
 * @param indentationUnits
 *        the number of indentation units to generate
 * @param project
 *        the project from which to get the formatter settings,
 *        <code>null</code> if the workspace default should be used
 * @return the indent string
 */
public static String createIndentString(int indentationUnits, IJavaProject project) {
	Map<String, String> options= project != null ? project.getOptions(true) : JavaCore.getOptions();
	return ToolFactory.createCodeFormatter(options).createIndentationString(indentationUnits);
}