Java Code Examples for org.eclipse.jdt.internal.corext.util.JavaModelUtil#setComplianceOptions()

The following examples show how to use org.eclipse.jdt.internal.corext.util.JavaModelUtil#setComplianceOptions() . 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: BuildPathSupport.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the compliance options from the given EE. If the result is not <code>null</code>,
 * it contains at least these core options:
 * <ul>
 * <li>{@link JavaCore#COMPILER_COMPLIANCE}</li>
 * <li>{@link JavaCore#COMPILER_SOURCE}</li>
 * <li>{@link JavaCore#COMPILER_CODEGEN_TARGET_PLATFORM}</li>
 * <li>{@link JavaCore#COMPILER_PB_ASSERT_IDENTIFIER}</li>
 * <li>{@link JavaCore#COMPILER_PB_ENUM_IDENTIFIER}</li>
 * <li>{@link JavaCore#COMPILER_CODEGEN_INLINE_JSR_BYTECODE} for compliance levels 1.5 and greater</li>
 * </ul>
 * 
 * @param ee the EE, can be <code>null</code>
 * @return the options, or <code>null</code> if none
 * @since 3.5
 */
public static Map<String, String> getEEOptions(IExecutionEnvironment ee) {
	if (ee == null)
		return null;
	Map<String, String> eeOptions= ee.getComplianceOptions();
	if (eeOptions == null)
		return null;
	
	Object complianceOption= eeOptions.get(JavaCore.COMPILER_COMPLIANCE);
	if (!(complianceOption instanceof String))
		return null;

	// eeOptions can miss some options, make sure they are complete:
	HashMap<String, String> options= new HashMap<String, String>();
	JavaModelUtil.setComplianceOptions(options, (String)complianceOption);
	options.putAll(eeOptions);
	return options;
}
 
Example 2
Source File: TypeMismatchQuickFixTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testMismatchingReturnTypeOnGenericMethod14() throws Exception {
	Map<String, String> options14 = new HashMap<>(fJProject1.getOptions(false));
	JavaModelUtil.setComplianceOptions(options14, JavaCore.VERSION_1_4);
	fJProject1.setOptions(options14);
	IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);

	StringBuilder buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("import java.lang.reflect.AccessibleObject;\n");
	buf.append("public class E {\n");
	buf.append("    void m() {\n");
	buf.append("        new AccessibleObject() {\n");
	buf.append("            public void getAnnotation(Class annotationClass) {\n");
	buf.append("            }\n");
	buf.append("        };\n");
	buf.append("    }\n");
	buf.append("}\n");
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("import java.lang.annotation.Annotation;\n");
	buf.append("import java.lang.reflect.AccessibleObject;\n");
	buf.append("public class E {\n");
	buf.append("    void m() {\n");
	buf.append("        new AccessibleObject() {\n");
	buf.append("            public Annotation getAnnotation(Class annotationClass) {\n");
	buf.append("            }\n");
	buf.append("        };\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e1 = new Expected("Change return type of 'getAnnotation(..)' to 'Annotation'", buf.toString());

	assertCodeActions(cu, e1);
}
 
Example 3
Source File: TypeContextChecker.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static Type parseSuperType(String superType, boolean isInterface) {
	if (! superType.trim().equals(superType)) {
		return null;
	}

	StringBuffer cuBuff= new StringBuffer();
	if (isInterface)
		cuBuff.append("class __X__ implements "); //$NON-NLS-1$
	else
		cuBuff.append("class __X__ extends "); //$NON-NLS-1$
	int offset= cuBuff.length();
	cuBuff.append(superType).append(" {}"); //$NON-NLS-1$

	ASTParser p= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
	p.setSource(cuBuff.toString().toCharArray());
	Map<String, String> options= new HashMap<String, String>();
	JavaModelUtil.setComplianceOptions(options, JavaModelUtil.VERSION_LATEST);
	p.setCompilerOptions(options);
	CompilationUnit cu= (CompilationUnit) p.createAST(null);
	ASTNode selected= NodeFinder.perform(cu, offset, superType.length());
	if (selected instanceof Name)
		selected= selected.getParent();
	if (selected.getStartPosition() != offset
			|| selected.getLength() != superType.length()
			|| ! (selected instanceof Type)
			|| selected instanceof PrimitiveType) {
		return null;
	}
	Type type= (Type) selected;

	String typeNodeRange= cuBuff.substring(type.getStartPosition(), ASTNodes.getExclusiveEnd(type));
	if (! superType.equals(typeNodeRange)){
		return null;
	}
	return type;
}
 
Example 4
Source File: PasteAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static List<ParsedCu> parseCus(IJavaProject javaProject, String compilerCompliance, String text) {
	ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
	if (javaProject != null) {
		parser.setProject(javaProject);
	} else if (compilerCompliance != null) {
		Map<String, String> options= JavaCore.getOptions();
		JavaModelUtil.setComplianceOptions(options, compilerCompliance);
		parser.setCompilerOptions(options);
	}
	parser.setSource(text.toCharArray());
	parser.setStatementsRecovery(true);
	CompilationUnit unit= (CompilationUnit) parser.createAST(null);

	if (unit.types().size() > 0)
		return parseAsTypes(text, unit);

	parser.setProject(javaProject);
	parser.setSource(text.toCharArray());
	parser.setStatementsRecovery(true);
	parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
	ASTNode root= parser.createAST(null);
	if (root instanceof TypeDeclaration) {
		List<BodyDeclaration> bodyDeclarations= ((TypeDeclaration) root).bodyDeclarations();
		if (bodyDeclarations.size() > 0)
			return Collections.singletonList(new ParsedCu(text, ASTParser.K_CLASS_BODY_DECLARATIONS, null, null));
	}

	parser.setProject(javaProject);
	parser.setSource(text.toCharArray());
	parser.setStatementsRecovery(true);
	parser.setKind(ASTParser.K_STATEMENTS);
	root= parser.createAST(null);
	if (root instanceof Block) {
		List<Statement> statements= ((Block) root).statements();
		if (statements.size() > 0)
			return Collections.singletonList(new ParsedCu(text, ASTParser.K_STATEMENTS, null, null));
	}

	return Collections.emptyList();
}
 
Example 5
Source File: JavaPreview.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public JavaPreview(Map<String, String> workingValues, Composite parent) {
		JavaTextTools tools= JavaPlugin.getDefault().getJavaTextTools();
		fPreviewDocument= new Document();
		fWorkingValues= workingValues;
		tools.setupJavaDocumentPartitioner( fPreviewDocument, IJavaPartitions.JAVA_PARTITIONING);

		PreferenceStore prioritizedSettings= new PreferenceStore();
		HashMap<String, String> complianceOptions= new HashMap<String, String>();
		JavaModelUtil.setComplianceOptions(complianceOptions, JavaModelUtil.VERSION_LATEST);
		for (Entry<String, String> complianceOption : complianceOptions.entrySet()) {
			prioritizedSettings.setValue(complianceOption.getKey(), complianceOption.getValue());
		}

		IPreferenceStore[] chain= { prioritizedSettings, JavaPlugin.getDefault().getCombinedPreferenceStore() };
		fPreferenceStore= new ChainedPreferenceStore(chain);
		fSourceViewer= new JavaSourceViewer(parent, null, null, false, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER, fPreferenceStore);
		fSourceViewer.setEditable(false);
		Cursor arrowCursor= fSourceViewer.getTextWidget().getDisplay().getSystemCursor(SWT.CURSOR_ARROW);
		fSourceViewer.getTextWidget().setCursor(arrowCursor);

		// Don't set caret to 'null' as this causes https://bugs.eclipse.org/293263
//		fSourceViewer.getTextWidget().setCaret(null);

		fViewerConfiguration= new SimpleJavaSourceViewerConfiguration(tools.getColorManager(), fPreferenceStore, null, IJavaPartitions.JAVA_PARTITIONING, true);
		fSourceViewer.configure(fViewerConfiguration);
		fSourceViewer.getTextWidget().setFont(JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT));

		fMarginPainter= new MarginPainter(fSourceViewer);
		final RGB rgb= PreferenceConverter.getColor(fPreferenceStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN_COLOR);
		fMarginPainter.setMarginRulerColor(tools.getColorManager().getColor(rgb));
		fSourceViewer.addPainter(fMarginPainter);

		new JavaSourcePreviewerUpdater();
		fSourceViewer.setDocument(fPreviewDocument);
	}
 
Example 6
Source File: ComplianceConfigurationBlock.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Sets the default compiler compliance options based on the current default JRE in the
 * workspace.
 * 
 * @since 3.5
 */
private void setDefaultCompilerComplianceValues() {
	IVMInstall defaultVMInstall= JavaRuntime.getDefaultVMInstall();
	if (defaultVMInstall instanceof IVMInstall2 && isOriginalDefaultCompliance()) {
		String complianceLevel= JavaModelUtil.getCompilerCompliance((IVMInstall2)defaultVMInstall, JavaCore.VERSION_1_4);
		Map<String, String> complianceOptions= new HashMap<String, String>();
		JavaModelUtil.setComplianceOptions(complianceOptions, complianceLevel);
		setDefaultValue(PREF_COMPLIANCE, complianceOptions.get(PREF_COMPLIANCE.getName()));
		setDefaultValue(PREF_PB_ASSERT_AS_IDENTIFIER, complianceOptions.get(PREF_PB_ASSERT_AS_IDENTIFIER.getName()));
		setDefaultValue(PREF_PB_ENUM_AS_IDENTIFIER, complianceOptions.get(PREF_PB_ENUM_AS_IDENTIFIER.getName()));
		setDefaultValue(PREF_SOURCE_COMPATIBILITY, complianceOptions.get(PREF_SOURCE_COMPATIBILITY.getName()));
		setDefaultValue(PREF_CODEGEN_TARGET_PLATFORM, complianceOptions.get(PREF_CODEGEN_TARGET_PLATFORM.getName()));
	}
}
 
Example 7
Source File: ProfileManager.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static void setLatestCompliance(Map<String, String> map) {
	JavaModelUtil.setComplianceOptions(map, JavaModelUtil.VERSION_LATEST);
}
 
Example 8
Source File: ProfileVersioner.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Updates the map to use the latest the source compliance
 * @param map The map to update
 */
public static void setLatestCompliance(Map<String, String> map) {
	JavaModelUtil.setComplianceOptions(map, JavaModelUtil.VERSION_LATEST);
}