From 3bb42f50395e7c27c8f536a482c2af292289e78d Mon Sep 17 00:00:00 2001 From: Alois Zoitl Date: Wed, 16 Oct 2024 23:27:59 +0200 Subject: [PATCH] Moved all streams into a try with resources setup Co-authored-by: Patrick Ziegler --- .../org/eclipse/draw2d/test/TestImages.java | 36 ++++++++----------- .../gef/examples/flow/ui/FlowWizardPage1.java | 7 ++-- .../examples/logicdesigner/LogicEditor.java | 8 ++--- .../logicdesigner/LogicWizardPage1.java | 14 +++----- .../logicdesigner/model/LogicSubpart.java | 13 ++----- .../examples/shapes/ShapesCreationWizard.java | 12 +++---- .../gef/examples/shapes/ShapesEditor.java | 8 ++--- .../gef/examples/shapes/model/Shape.java | 19 ++++------ 8 files changed, 44 insertions(+), 73 deletions(-) diff --git a/org.eclipse.draw2d.tests/src/org/eclipse/draw2d/test/TestImages.java b/org.eclipse.draw2d.tests/src/org/eclipse/draw2d/test/TestImages.java index 99a284ceb..3b757d03f 100644 --- a/org.eclipse.draw2d.tests/src/org/eclipse/draw2d/test/TestImages.java +++ b/org.eclipse.draw2d.tests/src/org/eclipse/draw2d/test/TestImages.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2024 IBM Corporation and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -13,29 +13,21 @@ package org.eclipse.draw2d.test; -import java.io.InputStream; - import org.eclipse.swt.graphics.Image; -public class TestImages { - - static final Image depth_8; - static final Image depth_24; - - static { - InputStream is = TestImages.class.getResourceAsStream("icons/bits8.gif"); //$NON-NLS-1$ - depth_8 = new Image(null, is); - try { - is.close(); - } catch (Exception e) { - } - - is = TestImages.class.getResourceAsStream("icons/bits24.jpg"); //$NON-NLS-1$ - depth_24 = new Image(null, is); - try { - is.close(); - } catch (Exception e) { - } +import org.eclipse.jface.resource.ImageDescriptor; + +public final class TestImages { + + static final Image depth_8 = loadImage("icons/bits8.jpg"); //$NON-NLS-1$ + static final Image depth_24 = loadImage("icons/bits24.jpg"); //$NON-NLS-1$ + + private static Image loadImage(final String imageName) { + return ImageDescriptor.createFromFile(TestImages.class, imageName).createImage(); + } + + private TestImages() { + throw new UnsupportedOperationException("Utility class shall not be instantiated!"); //$NON-NLS-1$ } } diff --git a/org.eclipse.gef.examples.flow/src/org/eclipse/gef/examples/flow/ui/FlowWizardPage1.java b/org.eclipse.gef.examples.flow/src/org/eclipse/gef/examples/flow/ui/FlowWizardPage1.java index bceda899f..b5dfc99a8 100644 --- a/org.eclipse.gef.examples.flow/src/org/eclipse/gef/examples/flow/ui/FlowWizardPage1.java +++ b/org.eclipse.gef.examples.flow/src/org/eclipse/gef/examples/flow/ui/FlowWizardPage1.java @@ -124,13 +124,10 @@ private static ActivityDiagram createWakeupModel() { protected InputStream getInitialContents() { ActivityDiagram diag = createWakeupModel(); ByteArrayInputStream bais = null; - try { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(baos); + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos);) { oos.writeObject(diag); oos.flush(); - oos.close(); - baos.close(); bais = new ByteArrayInputStream(baos.toByteArray()); bais.close(); } catch (Exception e) { diff --git a/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/LogicEditor.java b/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/LogicEditor.java index e3dd7dade..7f1018d01 100644 --- a/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/LogicEditor.java +++ b/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/LogicEditor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2022 IBM Corporation and others. + * Copyright (c) 2000, 2024 IBM Corporation and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -486,9 +486,9 @@ protected void configureZoomManager(ScrollingGraphicalViewer viewer, ZoomManager } protected void writeToOutputStream(OutputStream os) throws IOException { - ObjectOutputStream out = new ObjectOutputStream(os); - out.writeObject(getLogicDiagram()); - out.close(); + try (ObjectOutputStream out = new ObjectOutputStream(os)) { + out.writeObject(getLogicDiagram()); + } } @Override diff --git a/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/LogicWizardPage1.java b/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/LogicWizardPage1.java index 90115b0d1..051c434c3 100644 --- a/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/LogicWizardPage1.java +++ b/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/LogicWizardPage1.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2022 IBM Corporation and others. + * Copyright (c) 2000, 2024 IBM Corporation and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -41,7 +41,7 @@ public class LogicWizardPage1 extends WizardNewFileCreationPage implements SelectionListener { - private IWorkbench workbench; + private final IWorkbench workbench; private static int exampleCount = 1; private Button model1 = null; private Button model2 = null; @@ -90,13 +90,10 @@ protected InputStream getInitialContents() { ld = (LogicDiagram) LogicDiagramFactory.createLargeModel(); } ByteArrayInputStream bais = null; - try { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(baos); + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos);) { oos.writeObject(ld); oos.flush(); - oos.close(); - baos.close(); bais = new ByteArrayInputStream(baos.toByteArray()); bais.close(); } catch (Exception e) { @@ -107,8 +104,7 @@ protected InputStream getInitialContents() { public boolean finish() { IFile newFile = createNewFile(); - if (newFile == null) - { + if (newFile == null) { return false; // ie.- creation was unsuccessful } diff --git a/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/model/LogicSubpart.java b/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/model/LogicSubpart.java index d0b39f41e..bbf0596f3 100644 --- a/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/model/LogicSubpart.java +++ b/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/model/LogicSubpart.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2022 IBM Corporation and others. + * Copyright (c) 2000, 2024 IBM Corporation and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -12,8 +12,6 @@ *******************************************************************************/ package org.eclipse.gef.examples.logicdesigner.model; -import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -21,6 +19,7 @@ import org.eclipse.swt.graphics.Image; +import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.ui.views.properties.IPropertyDescriptor; import org.eclipse.ui.views.properties.PropertyDescriptor; @@ -51,13 +50,7 @@ public abstract class LogicSubpart extends LogicElement { } protected static Image createImage(Class rsrcClass, String name) { - InputStream stream = rsrcClass.getResourceAsStream(name); - Image image = new Image(null, stream); - try { - stream.close(); - } catch (IOException ioe) { - } - return image; + return ImageDescriptor.createFromFile(rsrcClass, name).createImage(); } protected LogicSubpart() { diff --git a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesCreationWizard.java b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesCreationWizard.java index e23405f09..256bcfae1 100644 --- a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesCreationWizard.java +++ b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesCreationWizard.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2023 Elias Volanakis and others. + * Copyright (c) 2004, 2024 Elias Volanakis and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -149,13 +149,11 @@ boolean finish() { @Override protected InputStream getInitialContents() { ByteArrayInputStream bais = null; - try { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(baos); - oos.writeObject(createDefaultContent()); // argument must be - // Serializable + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos);) { + // argument must be Serializable + oos.writeObject(createDefaultContent()); oos.flush(); - oos.close(); bais = new ByteArrayInputStream(baos.toByteArray()); } catch (IOException ioe) { ioe.printStackTrace(); diff --git a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditor.java b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditor.java index 23a85442a..4979cc694 100644 --- a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditor.java +++ b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2023 Elias Volanakis and others. + * Copyright (c) 2004, 2024 Elias Volanakis and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -123,9 +123,9 @@ public void commandStackChanged(EventObject event) { } private void createOutputStream(OutputStream os) throws IOException { - ObjectOutputStream oos = new ObjectOutputStream(os); - oos.writeObject(getModel()); - oos.close(); + try (ObjectOutputStream oos = new ObjectOutputStream(os)) { + oos.writeObject(getModel()); + } } /* diff --git a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/model/Shape.java b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/model/Shape.java index fac902585..c003f96b0 100644 --- a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/model/Shape.java +++ b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/model/Shape.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2023 Elias Volanakis and others. + * Copyright (c) 2004, 2024 Elias Volanakis and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -12,13 +12,12 @@ *******************************************************************************/ package org.eclipse.gef.examples.shapes.model; -import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.eclipse.swt.graphics.Image; +import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.ui.views.properties.IPropertyDescriptor; import org.eclipse.ui.views.properties.PropertyDescriptor; import org.eclipse.ui.views.properties.TextPropertyDescriptor; @@ -90,8 +89,10 @@ public abstract class Shape extends ModelElement { * @see #setPropertyValue(Object, Object) */ static { - descriptors = new IPropertyDescriptor[] { new TextPropertyDescriptor(XPOS_PROP, ShapesExampleMessages.Shape_X), // id and description pair - new TextPropertyDescriptor(YPOS_PROP, ShapesExampleMessages.Shape_Y), new TextPropertyDescriptor(WIDTH_PROP, ShapesExampleMessages.Shape_Width), + // id and description pair + descriptors = new IPropertyDescriptor[] { new TextPropertyDescriptor(XPOS_PROP, ShapesExampleMessages.Shape_X), + new TextPropertyDescriptor(YPOS_PROP, ShapesExampleMessages.Shape_Y), + new TextPropertyDescriptor(WIDTH_PROP, ShapesExampleMessages.Shape_Width), new TextPropertyDescriptor(HEIGHT_PROP, ShapesExampleMessages.Shape_Height), }; // use a custom cell editor validator for all four array entries for (IPropertyDescriptor descriptor : descriptors) { @@ -108,13 +109,7 @@ public abstract class Shape extends ModelElement { } // static protected static Image createImage(String name) { - InputStream stream = ShapesPlugin.class.getResourceAsStream(name); - Image image = new Image(null, stream); - try { - stream.close(); - } catch (IOException ioe) { - } - return image; + return ImageDescriptor.createFromFile(ShapesPlugin.class, name).createImage(); } /** Location of this shape. */