Thu, 04 Jul 2013 10:41:08 +0100
6356530: -Xlint:serial does not flag abstract classes with concrete methods/members
Reviewed-by: mcimadamore
1.1 --- a/src/share/classes/com/sun/tools/javac/code/Scope.java Thu Jul 04 10:35:33 2013 +0100 1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Scope.java Thu Jul 04 10:41:08 2013 +0100 1.3 @@ -1,5 +1,5 @@ 1.4 /* 1.5 - * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. 1.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.8 * 1.9 * This code is free software; you can redistribute it and/or modify it 1.10 @@ -316,6 +316,7 @@ 1.11 public Entry lookup(Name name) { 1.12 return lookup(name, noFilter); 1.13 } 1.14 + 1.15 public Entry lookup(Name name, Filter<Symbol> sf) { 1.16 Entry e = table[getIndex(name)]; 1.17 if (e == null || e == sentinel) 1.18 @@ -361,6 +362,10 @@ 1.19 } 1.20 } 1.21 1.22 + public boolean anyMatch(Filter<Symbol> sf) { 1.23 + return getElements(sf).iterator().hasNext(); 1.24 + } 1.25 + 1.26 public Iterable<Symbol> getElements() { 1.27 return getElements(noFilter); 1.28 }
2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java Thu Jul 04 10:35:33 2013 +0100 2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java Thu Jul 04 10:41:08 2013 +0100 2.3 @@ -4301,7 +4301,7 @@ 2.4 if (env.info.lint.isEnabled(LintCategory.SERIAL) && 2.5 isSerializable(c) && 2.6 (c.flags() & Flags.ENUM) == 0 && 2.7 - (c.flags() & ABSTRACT) == 0) { 2.8 + checkForSerial(c)) { 2.9 checkSerialVersionUID(tree, c); 2.10 } 2.11 if (allowTypeAnnos) { 2.12 @@ -4313,6 +4313,22 @@ 2.13 } 2.14 } 2.15 // where 2.16 + boolean checkForSerial(ClassSymbol c) { 2.17 + if ((c.flags() & ABSTRACT) == 0) { 2.18 + return true; 2.19 + } else { 2.20 + return c.members().anyMatch(anyNonAbstractOrDefaultMethod); 2.21 + } 2.22 + } 2.23 + 2.24 + public static final Filter<Symbol> anyNonAbstractOrDefaultMethod = new Filter<Symbol>() { 2.25 + @Override 2.26 + public boolean accepts(Symbol s) { 2.27 + return s.kind == Kinds.MTH && 2.28 + (s.flags() & (DEFAULT | ABSTRACT)) != ABSTRACT; 2.29 + } 2.30 + }; 2.31 + 2.32 /** get a diagnostic position for an attribute of Type t, or null if attribute missing */ 2.33 private DiagnosticPosition getDiagnosticPosition(JCClassDecl tree, Type t) { 2.34 for(List<JCAnnotation> al = tree.mods.annotations; !al.isEmpty(); al = al.tail) {
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/T6356530/SerializableAbstractClassWithNonAbstractMethodsTest.java Thu Jul 04 10:41:08 2013 +0100 3.3 @@ -0,0 +1,48 @@ 3.4 +/* 3.5 + * Copyright (c) 2013, 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 +/* 3.28 + * @test 3.29 + * @bug 6356530 3.30 + * @summary -Xlint:serial does not flag abstract classes with concrete methods/members 3.31 + * @compile/fail/ref=SerializableAbstractClassWithNonAbstractMethodsTest.out -XDrawDiagnostics -Werror -Xlint:serial SerializableAbstractClassWithNonAbstractMethodsTest.java 3.32 + */ 3.33 + 3.34 +abstract class SerializableAbstractClassWithNonAbstractMethodsTest implements java.io.Serializable { 3.35 + void m1() {} 3.36 + abstract void m2(); 3.37 + 3.38 + abstract class AWithUID implements java.io.Serializable { 3.39 + private static final long serialVersionUID = 0; 3.40 + void m(){} 3.41 + } 3.42 + 3.43 + interface IDefault extends java.io.Serializable { 3.44 + default int m() { return 1; } 3.45 + } 3.46 + 3.47 + interface IDefaultAndUID extends java.io.Serializable { 3.48 + static final long serialVersionUID = 0; 3.49 + default int m() { return 1; } 3.50 + } 3.51 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/T6356530/SerializableAbstractClassWithNonAbstractMethodsTest.out Thu Jul 04 10:41:08 2013 +0100 4.3 @@ -0,0 +1,5 @@ 4.4 +SerializableAbstractClassWithNonAbstractMethodsTest.java:40:5: compiler.warn.missing.SVUID: SerializableAbstractClassWithNonAbstractMethodsTest.IDefault 4.5 +SerializableAbstractClassWithNonAbstractMethodsTest.java:31:10: compiler.warn.missing.SVUID: SerializableAbstractClassWithNonAbstractMethodsTest 4.6 +- compiler.err.warnings.and.werror 4.7 +1 error 4.8 +2 warnings