Community
Participate
Working Groups
When under Preferences -> Java -> Compiler -> Errors/Warning -> Potential programming problems, the option Potential resource leak is set to Warning or Error, then a warning or error is generated for methods which return an InputStream. Obviously, a method returning an InputStream is not going to want to close the InputStream. In the following sample, the error or warning is reported for the line "return is;" The method _using_ the InputStream is the one where a warning needs to be displayed. -- Sample Code -- /** * Returns an input stream from the specified properties file. * * @param source String the name of the properties file. * @return InputStream the input stream, or null. */ private static InputStream getInputStream(String source) { boolean logDebugEnabled = LOG.isTraceEnabled(); if (logDebugEnabled) { Object[][] params = {{"source", source}}; LOG.entering(PropertyUtils.class, "getInputStream", params); } InputStream is = null; try { URL url = null; // Try retrieving with the class loader. if (is == null) { try { url = PropertyUtils.class.getResource("//" + source); if (url != null) { is = url.openStream(); } } catch (Exception e) { } } // Try retrieving with the system classloader. if (is == null) { url = ClassLoader.getSystemClassLoader().getResource(source); if (url != null) { try { is = url.openStream(); } catch (IOException e) { } } } // Try looking in the file system. if (is == null) { try { is = new FileInputStream(source); } catch (IOException e) { } } // Return the input stream, if found, otherwise null. return is; } finally { if (logDebugEnabled) { LOG.exiting(is != null ? "OK" : "not found"); } } } -- Configuration Details -- Product: Eclipse 1.5.0.20120131-1544 (org.eclipse.epp.package.jee.product) Installed Features: org.eclipse.jdt 3.8.0.v20120525-1249-8-8nFqlFNOfwKDRGz-pXLdGxEM83
Thanks for the report. This is indeed a bug in the implementation. What I can see from quick tests: The warning goes away if you comment one of the lines "is = url.openStream();" The bug is probably related to the fact that the compiler sees multiple assignments to "is" and fails to see that at most one of these assignments binds an actual non-null value. A workaround would be to change the second (or all) "if (is == null) {" into: if (is != null) return is; This has the same behavior and makes the bogus warning go away.
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug. If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant. -- The automated Eclipse Genie.
Still relevant, let's see if we can fix this for 4.16 (time permitting).
.
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant. -- The automated Eclipse Genie.