Thu, 04 Nov 2010 12:57:48 +0000
6993963: Project Coin: Use precise exception analysis for effectively final catch parameters
Summary: More precise rethrow analysis should be extended to effectively-final exception parameters. Multicatch parameters should be made implicitly final.
Reviewed-by: jjg, darcy
1.1 --- a/src/share/classes/com/sun/tools/javac/code/Flags.java Tue Nov 02 12:01:35 2010 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Flags.java Thu Nov 04 12:57:48 2010 +0000 1.3 @@ -247,6 +247,11 @@ 1.4 */ 1.5 public static final long OVERRIDE_BRIDGE = 1L<<41; 1.6 1.7 + /** 1.8 + * Flag that marks an 'effectively final' local variable 1.9 + */ 1.10 + public static final long EFFECTIVELY_FINAL = 1L<<42; 1.11 + 1.12 /** Modifier masks. 1.13 */ 1.14 public static final int
2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java Tue Nov 02 12:01:35 2010 +0000 2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java Thu Nov 04 12:57:48 2010 +0000 2.3 @@ -256,6 +256,8 @@ 2.4 } else { 2.5 log.error(pos, "cant.assign.val.to.final.var", v); 2.6 } 2.7 + } else if ((v.flags() & EFFECTIVELY_FINAL) != 0) { 2.8 + v.flags_field &= ~EFFECTIVELY_FINAL; 2.9 } 2.10 } 2.11 2.12 @@ -799,6 +801,7 @@ 2.13 memberEnter.memberEnter(tree, env); 2.14 annotate.flush(); 2.15 } 2.16 + tree.sym.flags_field |= EFFECTIVELY_FINAL; 2.17 } 2.18 2.19 VarSymbol v = tree.sym; 2.20 @@ -1061,11 +1064,8 @@ 2.21 localEnv.dup(c, localEnv.info.dup(localEnv.info.scope.dup())); 2.22 Type ctype = attribStat(c.param, catchEnv); 2.23 if (TreeInfo.isMultiCatch(c)) { 2.24 - //check that multi-catch parameter is marked as final 2.25 - if ((c.param.sym.flags() & FINAL) == 0) { 2.26 - log.error(c.param.pos(), "multicatch.param.must.be.final", c.param.sym); 2.27 - } 2.28 - c.param.sym.flags_field = c.param.sym.flags() | DISJUNCTION; 2.29 + //multi-catch parameter is implicitly marked as final 2.30 + c.param.sym.flags_field |= FINAL | DISJUNCTION; 2.31 } 2.32 if (c.param.sym.kind == Kinds.VAR) { 2.33 c.param.sym.setData(ElementKind.EXCEPTION_PARAMETER);
3.1 --- a/src/share/classes/com/sun/tools/javac/comp/Flow.java Tue Nov 02 12:01:35 2010 +0000 3.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Flow.java Thu Nov 04 12:57:48 2010 +0000 3.3 @@ -226,7 +226,7 @@ 3.4 */ 3.5 Bits uninits; 3.6 3.7 - HashMap<Symbol, List<Type>> multicatchTypes; 3.8 + HashMap<Symbol, List<Type>> preciseRethrowTypes; 3.9 3.10 /** The set of variables that are definitely unassigned everywhere 3.11 * in current try block. This variable is maintained lazily; it is 3.12 @@ -332,7 +332,7 @@ 3.13 if (!chk.isUnchecked(tree.pos(), exc)) { 3.14 if (!chk.isHandled(exc, caught)) 3.15 pendingExits.append(new PendingExit(tree, exc)); 3.16 - thrown = chk.incl(exc, thrown); 3.17 + thrown = chk.incl(exc, thrown); 3.18 } 3.19 } 3.20 3.21 @@ -1077,12 +1077,12 @@ 3.22 scan(param); 3.23 inits.incl(param.sym.adr); 3.24 uninits.excl(param.sym.adr); 3.25 - multicatchTypes.put(param.sym, chk.intersect(ctypes, rethrownTypes)); 3.26 + preciseRethrowTypes.put(param.sym, chk.intersect(ctypes, rethrownTypes)); 3.27 scanStat(l.head.body); 3.28 initsEnd.andSet(inits); 3.29 uninitsEnd.andSet(uninits); 3.30 nextadr = nextadrCatch; 3.31 - multicatchTypes.remove(param.sym); 3.32 + preciseRethrowTypes.remove(param.sym); 3.33 aliveEnd |= alive; 3.34 } 3.35 if (tree.finalizer != null) { 3.36 @@ -1215,10 +1215,10 @@ 3.37 Symbol sym = TreeInfo.symbol(tree.expr); 3.38 if (sym != null && 3.39 sym.kind == VAR && 3.40 - (sym.flags() & FINAL) != 0 && 3.41 - multicatchTypes.get(sym) != null && 3.42 + (sym.flags() & (FINAL | EFFECTIVELY_FINAL)) != 0 && 3.43 + preciseRethrowTypes.get(sym) != null && 3.44 allowRethrowAnalysis) { 3.45 - for (Type t : multicatchTypes.get(sym)) { 3.46 + for (Type t : preciseRethrowTypes.get(sym)) { 3.47 markThrown(tree, t); 3.48 } 3.49 } 3.50 @@ -1422,7 +1422,7 @@ 3.51 firstadr = 0; 3.52 nextadr = 0; 3.53 pendingExits = new ListBuffer<PendingExit>(); 3.54 - multicatchTypes = new HashMap<Symbol, List<Type>>(); 3.55 + preciseRethrowTypes = new HashMap<Symbol, List<Type>>(); 3.56 alive = true; 3.57 this.thrown = this.caught = null; 3.58 this.classDef = null;
4.1 --- a/src/share/classes/com/sun/tools/javac/resources/compiler.properties Tue Nov 02 12:01:35 2010 +0000 4.2 +++ b/src/share/classes/com/sun/tools/javac/resources/compiler.properties Thu Nov 04 12:57:48 2010 +0000 4.3 @@ -187,8 +187,6 @@ 4.4 automatic resource {0} may not be assigned 4.5 compiler.err.multicatch.parameter.may.not.be.assigned=\ 4.6 multi-catch parameter {0} may not be assigned 4.7 -compiler.err.multicatch.param.must.be.final=\ 4.8 - multi-catch parameter {0} must be final 4.9 compiler.err.finally.without.try=\ 4.10 ''finally'' without ''try'' 4.11 compiler.err.foreach.not.applicable.to.type=\
5.1 --- a/test/tools/javac/diags/examples/MulticatchMustBeFinal.java Tue Nov 02 12:01:35 2010 +0000 5.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 5.3 @@ -1,38 +0,0 @@ 5.4 -/* 5.5 - * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 5.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5.7 - * 5.8 - * This code is free software; you can redistribute it and/or modify it 5.9 - * under the terms of the GNU General Public License version 2 only, as 5.10 - * published by the Free Software Foundation. 5.11 - * 5.12 - * This code is distributed in the hope that it will be useful, but WITHOUT 5.13 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 5.14 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 5.15 - * version 2 for more details (a copy is included in the LICENSE file that 5.16 - * accompanied this code). 5.17 - * 5.18 - * You should have received a copy of the GNU General Public License version 5.19 - * 2 along with this work; if not, write to the Free Software Foundation, 5.20 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 5.21 - * 5.22 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 5.23 - * or visit www.oracle.com if you need additional information or have any 5.24 - * questions. 5.25 - */ 5.26 - 5.27 -// key: compiler.err.multicatch.param.must.be.final 5.28 - 5.29 -class MulticatchMustBeFinal { 5.30 - void e1() throws NullPointerException { } 5.31 - void e2() throws IllegalArgumentException { } 5.32 - 5.33 - void m() { 5.34 - try { 5.35 - e1(); 5.36 - e2(); 5.37 - } catch (NullPointerException | IllegalArgumentException e) { 5.38 - e.printStackTrace(); 5.39 - } 5.40 - } 5.41 -}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/test/tools/javac/multicatch/Neg01eff_final.java Thu Nov 04 12:57:48 2010 +0000 6.3 @@ -0,0 +1,28 @@ 6.4 +/* 6.5 + * @test /nodynamiccopyright/ 6.6 + * @bug 6943289 6.7 + * 6.8 + * @summary Project Coin: Improved Exception Handling for Java (aka 'multicatch') 6.9 + * @author darcy 6.10 + * @compile/fail/ref=Neg01eff_final.out -XDrawDiagnostics Neg01eff_final.java 6.11 + * @compile -source 6 -XDrawDiagnostics Neg01eff_final.java 6.12 + * 6.13 + */ 6.14 + 6.15 +class Neg01eff_final { 6.16 + static class A extends Exception {} 6.17 + static class B1 extends A {} 6.18 + static class B2 extends A {} 6.19 + 6.20 + class Test { 6.21 + void m() throws A { 6.22 + try { 6.23 + throw new B1(); 6.24 + } catch (A ex1) { 6.25 + try { 6.26 + throw ex1; // used to throw A, now throws B1! 6.27 + } catch (B2 ex2) { }//unreachable 6.28 + } 6.29 + } 6.30 + } 6.31 +}
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/test/tools/javac/multicatch/Neg01eff_final.out Thu Nov 04 12:57:48 2010 +0000 7.3 @@ -0,0 +1,2 @@ 7.4 +Neg01eff_final.java:24:19: compiler.err.except.never.thrown.in.try: Neg01eff_final.B2 7.5 +1 error
8.1 --- a/test/tools/javac/multicatch/Neg02.java Tue Nov 02 12:01:35 2010 +0000 8.2 +++ b/test/tools/javac/multicatch/Neg02.java Thu Nov 04 12:57:48 2010 +0000 8.3 @@ -20,6 +20,8 @@ 8.4 else { 8.5 throw new B(); 8.6 } 8.7 - } catch (A | B ex) { } 8.8 + } catch (final A | B ex) { 8.9 + ex = new B(); 8.10 + } 8.11 } 8.12 }
9.1 --- a/test/tools/javac/multicatch/Neg02.out Tue Nov 02 12:01:35 2010 +0000 9.2 +++ b/test/tools/javac/multicatch/Neg02.out Thu Nov 04 12:57:48 2010 +0000 9.3 @@ -1,2 +1,2 @@ 9.4 -Neg02.java:23:24: compiler.err.multicatch.param.must.be.final: ex 9.5 +Neg02.java:24:13: compiler.err.multicatch.parameter.may.not.be.assigned: ex 9.6 1 error
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 10.2 +++ b/test/tools/javac/multicatch/Neg02eff_final.java Thu Nov 04 12:57:48 2010 +0000 10.3 @@ -0,0 +1,27 @@ 10.4 +/* 10.5 + * @test /nodynamiccopyright/ 10.6 + * @bug 6943289 6993963 10.7 + * 10.8 + * @summary Project Coin: Improved Exception Handling for Java (aka 'multicatch') 10.9 + * @author mcimadamore 10.10 + * @compile/fail/ref=Neg02eff_final.out -XDrawDiagnostics Neg02eff_final.java 10.11 + * 10.12 + */ 10.13 + 10.14 +class Neg02eff_final { 10.15 + static class A extends Exception {} 10.16 + static class B extends Exception {} 10.17 + 10.18 + void m() { 10.19 + try { 10.20 + if (true) { 10.21 + throw new A(); 10.22 + } 10.23 + else { 10.24 + throw new B(); 10.25 + } 10.26 + } catch (A | B ex) { 10.27 + ex = new B(); 10.28 + } 10.29 + } 10.30 +}
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 11.2 +++ b/test/tools/javac/multicatch/Neg02eff_final.out Thu Nov 04 12:57:48 2010 +0000 11.3 @@ -0,0 +1,2 @@ 11.4 +Neg02eff_final.java:24:13: compiler.err.multicatch.parameter.may.not.be.assigned: ex 11.5 +1 error
12.1 --- a/test/tools/javac/multicatch/Neg03.java Tue Nov 02 12:01:35 2010 +0000 12.2 +++ b/test/tools/javac/multicatch/Neg03.java Thu Nov 04 12:57:48 2010 +0000 12.3 @@ -9,19 +9,22 @@ 12.4 */ 12.5 12.6 class Neg03 { 12.7 - static class A extends Exception {} 12.8 - static class B extends Exception {} 12.9 12.10 - void m() { 12.11 + static class A extends Exception { public void m() {}; public Object f;} 12.12 + static class B1 extends A {} 12.13 + static class B2 extends A {} 12.14 + 12.15 + void m() throws B1, B2 { 12.16 try { 12.17 if (true) { 12.18 - throw new A(); 12.19 + throw new B1(); 12.20 } 12.21 else { 12.22 - throw new B(); 12.23 + throw new B2(); 12.24 } 12.25 - } catch (final A | B ex) { 12.26 - ex = new B(); 12.27 + } catch (Exception ex) { 12.28 + ex = new B2(); //effectively final analysis disabled! 12.29 + throw ex; 12.30 } 12.31 } 12.32 }
13.1 --- a/test/tools/javac/multicatch/Neg03.out Tue Nov 02 12:01:35 2010 +0000 13.2 +++ b/test/tools/javac/multicatch/Neg03.out Thu Nov 04 12:57:48 2010 +0000 13.3 @@ -1,2 +1,2 @@ 13.4 -Neg03.java:24:13: compiler.err.multicatch.parameter.may.not.be.assigned: ex 13.5 +Neg03.java:27:13: compiler.err.unreported.exception.need.to.catch.or.throw: java.lang.Exception 13.6 1 error
14.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 14.2 +++ b/test/tools/javac/multicatch/Neg04eff_final.java Thu Nov 04 12:57:48 2010 +0000 14.3 @@ -0,0 +1,31 @@ 14.4 +/* 14.5 + * @test /nodynamiccopyright/ 14.6 + * @bug 6943289 14.7 + * 14.8 + * @summary Project Coin: Improved Exception Handling for Java (aka 'multicatch') 14.9 + * @author mcimadamore 14.10 + * @compile/fail/ref=Neg04eff_final.out -XDrawDiagnostics Neg04eff_final.java 14.11 + * 14.12 + */ 14.13 + 14.14 +class Neg04eff_final { 14.15 + static class A extends Exception {} 14.16 + static class B extends Exception {} 14.17 + 14.18 + void test() throws B { 14.19 + try { 14.20 + if (true) { 14.21 + throw new A(); 14.22 + } else if (false) { 14.23 + throw new B(); 14.24 + } else { 14.25 + throw (Throwable)new Exception(); 14.26 + } 14.27 + } 14.28 + catch (A e) {} 14.29 + catch (Exception e) { 14.30 + throw e; 14.31 + } 14.32 + catch (Throwable t) {} 14.33 + } 14.34 +}
15.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 15.2 +++ b/test/tools/javac/multicatch/Neg04eff_final.out Thu Nov 04 12:57:48 2010 +0000 15.3 @@ -0,0 +1,2 @@ 15.4 +Neg04eff_final.java:27:13: compiler.err.unreported.exception.need.to.catch.or.throw: java.lang.Exception 15.5 +1 error
16.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 16.2 +++ b/test/tools/javac/multicatch/Neg05.java Thu Nov 04 12:57:48 2010 +0000 16.3 @@ -0,0 +1,33 @@ 16.4 +/* 16.5 + * @test /nodynamiccopyright/ 16.6 + * @bug 6943289 16.7 + * 16.8 + * @summary Project Coin: Improved Exception Handling for Java (aka 'multicatch') 16.9 + * @author mcimadamore 16.10 + * @compile/fail/ref=Neg05.out -XDrawDiagnostics Neg05.java 16.11 + * 16.12 + */ 16.13 + 16.14 +class Neg02 { 16.15 + 16.16 + static class Foo<X> { 16.17 + Foo(X x) {} 16.18 + } 16.19 + 16.20 + static interface Base<X> {} 16.21 + static class A extends Exception implements Base<String> {} 16.22 + static class B extends Exception implements Base<Integer> {} 16.23 + 16.24 + void m() { 16.25 + try { 16.26 + if (true) { 16.27 + throw new A(); 16.28 + } 16.29 + else { 16.30 + throw new B(); 16.31 + } 16.32 + } catch (A | B ex) { 16.33 + Foo<?> f = new Foo<>(ex); 16.34 + } 16.35 + } 16.36 +}
17.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 17.2 +++ b/test/tools/javac/multicatch/Neg05.out Thu Nov 04 12:57:48 2010 +0000 17.3 @@ -0,0 +1,2 @@ 17.4 +Neg05.java:30:31: compiler.err.cant.apply.diamond.1: (compiler.misc.diamond: Neg02.Foo), (compiler.misc.diamond.invalid.arg: java.lang.Exception&Neg02.Base<? extends java.lang.Object&java.io.Serializable&java.lang.Comparable<? extends java.lang.Object&java.io.Serializable&java.lang.Comparable<?>>>, (compiler.misc.diamond: Neg02.Foo)) 17.5 +1 error
18.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 18.2 +++ b/test/tools/javac/multicatch/Pos06.java Thu Nov 04 12:57:48 2010 +0000 18.3 @@ -0,0 +1,27 @@ 18.4 +/* 18.5 + * @test /nodynamiccopyright/ 18.6 + * @bug 6993963 18.7 + * 18.8 + * @summary Project Coin: Use precise exception analysis for effectively final catch parameters 18.9 + * @author mcimadamore 18.10 + * @compile Pos06.java 18.11 + * 18.12 + */ 18.13 + 18.14 +class Pos06 { 18.15 + static class A extends Exception {} 18.16 + static class B extends Exception {} 18.17 + 18.18 + void m() { 18.19 + try { 18.20 + if (true) { 18.21 + throw new A(); 18.22 + } 18.23 + else { 18.24 + throw new B(); 18.25 + } 18.26 + } catch (A | B ex) { 18.27 + System.out.println(ex); 18.28 + } 18.29 + } 18.30 +}
19.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 19.2 +++ b/test/tools/javac/multicatch/Pos07.java Thu Nov 04 12:57:48 2010 +0000 19.3 @@ -0,0 +1,49 @@ 19.4 +/* 19.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 19.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 19.7 + * 19.8 + * This code is free software; you can redistribute it and/or modify it 19.9 + * under the terms of the GNU General Public License version 2 only, as 19.10 + * published by the Free Software Foundation. 19.11 + * 19.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 19.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 19.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 19.15 + * version 2 for more details (a copy is included in the LICENSE file that 19.16 + * accompanied this code). 19.17 + * 19.18 + * You should have received a copy of the GNU General Public License version 19.19 + * 2 along with this work; if not, write to the Free Software Foundation, 19.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19.21 + * 19.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19.23 + * or visit www.oracle.com if you need additional information or have any 19.24 + * questions. 19.25 + */ 19.26 + 19.27 +/* 19.28 + * @test 19.29 + * @bug 6993963 19.30 + * @summary Project Coin: Use precise exception analysis for effectively final catch parameters 19.31 + * @compile Pos07.java 19.32 + */ 19.33 + 19.34 +class Pos07 { 19.35 + 19.36 + static class A extends Exception { public void m() {}; public Object f;} 19.37 + static class B1 extends A {} 19.38 + static class B2 extends A {} 19.39 + 19.40 + void m() throws B1, B2 { 19.41 + try { 19.42 + if (true) { 19.43 + throw new B1(); 19.44 + } 19.45 + else { 19.46 + throw new B2(); 19.47 + } 19.48 + } catch (Exception ex) { //effectively final analysis 19.49 + throw ex; 19.50 + } 19.51 + } 19.52 +}
20.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 20.2 +++ b/test/tools/javac/multicatch/model/Check.java Thu Nov 04 12:57:48 2010 +0000 20.3 @@ -0,0 +1,27 @@ 20.4 +/* 20.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 20.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 20.7 + * 20.8 + * This code is free software; you can redistribute it and/or modify it 20.9 + * under the terms of the GNU General Public License version 2 only, as 20.10 + * published by the Free Software Foundation. 20.11 + * 20.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 20.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 20.15 + * version 2 for more details (a copy is included in the LICENSE file that 20.16 + * accompanied this code). 20.17 + * 20.18 + * You should have received a copy of the GNU General Public License version 20.19 + * 2 along with this work; if not, write to the Free Software Foundation, 20.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20.21 + * 20.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20.23 + * or visit www.oracle.com if you need additional information or have any 20.24 + * questions. 20.25 + */ 20.26 + 20.27 +/** 20.28 + * Annotation used by ModelChecker to mark the class whose model is to be checked 20.29 + */ 20.30 +@interface Check {}
21.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 21.2 +++ b/test/tools/javac/multicatch/model/Member.java Thu Nov 04 12:57:48 2010 +0000 21.3 @@ -0,0 +1,31 @@ 21.4 +/* 21.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 21.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 21.7 + * 21.8 + * This code is free software; you can redistribute it and/or modify it 21.9 + * under the terms of the GNU General Public License version 2 only, as 21.10 + * published by the Free Software Foundation. 21.11 + * 21.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 21.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 21.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 21.15 + * version 2 for more details (a copy is included in the LICENSE file that 21.16 + * accompanied this code). 21.17 + * 21.18 + * You should have received a copy of the GNU General Public License version 21.19 + * 2 along with this work; if not, write to the Free Software Foundation, 21.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21.21 + * 21.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21.23 + * or visit www.oracle.com if you need additional information or have any 21.24 + * questions. 21.25 + */ 21.26 + 21.27 +import javax.lang.model.element.ElementKind; 21.28 + 21.29 +/** 21.30 + * Annotation used by ModelChecker to mark a member that is to be checked 21.31 + */ 21.32 +@interface Member { 21.33 + ElementKind value(); 21.34 +}
22.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 22.2 +++ b/test/tools/javac/multicatch/model/Model01.java Thu Nov 04 12:57:48 2010 +0000 22.3 @@ -0,0 +1,48 @@ 22.4 +/* 22.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 22.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 22.7 + * 22.8 + * This code is free software; you can redistribute it and/or modify it 22.9 + * under the terms of the GNU General Public License version 2 only, as 22.10 + * published by the Free Software Foundation. 22.11 + * 22.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 22.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 22.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 22.15 + * version 2 for more details (a copy is included in the LICENSE file that 22.16 + * accompanied this code). 22.17 + * 22.18 + * You should have received a copy of the GNU General Public License version 22.19 + * 2 along with this work; if not, write to the Free Software Foundation, 22.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 22.21 + * 22.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22.23 + * or visit www.oracle.com if you need additional information or have any 22.24 + * questions. 22.25 + */ 22.26 + 22.27 +import javax.lang.model.element.ElementKind; 22.28 + 22.29 +@Check 22.30 +class Test { 22.31 + 22.32 + class A extends Exception { 22.33 + @Member(ElementKind.METHOD) 22.34 + public void m() {}; 22.35 + @Member(ElementKind.FIELD) 22.36 + public Object f; 22.37 + } 22.38 + 22.39 + class B1 extends A {} 22.40 + class B2 extends A {} 22.41 + 22.42 + void test(){ 22.43 + try { 22.44 + if (true) 22.45 + throw new B1(); 22.46 + else 22.47 + throw new B2(); 22.48 + } 22.49 + catch(B1 | B2 ex) { } 22.50 + } 22.51 +}
23.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 23.2 +++ b/test/tools/javac/multicatch/model/ModelChecker.java Thu Nov 04 12:57:48 2010 +0000 23.3 @@ -0,0 +1,95 @@ 23.4 +/* 23.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 23.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 23.7 + * 23.8 + * This code is free software; you can redistribute it and/or modify it 23.9 + * under the terms of the GNU General Public License version 2 only, as 23.10 + * published by the Free Software Foundation. 23.11 + * 23.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 23.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 23.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 23.15 + * version 2 for more details (a copy is included in the LICENSE file that 23.16 + * accompanied this code). 23.17 + * 23.18 + * You should have received a copy of the GNU General Public License version 23.19 + * 2 along with this work; if not, write to the Free Software Foundation, 23.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 23.21 + * 23.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23.23 + * or visit www.oracle.com if you need additional information or have any 23.24 + * questions. 23.25 + */ 23.26 + 23.27 +/* 23.28 + * @test 23.29 + * @bug 6993963 23.30 + * @summary Project Coin: Use precise exception analysis for effectively final catch parameters 23.31 + * @library ../../lib 23.32 + * @build JavacTestingAbstractProcessor ModelChecker 23.33 + * @compile -processor ModelChecker Model01.java 23.34 + */ 23.35 + 23.36 +import com.sun.source.tree.VariableTree; 23.37 +import com.sun.source.util.TreePathScanner; 23.38 +import com.sun.source.util.Trees; 23.39 +import com.sun.source.util.TreePath; 23.40 + 23.41 +import java.util.Set; 23.42 +import javax.annotation.processing.RoundEnvironment; 23.43 +import javax.annotation.processing.SupportedAnnotationTypes; 23.44 +import javax.lang.model.element.Element; 23.45 +import javax.lang.model.element.ElementKind; 23.46 +import javax.lang.model.element.TypeElement; 23.47 + 23.48 +@SupportedAnnotationTypes("Check") 23.49 +public class ModelChecker extends JavacTestingAbstractProcessor { 23.50 + 23.51 + @Override 23.52 + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 23.53 + if (roundEnv.processingOver()) 23.54 + return true; 23.55 + 23.56 + Trees trees = Trees.instance(processingEnv); 23.57 + 23.58 + TypeElement testAnno = elements.getTypeElement("Check"); 23.59 + for (Element elem: roundEnv.getElementsAnnotatedWith(testAnno)) { 23.60 + TreePath p = trees.getPath(elem); 23.61 + new MulticatchParamTester(trees).scan(p, null); 23.62 + } 23.63 + return true; 23.64 + } 23.65 + 23.66 + class MulticatchParamTester extends TreePathScanner<Void, Void> { 23.67 + Trees trees; 23.68 + 23.69 + public MulticatchParamTester(Trees trees) { 23.70 + super(); 23.71 + this.trees = trees; 23.72 + } 23.73 + 23.74 + @Override 23.75 + public Void visitVariable(VariableTree node, Void p) { 23.76 + Element ex = trees.getElement(getCurrentPath()); 23.77 + if (ex.getSimpleName().contentEquals("ex")) { 23.78 + assertTrue(ex.getKind() == ElementKind.EXCEPTION_PARAMETER, "Expected EXCEPTION_PARAMETER - found " + ex.getKind()); 23.79 + for (Element e : types.asElement(ex.asType()).getEnclosedElements()) { 23.80 + Member m = e.getAnnotation(Member.class); 23.81 + if (m != null) { 23.82 + assertTrue(e.getKind() == m.value(), "Expected " + m.value() + " - found " + e.getKind()); 23.83 + } 23.84 + } 23.85 + assertTrue(assertionCount == 3, "Expected 3 assertions - found " + assertionCount); 23.86 + } 23.87 + return super.visitVariable(node, p); 23.88 + } 23.89 + } 23.90 + 23.91 + private static void assertTrue(boolean cond, String msg) { 23.92 + assertionCount++; 23.93 + if (!cond) 23.94 + throw new AssertionError(msg); 23.95 + } 23.96 + 23.97 + static int assertionCount = 0; 23.98 +}