diff --git a/com.ibm.wala.cast.js/source/com/ibm/wala/cast/js/ipa/callgraph/LoadFileTargetSelector.java b/com.ibm.wala.cast.js/source/com/ibm/wala/cast/js/ipa/callgraph/LoadFileTargetSelector.java index 737b14eaf..c5321027d 100644 --- a/com.ibm.wala.cast.js/source/com/ibm/wala/cast/js/ipa/callgraph/LoadFileTargetSelector.java +++ b/com.ibm.wala.cast.js/source/com/ibm/wala/cast/js/ipa/callgraph/LoadFileTargetSelector.java @@ -74,8 +74,8 @@ public class LoadFileTargetSelector implements MethodTargetSelector { URL url = new URL(builder.getBaseURL(), str); if(!loadedFiles.contains(url)) { // try to open the input stream for the URL. if it fails, we'll get an IOException and fall through to default case - InputStream inputStream = url.openConnection().getInputStream(); - inputStream.close(); + try (InputStream inputStream = url.openConnection().getInputStream()) { + } JSCallGraphUtil.loadAdditionalFile(builder.getClassHierarchy() , cl, url); loadedFiles.add(url); IClass script = builder.getClassHierarchy().lookupClass(TypeReference.findOrCreate(cl.getReference(), "L" + url.getFile())); diff --git a/com.ibm.wala.core.tests/src/com/ibm/wala/core/tests/shrike/DynamicCallGraphTestBase.java b/com.ibm.wala.core.tests/src/com/ibm/wala/core/tests/shrike/DynamicCallGraphTestBase.java index bc5701f31..5c6d08e97 100644 --- a/com.ibm.wala.core.tests/src/com/ibm/wala/core/tests/shrike/DynamicCallGraphTestBase.java +++ b/com.ibm.wala.core.tests/src/com/ibm/wala/core/tests/shrike/DynamicCallGraphTestBase.java @@ -192,43 +192,42 @@ public abstract class DynamicCallGraphTestBase extends WalaTestCase { } protected void check(CallGraph staticCG, EdgesTest test, Predicate filter) throws IOException { - BufferedReader dynamicEdgesFile = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(cgLocation)))); - String line; int lines = 0; - loop: while ((line = dynamicEdgesFile.readLine()) != null) { - lines++; - StringTokenizer edge = new StringTokenizer(line, "\t"); - - CGNode caller; - String callerClass = edge.nextToken(); - if ("root".equals(callerClass)) { - caller = staticCG.getFakeRootNode(); - } else if ("clinit".equals(callerClass)) { - caller = staticCG.getFakeWorldClinitNode(); - } else if ("callbacks".equals(callerClass)) { - continue loop; - } else { - String callerMethod = edge.nextToken(); - MethodReference callerRef = MethodReference.findOrCreate(TypeReference.findOrCreate(ClassLoaderReference.Application, "L" + callerClass), Selector.make(callerMethod)); - Set nodes = staticCG.getNodes(callerRef); - if (! filter.test(callerRef)) { + try (final BufferedReader dynamicEdgesFile = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(cgLocation))))) { + String line; + loop: while ((line = dynamicEdgesFile.readLine()) != null) { + lines++; + StringTokenizer edge = new StringTokenizer(line, "\t"); + + CGNode caller; + String callerClass = edge.nextToken(); + if ("root".equals(callerClass)) { + caller = staticCG.getFakeRootNode(); + } else if ("clinit".equals(callerClass)) { + caller = staticCG.getFakeWorldClinitNode(); + } else if ("callbacks".equals(callerClass)) { + continue loop; + } else { + String callerMethod = edge.nextToken(); + MethodReference callerRef = MethodReference.findOrCreate(TypeReference.findOrCreate(ClassLoaderReference.Application, "L" + callerClass), Selector.make(callerMethod)); + Set nodes = staticCG.getNodes(callerRef); + if (! filter.test(callerRef)) { + continue loop; + } + Assert.assertEquals(1, nodes.size()); + caller = nodes.iterator().next(); + } + + String calleeClass = edge.nextToken(); + String calleeMethod = edge.nextToken(); + MethodReference callee = callee(calleeClass, calleeMethod); + if (! filter.test(callee)) { continue loop; } - Assert.assertEquals(1, nodes.size()); - caller = nodes.iterator().next(); + test.edgesTest(staticCG, caller, callee); } - - String calleeClass = edge.nextToken(); - String calleeMethod = edge.nextToken(); - MethodReference callee = callee(calleeClass, calleeMethod); - if (! filter.test(callee)) { - continue loop; - } - test.edgesTest(staticCG, caller, callee); } - dynamicEdgesFile.close(); - Assert.assertTrue("more than one edge", lines > 0); } diff --git a/com.ibm.wala.core.tests/src/com/ibm/wala/demandpa/driver/WalaUtil.java b/com.ibm.wala.core.tests/src/com/ibm/wala/demandpa/driver/WalaUtil.java index 1792304d6..812f8df64 100644 --- a/com.ibm.wala.core.tests/src/com/ibm/wala/demandpa/driver/WalaUtil.java +++ b/com.ibm.wala.core.tests/src/com/ibm/wala/demandpa/driver/WalaUtil.java @@ -67,8 +67,7 @@ public class WalaUtil { } System.err.print("dumping ir..."); String irFile = p.getProperty(WalaProperties.OUTPUT_DIR) + File.separatorChar + benchName + "-ir.txt"; - try { - PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(irFile))); + try (final PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(irFile)))) { for (Iterator iter = cg.iterator(); iter.hasNext();) { CGNode node = iter.next(); IR ir = node.getIR(); @@ -79,7 +78,6 @@ public class WalaUtil { writer.println(ir); writer.println(""); } - writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); diff --git a/com.ibm.wala.scandroid/source/org/scandroid/util/AndroidAnalysisContext.java b/com.ibm.wala.scandroid/source/org/scandroid/util/AndroidAnalysisContext.java index 1f7cfea35..1d50ab9c9 100644 --- a/com.ibm.wala.scandroid/source/org/scandroid/util/AndroidAnalysisContext.java +++ b/com.ibm.wala.scandroid/source/org/scandroid/util/AndroidAnalysisContext.java @@ -256,7 +256,6 @@ public class AndroidAnalysisContext { throw new IllegalArgumentException("cha cannot be null"); } - InputStream s = null; try { Set summaryClasses = HashSetFactory.make(); Map summaries = HashMapFactory.make(); @@ -269,42 +268,36 @@ public class AndroidAnalysisContext { } // for (MethodReference mr : summaries.keySet()) { // - // } + // } - s = new FileProvider().getInputStreamFromClassLoader(pathToSpec + try (final InputStream s = new FileProvider().getInputStreamFromClassLoader(pathToSpec + File.separator + methodSpec, - AndroidAnalysisContext.class.getClassLoader()); + AndroidAnalysisContext.class.getClassLoader())) { - XMLMethodSummaryReader nativeSummaries = loadMethodSummaries(scope, - s); + XMLMethodSummaryReader nativeSummaries = loadMethodSummaries(scope, + s); - summaries.putAll(nativeSummaries.getSummaries()); - summaryClasses.addAll(nativeSummaries.getAllocatableClasses()); - if (extraSummary != null) { - summaries.put((MethodReference) extraSummary.getMethod(), - extraSummary); - } - - MethodTargetSelector ms = new BypassMethodTargetSelector( - options.getMethodTargetSelector(), summaries, - nativeSummaries.getIgnoredPackages(), cha); - options.setSelector(ms); - - ClassTargetSelector cs = new BypassClassTargetSelector( - options.getClassTargetSelector(), summaryClasses, cha, - cha.getLoader(scope.getLoader(Atom - .findOrCreateUnicodeAtom("Synthetic")))); - options.setSelector(cs); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } finally { - if (null != s) { - try { - s.close(); - } catch (IOException e) { - e.printStackTrace(); + summaries.putAll(nativeSummaries.getSummaries()); + summaryClasses.addAll(nativeSummaries.getAllocatableClasses()); + if (extraSummary != null) { + summaries.put((MethodReference) extraSummary.getMethod(), + extraSummary); } + + MethodTargetSelector ms = new BypassMethodTargetSelector( + options.getMethodTargetSelector(), summaries, + nativeSummaries.getIgnoredPackages(), cha); + options.setSelector(ms); + + ClassTargetSelector cs = new BypassClassTargetSelector( + options.getClassTargetSelector(), summaryClasses, cha, + cha.getLoader(scope.getLoader(Atom + .findOrCreateUnicodeAtom("Synthetic")))); + options.setSelector(cs); + } + } catch (IOException e) { + e.printStackTrace(); } } diff --git a/com.ibm.wala.scandroid/source/org/scandroid/util/DexDotUtil.java b/com.ibm.wala.scandroid/source/org/scandroid/util/DexDotUtil.java index 3fa0be3f8..d11ba341c 100644 --- a/com.ibm.wala.scandroid/source/org/scandroid/util/DexDotUtil.java +++ b/com.ibm.wala.scandroid/source/org/scandroid/util/DexDotUtil.java @@ -162,9 +162,9 @@ public class DexDotUtil extends DotUtil { } try { File f = new File(dotfile); - FileWriter fw = new FileWriter(f); - fw.write(dotStringBuffer.toString()); - fw.close(); + try (final FileWriter fw = new FileWriter(f)) { + fw.write(dotStringBuffer.toString()); + } return f; } catch (Exception e) { diff --git a/com.ibm.wala.shrike/src/com/ibm/wala/shrike/bench/InterfaceAnalyzer.java b/com.ibm.wala.shrike/src/com/ibm/wala/shrike/bench/InterfaceAnalyzer.java index 3a60d8875..13e16061e 100644 --- a/com.ibm.wala.shrike/src/com/ibm/wala/shrike/bench/InterfaceAnalyzer.java +++ b/com.ibm.wala.shrike/src/com/ibm/wala/shrike/bench/InterfaceAnalyzer.java @@ -43,25 +43,25 @@ public class InterfaceAnalyzer { public static void main(String[] args) throws Exception { OfflineInstrumenter instrumenter = new OfflineInstrumenter(true); - Writer w = new BufferedWriter(new OutputStreamWriter(System.out)); + try (final Writer w = new BufferedWriter(new OutputStreamWriter(System.out))) { - args = instrumenter.parseStandardArgs(args); + args = instrumenter.parseStandardArgs(args); - instrumenter.beginTraversal(); - ClassInstrumenter ci; - while ((ci = instrumenter.nextClass()) != null) { - doClass(ci.getReader()); + instrumenter.beginTraversal(); + ClassInstrumenter ci; + while ((ci = instrumenter.nextClass()) != null) { + doClass(ci.getReader()); + } + instrumenter.close(); + + w.write("Type\t# Total\t# Method\t# Public Method\t# Public Method as Foreign\n"); + for (Iterator i = typeStats.keySet().iterator(); i.hasNext();) { + String k = i.next(); + TypeStats t = typeStats.get(k); + w.write(k + "\t" + t.totalOccurrences + "\t" + t.methodOccurrences + "\t" + t.publicMethodOccurrences + "\t" + + t.foreignPublicMethodOccurrences + "\n"); + } } - instrumenter.close(); - - w.write("Type\t# Total\t# Method\t# Public Method\t# Public Method as Foreign\n"); - for (Iterator i = typeStats.keySet().iterator(); i.hasNext();) { - String k = i.next(); - TypeStats t = typeStats.get(k); - w.write(k + "\t" + t.totalOccurrences + "\t" + t.methodOccurrences + "\t" + t.publicMethodOccurrences + "\t" - + t.foreignPublicMethodOccurrences + "\n"); - } - w.close(); } static int methodUID = 0; diff --git a/com.ibm.wala.shrike/src/com/ibm/wala/shrike/bench/Statistics.java b/com.ibm.wala.shrike/src/com/ibm/wala/shrike/bench/Statistics.java index ebc05214a..e44fb3503 100644 --- a/com.ibm.wala.shrike/src/com/ibm/wala/shrike/bench/Statistics.java +++ b/com.ibm.wala.shrike/src/com/ibm/wala/shrike/bench/Statistics.java @@ -41,17 +41,17 @@ public class Statistics { for (int i = 0; i < 1; i++) { instrumenter = new OfflineInstrumenter(true); - Writer w = new BufferedWriter(new FileWriter("report", false)); + try (Writer w = new BufferedWriter(new FileWriter("report", false))) { - args = instrumenter.parseStandardArgs(args); + args = instrumenter.parseStandardArgs(args); - instrumenter.beginTraversal(); - ClassInstrumenter ci; - while ((ci = instrumenter.nextClass()) != null) { - doClass(ci, w); + instrumenter.beginTraversal(); + ClassInstrumenter ci; + while ((ci = instrumenter.nextClass()) != null) { + doClass(ci, w); + } + instrumenter.close(); } - instrumenter.close(); - w.close(); } } diff --git a/com.ibm.wala.shrike/src/com/ibm/wala/shrike/instrumentation/CodeScraper.java b/com.ibm.wala.shrike/src/com/ibm/wala/shrike/instrumentation/CodeScraper.java index d0d928e0d..45030d2c7 100644 --- a/com.ibm.wala.shrike/src/com/ibm/wala/shrike/instrumentation/CodeScraper.java +++ b/com.ibm.wala.shrike/src/com/ibm/wala/shrike/instrumentation/CodeScraper.java @@ -40,9 +40,9 @@ public class CodeScraper implements ClassFileTransformer { if (className == null || sourceFile == null || !sourceFile.endsWith("java") || true) try { String log = prefix + File.separator + reader.getName() + ".class"; (new File(log)).getParentFile().mkdirs(); - FileOutputStream f = new FileOutputStream(log); - f.write(classfileBuffer); - f.close(); + try (final FileOutputStream f = new FileOutputStream(log)) { + f.write(classfileBuffer); + } } catch (IOException e) { assert false : e; } diff --git a/com.ibm.wala.shrike/src/com/ibm/wala/shrikeBT/shrikeCT/tools/AddSerialVersion.java b/com.ibm.wala.shrike/src/com/ibm/wala/shrikeBT/shrikeCT/tools/AddSerialVersion.java index afcf42703..8f2c7b742 100644 --- a/com.ibm.wala.shrike/src/com/ibm/wala/shrikeBT/shrikeCT/tools/AddSerialVersion.java +++ b/com.ibm.wala.shrike/src/com/ibm/wala/shrikeBT/shrikeCT/tools/AddSerialVersion.java @@ -89,96 +89,94 @@ public class AddSerialVersion { } catch (NoSuchAlgorithmException e) { throw new Error("SHA algorithm not supported: " + e.getMessage()); } - SinkOutputStream sink = new SinkOutputStream(); - DataOutputStream out = new DataOutputStream(new DigestOutputStream(sink, digest)); - - try { - // step 1 - out.writeUTF(r.getName()); - // step 2 - out.writeInt(r.getAccessFlags()); - - // step 3 - String[] interfaces = r.getInterfaceNames(); - Arrays.sort(interfaces); - for (int i = 0; i < interfaces.length; i++) { - out.writeUTF(interfaces[i]); - } - - // step 4 - Integer[] fields = new Integer[r.getFieldCount()]; - final String[] fieldNames = new String[fields.length]; - int fieldCount = 0; - for (int f = 0; f < fields.length; f++) { - int flags = r.getFieldAccessFlags(f); - if ((flags & ClassReader.ACC_PRIVATE) == 0 || (flags & (ClassReader.ACC_STATIC | ClassReader.ACC_TRANSIENT)) == 0) { - fields[fieldCount] = new Integer(f); - fieldNames[f] = r.getFieldName(f); - fieldCount++; - } - } - Arrays.sort(fields, 0, fieldCount, new Comparator() { - @Override - public int compare(Integer o1, Integer o2) { - String name1 = fieldNames[o1.intValue()]; - String name2 = fieldNames[o2.intValue()]; - return name1.compareTo(name2); - } - }); - for (int i = 0; i < fieldCount; i++) { - int f = fields[i].intValue(); - out.writeUTF(fieldNames[f]); - out.writeInt(r.getFieldAccessFlags(f)); - out.writeUTF(r.getFieldType(f)); - } - - // steps 5, 6 and 7 - Integer[] methods = new Integer[r.getMethodCount()]; - final int[] methodKinds = new int[methods.length]; - final String[] methodSigs = new String[methods.length]; - int methodCount = 0; - for (int m = 0; m < methodSigs.length; m++) { - String name = r.getMethodName(m); - int flags = r.getMethodAccessFlags(m); - if (name.equals("") || (flags & ClassReader.ACC_PRIVATE) == 0) { - methods[methodCount] = new Integer(m); - methodSigs[m] = name + r.getMethodType(m); - if (name.equals("")) { - methodKinds[m] = 0; - } else if (name.equals("")) { - methodKinds[m] = 1; - } else { - methodKinds[m] = 2; - } - methodCount++; - } - } - Arrays.sort(methods, 0, methodCount, new Comparator() { - @Override - public int compare(Integer o1, Integer o2) { - int m1 = o1.intValue(); - int m2 = o2.intValue(); - if (methodKinds[m1] != methodKinds[m2]) { - return methodKinds[m1] - methodKinds[m2]; - } - String name1 = methodSigs[m1]; - String name2 = methodSigs[m2]; - return name1.compareTo(name2); - } - }); - for (int i = 0; i < methodCount; i++) { - int m = methods[i].intValue(); - out.writeUTF(r.getMethodName(m)); - out.writeInt(r.getMethodAccessFlags(m)); - out.writeUTF(r.getMethodType(m)); - } - } catch (IOException e1) { - throw new Error("Unexpected IOException: " + e1.getMessage()); - } finally { + try ( + SinkOutputStream sink = new SinkOutputStream(); + DataOutputStream out = new DataOutputStream(new DigestOutputStream(sink, digest)); + ) { try { - out.close(); - } catch (IOException e2) { + // step 1 + out.writeUTF(r.getName()); + // step 2 + out.writeInt(r.getAccessFlags()); + + // step 3 + String[] interfaces = r.getInterfaceNames(); + Arrays.sort(interfaces); + for (int i = 0; i < interfaces.length; i++) { + out.writeUTF(interfaces[i]); + } + + // step 4 + Integer[] fields = new Integer[r.getFieldCount()]; + final String[] fieldNames = new String[fields.length]; + int fieldCount = 0; + for (int f = 0; f < fields.length; f++) { + int flags = r.getFieldAccessFlags(f); + if ((flags & ClassReader.ACC_PRIVATE) == 0 || (flags & (ClassReader.ACC_STATIC | ClassReader.ACC_TRANSIENT)) == 0) { + fields[fieldCount] = new Integer(f); + fieldNames[f] = r.getFieldName(f); + fieldCount++; + } + } + Arrays.sort(fields, 0, fieldCount, new Comparator() { + @Override + public int compare(Integer o1, Integer o2) { + String name1 = fieldNames[o1.intValue()]; + String name2 = fieldNames[o2.intValue()]; + return name1.compareTo(name2); + } + }); + for (int i = 0; i < fieldCount; i++) { + int f = fields[i].intValue(); + out.writeUTF(fieldNames[f]); + out.writeInt(r.getFieldAccessFlags(f)); + out.writeUTF(r.getFieldType(f)); + } + + // steps 5, 6 and 7 + Integer[] methods = new Integer[r.getMethodCount()]; + final int[] methodKinds = new int[methods.length]; + final String[] methodSigs = new String[methods.length]; + int methodCount = 0; + for (int m = 0; m < methodSigs.length; m++) { + String name = r.getMethodName(m); + int flags = r.getMethodAccessFlags(m); + if (name.equals("") || (flags & ClassReader.ACC_PRIVATE) == 0) { + methods[methodCount] = new Integer(m); + methodSigs[m] = name + r.getMethodType(m); + if (name.equals("")) { + methodKinds[m] = 0; + } else if (name.equals("")) { + methodKinds[m] = 1; + } else { + methodKinds[m] = 2; + } + methodCount++; + } + } + Arrays.sort(methods, 0, methodCount, new Comparator() { + @Override + public int compare(Integer o1, Integer o2) { + int m1 = o1.intValue(); + int m2 = o2.intValue(); + if (methodKinds[m1] != methodKinds[m2]) { + return methodKinds[m1] - methodKinds[m2]; + } + String name1 = methodSigs[m1]; + String name2 = methodSigs[m2]; + return name1.compareTo(name2); + } + }); + for (int i = 0; i < methodCount; i++) { + int m = methods[i].intValue(); + out.writeUTF(r.getMethodName(m)); + out.writeInt(r.getMethodAccessFlags(m)); + out.writeUTF(r.getMethodType(m)); + } + } catch (IOException e1) { + throw new Error("Unexpected IOException: " + e1.getMessage()); } + } catch (IOException e2) { } byte[] hash = digest.digest(); diff --git a/com.ibm.wala.shrike/src/com/ibm/wala/shrikeBT/shrikeCT/tools/ClassSearcher.java b/com.ibm.wala.shrike/src/com/ibm/wala/shrikeBT/shrikeCT/tools/ClassSearcher.java index 442d29755..7bc17732c 100644 --- a/com.ibm.wala.shrike/src/com/ibm/wala/shrikeBT/shrikeCT/tools/ClassSearcher.java +++ b/com.ibm.wala.shrike/src/com/ibm/wala/shrikeBT/shrikeCT/tools/ClassSearcher.java @@ -38,16 +38,16 @@ public class ClassSearcher { public static void main(String[] args) throws Exception { instrumenter = new OfflineInstrumenter(true); - Writer w = new BufferedWriter(new FileWriter("report", true)); + try (final Writer w = new BufferedWriter(new FileWriter("report", true))) { - instrumenter.parseStandardArgs(args); - instrumenter.beginTraversal(); - ClassInstrumenter ci; - while ((ci = instrumenter.nextClass()) != null) { - doClass(ci, w, instrumenter.getLastClassResourceName()); + instrumenter.parseStandardArgs(args); + instrumenter.beginTraversal(); + ClassInstrumenter ci; + while ((ci = instrumenter.nextClass()) != null) { + doClass(ci, w, instrumenter.getLastClassResourceName()); + } + instrumenter.close(); } - instrumenter.close(); - w.close(); System.out.println("Classes scanned: " + scanned); } diff --git a/com.ibm.wala.shrike/src/com/ibm/wala/shrikeBT/tools/OfflineInstrumenterBase.java b/com.ibm.wala.shrike/src/com/ibm/wala/shrikeBT/tools/OfflineInstrumenterBase.java index ef14203ef..148d096a5 100644 --- a/com.ibm.wala.shrike/src/com/ibm/wala/shrikeBT/tools/OfflineInstrumenterBase.java +++ b/com.ibm.wala.shrike/src/com/ibm/wala/shrikeBT/tools/OfflineInstrumenterBase.java @@ -231,13 +231,13 @@ public abstract class OfflineInstrumenterBase { * Add a JAR file containing source classes to instrument. */ final public void addInputJar(File f) throws IOException { - JarFile jf = new JarFile(f, false); - for (Enumeration e = jf.entries(); e.hasMoreElements();) { - JarEntry entry = e.nextElement(); - String name = entry.getName(); - inputs.add(new JarInput(f, name)); + try (final JarFile jf = new JarFile(f, false)) { + for (Enumeration e = jf.entries(); e.hasMoreElements();) { + JarEntry entry = e.nextElement(); + String name = entry.getName(); + inputs.add(new JarInput(f, name)); + } } - jf.close(); } /** @@ -389,14 +389,11 @@ public abstract class OfflineInstrumenterBase { if (ignoringInputs.get(inputIndex - 1) || !in.isClass()) { continue; } - BufferedInputStream s = new BufferedInputStream(in.open()); - try { + try (final BufferedInputStream s = new BufferedInputStream(in.open())) { Object r = makeClassFromStream(in.getInputName(), s); String name = getClassName(r); in.setClassName(name); return r; - } finally { - s.close(); } } } @@ -543,8 +540,7 @@ public abstract class OfflineInstrumenterBase { if (in instanceof JarInput) { JarInput jin = (JarInput) in; ZipEntry entry = jin.getEntry(); - InputStream s = jin.open(); - try { + try (final InputStream s = jin.open()) { ZipEntry newEntry = new ZipEntry(entry.getName()); newEntry.setComment(entry.getComment()); newEntry.setExtra(entry.getExtra()); @@ -552,8 +548,6 @@ public abstract class OfflineInstrumenterBase { putNextEntry(newEntry); copyStream(s, outputJar); outputJar.closeEntry(); - } finally { - s.close(); } } else { throw new Error("Unknown non-class input: " + in); @@ -561,8 +555,7 @@ public abstract class OfflineInstrumenterBase { } else { String name = in.getClassName(); if (name == null) { - BufferedInputStream s = new BufferedInputStream(in.open(), 65536); - try { + try (final BufferedInputStream s = new BufferedInputStream(in.open(), 65536)) { Object cl = makeClassFromStream(in.getInputName(), s); String entryName = toEntryName(getClassName(cl)); if (!entryNames.contains(entryName)) { @@ -572,21 +565,16 @@ public abstract class OfflineInstrumenterBase { clOut.flush(); outputJar.closeEntry(); } - } finally { - s.close(); } } else { String entryName = toEntryName(name); if (!entryNames.contains(entryName)) { - BufferedInputStream s = new BufferedInputStream(in.open()); - try { + try (final BufferedInputStream s = new BufferedInputStream(in.open())) { putNextEntry(new ZipEntry(entryName)); BufferedOutputStream clOut = new BufferedOutputStream(outputJar); copyStream(s, clOut); clOut.flush(); outputJar.closeEntry(); - } finally { - s.close(); } } } diff --git a/com.ibm.wala.util/src/com/ibm/wala/util/collections/Util.java b/com.ibm.wala.util/src/com/ibm/wala/util/collections/Util.java index 9c0d3b557..3d406beae 100644 --- a/com.ibm.wala.util/src/com/ibm/wala/util/collections/Util.java +++ b/com.ibm.wala.util/src/com/ibm/wala/util/collections/Util.java @@ -71,9 +71,9 @@ public class Util { } // create a memory buffer to which to dump the trace ByteArrayOutputStream traceDump = new ByteArrayOutputStream(); - PrintWriter w = new PrintWriter(traceDump); - thrown.printStackTrace(w); - w.close(); + try (final PrintWriter w = new PrintWriter(traceDump)) { + thrown.printStackTrace(w); + } return traceDump.toString(); } diff --git a/com.ibm.wala.util/src/com/ibm/wala/util/config/FileOfClasses.java b/com.ibm.wala.util/src/com/ibm/wala/util/config/FileOfClasses.java index 1b41971e7..748b9c02f 100644 --- a/com.ibm.wala.util/src/com/ibm/wala/util/config/FileOfClasses.java +++ b/com.ibm.wala.util/src/com/ibm/wala/util/config/FileOfClasses.java @@ -38,27 +38,26 @@ public class FileOfClasses extends SetOfClasses implements Serializable { if (input == null) { throw new IllegalArgumentException("null input"); } - BufferedReader is = new BufferedReader(new InputStreamReader(input)); - - StringBuffer regex = null; - String line; - while ((line = is.readLine()) != null) { - - if (line.startsWith("#")) continue; - - if (regex == null) { - regex = new StringBuffer("(" + line + ")"); - } else { - regex.append("|(" + line + ")"); + try (final BufferedReader is = new BufferedReader(new InputStreamReader(input))) { + + StringBuffer regex = null; + String line; + while ((line = is.readLine()) != null) { + + if (line.startsWith("#")) continue; + + if (regex == null) { + regex = new StringBuffer("(" + line + ")"); + } else { + regex.append("|(" + line + ")"); + } + } + + if (regex != null) { + this.regex = regex.toString(); + needsCompile = true; } } - - if (regex != null) { - this.regex = regex.toString(); - needsCompile = true; - } - - is.close(); } private void compile() { diff --git a/com.ibm.wala.util/src/com/ibm/wala/util/io/FileUtil.java b/com.ibm.wala.util/src/com/ibm/wala/util/io/FileUtil.java index 6b0873d6b..7a94219f0 100644 --- a/com.ibm.wala.util/src/com/ibm/wala/util/io/FileUtil.java +++ b/com.ibm.wala.util/src/com/ibm/wala/util/io/FileUtil.java @@ -84,27 +84,15 @@ public class FileUtil { if (destFileName == null) { throw new IllegalArgumentException("destFileName is null"); } - FileChannel src = null; - FileChannel dest = null; - try { - src = new FileInputStream(srcFileName).getChannel(); - dest = new FileOutputStream(destFileName).getChannel(); + try ( + final FileInputStream srcStream = new FileInputStream(srcFileName); + final FileOutputStream dstStream = new FileOutputStream(destFileName); + final FileChannel src = srcStream.getChannel(); + final FileChannel dest = dstStream.getChannel(); + ) { long n = src.size(); MappedByteBuffer buf = src.map(FileChannel.MapMode.READ_ONLY, 0, n); dest.write(buf); - } finally { - if (dest != null) { - try { - dest.close(); - } catch (IOException e1) { - } - } - if (src != null) { - try { - src.close(); - } catch (IOException e1) { - } - } } } @@ -175,16 +163,16 @@ public class FileUtil { if (s == null) { throw new IllegalArgumentException("null s"); } - ByteArrayOutputStream out = new ByteArrayOutputStream(); - byte[] b = new byte[1024]; - int n = s.read(b); - while (n != -1) { - out.write(b, 0, n); - n = s.read(b); + try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) { + byte[] b = new byte[1024]; + int n = s.read(b); + while (n != -1) { + out.write(b, 0, n); + n = s.read(b); + } + byte[] bb = out.toByteArray(); + return bb; } - byte[] bb = out.toByteArray(); - out.close(); - return bb; } /** @@ -195,9 +183,9 @@ public class FileUtil { * @throws IOException */ public static void writeFile(File f, String content) throws IOException { - final FileWriter fw = new FileWriter(f); - fw.append(content); - fw.close(); + try (final FileWriter fw = new FileWriter(f)) { + fw.append(content); + } } public static void recurseFiles(VoidFunction action, final Predicate filter, File top) { diff --git a/com.ibm.wala.util/src/com/ibm/wala/util/io/TemporaryFile.java b/com.ibm.wala.util/src/com/ibm/wala/util/io/TemporaryFile.java index 3f738b6ed..0bdf873b3 100644 --- a/com.ibm.wala.util/src/com/ibm/wala/util/io/TemporaryFile.java +++ b/com.ibm.wala.util/src/com/ibm/wala/util/io/TemporaryFile.java @@ -41,29 +41,28 @@ public class TemporaryFile { } public static File streamToFile(File F, InputStream... inputs) throws IOException { - FileOutputStream output = new FileOutputStream(F); - - int read; - byte[] buffer = new byte[ 1024 ]; - for(InputStream input : inputs) { - while ( (read = input.read(buffer)) != -1 ) { - output.write(buffer, 0, read); + try (final FileOutputStream output = new FileOutputStream(F)) { + + int read; + byte[] buffer = new byte[ 1024 ]; + for(InputStream input : inputs) { + while ( (read = input.read(buffer)) != -1 ) { + output.write(buffer, 0, read); + } + input.close(); } - input.close(); } - - output.close(); return F; } public static File stringToFile(File F, String... inputs) throws IOException { - FileOutputStream output = new FileOutputStream(F); - - for(String input : inputs) { - output.write(input.getBytes()); + try (final FileOutputStream output = new FileOutputStream(F)) { + + for(String input : inputs) { + output.write(input.getBytes()); + } } - - output.close(); + return F; } } diff --git a/com.ibm.wala.util/src/com/ibm/wala/util/processes/BasicLauncher.java b/com.ibm.wala.util/src/com/ibm/wala/util/processes/BasicLauncher.java index b86000f7a..561a09497 100644 --- a/com.ibm.wala.util/src/com/ibm/wala/util/processes/BasicLauncher.java +++ b/com.ibm.wala.util/src/com/ibm/wala/util/processes/BasicLauncher.java @@ -49,11 +49,9 @@ public class BasicLauncher extends Launcher { Thread d1 = isCaptureErr() ? captureStdErr(p) : drainStdErr(p); Thread d2 = isCaptureOutput() ? captureStdOut(p) : drainStdOut(p); if (getInput() != null) { - final BufferedOutputStream input = new BufferedOutputStream(p.getOutputStream()); - try { + try (final BufferedOutputStream input = new BufferedOutputStream(p.getOutputStream())) { input.write(getInput(), 0, getInput().length); input.flush(); - input.close(); } catch (IOException e) { e.printStackTrace(); throw new IOException("error priming stdin", e); diff --git a/com.ibm.wala.util/src/com/ibm/wala/viz/DotUtil.java b/com.ibm.wala.util/src/com/ibm/wala/viz/DotUtil.java index 4ca57b8c3..549cfdeef 100644 --- a/com.ibm.wala.util/src/com/ibm/wala/viz/DotUtil.java +++ b/com.ibm.wala.util/src/com/ibm/wala/viz/DotUtil.java @@ -168,9 +168,9 @@ public class DotUtil { } try { File f = new File(dotfile); - FileWriter fw = new FileWriter(f); - fw.write(dotStringBuffer.toString()); - fw.close(); + try (FileWriter fw = new FileWriter(f)) { + fw.write(dotStringBuffer.toString()); + } return f; } catch (Exception e) {