Java Code Examples for org.eclipse.jdt.core.JavaCore#setOptions()

The following examples show how to use org.eclipse.jdt.core.JavaCore#setOptions() . 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: TypeFilterPreferencePage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean performOk() {
 		IPreferenceStore prefs= JavaPlugin.getDefault().getPreferenceStore();

 		List<String> checked= fFilterListField.getCheckedElements();
 		List<String> unchecked= fFilterListField.getElements();
 		unchecked.removeAll(checked);

 		prefs.setValue(PREF_FILTER_ENABLED, packOrderList(checked));
 		prefs.setValue(PREF_FILTER_DISABLED, packOrderList(unchecked));
	JavaPlugin.flushInstanceScope();

	Hashtable<String, String> coreOptions= JavaCore.getOptions();
	String hideForbidden= fHideForbiddenField.isSelected() ? JavaCore.ENABLED : JavaCore.DISABLED;
	coreOptions.put(JavaCore.CODEASSIST_FORBIDDEN_REFERENCE_CHECK, hideForbidden);
	String hideDiscouraged= fHideDiscouragedField.isSelected() ? JavaCore.ENABLED : JavaCore.DISABLED;
	coreOptions.put(JavaCore.CODEASSIST_DISCOURAGED_REFERENCE_CHECK, hideDiscouraged);
	JavaCore.setOptions(coreOptions);

       return true;
   }
 
Example 2
Source File: TestingEnvironment.java    From dacapobench with Apache License 2.0 5 votes vote down vote up
private synchronized IProject createProject(String projectName) {
  final IProject project = this.workspace.getRoot().getProject(projectName);

  Object cmpl = JavaCore.getOption(CompilerOptions.OPTION_Compliance);
  Object src  = JavaCore.getOption(CompilerOptions.OPTION_Source);
  Object tgt  = JavaCore.getOption(CompilerOptions.OPTION_TargetPlatform);

  try {
    IWorkspaceRunnable create = new IWorkspaceRunnable() {
      public void run(IProgressMonitor monitor) throws CoreException {
        project.create(null, null);
        project.open(null);
      }
    };
    this.workspace.run(create, null);
    this.projects.put(projectName, project);
    addBuilderSpecs(projectName);
  } catch (CoreException e) {
    handle(e);
  } finally {
    // restore workspace settings
    Hashtable options = JavaCore.getOptions();
    options.put(CompilerOptions.OPTION_Compliance,cmpl);
    options.put(CompilerOptions.OPTION_Source,src);
    options.put(CompilerOptions.OPTION_TargetPlatform,tgt);
    JavaCore.setOptions(options);
  }
  return project;
}
 
Example 3
Source File: JVMConfigurator.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void defaultVMInstallChanged(IVMInstall previous, IVMInstall current) {
	if (Objects.equals(previous, current)) {
		return;
	}
	String prev = (previous == null) ? null : previous.getId() + "-" + previous.getInstallLocation();
	String curr = (current == null) ? null : current.getId() + "-" + current.getInstallLocation();

	JavaLanguageServerPlugin.logInfo("Default VM Install changed from  " + prev + " to " + curr);

	//Reset global compliance settings
	AbstractVMInstall jvm = (AbstractVMInstall) current;
	long jdkLevel = CompilerOptions.versionToJdkLevel(jvm.getJavaVersion());
	String compliance = CompilerOptions.versionFromJdkLevel(jdkLevel);
	Hashtable<String, String> options = JavaCore.getOptions();
	JavaCore.setComplianceOptions(compliance, options);
	JavaCore.setOptions(options);

	IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
	for (IProject project : projects) {
		if (!ProjectUtils.isVisibleProject(project) && ProjectUtils.isJavaProject(project)) {
			IJavaProject javaProject = JavaCore.create(project);
			configureJVMSettings(javaProject, current);
		}
		ProjectsManager projectsManager = JavaLanguageServerPlugin.getProjectsManager();
		if (projectsManager != null) {
			//TODO Only trigger update if the project uses the default JVM
			JavaLanguageServerPlugin.logInfo("defaultVMInstallChanged -> force update of " + project.getName());
			projectsManager.updateProject(project, true);
		}
	}
}
 
Example 4
Source File: FormatterManager.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static void configureFormatter(PreferenceManager preferenceManager, ProjectsManager projectsManager) {
	String formatterUrl = preferenceManager.getPreferences().getFormatterUrl();
	Map<String, String> options = null;
	if (formatterUrl != null) {
		URL url = projectsManager.getUrl(formatterUrl);
		if (url != null) {
			try (InputStream inputStream = url.openStream()) {
				InputSource inputSource = new InputSource(inputStream);
				String profileName = preferenceManager.getPreferences().getFormatterProfileName();
				options = FormatterManager.readSettingsFromStream(inputSource, profileName);
			} catch (Exception e) {
				JavaLanguageServerPlugin.logException(e.getMessage(), e);
			}
		} else {
			JavaLanguageServerPlugin.logInfo("Invalid formatter:" + formatterUrl);
		}
	}
	if (options != null && !options.isEmpty()) {
		setFormattingOptions(options);
	} else {
		Map<String, String> defaultOptions = DefaultCodeFormatterOptions.getEclipseDefaultSettings().getMap();
		Hashtable<String, String> javaOptions = JavaCore.getOptions();
		defaultOptions.forEach((k, v) -> {
			javaOptions.put(k, v);
		});
		JavaCore.setOptions(javaOptions);
		JavaLanguageServerPlugin.getPreferencesManager().initialize();
	}
}
 
Example 5
Source File: FormatterManager.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static void setFormattingOptions(Map<String, String> options) {
	Map<String, String> defaultOptions = DefaultCodeFormatterOptions.getEclipseDefaultSettings().getMap();
	defaultOptions.putAll(options);
	Hashtable<String, String> javaOptions = JavaCore.getOptions();
	defaultOptions.entrySet().stream().filter(p -> p.getKey().startsWith(FORMATTER_OPTION_PREFIX)).forEach(p -> {
		javaOptions.put(p.getKey(), p.getValue());
	});
	JavaCore.setOptions(javaOptions);
}
 
Example 6
Source File: TestVMType.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static void setTestJREAsDefault(String vmId) throws CoreException {
	IVMInstallType vmInstallType = JavaRuntime.getVMInstallType(VMTYPE_ID);
	IVMInstall testVMInstall = vmInstallType.findVMInstall(vmId);
	if (!testVMInstall.equals(JavaRuntime.getDefaultVMInstall())) {
		// set the 1.8 test JRE as the new default JRE
		JavaRuntime.setDefaultVMInstall(testVMInstall, new NullProgressMonitor());
		Hashtable<String, String> options = JavaCore.getOptions();
		JavaCore.setComplianceOptions(vmId, options);
		JavaCore.setOptions(options);
	}
	JDTUtils.setCompatibleVMs(VMTYPE_ID);
}
 
Example 7
Source File: JavaCompletionProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Tells this processor to restrict its proposal to those element
 * visible in the actual invocation context.
 *
 * @param restrict <code>true</code> if proposals should be restricted
 */
public void restrictProposalsToVisibility(boolean restrict) {
	Hashtable<String, String> options= JavaCore.getOptions();
	Object value= options.get(VISIBILITY);
	if (value instanceof String) {
		String newValue= restrict ? ENABLED : DISABLED;
		if ( !newValue.equals(value)) {
			options.put(VISIBILITY, newValue);
			JavaCore.setOptions(options);
		}
	}
}
 
Example 8
Source File: ProjectClasspathFactory.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
protected void updateCompilerJavaCompliance(String javaVersion) {
    Hashtable<String, String> options = JavaCore.getOptions();
    options.put(CompilerOptions.OPTION_TargetPlatform, javaVersion);
    options.put(CompilerOptions.OPTION_Source, javaVersion);
    options.put(CompilerOptions.OPTION_Compliance, javaVersion);
    JavaCore.setOptions(options);
}
 
Example 9
Source File: FullSourceWorkspaceBuildTests.java    From dacapobench with Apache License 2.0 4 votes vote down vote up
/**
 * Start a build on given project or workspace using given options.
 * 
 * @param javaProject Project which must be (full) build or null if all
 * workspace has to be built.
 * @param options Options used while building
 */
void build(final IJavaProject javaProject, Hashtable options, boolean noWarning) throws IOException, CoreException {
  if (DEBUG)
    System.out.print("\tstart build...");
  JavaCore.setOptions(options);
  if (PRINT)
    System.out.println("JavaCore options: " + options);

  // Build workspace if no project
  if (javaProject == null) {
    // single measure
    ENV.fullBuild();
  } else {
    if (PRINT)
      System.out.println("Project options: " + javaProject.getOptions(false));
    IWorkspaceRunnable compilation = new IWorkspaceRunnable() {
      public void run(IProgressMonitor monitor) throws CoreException {
        ENV.fullBuild(javaProject.getPath());
      }
    };
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    workspace.run(compilation, null/* don't take any lock */, IWorkspace.AVOID_UPDATE, null/*
                                                                                            * no
                                                                                            * progress
                                                                                            * available
                                                                                            * here
                                                                                            */);
  }

  // Verify markers
  IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
  IMarker[] markers = workspaceRoot.findMarkers(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, true, IResource.DEPTH_INFINITE);
  List resources = new ArrayList();
  List messages = new ArrayList();
  int warnings = 0;
  for (int i = 0, length = markers.length; i < length; i++) {
    IMarker marker = markers[i];
    switch (((Integer) marker.getAttribute(IMarker.SEVERITY)).intValue()) {
    case IMarker.SEVERITY_ERROR:
      resources.add(marker.getResource().getName());
      messages.add(marker.getAttribute(IMarker.MESSAGE));
      break;
    case IMarker.SEVERITY_WARNING:
      warnings++;
      if (noWarning) {
        resources.add(marker.getResource().getName());
        messages.add(marker.getAttribute(IMarker.MESSAGE));
      }
      break;
    }
  }
  workspaceRoot.deleteMarkers(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, true, IResource.DEPTH_INFINITE);

  // Assert result
  int size = messages.size();
  if (size > 0) {
    StringBuffer debugBuffer = new StringBuffer();
    for (int i = 0; i < size; i++) {
      debugBuffer.append(resources.get(i));
      debugBuffer.append(":\n\t");
      debugBuffer.append(messages.get(i));
      debugBuffer.append('\n');
    }
    System.out.println("Unexpected ERROR marker(s):\n" + debugBuffer.toString());
    System.out.println("--------------------");
    String target = javaProject == null ? "workspace" : javaProject.getElementName();
    // assertEquals("Found "+size+" unexpected errors while building "+target,
    // 0, size);
  }
  if (DEBUG)
    System.out.println("done");
}