Skip to content

Commit ee3e05c

Browse files
committed
Provide static acessors in the interface as an alternative to Platform
Currently one obtains ILog either by injection/di or a static call to Platform.getLog(...)., but is is more semantic to only use ILog interface and also decreases the coupling of the code to a central singleton class.
1 parent 39d33ea commit ee3e05c

File tree

5 files changed

+54
-8
lines changed

5 files changed

+54
-8
lines changed

runtime/bundles/org.eclipse.core.runtime/META-INF/MANIFEST.MF

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %pluginName
4-
Bundle-Version: 3.27.100.qualifier
4+
Bundle-Version: 3.28.0.qualifier
55
Bundle-SymbolicName: org.eclipse.core.runtime; singleton:=true
66
Bundle-Vendor: %providerName
77
Bundle-Activator: org.eclipse.core.internal.runtime.PlatformActivator

runtime/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/runtime/InternalPlatform.java

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public final class InternalPlatform {
5454

5555
private static final String[] ARCH_LIST = { Platform.ARCH_AARCH64, Platform.ARCH_X86, Platform.ARCH_X86_64 };
5656

57+
public static final StackWalker STACK_WALKER = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
58+
5759
// debug support: set in loadOptions()
5860
public static boolean DEBUG = false;
5961
public static boolean DEBUG_PLUGIN_PREFERENCES = false;

runtime/bundles/org.eclipse.core.runtime/src/org/eclipse/core/runtime/ILog.java

+43
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
*******************************************************************************/
1515
package org.eclipse.core.runtime;
1616

17+
import org.eclipse.core.internal.runtime.InternalPlatform;
1718
import org.osgi.framework.Bundle;
19+
import org.osgi.framework.FrameworkUtil;
1820

1921
/**
2022
* A log to which status events can be written. Logs appear on individual
@@ -132,4 +134,45 @@ default void error(String message) {
132134
default void error(String message, Throwable throwable) {
133135
log(new Status(IStatus.ERROR, getBundle().getSymbolicName(), message, throwable));
134136
}
137+
138+
/**
139+
* Returns the log for the given bundle. If no such log exists, one is created.
140+
*
141+
* @param bundle the bundle whose log is returned
142+
* @return the log for the given bundle
143+
* @since 3.28
144+
*/
145+
public static ILog of(Bundle bundle) {
146+
return InternalPlatform.getDefault().getLog(bundle);
147+
}
148+
149+
/**
150+
* Returns the log for the bundle of the given class. If no such log exists, one
151+
* is created.
152+
*
153+
* @param clazz the class in a bundle whose log is returned
154+
* @return the log for the bundle to which the clazz belongs
155+
*
156+
* @since 3.28
157+
*/
158+
public static ILog of(Class<?> clazz) {
159+
Bundle bundle = FrameworkUtil.getBundle(clazz);
160+
return InternalPlatform.getDefault().getLog(bundle);
161+
}
162+
163+
/**
164+
* Returns the log for the bundle of the calling class. If no such log exists,
165+
* one is created.
166+
*
167+
* @return the log for the bundle to which the caller belongs
168+
*
169+
* @since 3.28
170+
*/
171+
public static ILog get() {
172+
try {
173+
return of(InternalPlatform.STACK_WALKER.getCallerClass());
174+
} catch (IllegalCallerException e) {
175+
return of(ILog.class);
176+
}
177+
}
135178
}

runtime/bundles/org.eclipse.core.runtime/src/org/eclipse/core/runtime/Platform.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ public static long getStateStamp() {
922922
* @since 3.0
923923
*/
924924
public static ILog getLog(Bundle bundle) {
925-
return InternalPlatform.getDefault().getLog(bundle);
925+
return ILog.of(bundle);
926926
}
927927

928928
/**
@@ -935,8 +935,7 @@ public static ILog getLog(Bundle bundle) {
935935
* @since 3.16
936936
*/
937937
public static ILog getLog(Class<?> clazz) {
938-
Bundle bundle = FrameworkUtil.getBundle(clazz);
939-
return InternalPlatform.getDefault().getLog(bundle);
938+
return ILog.of(clazz);
940939
}
941940

942941
/**

runtime/bundles/org.eclipse.core.runtime/src/org/eclipse/core/runtime/Plugin.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,15 @@ public final URL find(IPath path, Map<String,String> override) {
230230

231231

232232
/**
233-
* Returns the log for this plug-in. If no such log exists, one is created.
233+
* Returns the log for this plug-in. If no such log exists, one is created.
234+
* <b>Hint: </b> instead of caling this method, consider using
235+
* {@link ILog#of(Class)} instead that is independent from implementing a
236+
* {@link Plugin}
234237
*
235238
* @return the log for this plug-in
236-
* XXX change this into a LogMgr service that would keep track of the map. See if it can be a service factory.
237239
*/
238240
public final ILog getLog() {
239-
return InternalPlatform.getDefault().getLog(getBundle());
241+
return ILog.of(getBundle());
240242
}
241243

242244
/**
@@ -354,7 +356,7 @@ public final void savePluginPreferences() {
354356
// need to save them because someone else might have
355357
// made changes via the OSGi APIs.
356358
getPluginPreferences();
357-
359+
358360
// Performance: isolate PreferenceForwarder and BackingStoreException into
359361
// an inner class to avoid class loading (and then activation of the Preferences plugin)
360362
// as the Plugin class is loaded.

0 commit comments

Comments
 (0)