|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Red Hat, Inc. and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + *******************************************************************************/ |
| 11 | +package org.eclipse.jdt.core.javac.configurator; |
| 12 | + |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Objects; |
| 15 | + |
| 16 | +import org.eclipse.core.runtime.preferences.IEclipsePreferences; |
| 17 | +import org.eclipse.core.runtime.preferences.InstanceScope; |
| 18 | +import org.osgi.framework.BundleActivator; |
| 19 | +import org.osgi.framework.BundleContext; |
| 20 | + |
| 21 | +/// Activates the bundle, configure system properties according to preferences |
| 22 | +/// and set up preference listener. |
| 23 | +/// |
| 24 | +/// ⚠️ This bundle must *NOT* depend on JDT Core, as it has to start before. |
| 25 | +/// It also deals with startLevels and autostart (in p2.inf), keep it minimal |
| 26 | +public class JavacConfigurationActivator implements BundleActivator { |
| 27 | + |
| 28 | + /// Value can be {@link #ENABLED} or {@link #DISABLED} as we want to distinguish |
| 29 | + /// unset vs false and boolean preferences don't allow that |
| 30 | + /// TODO consider drill down to individual properties? |
| 31 | + public static final String PREF_JAVAC_ENABLED = "javacEnabled"; |
| 32 | + public static String ENABLED = "enabled"; |
| 33 | + public static String DISABLED = "disabled"; |
| 34 | + |
| 35 | + /// Those are the value of system properties to fully enable Javac backend |
| 36 | + public static Map<String, String> SYSTEM_PROPERTIES = Map.of( |
| 37 | + "ICompilationUnitResolver", "org.eclipse.jdt.core.dom.JavacCompilationUnitResolver", |
| 38 | + "AbstractImageBuilder.compilerFactory", "org.eclipse.jdt.internal.javac.JavacCompilerFactory", |
| 39 | + "CompilationUnit.DOM_BASED_OPERATIONS", "true", |
| 40 | + "ICompletionEngineProvider", "org.eclipse.jdt.core.dom.DOMCompletionEngineProvider", |
| 41 | + "SourceIndexer.DOM_BASED_INDEXER", "true", |
| 42 | + "MatchLocator.DOM_BASED_MATCH", "true", |
| 43 | + "IJavaSearchDelegate", "org.eclipse.jdt.internal.core.search.DOMJavaSearchDelegate"); |
| 44 | + |
| 45 | + private static BundleContext context; |
| 46 | + private static JavacConfigurationActivator instance; |
| 47 | + private IEclipsePreferences instancePreferences; |
| 48 | + |
| 49 | + public JavacConfigurationActivator() { |
| 50 | + instance = this; |
| 51 | + } |
| 52 | + public static JavacConfigurationActivator getInstance() { |
| 53 | + return instance; |
| 54 | + } |
| 55 | + |
| 56 | + static BundleContext getContext() { |
| 57 | + return context; |
| 58 | + } |
| 59 | + |
| 60 | + public void start(BundleContext bundleContext) throws Exception { |
| 61 | + JavacConfigurationActivator.context = bundleContext; |
| 62 | + instancePreferences = InstanceScope.INSTANCE.getNode(symbolicName()); |
| 63 | + // ensure the initial value is available from preference store |
| 64 | + instancePreferences.put(PREF_JAVAC_ENABLED, isJavacEnabled() ? ENABLED : DISABLED); |
| 65 | + instancePreferences.flush(); |
| 66 | + instancePreferences.addPreferenceChangeListener(event -> { |
| 67 | + refreshSystemProperties(); |
| 68 | + }); |
| 69 | + refreshSystemProperties(); |
| 70 | + } |
| 71 | + public static String symbolicName() { |
| 72 | + return JavacConfigurationActivator.context.getBundle().getSymbolicName(); |
| 73 | + } |
| 74 | + |
| 75 | + private void refreshSystemProperties() { |
| 76 | + // Those need to be in sync with system properties usually |
| 77 | + // added to eclipse.ini thanks to p2.inf instructions |
| 78 | + if (isJavacEnabled()) { |
| 79 | + SYSTEM_PROPERTIES.entrySet().forEach(e -> System.setProperty(e.getKey(), e.getValue())); |
| 80 | + } else { |
| 81 | + SYSTEM_PROPERTIES.keySet().forEach(System::clearProperty); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public boolean isJavacEnabled() { |
| 86 | + boolean enabled = System.getProperty("ICompilationUnitResolver", "").toLowerCase().contains("javac"); |
| 87 | + String pref = instancePreferences.get(PREF_JAVAC_ENABLED, null); |
| 88 | + if (pref != null) { |
| 89 | + enabled = Objects.equals(pref, ENABLED); |
| 90 | + } |
| 91 | + return enabled; |
| 92 | + } |
| 93 | + |
| 94 | + public void stop(BundleContext bundleContext) throws Exception { |
| 95 | + JavacConfigurationActivator.context = null; |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments