Skip to content

Commit e53c17a

Browse files
committed
Add missing indices to message format in ModelAssembler
The fallback logging mechanism in ModelAssembler uses: java.text.MessageFormat.format(String, Object...) Supplied message formats bind arguments without an index, i.e. message formats use "{}" instead of "{n}". This causes a NFE in MessageFormat.format(), resulting in no log output in the fallback case. In the usual case with an available logger, the indices are added by: com.sun.org.slf4j.internal.Logger.addIndex(String) This change likewise adds indices to the message format, for the fallback case. Fixes: #3873
1 parent f1a031a commit e53c17a

File tree

1 file changed

+21
-1
lines changed
  • bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/internal/workbench

1 file changed

+21
-1
lines changed

bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/internal/workbench/ModelAssembler.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import java.util.TreeSet;
4040
import java.util.concurrent.CopyOnWriteArrayList;
4141
import java.util.concurrent.atomic.AtomicReference;
42+
import java.util.regex.Matcher;
43+
import java.util.regex.Pattern;
4244
import org.eclipse.core.runtime.IConfigurationElement;
4345
import org.eclipse.core.runtime.IExtension;
4446
import org.eclipse.core.runtime.IExtensionPoint;
@@ -209,6 +211,8 @@ public void removedBundle(Bundle bundle, BundleEvent event, List<FragmentWrapper
209211

210212
private final List<ServiceReference<IModelProcessorContribution>> processorContributions = new CopyOnWriteArrayList<>();
211213

214+
private static final Pattern MESSAGE_FORMAT_BRACKETS_PATTERN = Pattern.compile("\\{\\}"); //$NON-NLS-1$
215+
212216
private BundleContext bundleContext;
213217

214218
private BundleTracker<List<FragmentWrapperElementMapping>> tracker;
@@ -820,7 +824,7 @@ private void log(LogMethod logMethod, PrintStream stream, String message, Object
820824
logMethod.invoke(log, message, args);
821825
} else {
822826
// fallback if no LogService is available
823-
stream.println(MessageFormat.format(message, args));
827+
stream.println(MessageFormat.format(addIndex(message), args));
824828
}
825829
}
826830

@@ -845,4 +849,20 @@ public void unsetLogger(LoggerFactory loggerFactory) {
845849
this.logger = null;
846850
}
847851
}
852+
853+
/**
854+
* Adds missing indices to {@code {}} sequences in {@code format}. See:
855+
* {@link com.sun.org.slf4j.internal.Logger#addIndex(String)}
856+
*/
857+
private static String addIndex(String format) {
858+
Matcher matcher = MESSAGE_FORMAT_BRACKETS_PATTERN.matcher(format);
859+
StringBuilder sb = new StringBuilder();
860+
int index = 0;
861+
while (matcher.find()) {
862+
String r = "{" + Integer.toString(index++) + "}"; //$NON-NLS-1$ //$NON-NLS-2$
863+
matcher.appendReplacement(sb, r);
864+
}
865+
matcher.appendTail(sb);
866+
return sb.toString();
867+
}
848868
}

0 commit comments

Comments
 (0)