From ed97b499fd084a1e1ceb6dd7e4e926164c9e129f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Dinis=20Ferreira?= Date: Sat, 7 Mar 2026 17:00:06 +0100 Subject: [PATCH 1/2] refactor: convert manual try/finally to try-with-resources Convert straightforward cases and suppress with NOPMD where try-with-resources cannot be used (conditional close logic). Co-Authored-By: Claude Opus 4.6 --- .../check/core/test/AbstractCheckTestCase.java | 17 ++--------------- .../ui/builder/CheckExtensionGenerator.java | 18 +----------------- .../editor/model/XtextGMFDocumentProvider.java | 2 +- .../templates/ConfigurableTemplateStore.java | 5 +---- .../xtext/resource/XtextGMFResourceUtil.java | 2 +- 5 files changed, 6 insertions(+), 38 deletions(-) diff --git a/com.avaloq.tools.ddk.check.core.test/src/com/avaloq/tools/ddk/check/core/test/AbstractCheckTestCase.java b/com.avaloq.tools.ddk.check.core.test/src/com/avaloq/tools/ddk/check/core/test/AbstractCheckTestCase.java index 21ac794f36..5f61ec19aa 100644 --- a/com.avaloq.tools.ddk.check.core.test/src/com/avaloq/tools/ddk/check/core/test/AbstractCheckTestCase.java +++ b/com.avaloq.tools.ddk.check.core.test/src/com/avaloq/tools/ddk/check/core/test/AbstractCheckTestCase.java @@ -183,8 +183,7 @@ protected String getFileName(final String name) { * the exception */ public String getContents(final IFile file) throws Exception { // NOPMD - InputStream inputStream = file.getContents(); - try { + try (InputStream inputStream = file.getContents()) { byte[] buffer = new byte[TWO_KILO_BYTES]; int bytesRead; StringBuffer b = new StringBuffer(); @@ -195,8 +194,6 @@ public String getContents(final IFile file) throws Exception { // NOPMD } } while (bytesRead != -1); return b.toString(); - } finally { - inputStream.close(); } } @@ -251,18 +248,8 @@ public ResourceSet getResourceSet() { public EObject getModel(final String fileName) throws Exception { // NOPMD IFile file = getFile(fileName); Resource resource = get(XtextResourceSet.class).createResource(uri(file)); - InputStream s = null; - try { - s = file.getContents(); + try (InputStream s = file.getContents()) { resource.load(s, null); - } finally { - if (s != null) { - try { - s.close(); - } catch (IOException e) { - LOGGER.info("Failed to close test file " + fileName); - } - } } assertEquals(0, resource.getErrors().size(), resource.getErrors().toString()); return resource.getContents().get(0); diff --git a/com.avaloq.tools.ddk.check.ui/src/com/avaloq/tools/ddk/check/ui/builder/CheckExtensionGenerator.java b/com.avaloq.tools.ddk.check.ui/src/com/avaloq/tools/ddk/check/ui/builder/CheckExtensionGenerator.java index 88f6f5e13d..6d5f97d823 100644 --- a/com.avaloq.tools.ddk.check.ui/src/com/avaloq/tools/ddk/check/ui/builder/CheckExtensionGenerator.java +++ b/com.avaloq.tools.ddk.check.ui/src/com/avaloq/tools/ddk/check/ui/builder/CheckExtensionGenerator.java @@ -22,8 +22,6 @@ import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParserFactory; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; @@ -91,8 +89,6 @@ @SuppressWarnings({"restriction", "nls"}) class CheckExtensionGenerator { // CHECKSTYLE:ON - private static final Logger LOGGER = LogManager.getLogger(CheckExtensionGenerator.class); - static final String PREFERENCE_PLUGIN_XML_FILENAME = "PluginXmlFilename"; static final String STANDARD_PLUGIN_FILENAME = ICoreConstants.PLUGIN_FILENAME_DESCRIPTOR; static final String STANDARD_FRAGMENT_FILENAME = ICoreConstants.FRAGMENT_FILENAME_DESCRIPTOR; @@ -504,30 +500,18 @@ private void mergeManifest(final CheckCatalog catalog, final IProgressMonitor mo final IFile file = PDEProject.getManifest(project); if (file.exists() && catalog.getGrammar() != null) { - InputStream fileContents = null; - try { - fileContents = file.getContents(); + try (InputStream fileContents = file.getContents()) { MergeableManifest2 manifest = new MergeableManifest2(fileContents, project.getName()); - fileContents.close(); manifest.addRequiredBundles(new GrammarHelper(catalog.getGrammar()).getRequiredBundleSymbolicNames()); if (manifest.isModified()) { ByteArrayOutputStream os = new ByteArrayOutputStream(); manifest.write(os); - os.close(); file.setContents(new ByteArrayInputStream(os.toByteArray()), false, false, monitor); } } catch (IOException e) { throw new WrappedException(e); } catch (CoreException e) { throw new WrappedException(e); - } finally { - if (fileContents != null) { - try { - fileContents.close(); - } catch (IOException e) { - LOGGER.warn("Could not close the Manifest file after modifying it.", e); - } - } } } diff --git a/com.avaloq.tools.ddk.xtext.ui/src/com/avaloq/tools/ddk/xtext/ui/editor/model/XtextGMFDocumentProvider.java b/com.avaloq.tools.ddk.xtext.ui/src/com/avaloq/tools/ddk/xtext/ui/editor/model/XtextGMFDocumentProvider.java index b89eb72d6a..65b69f0158 100644 --- a/com.avaloq.tools.ddk.xtext.ui/src/com/avaloq/tools/ddk/xtext/ui/editor/model/XtextGMFDocumentProvider.java +++ b/com.avaloq.tools.ddk.xtext.ui/src/com/avaloq/tools/ddk/xtext/ui/editor/model/XtextGMFDocumentProvider.java @@ -83,7 +83,7 @@ protected boolean setDocumentContent(final IDocument document, final IEditorInpu protected void setDocumentContentQuietly(final XtextGMFDocument document, final InputStream contentStream, final String encoding) throws CoreException { Reader in = null; - try { + try { // NOPMD UseTryWithResources - conditional close: closes in (if created) or contentStream (caller's) String actualEncoding = encoding; if (actualEncoding == null) { actualEncoding = getDefaultEncoding(); diff --git a/com.avaloq.tools.ddk.xtext.ui/src/com/avaloq/tools/ddk/xtext/ui/editor/templates/ConfigurableTemplateStore.java b/com.avaloq.tools.ddk.xtext.ui/src/com/avaloq/tools/ddk/xtext/ui/editor/templates/ConfigurableTemplateStore.java index 0b09be3047..80ae7fac7e 100644 --- a/com.avaloq.tools.ddk.xtext.ui/src/com/avaloq/tools/ddk/xtext/ui/editor/templates/ConfigurableTemplateStore.java +++ b/com.avaloq.tools.ddk.xtext.ui/src/com/avaloq/tools/ddk/xtext/ui/editor/templates/ConfigurableTemplateStore.java @@ -90,8 +90,7 @@ private void addTemplatesFromFile(final URL templates) { if (templates != null) { TemplateReaderWriter reader = new TemplateReaderWriter(); try { - InputStream openStream = templates.openStream(); - try { + try (InputStream openStream = templates.openStream()) { TemplatePersistenceData[] datas = reader.read(openStream, null); int templateCounter = 0; for (TemplatePersistenceData data : datas) { @@ -106,8 +105,6 @@ private void addTemplatesFromFile(final URL templates) { internalAdd(data); } } - } finally { - openStream.close(); } } catch (IOException e) { LOG.error(e); diff --git a/com.avaloq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/resource/XtextGMFResourceUtil.java b/com.avaloq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/resource/XtextGMFResourceUtil.java index 8ce7efe13f..bf26950ee8 100644 --- a/com.avaloq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/resource/XtextGMFResourceUtil.java +++ b/com.avaloq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/resource/XtextGMFResourceUtil.java @@ -68,7 +68,7 @@ public static byte[] getSeparator(final String encoding) { public static byte[] readFullStream(final InputStream input) throws IOException { InputStream readStream = null; boolean wrappedInput = false; - try { + try { // NOPMD UseTryWithResources - only closes when we created the wrapper, not the caller's stream ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] buffer = new byte[INPUT_BUFFER_SIZE]; if (!(input instanceof BufferedInputStream)) { From c24e9303457471e92e8e49689f8e8db002c69bd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Dinis=20Ferreira?= Date: Sat, 7 Mar 2026 17:00:20 +0100 Subject: [PATCH 2/2] chore: enable PMD UseTryWithResources rule Co-Authored-By: Claude Opus 4.6 --- ddk-configuration/pmd/ruleset.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/ddk-configuration/pmd/ruleset.xml b/ddk-configuration/pmd/ruleset.xml index cca425c723..cdc4596bc5 100644 --- a/ddk-configuration/pmd/ruleset.xml +++ b/ddk-configuration/pmd/ruleset.xml @@ -87,7 +87,6 @@ -