Return-Path: X-Original-To: apmail-tomcat-dev-archive@www.apache.org Delivered-To: apmail-tomcat-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 91C8810C5A for ; Wed, 26 Mar 2014 16:00:43 +0000 (UTC) Received: (qmail 58064 invoked by uid 500); 26 Mar 2014 16:00:40 -0000 Delivered-To: apmail-tomcat-dev-archive@tomcat.apache.org Received: (qmail 57969 invoked by uid 500); 26 Mar 2014 16:00:39 -0000 Mailing-List: contact dev-help@tomcat.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Tomcat Developers List" Delivered-To: mailing list dev@tomcat.apache.org Received: (qmail 57956 invoked by uid 99); 26 Mar 2014 16:00:37 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 26 Mar 2014 16:00:37 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 26 Mar 2014 16:00:34 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 87BD92388AA6 for ; Wed, 26 Mar 2014 16:00:11 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1581900 - in /tomcat/trunk: ./ java/org/apache/catalina/startup/ java/org/apache/jasper/compiler/ java/org/apache/jasper/servlet/ java/org/apache/tomcat/util/scan/ Date: Wed, 26 Mar 2014 16:00:11 -0000 To: dev@tomcat.apache.org From: markt@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20140326160011.87BD92388AA6@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: markt Date: Wed Mar 26 16:00:10 2014 New Revision: 1581900 URL: http://svn.apache.org/r1581900 Log: More try-with-resources Modified: tomcat/trunk/TOMCAT-NEXT.txt tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java tomcat/trunk/java/org/apache/jasper/compiler/Compiler.java tomcat/trunk/java/org/apache/jasper/compiler/JDTCompiler.java tomcat/trunk/java/org/apache/jasper/compiler/JavacErrorDetail.java tomcat/trunk/java/org/apache/jasper/compiler/ParserController.java tomcat/trunk/java/org/apache/jasper/compiler/ServletWriter.java tomcat/trunk/java/org/apache/jasper/servlet/JspCServletContext.java tomcat/trunk/java/org/apache/jasper/servlet/TldScanner.java tomcat/trunk/java/org/apache/tomcat/util/scan/Jar.java Modified: tomcat/trunk/TOMCAT-NEXT.txt URL: http://svn.apache.org/viewvc/tomcat/trunk/TOMCAT-NEXT.txt?rev=1581900&r1=1581899&r2=1581900&view=diff ============================================================================== --- tomcat/trunk/TOMCAT-NEXT.txt (original) +++ tomcat/trunk/TOMCAT-NEXT.txt Wed Mar 26 16:00:10 2014 @@ -214,8 +214,8 @@ but possibly 7.1.x). - Use of try with resources - Started. - javax.* complete - - o.a.[catalina to el ] complete - - o.a.jasper in progress + - o.a.[catalina to jasper ] complete + - o.a.juli in progress - remainder TODO - Catching multiple exceptions - Started Modified: tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java?rev=1581900&r1=1581899&r2=1581900&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java (original) +++ tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java Wed Mar 26 16:00:10 2014 @@ -1655,21 +1655,21 @@ public class ContextConfig implements Li protected void processResourceJARs(Set fragments) { for (WebXml fragment : fragments) { URL url = fragment.getURL(); - Jar jar = null; try { if ("jar".equals(url.getProtocol())) { - jar = JarFactory.newInstance(url); - jar.nextEntry(); - String entryName = jar.getEntryName(); - while (entryName != null) { - if (entryName.startsWith("META-INF/resources/")) { - context.getResources().createWebResourceSet( - WebResourceRoot.ResourceSetType.RESOURCE_JAR, - "/", url, "/META-INF/resources"); - break; - } + try (Jar jar = JarFactory.newInstance(url)) { jar.nextEntry(); - entryName = jar.getEntryName(); + String entryName = jar.getEntryName(); + while (entryName != null) { + if (entryName.startsWith("META-INF/resources/")) { + context.getResources().createWebResourceSet( + WebResourceRoot.ResourceSetType.RESOURCE_JAR, + "/", url, "/META-INF/resources"); + break; + } + jar.nextEntry(); + entryName = jar.getEntryName(); + } } } else if ("file".equals(url.getProtocol())) { File file = new File(url.toURI()); @@ -1686,10 +1686,6 @@ public class ContextConfig implements Li } catch (URISyntaxException e) { log.error(sm.getString("contextConfig.resourceJarFail", url, context.getName())); - } finally { - if (jar != null) { - jar.close(); - } } } } @@ -1937,11 +1933,7 @@ public class ContextConfig implements Li protected void processAnnotationsJar(URL url, WebXml fragment, boolean handlesTypesOnly) { - Jar jar = null; - - try { - jar = JarFactory.newInstance(url); - + try (Jar jar = JarFactory.newInstance(url)) { jar.nextEntry(); String entryName = jar.getEntryName(); while (entryName != null) { @@ -1962,10 +1954,6 @@ public class ContextConfig implements Li } } catch (IOException e) { log.error(sm.getString("contextConfig.jarFile", url), e); - } finally { - if (jar != null) { - jar.close(); - } } } Modified: tomcat/trunk/java/org/apache/jasper/compiler/Compiler.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/jasper/compiler/Compiler.java?rev=1581900&r1=1581899&r2=1581900&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/jasper/compiler/Compiler.java (original) +++ tomcat/trunk/java/org/apache/jasper/compiler/Compiler.java Wed Mar 26 16:00:10 2014 @@ -173,7 +173,6 @@ public abstract class Compiler { ctxt.checkOutputDir(); String javaFileName = ctxt.getServletJavaFileName(); - ServletWriter writer = null; try { /* * The setting of isELIgnored changes the behaviour of the parser @@ -207,11 +206,10 @@ public abstract class Compiler { if (ctxt.isPrototypeMode()) { // generate prototype .java file for the tag file - writer = setupContextWriter(javaFileName); - Generator.generate(writer, this, pageNodes); - writer.close(); - writer = null; - return null; + try (ServletWriter writer = setupContextWriter(javaFileName)) { + Generator.generate(writer, this, pageNodes); + return null; + } } // Validate and process attributes - don't re-validate the @@ -248,10 +246,9 @@ public abstract class Compiler { ELFunctionMapper.map(pageNodes); // generate servlet .java file - writer = setupContextWriter(javaFileName); - Generator.generate(writer, this, pageNodes); - writer.close(); - writer = null; + try (ServletWriter writer = setupContextWriter(javaFileName)) { + Generator.generate(writer, this, pageNodes); + } // The writer is only used during the compile, dereference // it in the JspCompilationContext when done to allow it @@ -265,14 +262,6 @@ public abstract class Compiler { } } catch (Exception e) { - if (writer != null) { - try { - writer.close(); - writer = null; - } catch (Exception e1) { - // do nothing - } - } // Remove the generated .java file File file = new File(javaFileName); if (file.exists()) { @@ -283,14 +272,6 @@ public abstract class Compiler { } } throw e; - } finally { - if (writer != null) { - try { - writer.close(); - } catch (Exception e2) { - // do nothing - } - } } // JSR45 Support Modified: tomcat/trunk/java/org/apache/jasper/compiler/JDTCompiler.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/jasper/compiler/JDTCompiler.java?rev=1581900&r1=1581899&r2=1581900&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/jasper/compiler/JDTCompiler.java (original) +++ tomcat/trunk/java/org/apache/jasper/compiler/JDTCompiler.java Wed Mar 26 16:00:10 2014 @@ -105,14 +105,10 @@ public class JDTCompiler extends org.apa @Override public char[] getContents() { char[] result = null; - FileInputStream is = null; - InputStreamReader isr = null; - Reader reader = null; - try { - is = new FileInputStream(sourceFile); - isr = new InputStreamReader(is, - ctxt.getOptions().getJavaEncoding()); - reader = new BufferedReader(isr); + try (FileInputStream is = new FileInputStream(sourceFile); + InputStreamReader isr = new InputStreamReader( + is, ctxt.getOptions().getJavaEncoding()); + Reader reader = new BufferedReader(isr)) { char[] chars = new char[8192]; StringBuilder buf = new StringBuilder(); int count; @@ -124,22 +120,6 @@ public class JDTCompiler extends org.apa buf.getChars(0, result.length, result, 0); } catch (IOException e) { log.error("Compilation error", e); - } finally { - if (reader != null) { - try { - reader.close(); - } catch (IOException ioe) {/*Ignore*/} - } - if (isr != null) { - try { - isr.close(); - } catch (IOException ioe) {/*Ignore*/} - } - if (is != null) { - try { - is.close(); - } catch (IOException exc) {/*Ignore*/} - } } return result; } @@ -204,17 +184,17 @@ public class JDTCompiler extends org.apa private NameEnvironmentAnswer findType(String className) { - InputStream is = null; - try { - if (className.equals(targetClassName)) { - ICompilationUnit compilationUnit = - new CompilationUnit(sourceFile, className); - return - new NameEnvironmentAnswer(compilationUnit, null); - } - String resourceName = + if (className.equals(targetClassName)) { + ICompilationUnit compilationUnit = + new CompilationUnit(sourceFile, className); + return + new NameEnvironmentAnswer(compilationUnit, null); + } + + String resourceName = className.replace('.', '/') + ".class"; - is = classLoader.getResourceAsStream(resourceName); + + try (InputStream is = classLoader.getResourceAsStream(resourceName)) { if (is != null) { byte[] classBytes; byte[] buf = new byte[8192]; @@ -237,14 +217,6 @@ public class JDTCompiler extends org.apa log.error("Compilation error", exc); } catch (org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException exc) { log.error("Compilation error", exc); - } finally { - if (is != null) { - try { - is.close(); - } catch (IOException exc) { - // Ignore - } - } } return null; } Modified: tomcat/trunk/java/org/apache/jasper/compiler/JavacErrorDetail.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/jasper/compiler/JavacErrorDetail.java?rev=1581900&r1=1581899&r2=1581900&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/jasper/compiler/JavacErrorDetail.java (original) +++ tomcat/trunk/java/org/apache/jasper/compiler/JavacErrorDetail.java Wed Mar 26 16:00:10 2014 @@ -86,71 +86,52 @@ public class JavacErrorDetail { // be modified (corrected) during the execution of this method if (jspBeginLineNum > 0 && ctxt != null) { - InputStream is = null; - FileInputStream fis = null; - - try { + try (InputStream is = ctxt.getResourceAsStream(jspFileName)) { // Read both files in, so we can inspect them - is = ctxt.getResourceAsStream(jspFileName); String[] jspLines = readFile(is); - fis = new FileInputStream(ctxt.getServletJavaFileName()); - String[] javaLines = readFile(fis); + try (FileInputStream fis = new FileInputStream(ctxt.getServletJavaFileName())) { + String[] javaLines = readFile(fis); - if (jspLines.length < jspBeginLineNum) { - // Avoid ArrayIndexOutOfBoundsException - // Probably bug 48498 but could be some other cause - jspExtract = Localizer.getMessage("jsp.error.bug48498"); - return; - } + if (jspLines.length < jspBeginLineNum) { + // Avoid ArrayIndexOutOfBoundsException + // Probably bug 48498 but could be some other cause + jspExtract = Localizer.getMessage("jsp.error.bug48498"); + return; + } - // If the line contains the opening of a multi-line scriptlet - // block, then the JSP line number we got back is probably - // faulty. Scan forward to match the java line... - if (jspLines[jspBeginLineNum-1].lastIndexOf("<%") > - jspLines[jspBeginLineNum-1].lastIndexOf("%>")) { - String javaLine = javaLines[javaLineNum-1].trim(); - - for (int i=jspBeginLineNum-1; i + jspLines[jspBeginLineNum-1].lastIndexOf("%>")) { + String javaLine = javaLines[javaLineNum-1].trim(); + + for (int i=jspBeginLineNum-1; i