diff options
author | Jonah Graham | 2020-09-01 01:32:32 +0000 |
---|---|---|
committer | Jonah Graham | 2020-09-01 01:32:32 +0000 |
commit | 1562080a3c0149b3df52b38958460946cbbd1100 (patch) | |
tree | b0d13a5570ee6fdc417a238be00a2292559c1784 | |
parent | 7818f6e4945e0e3324e6d3f9a7cd444e953d96d8 (diff) | |
download | org.eclipse.cdt-1562080a3c0149b3df52b38958460946cbbd1100.tar.gz org.eclipse.cdt-1562080a3c0149b3df52b38958460946cbbd1100.tar.xz org.eclipse.cdt-1562080a3c0149b3df52b38958460946cbbd1100.zip |
Bug 553674: Make Binary parsers and related classes Autocloseable
And prepare to make it an error in CDT to not have properly handled
an Autocloseable which means a number of fixes to make sure handles
are closed.
Change-Id: I36cd46017bbce6ece1703d688d7754e523eca68f
35 files changed, 262 insertions, 217 deletions
diff --git a/build/org.eclipse.cdt.managedbuilder.core.tests/suite/org/eclipse/cdt/managedbuilder/testplugin/ManagedBuildTestHelper.java b/build/org.eclipse.cdt.managedbuilder.core.tests/suite/org/eclipse/cdt/managedbuilder/testplugin/ManagedBuildTestHelper.java index 58f3faecc86..03036de452a 100644 --- a/build/org.eclipse.cdt.managedbuilder.core.tests/suite/org/eclipse/cdt/managedbuilder/testplugin/ManagedBuildTestHelper.java +++ b/build/org.eclipse.cdt.managedbuilder.core.tests/suite/org/eclipse/cdt/managedbuilder/testplugin/ManagedBuildTestHelper.java @@ -698,8 +698,7 @@ public class ManagedBuildTestHelper { private static ArrayList<String> getContents(IPath fullPath) { ArrayList<String> lines = new ArrayList<>(); - try { - BufferedReader in = new BufferedReader(new FileReader(fullPath.toFile())); + try (BufferedReader in = new BufferedReader(new FileReader(fullPath.toFile()))) { String line; do { line = in.readLine(); @@ -876,7 +875,8 @@ public class ManagedBuildTestHelper { return buff; } - static public IPath copyFilesToTempDir(IPath srcDir, IPath tmpRootDir, IPath tmpSubDir, IPath[] files) { + static public IPath copyFilesToTempDir(IPath srcDir, IPath tmpRootDir, IPath tmpSubDir, IPath[] files) + throws IOException { IPath tmpSrcDir = null; tmpSrcDir = tmpRootDir.append(tmpSubDir); if (tmpRootDir.toString().equalsIgnoreCase(tmpSrcDir.toString())) { @@ -898,46 +898,31 @@ public class ManagedBuildTestHelper { for (int i = 0; i < files.length; i++) { IPath file = files[i]; IPath srcFile = srcDir.append(file); - FileReader srcReader = null; - try { - srcReader = new FileReader(srcFile.toFile()); - } catch (Exception e) { - Assert.fail("File " + file.toString() + " could not be read."); - return null; - } - if (file.segmentCount() > 1) { - IPath newDir = tmpSrcDir; - do { - IPath dir = file.uptoSegment(1); - newDir = newDir.append(dir); - file = file.removeFirstSegments(1); - newDir.toFile().mkdir(); - if (!newDir.toFile().exists()) { - Assert.fail("Can't create temporary directory " + tmpSrcDirFile.toString()); - } - } while (file.segmentCount() > 1); - } - IPath destFile = tmpSrcDir.append(files[i]); - FileWriter writer = null; - try { - writer = new FileWriter(destFile.toFile()); - } catch (Exception e) { - Assert.fail("File " + files[i].toString() + " could not be written."); - return null; - } - try { - int c; - do { - c = srcReader.read(); - if (c == -1) - break; - writer.write(c); - } while (c != -1); - srcReader.close(); - writer.close(); - } catch (Exception e) { - Assert.fail("File " + file.toString() + " could not be copied."); + try (FileReader srcReader = new FileReader(srcFile.toFile())) { + if (file.segmentCount() > 1) { + IPath newDir = tmpSrcDir; + do { + IPath dir = file.uptoSegment(1); + newDir = newDir.append(dir); + file = file.removeFirstSegments(1); + newDir.toFile().mkdir(); + if (!newDir.toFile().exists()) { + Assert.fail("Can't create temporary directory " + tmpSrcDirFile.toString()); + } + } while (file.segmentCount() > 1); + } + IPath destFile = tmpSrcDir.append(files[i]); + try (FileWriter writer = new FileWriter(destFile.toFile())) { + int c; + do { + c = srcReader.read(); + if (c == -1) + break; + writer.write(c); + } while (c != -1); + } } + } } diff --git a/build/org.eclipse.cdt.managedbuilder.core.tests/tests/org/eclipse/cdt/managedbuilder/core/tests/MultiVersionSupportTests.java b/build/org.eclipse.cdt.managedbuilder.core.tests/tests/org/eclipse/cdt/managedbuilder/core/tests/MultiVersionSupportTests.java index b8b42c3c01f..992f945880a 100644 --- a/build/org.eclipse.cdt.managedbuilder.core.tests/tests/org/eclipse/cdt/managedbuilder/core/tests/MultiVersionSupportTests.java +++ b/build/org.eclipse.cdt.managedbuilder.core.tests/tests/org/eclipse/cdt/managedbuilder/core/tests/MultiVersionSupportTests.java @@ -409,11 +409,12 @@ public class MultiVersionSupportTests extends TestCase { String expectedContent = "Converter for CDT 2.0 Project is invoked"; //$NON-NLS-1$ - BufferedReader data = new BufferedReader(new FileReader(inputFile)); - String actualContent; + try (BufferedReader data = new BufferedReader(new FileReader(inputFile))) { + String actualContent; - if ((actualContent = data.readLine()) != null) { - assertEquals(actualContent, expectedContent); + if ((actualContent = data.readLine()) != null) { + assertEquals(actualContent, expectedContent); + } } } catch (IOException e) { @@ -434,11 +435,12 @@ public class MultiVersionSupportTests extends TestCase { String expectedContent = "Converter for CDT 2.1 Project is invoked"; //$NON-NLS-1$ - BufferedReader data = new BufferedReader(new FileReader(inputFile)); - String actualContent; + try (BufferedReader data = new BufferedReader(new FileReader(inputFile))) { + String actualContent; - if ((actualContent = data.readLine()) != null) { - assertEquals(actualContent, expectedContent); + if ((actualContent = data.readLine()) != null) { + assertEquals(actualContent, expectedContent); + } } } catch (IOException e) { @@ -459,11 +461,12 @@ public class MultiVersionSupportTests extends TestCase { String expectedContent = "The converter for the projectType testProject_1.0.0 is invoked"; //$NON-NLS-1$ - BufferedReader data = new BufferedReader(new FileReader(inputFile)); - String actualContent; + try (BufferedReader data = new BufferedReader(new FileReader(inputFile))) { + String actualContent; - if ((actualContent = data.readLine()) != null) { - assertEquals(actualContent, expectedContent); + if ((actualContent = data.readLine()) != null) { + assertEquals(actualContent, expectedContent); + } } } catch (IOException e) { diff --git a/build/org.eclipse.cdt.managedbuilder.core.tests/tests/org/eclipse/cdt/managedbuilder/core/tests/ProjectConverter.java b/build/org.eclipse.cdt.managedbuilder.core.tests/tests/org/eclipse/cdt/managedbuilder/core/tests/ProjectConverter.java index fbffdbc0a51..de243193a3f 100644 --- a/build/org.eclipse.cdt.managedbuilder.core.tests/tests/org/eclipse/cdt/managedbuilder/core/tests/ProjectConverter.java +++ b/build/org.eclipse.cdt.managedbuilder.core.tests/tests/org/eclipse/cdt/managedbuilder/core/tests/ProjectConverter.java @@ -28,8 +28,7 @@ public class ProjectConverter implements IConvertManagedBuildObject { String tmpDir = System.getProperty("java.io.tmpdir"); File outputFile = new File(tmpDir + "/converterOutput.txt"); - try { - FileWriter out = new FileWriter(outputFile); + try (FileWriter out = new FileWriter(outputFile)) { out.write("---------- Start-------"); out.write("Converter for the build object : '" + buildObj.getName() + "' is invoked."); out.write("From Id : " + fromId); diff --git a/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/makegen/gnu/GnuMakefileGenerator.java b/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/makegen/gnu/GnuMakefileGenerator.java index 6e081c3e0e8..13091ae944d 100644 --- a/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/makegen/gnu/GnuMakefileGenerator.java +++ b/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/makegen/gnu/GnuMakefileGenerator.java @@ -3442,18 +3442,18 @@ public class GnuMakefileGenerator implements IManagedBuilderMakefileGenerator2 { return false; // Get the contents of the dependency file + StringBuffer inBuffer; InputStream contentStream = makefile.getContents(false); - Reader in = new InputStreamReader(contentStream); - StringBuffer inBuffer = null; - int chunkSize = contentStream.available(); - inBuffer = new StringBuffer(chunkSize); - char[] readBuffer = new char[chunkSize]; - int n = in.read(readBuffer); - while (n > 0) { - inBuffer.append(readBuffer); - n = in.read(readBuffer); - } - contentStream.close(); + try (Reader in = new InputStreamReader(contentStream)) { + int chunkSize = contentStream.available(); + inBuffer = new StringBuffer(chunkSize); + char[] readBuffer = new char[chunkSize]; + int n = in.read(readBuffer); + while (n > 0) { + inBuffer.append(readBuffer); + n = in.read(readBuffer); + } + } // The rest of this operation is equally expensive, so // if we are doing an incremental build, only update the @@ -3613,6 +3613,7 @@ public class GnuMakefileGenerator implements IManagedBuilderMakefileGenerator2 { save(outBuffer, makefile); return true; } + return false; } diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/Binary.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/Binary.java index ee9005af52e..17a4a36e620 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/Binary.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/Binary.java @@ -474,8 +474,7 @@ public class Binary extends Openable implements IBinary { IBinaryObject bin = getBinaryObject(); if (bin != null) { StringBuilder sb = new StringBuilder(); - try { - BufferedReader stream = new BufferedReader(new InputStreamReader(bin.getContents())); + try (BufferedReader stream = new BufferedReader(new InputStreamReader(bin.getContents()))) { char[] buf = new char[512]; int len; while ((len = stream.read(buf, 0, buf.length)) != -1) { diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/AR.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/AR.java index b7ddf28feb8..5cdc825377d 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/AR.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/AR.java @@ -30,13 +30,18 @@ import org.eclipse.cdt.core.CCorePlugin; * class operations. * @see ARHeader */ -public class AR { +public class AR implements AutoCloseable { protected String filename; protected ERandomAccessFile efile; protected long strtbl_pos = -1; private ARHeader[] headers; + @Override + public void close() { + dispose(); + } + public void dispose() { try { if (efile != null) { @@ -197,11 +202,10 @@ public class AR { efile.seek(obj_offset); efile.read(temp); } else { - efile = new ERandomAccessFile(filename, "r"); //$NON-NLS-1$ - efile.seek(obj_offset); - efile.read(temp); - efile.close(); - efile = null; + try (ERandomAccessFile tempfile = new ERandomAccessFile(filename, "r")) { //$NON-NLS-1$ + tempfile.seek(obj_offset); + tempfile.read(temp); + } } return temp; } diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/Exe.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/Exe.java index 784128ee734..85205e19620 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/Exe.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/Exe.java @@ -18,11 +18,12 @@ import java.io.RandomAccessFile; import org.eclipse.cdt.core.CCorePlugin; -public class Exe { +public class Exe implements AutoCloseable { public static final String NL = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$ protected RandomAccessFile rfile; ExeHeader ehdr; + private String file; static public class ExeHeader { @@ -160,12 +161,13 @@ public class Exe { @Override public String toString() { StringBuilder buffer = new StringBuilder(); - buffer.append(rfile).append(NL); + buffer.append(file).append(NL); buffer.append(ehdr); return buffer.toString(); } public Exe(String file) throws IOException { + this.file = file; rfile = new RandomAccessFile(file, "r"); //$NON-NLS-1$ try { ehdr = new ExeHeader(rfile); @@ -176,6 +178,15 @@ public class Exe { } } + @Override + public void close() throws Exception { + if (rfile != null) { + rfile.close(); + rfile = null; + ehdr = null; + } + } + public static void main(String[] args) { try { Exe exe = new Exe(args[0]); diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/PE.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/PE.java index 0cdf8a67d3c..f9e3c00c689 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/PE.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/PE.java @@ -73,7 +73,7 @@ import org.eclipse.cdt.utils.debug.stabs.StabsReader; * This class is planned for removal in next major release. */ @Deprecated -public class PE { +public class PE implements AutoCloseable { public static final String NL = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$ RandomAccessFile rfile; @@ -573,10 +573,14 @@ public class PE { } public static Attribute getAttribute(String file) throws IOException { - PE pe = new PE(file); - Attribute attrib = pe.getAttribute(); - pe.dispose(); - return attrib; + try (PE pe = new PE(file)) { + return pe.getAttribute(); + } + } + + @Override + public void close() throws IOException { + dispose(); } public void dispose() throws IOException { diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/PE64.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/PE64.java index 6585605a7cf..5e4d8a4ba17 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/PE64.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/PE64.java @@ -68,7 +68,7 @@ import org.eclipse.cdt.utils.debug.stabs.StabsReader; * </pre> * @since 6.9 */ -public class PE64 { +public class PE64 implements AutoCloseable { public static final String NL = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$ RandomAccessFile rfile; @@ -667,10 +667,14 @@ public class PE64 { } public static Attribute getAttribute(String file) throws IOException { - PE64 pe = new PE64(file); - Attribute attrib = pe.getAttribute(); - pe.dispose(); - return attrib; + try (PE64 pe = new PE64(file)) { + return pe.getAttribute(); + } + } + + @Override + public void close() throws IOException { + dispose(); } public void dispose() throws IOException { diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/PEArchive.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/PEArchive.java index 226ee4d8cd5..33a26f029ae 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/PEArchive.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/PEArchive.java @@ -30,13 +30,18 @@ import org.eclipse.cdt.core.CCorePlugin; * @see ARHeader */ @Deprecated -public class PEArchive { +public class PEArchive implements AutoCloseable { protected String filename; protected RandomAccessFile rfile; protected long strtbl_pos = -1; private ARHeader[] headers; + @Override + public void close() { + dispose(); + } + public void dispose() { try { if (rfile != null) { diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/parser/PEBinaryArchive.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/parser/PEBinaryArchive.java index b1dad7903bb..8d833997de2 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/parser/PEBinaryArchive.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/parser/PEBinaryArchive.java @@ -35,7 +35,9 @@ public class PEBinaryArchive extends BinaryFile implements IBinaryArchive { public PEBinaryArchive(PEParser parser, IPath path) throws IOException { super(parser, path, IBinaryFile.ARCHIVE); - new AR(path.toOSString()).dispose(); // check file type + try (AR ar = new AR(path.toOSString())) { + // create the object just to check file type + } children = new ArrayList<>(5); } diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/parser/PEBinaryArchive64.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/parser/PEBinaryArchive64.java index d4aa241357e..c37b4774506 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/parser/PEBinaryArchive64.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/parser/PEBinaryArchive64.java @@ -34,7 +34,9 @@ public class PEBinaryArchive64 extends BinaryFile implements IBinaryArchive { public PEBinaryArchive64(PEParser64 parser, IPath path) throws IOException { super(parser, path, IBinaryFile.ARCHIVE); - new AR(path.toOSString()).dispose(); // check file type + try (AR ar = new AR(path.toOSString())) { + // create the object just to check file type + } children = new ArrayList<>(5); } diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/parser/PEBinaryObject.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/parser/PEBinaryObject.java index 516c7de1f4d..9f7fd4e81fc 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/parser/PEBinaryObject.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/parser/PEBinaryObject.java @@ -123,33 +123,21 @@ public class PEBinaryObject extends BinaryObjectAdapter { } protected void loadAll() throws IOException { - PE pe = null; - try { - pe = getPE(); + try (PE pe = getPE()) { loadInfo(pe); loadSymbols(pe); - } finally { - if (pe != null) { - pe.dispose(); - } } } protected void loadInfo() throws IOException { - PE pe = null; - try { - pe = getPE(); + try (PE pe = getPE()) { loadInfo(pe); - } finally { - if (pe != null) { - pe.dispose(); - } } } protected void loadInfo(PE pe) throws IOException { info = new BinaryObjectInfo(); - PE.Attribute attribute = getPE().getAttribute(); + PE.Attribute attribute = pe.getAttribute(); info.isLittleEndian = attribute.isLittleEndian(); info.hasDebug = attribute.hasDebug(); info.cpu = attribute.getCPU(); diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/parser/PEBinaryObject64.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/parser/PEBinaryObject64.java index e9ea46dd712..849f36e355b 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/parser/PEBinaryObject64.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/parser/PEBinaryObject64.java @@ -125,33 +125,21 @@ public class PEBinaryObject64 extends BinaryObjectAdapter { } protected void loadAll() throws IOException { - PE64 pe = null; - try { - pe = getPE(); + try (PE64 pe = getPE()) { loadInfo(pe); loadSymbols(pe); - } finally { - if (pe != null) { - pe.dispose(); - } } } protected void loadInfo() throws IOException { - PE64 pe = null; - try { - pe = getPE(); + try (PE64 pe = getPE()) { loadInfo(pe); - } finally { - if (pe != null) { - pe.dispose(); - } } } protected void loadInfo(PE64 pe) throws IOException { info = new BinaryObjectInfo(); - PE64.Attribute attribute = getPE().getAttribute(); + PE64.Attribute attribute = pe.getAttribute(); info.isLittleEndian = attribute.isLittleEndian(); info.hasDebug = attribute.hasDebug(); info.cpu = attribute.getCPU(); diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/debug/dwarf/Dwarf.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/debug/dwarf/Dwarf.java index 01a2f54ffe1..132d8e026ca 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/debug/dwarf/Dwarf.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/debug/dwarf/Dwarf.java @@ -186,9 +186,9 @@ public class Dwarf { boolean printEnabled = true; public Dwarf(String file) throws IOException { - Elf exe = new Elf(file); - init(exe); - exe.dispose(); + try (Elf exe = new Elf(file)) { + init(exe); + } } public Dwarf(Elf exe) throws IOException { @@ -238,8 +238,9 @@ public class Dwarf { } if (debugFile.exists()) { // if the debug file exists from above, open it and get the section info from it - Elf debugInfo = new Elf(debugFile.getCanonicalPath()); - sections = debugInfo.getSections(); + try (Elf debugInfo = new Elf(debugFile.getCanonicalPath())) { + sections = debugInfo.getSections(); + } debugInfoPath = new Path(debugFile.getCanonicalPath()); } } @@ -264,17 +265,17 @@ public class Dwarf { } File altFile = altPath.toFile(); if (altFile.exists()) { - Elf altInfo = new Elf(altFile.getCanonicalPath()); - Elf.Section[] altSections = altInfo.getSections(); - for (Section altSection : altSections) { - String altName = altSection.toString(); - for (String element : DWARF_ALT_SCNNAMES) { - if (altName.equals(element)) { - try { - dwarfAltSections.put(element, altSection.mapSectionData()); - } catch (Exception e) { - e.printStackTrace(); - CCorePlugin.log(e); + try (Elf altInfo = new Elf(altFile.getCanonicalPath())) { + Elf.Section[] altSections = altInfo.getSections(); + for (Section altSection : altSections) { + String altName = altSection.toString(); + for (String element : DWARF_ALT_SCNNAMES) { + if (altName.equals(element)) { + try { + dwarfAltSections.put(element, altSection.mapSectionData()); + } catch (Exception e) { + CCorePlugin.log(e); + } } } } diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/debug/dwarf/DwarfReader.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/debug/dwarf/DwarfReader.java index 16a390b96eb..f2ca84827fe 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/debug/dwarf/DwarfReader.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/debug/dwarf/DwarfReader.java @@ -134,8 +134,9 @@ public class DwarfReader extends Dwarf implements ISymbolReader, ICompileOptions File buildIdFile = buildIdPath.toFile(); if (buildIdFile.exists()) { // if the debug file exists from above, open it and get the section info from it - Elf debugInfo = new Elf(buildIdFile.getCanonicalPath()); - sections = debugInfo.getSections(); + try (Elf debugInfo = new Elf(buildIdFile.getCanonicalPath())) { + sections = debugInfo.getSections(); + } have_build_id = true; debugInfoPath = new Path(buildIdFile.getCanonicalPath()).removeLastSegments(1); break; @@ -185,8 +186,9 @@ public class DwarfReader extends Dwarf implements ISymbolReader, ICompileOptions } if (debugFile.exists()) { // if the debug file exists from above, open it and get the section info from it - Elf debugInfo = new Elf(debugFile.getCanonicalPath()); - sections = debugInfo.getSections(); + try (Elf debugInfo = new Elf(debugFile.getCanonicalPath())) { + sections = debugInfo.getSections(); + } debugInfoPath = new Path(debugFile.getCanonicalPath()).removeLastSegments(1); } } @@ -215,17 +217,17 @@ public class DwarfReader extends Dwarf implements ISymbolReader, ICompileOptions } File altFile = altPath.toFile(); if (altFile.exists()) { - Elf altInfo = new Elf(altFile.getCanonicalPath()); - Elf.Section[] altSections = altInfo.getSections(); - for (Section altSection : altSections) { - String altName = altSection.toString(); - for (String element : DWARF_ALT_SectionsToParse) { - if (altName.equals(element)) { - try { - dwarfAltSections.put(element, altSection.mapSectionData()); - } catch (Exception e) { - e.printStackTrace(); - CCorePlugin.log(e); + try (Elf altInfo = new Elf(altFile.getCanonicalPath())) { + Elf.Section[] altSections = altInfo.getSections(); + for (Section altSection : altSections) { + String altName = altSection.toString(); + for (String element : DWARF_ALT_SectionsToParse) { + if (altName.equals(element)) { + try { + dwarfAltSections.put(element, altSection.mapSectionData()); + } catch (Exception e) { + CCorePlugin.log(e); + } } } } diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/debug/stabs/Stabs.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/debug/stabs/Stabs.java index c99897e1bb8..69ecba5cc61 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/debug/stabs/Stabs.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/debug/stabs/Stabs.java @@ -68,14 +68,12 @@ public class Stabs { // we support Elf and PE executable formats. try Elf // and then PE. - try { - Elf exe = new Elf(file); + try (Elf exe = new Elf(file)) { init(exe); - exe.dispose(); } catch (IOException e) { - PE exe = new PE(file); - init(exe); - exe.dispose(); + try (PE exe = new PE(file)) { + init(exe); + } } } diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/debug/tools/DebugAddr2line.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/debug/tools/DebugAddr2line.java index 3c24e1c8a7b..52dd8642b78 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/debug/tools/DebugAddr2line.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/debug/tools/DebugAddr2line.java @@ -31,9 +31,9 @@ public class DebugAddr2line { DebugSymsRequestor symreq; public DebugAddr2line(String file) throws IOException { - Elf elf = new Elf(file); - init(elf); - elf.dispose(); + try (Elf elf = new Elf(file)) { + init(elf); + } } public DebugAddr2line(Elf elf) throws IOException { diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/debug/tools/DebugDump.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/debug/tools/DebugDump.java index 071f4073c94..7fae54ef2c8 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/debug/tools/DebugDump.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/debug/tools/DebugDump.java @@ -46,9 +46,9 @@ public class DebugDump implements IDebugEntryRequestor { } void parse(String file) throws IOException { - Elf elf = new Elf(file); - parse(elf); - elf.dispose(); + try (Elf elf = new Elf(file)) { + parse(elf); + } } void parse(Elf elf) throws IOException { diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/AR.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/AR.java index bb37c2db604..bb88792ac09 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/AR.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/AR.java @@ -31,13 +31,18 @@ import org.eclipse.cdt.utils.ERandomAccessFile; * @see ARHeader */ @Deprecated -public class AR { +public class AR implements AutoCloseable { protected String filename; protected ERandomAccessFile efile; protected long strtbl_pos = -1; private ARHeader[] headers; + @Override + public void close() { + dispose(); + } + public void dispose() { try { if (efile != null) { @@ -207,11 +212,10 @@ public class AR { efile.seek(elf_offset); efile.read(temp); } else { - efile = new ERandomAccessFile(filename, "r"); //$NON-NLS-1$ - efile.seek(elf_offset); - efile.read(temp); - efile.close(); - efile = null; + try (ERandomAccessFile tempfile = new ERandomAccessFile(filename, "r")) { //$NON-NLS-1$ + tempfile.seek(elf_offset); + tempfile.read(temp); + } } return temp; } @@ -237,6 +241,7 @@ public class AR { String hdr = efile.readLine(); if (hdr == null || hdr.compareTo("!<arch>") != 0) { //$NON-NLS-1$ efile.close(); + efile = null; throw new IOException(CCorePlugin.getResourceString("Util.exception.invalidArchive")); //$NON-NLS-1$ } } diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/Elf.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/Elf.java index ba8d81773f9..c6334470ccc 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/Elf.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/Elf.java @@ -41,7 +41,7 @@ import org.eclipse.cdt.utils.Addr64Factory; import org.eclipse.cdt.utils.ERandomAccessFile; import org.eclipse.cdt.utils.debug.dwarf.DwarfReader; -public class Elf { +public class Elf implements AutoCloseable { public static final int ELF32_ADDR_SIZE = 4; public static final int ELF32_OFF_SIZE = 4; public static final int ELF64_ADDR_SIZE = 8; @@ -956,21 +956,20 @@ public class Elf { } public static Attribute getAttributes(String file) throws IOException { - Elf elf = new Elf(file); - Attribute attrib = elf.getAttributes(); - elf.dispose(); - return attrib; + try (Elf elf = new Elf(file)) { + Attribute attrib = elf.getAttributes(); + return attrib; + } } public static Attribute getAttributes(byte[] array) throws IOException { + try (Elf emptyElf = new Elf()) { + emptyElf.ehdr = emptyElf.new ELFhdr(array); + emptyElf.sections = new Elf.Section[0]; + Attribute attrib = emptyElf.getAttributes(); - Elf emptyElf = new Elf(); - emptyElf.ehdr = emptyElf.new ELFhdr(array); - emptyElf.sections = new Elf.Section[0]; - Attribute attrib = emptyElf.getAttributes(); - emptyElf.dispose(); - - return attrib; + return attrib; + } } public static boolean isElfHeader(byte[] e_ident) { @@ -978,6 +977,11 @@ public class Elf { && e_ident[ELFhdr.EI_MAG1] == 'E' && e_ident[ELFhdr.EI_MAG2] == 'L' && e_ident[ELFhdr.EI_MAG3] == 'F'; } + @Override + public void close() { + dispose(); + } + public void dispose() { try { if (efile != null) { diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/ElfHelper.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/ElfHelper.java index b5063ff16df..7ce48e1f3bf 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/ElfHelper.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/ElfHelper.java @@ -26,7 +26,7 @@ import org.eclipse.cdt.utils.elf.Elf.Symbol; * * @see Elf */ -public class ElfHelper { +public class ElfHelper implements AutoCloseable { private Elf elf; private Elf.Symbol[] dynsyms; @@ -34,6 +34,11 @@ public class ElfHelper { private Elf.Section[] sections; private Elf.Dynamic[] dynamics; + @Override + public void close() { + dispose(); + } + public void dispose() { if (elf != null) { elf.dispose(); diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/ElfBinaryArchive.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/ElfBinaryArchive.java index cb308cbb3bf..a4affdd11c3 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/ElfBinaryArchive.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/ElfBinaryArchive.java @@ -32,9 +32,11 @@ public class ElfBinaryArchive extends BinaryFile implements IBinaryArchive { private ArrayList<IBinaryObject> children; - public ElfBinaryArchive(IBinaryParser parser, IPath p) throws IOException { - super(parser, p, IBinaryFile.ARCHIVE); - new AR(p.toOSString()).dispose(); // check file type + public ElfBinaryArchive(IBinaryParser parser, IPath path) throws IOException { + super(parser, path, IBinaryFile.ARCHIVE); + try (AR ar = new AR(path.toOSString())) { + // create the object just to check file type + } children = new ArrayList<>(5); } diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/ElfBinaryObject.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/ElfBinaryObject.java index 83f98d52085..1edf66298ce 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/ElfBinaryObject.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/ElfBinaryObject.java @@ -111,27 +111,15 @@ public class ElfBinaryObject extends BinaryObjectAdapter { } protected void loadAll() throws IOException { - ElfHelper helper = null; - try { - helper = getElfHelper(); + try (ElfHelper helper = getElfHelper()) { loadInfo(helper); loadSymbols(helper); - } finally { - if (helper != null) { - helper.dispose(); - } } } protected void loadInfo() throws IOException { - ElfHelper helper = null; - try { - helper = getElfHelper(); + try (ElfHelper helper = getElfHelper()) { loadInfo(helper); - } finally { - if (helper != null) { - helper.dispose(); - } } } diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/ElfParser.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/ElfParser.java index a6b8c563dc1..c1c245a1f3b 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/ElfParser.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/ElfParser.java @@ -184,11 +184,8 @@ public class ElfParser extends AbstractCExtension implements IBinaryParser { } private static PHdr[] getPHdrs(IPath path) throws IOException { - Elf elf = new Elf(path.toOSString()); - try { + try (Elf elf = new Elf(path.toOSString())) { return elf.getPHdrs(); - } finally { - elf.dispose(); } } } diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/macho/AR.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/macho/AR.java index cc7ebb714f8..9eb8dbc2cf7 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/macho/AR.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/macho/AR.java @@ -29,13 +29,18 @@ import org.eclipse.cdt.core.CCorePlugin; * class operations. * @see ARHeader */ -public class AR { +public class AR implements AutoCloseable { protected String filename; protected ERandomAccessFile efile; protected long strtbl_pos = -1; private ARHeader[] headers; + @Override + public void close() { + dispose(); + } + public void dispose() { try { if (efile != null) { diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/macho/parser/MachOBinaryArchive.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/macho/parser/MachOBinaryArchive.java index 4fd1b1686bb..e06d73de0a4 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/macho/parser/MachOBinaryArchive.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/macho/parser/MachOBinaryArchive.java @@ -35,7 +35,9 @@ public class MachOBinaryArchive extends BinaryFile implements IBinaryArchive { public MachOBinaryArchive(IBinaryParser parser, IPath p) throws IOException { super(parser, p, IBinaryFile.ARCHIVE); - new AR(p.toOSString()).dispose(); // check file type + try (AR ar = new AR(p.toOSString())) { + // create the object just to check file type + } children = new ArrayList<>(5); } diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/macho/parser/MachOBinaryArchive64.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/macho/parser/MachOBinaryArchive64.java index 8de3bf3cec9..3a09e1b258f 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/macho/parser/MachOBinaryArchive64.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/macho/parser/MachOBinaryArchive64.java @@ -33,7 +33,10 @@ public class MachOBinaryArchive64 extends BinaryFile implements IBinaryArchive { public MachOBinaryArchive64(IBinaryParser parser, IPath p) throws IOException { super(parser, p, IBinaryFile.ARCHIVE); - new AR(p.toOSString()).dispose(); // check file type + try (AR ar = new AR(p.toOSString())) { + // create the object just to check file type + + } children = new ArrayList<>(5); } diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/som/AR.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/som/AR.java index 4499a1c88fa..1bbe85f8ade 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/som/AR.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/som/AR.java @@ -26,7 +26,7 @@ import org.eclipse.cdt.utils.coff.ReadMemoryAccess; * * @author vhirsl */ -public class AR { +public class AR implements AutoCloseable { public static final String NL = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$ protected String filename; @@ -190,6 +190,11 @@ public class AR { lstHeader = new LSTHeader(); } + @Override + public void close() { + dispose(); + } + public void dispose() { try { if (file != null) { diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/som/parser/SOMBinaryArchive.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/som/parser/SOMBinaryArchive.java index f75fd043283..18cebb87dfe 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/som/parser/SOMBinaryArchive.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/som/parser/SOMBinaryArchive.java @@ -39,7 +39,9 @@ public class SOMBinaryArchive extends BinaryFile implements IBinaryArchive { */ public SOMBinaryArchive(IBinaryParser parser, IPath path) throws IOException { super(parser, path, IBinaryFile.ARCHIVE); - new AR(path.toOSString()).dispose(); // check file type + try (AR ar = new AR(path.toOSString())) { + // create the object just to check file type + } children = new ArrayList<>(5); } diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/xcoff/AR.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/xcoff/AR.java index aea7534e873..43abe364888 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/xcoff/AR.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/xcoff/AR.java @@ -30,7 +30,7 @@ import org.eclipse.cdt.core.CCorePlugin; * * @author vhirsl */ -public class AR { +public class AR implements AutoCloseable { protected String filename; private RandomAccessFile file; private ARHeader header; @@ -105,6 +105,11 @@ public class AR { } } + @Override + public void close() { + dispose(); + } + public void dispose() { try { if (file != null) { @@ -365,4 +370,5 @@ public class AR { e.printStackTrace(); } } + } diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/xcoff/parser/XCOFFBinaryArchive.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/xcoff/parser/XCOFFBinaryArchive.java index a425cde72cf..d0cdf5f7299 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/xcoff/parser/XCOFFBinaryArchive.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/xcoff/parser/XCOFFBinaryArchive.java @@ -39,7 +39,9 @@ public class XCOFFBinaryArchive extends BinaryFile implements IBinaryArchive { */ public XCOFFBinaryArchive(IBinaryParser parser, IPath path) throws IOException { super(parser, path, IBinaryFile.ARCHIVE); - new AR(path.toOSString()).dispose(); // check file type + try (AR ar = new AR(path.toOSString())) { + // create the object just to check file type + } children = new ArrayList<>(5); } diff --git a/doc/org.eclipse.cdt.doc.isv/guide/deprecated_API_removals.html b/doc/org.eclipse.cdt.doc.isv/guide/deprecated_API_removals.html index 129b22eeb83..8f9b15c3a70 100644 --- a/doc/org.eclipse.cdt.doc.isv/guide/deprecated_API_removals.html +++ b/doc/org.eclipse.cdt.doc.isv/guide/deprecated_API_removals.html @@ -46,6 +46,7 @@ <li><a href="#terminal">TM Terminal has major changes to support new color and preference functionality.</a></li> <li><a href="#casesensitive">Environment Variables are always case sensitive in CDT.</a></li> <li><a href="#escaping">Environment variables no longer support \${ to avoid expanding.</a></li> + <li><a href="#autocloseable">The binary parsers are now implement Autocloseable</a></li> </ol> <p> Planned Removals after June 2022 @@ -303,6 +304,27 @@ <li>org.eclipse.cdt.core.envvar.IEnvironmentVariableManager.isVariableCaseSensitive() removed.</li></li> </ul> + + <h3>15. <a name="autocloseable">The binary parsers are now implement AutoCloseable</a></h3> + <p> + The binary parsers part of CDT core now implement the AutoCloseable interface and can be used + in try-with-resources blocks. See list below for all the classes that are now AutoCloseable. See <a + href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=553674" target="_blank">Bug 553674</a>. + </p> + <ul> + <li>org.eclipse.cdt.utils.coff.Exe</li> + <li>org.eclipse.cdt.utils.coff.PE</li> + <li>org.eclipse.cdt.utils.coff.PE64</li> + <li>org.eclipse.cdt.utils.coff.PEArchive</li> + <li>org.eclipse.cdt.utils.elf.Elf</li> + <li>org.eclipse.cdt.utils.elf.ElfHelper</li> + <li>org.eclipse.cdt.utils.AR</li> + <li>org.eclipse.cdt.utils.elf.AR</li> + <li>org.eclipse.cdt.utils.som.AR</li> + <li>org.eclipse.cdt.utils.xcoff.AR</li> + <li>org.eclipse.cdt.utils.macho.AR</li> + </ul> + <hr> <h2>Future Deletions</h2> diff --git a/dsf/org.eclipse.cdt.examples.dsf.pda/src/org/eclipse/cdt/examples/dsf/pda/service/PDABackend.java b/dsf/org.eclipse.cdt.examples.dsf.pda/src/org/eclipse/cdt/examples/dsf/pda/service/PDABackend.java index 91ab830cb01..58d75e81b0c 100644 --- a/dsf/org.eclipse.cdt.examples.dsf.pda/src/org/eclipse/cdt/examples/dsf/pda/service/PDABackend.java +++ b/dsf/org.eclipse.cdt.examples.dsf.pda/src/org/eclipse/cdt/examples/dsf/pda/service/PDABackend.java @@ -151,17 +151,19 @@ public class PDABackend extends AbstractDsfService { Thread.sleep(1000); } catch (InterruptedException e) { } - Socket socket = new Socket("localhost", fRequestPort); - fRequestOutputStream = socket.getOutputStream(); - fRequestInputStream = socket.getInputStream(); + try (Socket socket = new Socket("localhost", fRequestPort)) { + fRequestOutputStream = socket.getOutputStream(); + fRequestInputStream = socket.getInputStream(); + } // give interpreter a chance to open next socket try { Thread.sleep(1000); } catch (InterruptedException e) { } - socket = new Socket("localhost", fEventPort); - fEventInputStream = socket.getInputStream(); + try (Socket socket = new Socket("localhost", fEventPort)) { + fEventInputStream = socket.getInputStream(); + } } catch (UnknownHostException e) { rm.setStatus(new Status(IStatus.ERROR, PDAPlugin.PLUGIN_ID, REQUEST_FAILED, diff --git a/launch/org.eclipse.cdt.launch/src/org/eclipse/cdt/launch/ui/CEnvironmentTab.java b/launch/org.eclipse.cdt.launch/src/org/eclipse/cdt/launch/ui/CEnvironmentTab.java index 7aa9f052b5f..e2a8ddd39cd 100644 --- a/launch/org.eclipse.cdt.launch/src/org/eclipse/cdt/launch/ui/CEnvironmentTab.java +++ b/launch/org.eclipse.cdt.launch/src/org/eclipse/cdt/launch/ui/CEnvironmentTab.java @@ -398,8 +398,7 @@ public class CEnvironmentTab extends CLaunchConfigurationTab { } //Iterate through each key/value property we discover - try { - BufferedReader reader = new BufferedReader(new FileReader(file)); + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { String line, key, value; while ((line = reader.readLine()) != null) { |