Tue, 27 May 2014 22:26:53 +0100
8042741: Java 8 compiler throws NullPointerException depending location in source file
Reviewed-by: vromero, jlahoda
1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Flow.java Wed May 28 21:47:30 2014 +0100 1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Flow.java Tue May 27 22:26:53 2014 +0100 1.3 @@ -232,7 +232,8 @@ 1.4 } 1.5 } 1.6 1.7 - public List<Type> analyzeLambdaThrownTypes(Env<AttrContext> env, JCLambda that, TreeMaker make) { 1.8 + public List<Type> analyzeLambdaThrownTypes(final Env<AttrContext> env, 1.9 + JCLambda that, TreeMaker make) { 1.10 //we need to disable diagnostics temporarily; the problem is that if 1.11 //a lambda expression contains e.g. an unreachable statement, an error 1.12 //message will be reported and will cause compilation to skip the flow analyis 1.13 @@ -240,7 +241,13 @@ 1.14 //related errors, which will allow for more errors to be detected 1.15 Log.DiagnosticHandler diagHandler = new Log.DiscardDiagnosticHandler(log); 1.16 try { 1.17 - new AssignAnalyzer(log, syms, lint, names, enforceThisDotInit).analyzeTree(env); 1.18 + new AssignAnalyzer(log, syms, lint, names, enforceThisDotInit) { 1.19 + @Override 1.20 + protected boolean trackable(VarSymbol sym) { 1.21 + return !env.info.scope.includes(sym) && 1.22 + sym.owner.kind == MTH; 1.23 + } 1.24 + }.analyzeTree(env); 1.25 LambdaFlowAnalyzer flowAnalyzer = new LambdaFlowAnalyzer(); 1.26 flowAnalyzer.analyzeTree(env, that, make); 1.27 return flowAnalyzer.inferredThrownTypes;
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/flow/T8042741/A.java Tue May 27 22:26:53 2014 +0100 2.3 @@ -0,0 +1,37 @@ 2.4 +/* 2.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 + * or visit www.oracle.com if you need additional information or have any 2.24 + * questions. 2.25 + */ 2.26 + 2.27 +// str must be at absolute position greater than that of lambda 2.28 +// expression in PositionTest.java 2.29 +// padding..........................................................padding 2.30 +// padding..........................................................padding 2.31 +// padding..........................................................padding 2.32 +// padding..........................................................padding 2.33 +// padding..........................................................padding 2.34 + 2.35 +public class A { 2.36 + public final String str; 2.37 + { 2.38 + str = ""; 2.39 + } 2.40 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/flow/T8042741/PositionTest.java Tue May 27 22:26:53 2014 +0100 3.3 @@ -0,0 +1,62 @@ 3.4 +/* 3.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3.23 + * or visit www.oracle.com if you need additional information or have any 3.24 + * questions. 3.25 + */ 3.26 + 3.27 +/* @test 3.28 + * @bug 8042741 3.29 + * @summary Java 8 compiler throws NullPointerException depending location in source file 3.30 + * @compile A.java PositionTest.java 3.31 + */ 3.32 + 3.33 +public class PositionTest extends A { 3.34 + <E extends Exception> void test(SAM<E> r) throws E { 3.35 + test(() -> { System.err.println(str); }); 3.36 + } 3.37 + interface SAM<E extends Exception> { 3.38 + public void run() throws E; 3.39 + } 3.40 + void f() { 3.41 + try { 3.42 + test(() -> { 3.43 + test(() -> { 3.44 + try { 3.45 + test(() -> { System.err.println(str); }); 3.46 + System.err.println(str); 3.47 + } catch (Exception e) {} 3.48 + System.err.println(str); 3.49 + }); 3.50 + System.err.println(str); 3.51 + }); 3.52 + } catch (Exception e) { } 3.53 + } 3.54 + void g() throws Exception { 3.55 + test(() -> { 3.56 + try { 3.57 + try { 3.58 + test(() -> { System.err.println(str); }); 3.59 + } catch (Exception e) {} 3.60 + System.err.println(str); 3.61 + } catch (Exception e) {} 3.62 + System.err.println(str); 3.63 + }); 3.64 + } 3.65 +}