diff --git a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/TracingOptionsManager.java b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/TracingOptionsManager.java index 0549ca98ce..5d53461899 100644 --- a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/TracingOptionsManager.java +++ b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/TracingOptionsManager.java @@ -17,15 +17,17 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.nio.charset.Charset; -import java.util.Enumeration; -import java.util.Hashtable; -import java.util.Iterator; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.HashMap; import java.util.Map; +import java.util.Objects; import java.util.Properties; import java.util.Set; import java.util.zip.ZipEntry; @@ -36,69 +38,56 @@ import org.eclipse.pde.core.plugin.PluginRegistry; public class TracingOptionsManager { - private Properties template; + private Map template; public TracingOptionsManager() { super(); } - private Properties createTemplate() { - Properties temp = new Properties(); - IPluginModelBase[] models = PluginRegistry.getAllModels(); - for (IPluginModelBase model : models) { - addToTemplate(temp, model); - } - return temp; - } - - private void addToTemplate(Properties temp, IPluginModelBase model) { - Properties modelOptions = getOptions(model); - if (modelOptions != null) { - temp.putAll(modelOptions); - } - } - - public Hashtable getTemplateTable(String pluginId) { - Properties tracingTemplate = getTracingTemplate(); - Hashtable defaults = new Hashtable<>(); - for (Enumeration keys = tracingTemplate.keys(); keys.hasMoreElements();) { - String key = keys.nextElement().toString(); + public Map getTemplateTable(String pluginId) { + Map tracingTemplate = getTracingTemplate(); + Map defaults = new HashMap<>(); + tracingTemplate.forEach((key, value) -> { if (belongsTo(key, pluginId)) { - defaults.put(key, tracingTemplate.get(key).toString()); + defaults.put(key, value); } - } + }); return defaults; } private boolean belongsTo(String option, String pluginId) { - IPath path = IPath.fromOSString(option); - String firstSegment = path.segment(0); + String firstSegment = IPath.fromOSString(option).segment(0); return pluginId.equalsIgnoreCase(firstSegment); } - public Properties getTracingOptions(Map storedOptions) { + public Map getTracingOptions(Map storedOptions) { // Start with the fresh template from plugins - Properties defaults = getTracingTemplateCopy(); + Map defaults = getTracingTemplateCopy(); if (storedOptions != null) { // Load stored values, but only for existing keys - Iterator iter = storedOptions.keySet().iterator(); - while (iter.hasNext()) { - String key = iter.next(); + storedOptions.forEach((key, value) -> { if (defaults.containsKey(key)) { - defaults.setProperty(key, storedOptions.get(key)); + defaults.put(key, value); } - } + }); } return defaults; } - public Properties getTracingTemplateCopy() { - return (Properties) getTracingTemplate().clone(); + public Map getTracingTemplateCopy() { + return new HashMap<>(getTracingTemplate()); } - private synchronized Properties getTracingTemplate() { + private synchronized Map getTracingTemplate() { if (template == null) { - template = createTemplate(); + Map temp = new HashMap<>(); + IPluginModelBase[] models = PluginRegistry.getAllModels(); + Arrays.stream(models).map(TracingOptionsManager::getOptions).filter(Objects::nonNull).forEach(p -> { + @SuppressWarnings({ "rawtypes", "unchecked" }) + Map entries = (Map) p; + temp.putAll(entries); // All entries are of String/String + }); + template = temp; } return template; } @@ -129,32 +118,30 @@ public synchronized void reset() { template = null; } - private void save(String fileName, Properties properties) { - try (FileOutputStream stream = new FileOutputStream(fileName);) { + private void saveOptions(Path file, Map entries) { + Properties properties = new Properties(); + properties.putAll(entries); + try (OutputStream stream = Files.newOutputStream(file);) { properties.store(stream, "Master Tracing Options"); //$NON-NLS-1$ - stream.flush(); } catch (IOException e) { PDECore.logException(e); } } - public void save(String filename, Map map, Set selected) { - Properties properties = getTracingOptions(map); - for (Enumeration keys = properties.keys(); keys.hasMoreElements();) { - String key = keys.nextElement().toString(); + public void save(Path file, Map map, Set selected) { + Map properties = getTracingOptions(map); + properties.keySet().removeIf(key -> { IPath path = IPath.fromOSString(key); - if (path.segmentCount() < 1 || !selected.contains(path.segment(0))) { - properties.remove(key); - } - } - save(filename, properties); + return path.segmentCount() < 1 || !selected.contains(path.segment(0)); + }); + saveOptions(file, properties); } - public void save(String filename, Map map) { - save(filename, getTracingOptions(map)); + public void save(Path file, Map map) { + saveOptions(file, getTracingOptions(map)); } - private Properties getOptions(IPluginModelBase model) { + private static Properties getOptions(IPluginModelBase model) { String location = model.getInstallLocation(); if (location == null) { return null; @@ -203,23 +190,22 @@ private Properties getOptions(IPluginModelBase model) { * support cases when: key is split in multiple lines; key use escape * characters; comment uses ! start char */ - private void loadComments(InputStream stream, Properties modelOptions) throws IOException { + private static void loadComments(InputStream stream, Properties modelOptions) throws IOException { String prevComment = ""; //$NON-NLS-1$ try (BufferedReader bufferedReader = new BufferedReader( - new InputStreamReader(stream, Charset.defaultCharset()))) { - String line; - while ((line = bufferedReader.readLine()) != null) { + // Properties.store() always uses ISO_8859_1 encoding + new InputStreamReader(stream, StandardCharsets.ISO_8859_1))) { + for (String line; (line = bufferedReader.readLine()) != null;) { if (line.startsWith("#") || line.trim().isEmpty()) { //$NON-NLS-1$ prevComment += "\n" + line.trim(); //$NON-NLS-1$ } else { - if (prevComment.trim().isEmpty()) { + if (prevComment.isBlank()) { continue; } - int eq = line.indexOf('='); if (eq >= 0) { String key = line.substring(0, eq).trim(); - modelOptions.put("#" + key, prevComment); //$NON-NLS-1$ + modelOptions.put("#" + key, prevComment.strip()); //$NON-NLS-1$ } prevComment = ""; //$NON-NLS-1$ } diff --git a/ui/org.eclipse.pde.launching/src/org/eclipse/pde/internal/launching/launcher/LaunchArgumentsHelper.java b/ui/org.eclipse.pde.launching/src/org/eclipse/pde/internal/launching/launcher/LaunchArgumentsHelper.java index d051f1fded..c40f1b4bf3 100644 --- a/ui/org.eclipse.pde.launching/src/org/eclipse/pde/internal/launching/launcher/LaunchArgumentsHelper.java +++ b/ui/org.eclipse.pde.launching/src/org/eclipse/pde/internal/launching/launcher/LaunchArgumentsHelper.java @@ -17,11 +17,13 @@ import java.io.FilenameFilter; import java.io.IOException; import java.net.URL; -import java.util.ArrayList; +import java.nio.file.Path; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; -import java.util.StringTokenizer; +import java.util.Set; +import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; @@ -240,44 +242,41 @@ public static Map getVMSpecificAttributesMap(ILaunchConfiguratio return map; } - public static String getTracingFileArgument(ILaunchConfiguration config, String optionsFileName) { + public static String getTracingFileArgument(ILaunchConfiguration config, Path optionsFile) { try { TracingOptionsManager mng = PDECore.getDefault().getTracingOptionsManager(); Map options = config.getAttribute(IPDELauncherConstants.TRACING_OPTIONS, (Map) null); String selected = config.getAttribute(IPDELauncherConstants.TRACING_CHECKED, (String) null); if (selected == null) { - mng.save(optionsFileName, options); + mng.save(optionsFile, options); } else if (!selected.equals(IPDELauncherConstants.TRACING_NONE)) { - HashSet result = new HashSet<>(); - StringTokenizer tokenizer = new StringTokenizer(selected, ","); //$NON-NLS-1$ - while (tokenizer.hasMoreTokens()) { - result.add(tokenizer.nextToken()); - } - mng.save(optionsFileName, options, result); + Set result = splitElementsByComma(selected).collect(Collectors.toSet()); + mng.save(optionsFile, options, result); } } catch (CoreException e) { return ""; //$NON-NLS-1$ } - return optionsFileName; + return optionsFile.toString(); } public static String[] constructClasspath(ILaunchConfiguration configuration) throws CoreException { double targetVersion = TargetPlatformHelper.getTargetVersion(); String jarPath = targetVersion >= 3.3 ? getEquinoxStartupPath(IPDEBuildConstants.BUNDLE_EQUINOX_LAUNCHER) : getStartupJarPath(); - if (jarPath == null && targetVersion < 3.3) + if (jarPath == null && targetVersion < 3.3) { jarPath = getEquinoxStartupPath("org.eclipse.core.launcher"); //$NON-NLS-1$ - - if (jarPath == null) + } + if (jarPath == null) { return null; + } + String bootstrap = configuration.getAttribute(IPDELauncherConstants.BOOTSTRAP_ENTRIES, ""); //$NON-NLS-1$ + Stream bootstrapElements = splitElementsByComma(getSubstitutedString(bootstrap)); + return Stream.concat(Stream.of(jarPath), bootstrapElements).toArray(String[]::new); + } - ArrayList entries = new ArrayList<>(); - entries.add(jarPath); + private static final Pattern COMMA = Pattern.compile(","); //$NON-NLS-1$ - String bootstrap = configuration.getAttribute(IPDELauncherConstants.BOOTSTRAP_ENTRIES, ""); //$NON-NLS-1$ - StringTokenizer tok = new StringTokenizer(getSubstitutedString(bootstrap), ","); //$NON-NLS-1$ - while (tok.hasMoreTokens()) - entries.add(tok.nextToken().trim()); - return entries.toArray(new String[entries.size()]); + public static Stream splitElementsByComma(String value) { + return COMMA.splitAsStream(value).map(String::trim); } /** diff --git a/ui/org.eclipse.pde.launching/src/org/eclipse/pde/launching/AbstractPDELaunchConfiguration.java b/ui/org.eclipse.pde.launching/src/org/eclipse/pde/launching/AbstractPDELaunchConfiguration.java index 5a61b1053e..09721df160 100644 --- a/ui/org.eclipse.pde.launching/src/org/eclipse/pde/launching/AbstractPDELaunchConfiguration.java +++ b/ui/org.eclipse.pde.launching/src/org/eclipse/pde/launching/AbstractPDELaunchConfiguration.java @@ -25,7 +25,6 @@ import org.eclipse.core.resources.IMarker; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; @@ -379,7 +378,7 @@ public String[] getProgramArguments(ILaunchConfiguration configuration) throws C // add tracing, if turned on if (configuration.getAttribute(IPDELauncherConstants.TRACING, false) && !IPDELauncherConstants.TRACING_NONE.equals(configuration.getAttribute(IPDELauncherConstants.TRACING_CHECKED, (String) null))) { programArgs.add("-debug"); //$NON-NLS-1$ - programArgs.add(LaunchArgumentsHelper.getTracingFileArgument(configuration, getConfigDir(configuration).toString() + IPath.SEPARATOR + ICoreConstants.OPTIONS_FILENAME)); + programArgs.add(LaunchArgumentsHelper.getTracingFileArgument(configuration, getConfigDir(configuration).toPath().resolve(ICoreConstants.OPTIONS_FILENAME))); } // add the program args specified by the user @@ -459,8 +458,9 @@ protected void preLaunchCheck(ILaunchConfiguration configuration, ILaunch launch * configuration */ protected File getConfigDir(ILaunchConfiguration configuration) { - if (fConfigDir == null) + if (fConfigDir == null) { fConfigDir = LaunchConfigurationHelper.getConfigurationArea(configuration); + } return fConfigDir; } diff --git a/ui/org.eclipse.pde.launching/src/org/eclipse/pde/launching/JUnitLaunchConfigurationDelegate.java b/ui/org.eclipse.pde.launching/src/org/eclipse/pde/launching/JUnitLaunchConfigurationDelegate.java index edf3db86bc..24b545f838 100644 --- a/ui/org.eclipse.pde.launching/src/org/eclipse/pde/launching/JUnitLaunchConfigurationDelegate.java +++ b/ui/org.eclipse.pde.launching/src/org/eclipse/pde/launching/JUnitLaunchConfigurationDelegate.java @@ -18,6 +18,7 @@ package org.eclipse.pde.launching; import java.io.File; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -246,7 +247,7 @@ protected void collectExecutionArguments(ILaunchConfiguration configuration, Lis // Create the .options file if tracing is turned on if (configuration.getAttribute(IPDELauncherConstants.TRACING, false) && !IPDELauncherConstants.TRACING_NONE.equals(configuration.getAttribute(IPDELauncherConstants.TRACING_CHECKED, (String) null))) { programArgs.add("-debug"); //$NON-NLS-1$ - String path = getConfigurationDirectory(configuration).getPath() + IPath.SEPARATOR + ICoreConstants.OPTIONS_FILENAME; + Path path = getConfigurationDirectory(configuration).toPath().resolve(ICoreConstants.OPTIONS_FILENAME); programArgs.add(LaunchArgumentsHelper.getTracingFileArgument(configuration, path)); } diff --git a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/launcher/TracingBlock.java b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/launcher/TracingBlock.java index aa40c8049a..759c45bccc 100644 --- a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/launcher/TracingBlock.java +++ b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/launcher/TracingBlock.java @@ -19,13 +19,10 @@ import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter; import java.util.ArrayList; -import java.util.Enumeration; import java.util.HashMap; -import java.util.Hashtable; +import java.util.List; import java.util.Map; -import java.util.Map.Entry; -import java.util.Properties; -import java.util.StringTokenizer; +import java.util.Objects; import org.eclipse.core.runtime.CoreException; import org.eclipse.debug.core.ILaunchConfiguration; @@ -39,6 +36,7 @@ import org.eclipse.pde.core.plugin.PluginRegistry; import org.eclipse.pde.internal.core.PDECore; import org.eclipse.pde.internal.core.TracingOptionsManager; +import org.eclipse.pde.internal.launching.launcher.LaunchArgumentsHelper; import org.eclipse.pde.internal.ui.PDEPlugin; import org.eclipse.pde.internal.ui.PDEUIMessages; import org.eclipse.pde.internal.ui.util.SWTUtil; @@ -48,8 +46,7 @@ import org.eclipse.pde.ui.launcher.TracingTab; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.SashForm; -import org.eclipse.swt.events.SelectionAdapter; -import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; @@ -65,12 +62,12 @@ public class TracingBlock { private Button fTracingCheck; private CheckboxTableViewer fPluginViewer; private IPluginModelBase[] fTraceableModels; - private final Properties fMasterOptions = new Properties(); + private final Map fMasterOptions = new HashMap<>(); private Button fSelectAllButton; private Button fDeselectAllButton; private Button fRestoreSelectedDefaultButton; private Button fRestoreDefaultButton; - private final Hashtable fPropertySources = new Hashtable<>(); + private final Map fPropertySources = new HashMap<>(); private FormToolkit fToolkit; private ScrolledPageBook fPageBook; @@ -93,12 +90,13 @@ public void createControl(Composite parent) { fTracingCheck.setText(PDEUIMessages.TracingLauncherTab_tracing); fTracingCheck.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); fTracingCheck.addSelectionListener(widgetSelectedAdapter(e -> { - masterCheckChanged(true); + masterCheckChanged(); fTab.updateLaunchConfigurationDialog(); if (fTracingCheck.getSelection()) { IStructuredSelection selection = fPluginViewer.getStructuredSelection(); if (!selection.isEmpty()) { - pluginSelected((IPluginModelBase) selection.getFirstElement(), fPluginViewer.getChecked(selection.getFirstElement())); + pluginSelected((IPluginModelBase) selection.getFirstElement(), + fPluginViewer.getChecked(selection.getFirstElement())); } } })); @@ -198,58 +196,49 @@ private void createRestoreButtonSection(Composite parent) { fRestoreSelectedDefaultButton.setText(PDEUIMessages.TracingBlock_restore_default_selected); fRestoreSelectedDefaultButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING)); SWTUtil.setButtonDimensionHint(fRestoreSelectedDefaultButton); - fRestoreSelectedDefaultButton.addSelectionListener(new SelectionAdapter() { - @Override - public void widgetSelected(SelectionEvent e) { - IStructuredSelection selec = fPluginViewer.getStructuredSelection(); - if (selec.getFirstElement() instanceof IPluginModelBase) { - IPluginModelBase model = (IPluginModelBase) selec.getFirstElement(); - String modelName = model.getBundleDescription().getSymbolicName(); - if (modelName != null) { - Properties properties = PDECore.getDefault().getTracingOptionsManager() - .getTracingTemplateCopy(); - for (String key : properties.stringPropertyNames()) { - if (key.startsWith(modelName + '/')) { - fMasterOptions.remove(key); - fMasterOptions.put(key, properties.getProperty(key)); - TracingPropertySource source = getPropertySource(model); - source.setChanged(true); - } + fRestoreSelectedDefaultButton.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> { + IStructuredSelection selec = fPluginViewer.getStructuredSelection(); + if (selec.getFirstElement() instanceof IPluginModelBase model) { + String modelName = model.getBundleDescription().getSymbolicName(); + if (modelName != null) { + Map properties = PDECore.getDefault().getTracingOptionsManager() + .getTracingTemplateCopy(); + properties.forEach((key, value) -> { + if (key.startsWith(modelName + '/')) { + fMasterOptions.put(key, value); + TracingPropertySource source = getPropertySource(model); + source.setChanged(true); } - pluginSelected(model, fPluginViewer.getChecked(model)); - } + }); + pluginSelected(model, fPluginViewer.getChecked(model)); } } - }); + })); fRestoreDefaultButton = new Button(container, SWT.PUSH); fRestoreDefaultButton.setText(PDEUIMessages.TracingBlock_restore_default); fRestoreDefaultButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING)); SWTUtil.setButtonDimensionHint(fRestoreDefaultButton); - fRestoreDefaultButton.addSelectionListener(new SelectionAdapter() { - @Override - public void widgetSelected(SelectionEvent e) { - disposePropertySources(); - fMasterOptions.clear(); - fMasterOptions.putAll(PDECore.getDefault().getTracingOptionsManager().getTracingTemplateCopy()); - Object elements[] = fPluginViewer.getCheckedElements(); - for (int i = 0; i < elements.length; i++) { - if (elements[i] instanceof IPluginModelBase) { - IPluginModelBase model = (IPluginModelBase) (elements[i]); - TracingPropertySource source = getPropertySource(model); - PageBookKey key = new PageBookKey(model, true); - Composite parent = fPageBook.createPage(key); - source.createContents(parent, true); - source.setChanged(false); - } - } - IStructuredSelection selec = fPluginViewer.getStructuredSelection(); - if (selec.getFirstElement() instanceof IPluginModelBase) { - IPluginModelBase model = (IPluginModelBase) fPluginViewer.getStructuredSelection().getFirstElement(); - pluginSelected(model, fPluginViewer.getChecked(model)); + fRestoreDefaultButton.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> { + disposePropertySources(); + fMasterOptions.clear(); + fMasterOptions.putAll(PDECore.getDefault().getTracingOptionsManager().getTracingTemplateCopy()); + Object[] elements = fPluginViewer.getCheckedElements(); + for (Object element : elements) { + if (element instanceof IPluginModelBase model) { + TracingPropertySource source = getPropertySource(model); + PageBookKey key = new PageBookKey(model, true); + Composite parentPage = fPageBook.createPage(key); + source.createContents(parentPage, true); + source.setChanged(false); } } - }); + IStructuredSelection selec = fPluginViewer.getStructuredSelection(); + if (selec.getFirstElement() instanceof IPluginModelBase) { + IPluginModelBase model = (IPluginModelBase) fPluginViewer.getStructuredSelection().getFirstElement(); + pluginSelected(model, fPluginViewer.getChecked(model)); + } + })); } protected int createPropertySheet(Composite parent) { @@ -282,28 +271,18 @@ public void initializeFrom(ILaunchConfiguration config) { try { fTracingCheck.setSelection(config.getAttribute(IPDELauncherConstants.TRACING, false)); Map options = config.getAttribute(IPDELauncherConstants.TRACING_OPTIONS, (Map) null); - if (options == null) { - fMasterOptions.putAll(PDECore.getDefault().getTracingOptionsManager().getTracingTemplateCopy()); - } else { - fMasterOptions.putAll(PDECore.getDefault().getTracingOptionsManager().getTracingOptions(options)); - } - masterCheckChanged(false); + TracingOptionsManager mgr = PDECore.getDefault().getTracingOptionsManager(); + fMasterOptions.putAll(options == null ? mgr.getTracingTemplateCopy() : mgr.getTracingOptions(options)); + masterCheckChanged(); String checked = config.getAttribute(IPDELauncherConstants.TRACING_CHECKED, (String) null); if (checked == null) { fPluginViewer.setAllChecked(true); } else if (checked.equals(IPDELauncherConstants.TRACING_NONE)) { fPluginViewer.setAllChecked(false); } else { - StringTokenizer tokenizer = new StringTokenizer(checked, ","); //$NON-NLS-1$ - ArrayList list = new ArrayList<>(); - while (tokenizer.hasMoreTokens()) { - String id = tokenizer.nextToken(); - IPluginModelBase model = PluginRegistry.findModel(id); - model = PluginRegistry.findModel(id); - if (model != null) { - list.add(model); - } - } + List list = LaunchArgumentsHelper.splitElementsByComma(checked) // + .map(PluginRegistry::findModel).filter(Objects::nonNull).toList(); + fPluginViewer.setCheckedElements(list.toArray()); IPluginModelBase model = getLastSelectedPlugin(); if (model == null && !list.isEmpty()) { @@ -328,21 +307,20 @@ public void performApply(ILaunchConfigurationWorkingCopy config) { config.setAttribute(IPDELauncherConstants.TRACING, tracingEnabled); if (tracingEnabled) { boolean changes = false; - for (Enumeration elements = fPropertySources.elements(); elements.hasMoreElements();) { - TracingPropertySource source = elements.nextElement(); + for (TracingPropertySource source : fPropertySources.values()) { if (source.isModified()) { changes = true; source.save(); } } if (changes) { - HashMap atts = new HashMap<>(fMasterOptions.size()); - for (Entry entry : fMasterOptions.entrySet()) { - String key = (String) entry.getKey(); + Map atts = new HashMap<>(fMasterOptions.size()); + fMasterOptions.forEach((key, value) -> { // these are comment keys which we don't want to save - if (!key.startsWith("#")) //$NON-NLS-1$ - atts.put(key, (String) entry.getValue()); - } + if (!key.startsWith("#")) { //$NON-NLS-1$ + atts.put(key, value); + } + }); config.setAttribute(IPDELauncherConstants.TRACING_OPTIONS, atts); } } @@ -383,10 +361,9 @@ public FormToolkit getToolkit() { } private IPluginModelBase getSelectedModel() { - if (fTracingCheck.isEnabled()) { - Object item = fPluginViewer.getStructuredSelection().getFirstElement(); - if (item instanceof IPluginModelBase) - return ((IPluginModelBase) item); + if (fTracingCheck.isEnabled() + && fPluginViewer.getStructuredSelection().getFirstElement() instanceof IPluginModelBase pluginModel) { + return pluginModel; } return null; } @@ -409,7 +386,7 @@ private void pluginSelected(IPluginModelBase model, boolean checked) { private IPluginModelBase[] getTraceableModels() { if (fTraceableModels == null) { IPluginModelBase[] models = PluginRegistry.getActiveModels(); - ArrayList result = new ArrayList<>(); + List result = new ArrayList<>(); for (IPluginModelBase model : models) { if (TracingOptionsManager.isTraceable(model)) result.add(model); @@ -456,22 +433,20 @@ private void storeSelectedModel() { private TracingPropertySource getPropertySource(IPluginModelBase model) { if (model == null) return null; - TracingPropertySource source = fPropertySources.get(model); - if (source == null) { - String id = model.getPluginBase().getId(); - Hashtable defaults = PDECore.getDefault().getTracingOptionsManager().getTemplateTable(id); - source = new TracingPropertySource(model, fMasterOptions, defaults, this); - fPropertySources.put(model, source); + return fPropertySources.computeIfAbsent(model, m -> { + String id = m.getPluginBase().getId(); + Map defaults = PDECore.getDefault().getTracingOptionsManager().getTemplateTable(id); + TracingPropertySource source = new TracingPropertySource(m, fMasterOptions, defaults, this); source.setChanged(true); - } - return source; + return source; + }); } - private void masterCheckChanged(boolean userChange) { + private void masterCheckChanged() { boolean enabled = fTracingCheck.getSelection(); fPluginViewer.getTable().setEnabled(enabled); Control currentPage = fPageBook.getCurrentPage(); - if (currentPage != null && enabled == false) { + if (currentPage != null && !enabled) { fPageBook.showEmptyPage(); } if (enabled) { @@ -479,47 +454,25 @@ private void masterCheckChanged(boolean userChange) { } int count = 0; - if(fPluginViewer!=null) + if (fPluginViewer != null) count = fPluginViewer.getTable().getItemCount(); fSelectAllButton.setEnabled(enabled && count > 0); fDeselectAllButton.setEnabled(enabled && count > 0); fRestoreDefaultButton.setEnabled(enabled && count > 0); fRestoreSelectedDefaultButton.setEnabled(!fPluginViewer.getStructuredSelection().isEmpty()); - if (enabled == false) { + if (!enabled) { fRestoreSelectedDefaultButton.setEnabled(false); } } private void disposePropertySources() { - Enumeration elements = fPropertySources.elements(); - while (elements.hasMoreElements()) { - TracingPropertySource source = elements.nextElement(); + for (TracingPropertySource source : fPropertySources.values()) { fPageBook.removePage(source.getModel()); } fPropertySources.clear(); } - private static class PageBookKey { - IPluginModelBase fModel; - boolean fEnabled; - - PageBookKey(IPluginModelBase model, boolean enabled) { - fModel = model; - fEnabled = enabled; - } - - @Override - public boolean equals(Object object) { - if (object instanceof PageBookKey) { - return fEnabled == ((PageBookKey) object).fEnabled && fModel.equals(((PageBookKey) object).fModel); - } - return false; - } - - @Override - public int hashCode() { - return fModel.hashCode() + (fEnabled ? 1 : 0); - } + private record PageBookKey(IPluginModelBase fModel, boolean fEnabled) { } } diff --git a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/launcher/TracingPropertySource.java b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/launcher/TracingPropertySource.java index 16d1589607..20a21e3f21 100644 --- a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/launcher/TracingPropertySource.java +++ b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/launcher/TracingPropertySource.java @@ -16,12 +16,9 @@ import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter; -import java.util.Arrays; -import java.util.Enumeration; -import java.util.Hashtable; -import java.util.Locale; -import java.util.Properties; -import java.util.Vector; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Pattern; import org.eclipse.core.runtime.IPath; import org.eclipse.pde.core.plugin.IPluginModelBase; @@ -36,11 +33,9 @@ public class TracingPropertySource { private final IPluginModelBase fModel; - private Vector fDescriptors; - private final Hashtable fTemplate; - private final Hashtable fValues; - private static final String[] fBooleanChoices = {"false", "true"}; //$NON-NLS-1$ //$NON-NLS-2$ - private final Properties fMasterOptions; + private final Map fTemplate; + private final Map fValues = new HashMap<>(); + private final Map fMasterOptions; private boolean fModified; // the flag fChanged is used to determine whether the model's content page @@ -90,13 +85,15 @@ protected void valueModified(Object value) { * - the control to be decorated * @param enabled */ - protected void createCommentDecorator(Control target, boolean enabled) { + protected void createCommentDecorator(Control target) { String commentText = getFormattedComment(); if (!commentText.isEmpty()) { target.setToolTipText(commentText); } } + private static final Pattern NEW_LINE = Pattern.compile("\\r?\\n"); //$NON-NLS-1$ + /** * Takes the comment lines prefixed by # and formats them. If two or * more lines sequentially start with # without empty lines in between @@ -107,21 +104,22 @@ protected void createCommentDecorator(Control target, boolean enabled) { */ protected String getFormattedComment() { String commentOrig = getComment(); - if (commentOrig == null || commentOrig.trim().isEmpty()) + if (commentOrig == null || commentOrig.isEmpty()) { return ""; //$NON-NLS-1$ - - String lines[] = commentOrig.trim().split("\\r?\\n"); //$NON-NLS-1$ + } + String[] lines = NEW_LINE.split(commentOrig); StringBuilder commentBuilder = new StringBuilder(); boolean needsSpace = false; for (String string : lines) { // remove leading hash and trim spaces around - string = string.replaceFirst("^#", "").trim(); //$NON-NLS-1$ //$NON-NLS-2$ + string = (string.startsWith("#") ? string.substring(1) : string).trim(); //$NON-NLS-1$ if (string.isEmpty()) { commentBuilder.append("\n\n"); //$NON-NLS-1$ needsSpace = false; } else { - if (needsSpace) + if (needsSpace) { commentBuilder.append(" "); //$NON-NLS-1$ + } commentBuilder.append(string); needsSpace = true; } @@ -151,23 +149,15 @@ public void create(Composite parent, boolean enabled) { td.colspan = 2; checkbox.setLayoutData(td); checkbox.setEnabled(enabled); - createCommentDecorator(checkbox, enabled); - } - - public void update() { - Integer value = (Integer) fValues.get(getKey()); - checkbox.setSelection(value.intValue() == 1); + createCommentDecorator(checkbox); } @Override public void initialize() { - update(); - checkbox.addSelectionListener(widgetSelectedAdapter(e -> { - int value = checkbox.getSelection() ? 1 : 0; - valueModified(Integer.valueOf(value)); - })); - int value = checkbox.getSelection() ? 1 : 0; - valueModified(Integer.valueOf(value)); + boolean value = (Boolean) fValues.get(getKey()); + checkbox.setSelection(value); + checkbox.addSelectionListener(widgetSelectedAdapter(e -> valueModified(checkbox.getSelection()))); + valueModified(value); } } @@ -190,81 +180,48 @@ public void create(Composite parent, boolean enabled) { //gd.widthHint = 100; text.setLayoutData(td); text.setEnabled(enabled); - createCommentDecorator(label, enabled); - } - - public void update() { - String value = (String) fValues.get(getKey()); - text.setText(value); + createCommentDecorator(label); } @Override public void initialize() { - update(); + String value = (String) fValues.get(getKey()); + text.setText(value); text.addModifyListener(e -> valueModified(text.getText())); - valueModified(text.getText()); + valueModified(value); } } - public TracingPropertySource(IPluginModelBase model, Properties masterOptions, Hashtable template, - TracingBlock block) { + public TracingPropertySource(IPluginModelBase model, Map masterOptions, + Map template, TracingBlock block) { fModel = model; fMasterOptions = masterOptions; fTemplate = template; fBlock = block; - fValues = new Hashtable<>(); } public IPluginModelBase getModel() { return fModel; } - private Object[] getSortedKeys(int size) { - Object[] keyArray = new Object[size]; - int i = 0; - for (Enumeration keys = fTemplate.keys(); keys.hasMoreElements();) { - String key = keys.nextElement(); - keyArray[i++] = key; - } - Arrays.sort(keyArray, this::compareKeys); - return keyArray; - } - - private int compareKeys(Object o1, Object o2) { - String s1 = (String) o1; - String s2 = (String) o2; - // equal - return s1.compareTo(s2); - } - public void createContents(Composite parent, boolean enabled) { - fDescriptors = new Vector<>(); TableWrapLayout layout = new TableWrapLayout(); layout.numColumns = 2; layout.rightMargin = 10; layout.leftMargin = 10; parent.setLayout(layout); boolean bordersNeeded = false; - Object[] sortedKeys = getSortedKeys(fTemplate.size()); - for (Object keyObject : sortedKeys) { - String key = (String) keyObject; - IPath path = IPath.fromOSString(key); - path = path.removeFirstSegments(1); - String shortKey = path.toString(); + Iterable sortedKeys = fTemplate.keySet().stream().sorted()::iterator; + for (String key : sortedKeys) { + String shortKey = IPath.fromOSString(key).removeFirstSegments(1).toString(); String value = fTemplate.get(key); - String lvalue = null; - String masterValue = fMasterOptions.getProperty(key); - String commentValue = fMasterOptions.getProperty("#" + key); //$NON-NLS-1$ + String masterValue = fMasterOptions.get(key); + String commentValue = fMasterOptions.get("#" + key); //$NON-NLS-1$ PropertyEditor editor; - if (value != null) - lvalue = value.toLowerCase(Locale.ENGLISH); - if (lvalue != null && (lvalue.equals("true") || lvalue.equals("false"))) { //$NON-NLS-1$ //$NON-NLS-2$ + if (value != null && (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false"))) { //$NON-NLS-1$ //$NON-NLS-2$ editor = new BooleanEditor(shortKey, shortKey, commentValue); if (masterValue != null) { - Integer mvalue = Integer.valueOf(masterValue.equals("true") //$NON-NLS-1$ - ? 1 - : 0); - fValues.put(shortKey, mvalue); + fValues.put(shortKey, Boolean.valueOf(masterValue)); } } else { editor = new TextEditor(shortKey, shortKey, commentValue); @@ -275,31 +232,21 @@ public void createContents(Composite parent, boolean enabled) { } editor.create(parent, enabled); editor.initialize(); - fDescriptors.add(editor); - if (bordersNeeded) + if (bordersNeeded) { fBlock.getToolkit().paintBordersFor(parent); + } } } - /** - */ public void save() { String pid = fModel.getPluginBase().getId(); - for (Enumeration keys = fValues.keys(); keys.hasMoreElements();) { - String shortKey = keys.nextElement(); - Object value = fValues.get(shortKey); - String svalue = value.toString(); - if (value instanceof Integer) - svalue = fBooleanChoices[((Integer) value).intValue()]; - IPath path = IPath.fromOSString(pid).append(shortKey); - fMasterOptions.setProperty(path.toString(), svalue); - } + fValues.forEach((key, value) -> { + IPath path = IPath.fromOSString(pid).append(key); + fMasterOptions.put(path.toString(), value.toString()); + }); fModified = false; } - public void dispose() { - } - public boolean isModified() { return fModified; } diff --git a/ui/org.eclipse.pde.unittest.junit/src/org/eclipse/pde/unittest/junit/launcher/JUnitPluginLaunchConfigurationDelegate.java b/ui/org.eclipse.pde.unittest.junit/src/org/eclipse/pde/unittest/junit/launcher/JUnitPluginLaunchConfigurationDelegate.java index 3d3a272f13..37f215263c 100644 --- a/ui/org.eclipse.pde.unittest.junit/src/org/eclipse/pde/unittest/junit/launcher/JUnitPluginLaunchConfigurationDelegate.java +++ b/ui/org.eclipse.pde.unittest.junit/src/org/eclipse/pde/unittest/junit/launcher/JUnitPluginLaunchConfigurationDelegate.java @@ -22,6 +22,7 @@ import java.net.URISyntaxException; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.nio.file.Path; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Arrays; @@ -540,8 +541,7 @@ protected void collectExecutionArguments(ILaunchConfiguration configuration, Lis TargetPlatformHelper.checkPluginPropertiesConsistency(fAllBundles, getConfigurationDirectory(configuration)); programArgs.add("-configuration"); //$NON-NLS-1$ - programArgs.add("file:" //$NON-NLS-1$ - + IPath.fromOSString(getConfigurationDirectory(configuration).getPath()).addTrailingSeparator().toString()); + programArgs.add("file:" + IPath.fromFile(getConfigurationDirectory(configuration)).addTrailingSeparator()); //$NON-NLS-1$ // Specify the output folder names programArgs.add("-dev"); //$NON-NLS-1$ @@ -552,8 +552,7 @@ protected void collectExecutionArguments(ILaunchConfiguration configuration, Lis if (configuration.getAttribute(IPDELauncherConstants.TRACING, false) && !IPDELauncherConstants.TRACING_NONE .equals(configuration.getAttribute(IPDELauncherConstants.TRACING_CHECKED, (String) null))) { programArgs.add("-debug"); //$NON-NLS-1$ - String path = getConfigurationDirectory(configuration).getPath() + IPath.SEPARATOR - + ICoreConstants.OPTIONS_FILENAME; + Path path = getConfigurationDirectory(configuration).toPath().resolve(ICoreConstants.OPTIONS_FILENAME); programArgs.add(LaunchArgumentsHelper.getTracingFileArgument(configuration, path)); }