Thu, 01 Nov 2012 10:48:36 +0100
7153951: Add new lint option -Xlint:auxiliaryclass
Reviewed-by: jjg, mcimadamore, forax
1.1 --- a/src/share/classes/com/sun/tools/javac/code/Flags.java Wed Oct 31 13:48:15 2012 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Flags.java Thu Nov 01 10:48:36 2012 +0100 1.3 @@ -258,6 +258,12 @@ 1.4 */ 1.5 public static final long DEFAULT = 1L<<43; 1.6 1.7 + /** 1.8 + * Flag that marks class as auxiliary, ie a non-public class following 1.9 + * the public class in a source file, that could block implicit compilation. 1.10 + */ 1.11 + public static final long AUXILIARY = 1L<<43; 1.12 + 1.13 /** Modifier masks. 1.14 */ 1.15 public static final int
2.1 --- a/src/share/classes/com/sun/tools/javac/code/Lint.java Wed Oct 31 13:48:15 2012 -0700 2.2 +++ b/src/share/classes/com/sun/tools/javac/code/Lint.java Thu Nov 01 10:48:36 2012 +0100 2.3 @@ -129,6 +129,13 @@ 2.4 */ 2.5 public enum LintCategory { 2.6 /** 2.7 + * Warn when code refers to a auxiliary class that is hidden in a source file (ie source file name is 2.8 + * different from the class name, and the type is not properly nested) and the referring code 2.9 + * is not located in the same source file. 2.10 + */ 2.11 + AUXILIARYCLASS("auxiliaryclass"), 2.12 + 2.13 + /** 2.14 * Warn about use of unnecessary casts. 2.15 */ 2.16 CAST("cast"),
3.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java Wed Oct 31 13:48:15 2012 -0700 3.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java Thu Nov 01 10:48:36 2012 +0100 3.3 @@ -3050,6 +3050,7 @@ 3.4 // except for two situations: 3.5 owntype = sym.type; 3.6 if (owntype.hasTag(CLASS)) { 3.7 + chk.checkForBadAuxiliaryClassAccess(tree.pos(), env, (ClassSymbol)sym); 3.8 Type ownOuter = owntype.getEnclosingType(); 3.9 3.10 // (a) If the symbol's type is parameterized, erase it
4.1 --- a/src/share/classes/com/sun/tools/javac/comp/Check.java Wed Oct 31 13:48:15 2012 -0700 4.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Check.java Thu Nov 01 10:48:36 2012 +0100 4.3 @@ -27,6 +27,7 @@ 4.4 4.5 import java.util.*; 4.6 import java.util.Set; 4.7 +import javax.tools.JavaFileManager; 4.8 4.9 import com.sun.tools.javac.code.*; 4.10 import com.sun.tools.javac.jvm.*; 4.11 @@ -77,6 +78,7 @@ 4.12 private boolean suppressAbortOnBadClassFile; 4.13 private boolean enableSunApiLintControl; 4.14 private final TreeInfo treeinfo; 4.15 + private final JavaFileManager fileManager; 4.16 4.17 // The set of lint options currently in effect. It is initialized 4.18 // from the context, and then is set/reset as needed by Attr as it 4.19 @@ -109,6 +111,7 @@ 4.20 Options options = Options.instance(context); 4.21 lint = Lint.instance(context); 4.22 treeinfo = TreeInfo.instance(context); 4.23 + fileManager = context.get(JavaFileManager.class); 4.24 4.25 Source source = Source.instance(context); 4.26 allowGenerics = source.allowGenerics(); 4.27 @@ -3230,6 +3233,19 @@ 4.28 return true; 4.29 } 4.30 4.31 + /** Check that an auxiliary class is not accessed from any other file than its own. 4.32 + */ 4.33 + void checkForBadAuxiliaryClassAccess(DiagnosticPosition pos, Env<AttrContext> env, ClassSymbol c) { 4.34 + if (lint.isEnabled(Lint.LintCategory.AUXILIARYCLASS) && 4.35 + (c.flags() & AUXILIARY) != 0 && 4.36 + rs.isAccessible(env, c) && 4.37 + !fileManager.isSameFile(c.sourcefile, env.toplevel.sourcefile)) 4.38 + { 4.39 + log.warning(pos, "auxiliary.class.accessed.from.outside.of.its.source.file", 4.40 + c, c.sourcefile); 4.41 + } 4.42 + } 4.43 + 4.44 private class ConversionWarner extends Warner { 4.45 final String uncheckedKey; 4.46 final Type found;
5.1 --- a/src/share/classes/com/sun/tools/javac/comp/MemberEnter.java Wed Oct 31 13:48:15 2012 -0700 5.2 +++ b/src/share/classes/com/sun/tools/javac/comp/MemberEnter.java Thu Nov 01 10:48:36 2012 +0100 5.3 @@ -1022,11 +1022,13 @@ 5.4 // name as a top-level package. 5.5 if (checkClash && 5.6 c.owner.kind == PCK && c.owner != syms.unnamedPackage && 5.7 - reader.packageExists(c.fullname)) 5.8 - { 5.9 - log.error(tree.pos, "clash.with.pkg.of.same.name", Kinds.kindName(sym), c); 5.10 - } 5.11 - 5.12 + reader.packageExists(c.fullname)) { 5.13 + log.error(tree.pos, "clash.with.pkg.of.same.name", Kinds.kindName(sym), c); 5.14 + } 5.15 + if (c.owner.kind == PCK && (c.flags_field & PUBLIC) == 0 && 5.16 + !env.toplevel.sourcefile.isNameCompatible(c.name.toString(),JavaFileObject.Kind.SOURCE)) { 5.17 + c.flags_field |= AUXILIARY; 5.18 + } 5.19 } catch (CompletionFailure ex) { 5.20 chk.completionError(tree.pos(), ex); 5.21 } finally {
6.1 --- a/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java Wed Oct 31 13:48:15 2012 -0700 6.2 +++ b/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java Thu Nov 01 10:48:36 2012 +0100 6.3 @@ -1018,6 +1018,15 @@ 6.4 ClassSymbol c = (ClassSymbol) sym; 6.5 Name n = readName(nextChar()); 6.6 c.sourcefile = new SourceFileObject(n, c.flatname); 6.7 + // If the class is a toplevel class, originating from a Java source file, 6.8 + // but the class name does not match the file name, then it is 6.9 + // an auxiliary class. 6.10 + String sn = n.toString(); 6.11 + if (c.owner.kind == Kinds.PCK && 6.12 + sn.endsWith(".java") && 6.13 + !sn.equals(c.name.toString()+".java")) { 6.14 + c.flags_field |= AUXILIARY; 6.15 + } 6.16 } 6.17 }, 6.18
7.1 --- a/src/share/classes/com/sun/tools/javac/resources/compiler.properties Wed Oct 31 13:48:15 2012 -0700 7.2 +++ b/src/share/classes/com/sun/tools/javac/resources/compiler.properties Thu Nov 01 10:48:36 2012 +0100 7.3 @@ -1847,6 +1847,11 @@ 7.4 7.5 ##### 7.6 7.7 +# 0: type, 1: file name 7.8 +compiler.warn.auxiliary.class.accessed.from.outside.of.its.source.file=\ 7.9 + auxiliary class {0} in {1} should not be accessed from outside its own source file 7.10 + 7.11 + 7.12 ## The first argument ({0}) is a "kindname". 7.13 # 0: symbol kind, 1: symbol, 2: symbol 7.14 compiler.err.abstract.cant.be.accessed.directly=\
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/test/tools/javac/diags/examples/AuxiliaryClassWarning/ClassUsingAuxiliary.java Thu Nov 01 10:48:36 2012 +0100 8.3 @@ -0,0 +1,29 @@ 8.4 +/* 8.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 8.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 8.7 + * 8.8 + * This code is free software; you can redistribute it and/or modify it 8.9 + * under the terms of the GNU General Public License version 2 only, as 8.10 + * published by the Free Software Foundation. 8.11 + * 8.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 8.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 8.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 8.15 + * version 2 for more details (a copy is included in the LICENSE file that 8.16 + * accompanied this code). 8.17 + * 8.18 + * You should have received a copy of the GNU General Public License version 8.19 + * 2 along with this work; if not, write to the Free Software Foundation, 8.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 8.21 + * 8.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 8.23 + * or visit www.oracle.com if you need additional information or have any 8.24 + * questions. 8.25 + */ 8.26 + 8.27 +// key: compiler.warn.auxiliary.class.accessed.from.outside.of.its.source.file 8.28 +// options: -Xlint:auxiliaryclass 8.29 + 8.30 +class ClassUsingAuxiliary { 8.31 + AuxiliaryClass ahem; 8.32 +}
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 9.2 +++ b/test/tools/javac/diags/examples/AuxiliaryClassWarning/ClassWithAuxiliary.java Thu Nov 01 10:48:36 2012 +0100 9.3 @@ -0,0 +1,29 @@ 9.4 +/* 9.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 9.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 9.7 + * 9.8 + * This code is free software; you can redistribute it and/or modify it 9.9 + * under the terms of the GNU General Public License version 2 only, as 9.10 + * published by the Free Software Foundation. 9.11 + * 9.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 9.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 9.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 9.15 + * version 2 for more details (a copy is included in the LICENSE file that 9.16 + * accompanied this code). 9.17 + * 9.18 + * You should have received a copy of the GNU General Public License version 9.19 + * 2 along with this work; if not, write to the Free Software Foundation, 9.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 9.21 + * 9.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 9.23 + * or visit www.oracle.com if you need additional information or have any 9.24 + * questions. 9.25 + */ 9.26 + 9.27 +class ClassWithAuxiliaryClass { 9.28 +} 9.29 + 9.30 +// Auxiliary class that cannot be found through implicit compilation. 9.31 +class AuxiliaryClass { 9.32 +}
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 10.2 +++ b/test/tools/javac/warnings/AuxiliaryClass/ClassUsingAnotherAuxiliary.java Thu Nov 01 10:48:36 2012 +0100 10.3 @@ -0,0 +1,34 @@ 10.4 +/* 10.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 10.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 10.7 + * 10.8 + * This code is free software; you can redistribute it and/or modify it 10.9 + * under the terms of the GNU General Public License version 2 only, as 10.10 + * published by the Free Software Foundation. 10.11 + * 10.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 10.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 10.15 + * version 2 for more details (a copy is included in the LICENSE file that 10.16 + * accompanied this code). 10.17 + * 10.18 + * You should have received a copy of the GNU General Public License version 10.19 + * 2 along with this work; if not, write to the Free Software Foundation, 10.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 10.21 + * 10.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 10.23 + * or visit www.oracle.com if you need additional information or have any 10.24 + * questions. 10.25 + */ 10.26 + 10.27 +/** 10.28 + * @test 10.29 + * @compile ClassUsingAnotherAuxiliary.java NotAClassName.java 10.30 + * @compile -Xlint:auxiliaryclass ClassUsingAnotherAuxiliary.java NotAClassName.java 10.31 + * @compile/fail/ref=ClassUsingAnotherAuxiliary.out -XDrawDiagnostics -Werror -Xlint:auxiliaryclass ClassUsingAnotherAuxiliary.java NotAClassName.java 10.32 + */ 10.33 + 10.34 +class ClassUsingAnotherAuxiliary { 10.35 + AnAuxiliaryClass ahem; 10.36 +} 10.37 +
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 11.2 +++ b/test/tools/javac/warnings/AuxiliaryClass/ClassUsingAnotherAuxiliary.out Thu Nov 01 10:48:36 2012 +0100 11.3 @@ -0,0 +1,4 @@ 11.4 +ClassUsingAnotherAuxiliary.java:32:5: compiler.warn.auxiliary.class.accessed.from.outside.of.its.source.file: AnAuxiliaryClass, NotAClassName.java 11.5 +- compiler.err.warnings.and.werror 11.6 +1 error 11.7 +1 warning
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 12.2 +++ b/test/tools/javac/warnings/AuxiliaryClass/ClassUsingAuxiliary.java Thu Nov 01 10:48:36 2012 +0100 12.3 @@ -0,0 +1,34 @@ 12.4 +/* 12.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 12.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 12.7 + * 12.8 + * This code is free software; you can redistribute it and/or modify it 12.9 + * under the terms of the GNU General Public License version 2 only, as 12.10 + * published by the Free Software Foundation. 12.11 + * 12.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 12.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12.15 + * version 2 for more details (a copy is included in the LICENSE file that 12.16 + * accompanied this code). 12.17 + * 12.18 + * You should have received a copy of the GNU General Public License version 12.19 + * 2 along with this work; if not, write to the Free Software Foundation, 12.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 12.21 + * 12.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 12.23 + * or visit www.oracle.com if you need additional information or have any 12.24 + * questions. 12.25 + */ 12.26 + 12.27 +/** 12.28 + * @test 12.29 + * @clean ClassUsingAuxiliary ClassWithAuxiliary AuxiliaryClass ClassWithAuxiliary$NotAnAuxiliaryClass ClassWithAuxiliary$NotAnAuxiliaryClassEither 12.30 + * @run compile ClassUsingAuxiliary.java ClassWithAuxiliary.java 12.31 + * @run compile/fail/ref=ClassUsingAuxiliary1.out -XDrawDiagnostics -Werror -Xlint:auxiliaryclass ClassUsingAuxiliary.java ClassWithAuxiliary.java 12.32 + * @run compile/fail/ref=ClassUsingAuxiliary2.out -XDrawDiagnostics -Werror -Xlint:auxiliaryclass ClassUsingAuxiliary.java 12.33 + */ 12.34 + 12.35 +class ClassUsingAuxiliary { 12.36 + AuxiliaryClass ahem; 12.37 +}
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 13.2 +++ b/test/tools/javac/warnings/AuxiliaryClass/ClassUsingAuxiliary1.out Thu Nov 01 10:48:36 2012 +0100 13.3 @@ -0,0 +1,4 @@ 13.4 +ClassUsingAuxiliary.java:33:5: compiler.warn.auxiliary.class.accessed.from.outside.of.its.source.file: AuxiliaryClass, ClassWithAuxiliary.java 13.5 +- compiler.err.warnings.and.werror 13.6 +1 error 13.7 +1 warning
14.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 14.2 +++ b/test/tools/javac/warnings/AuxiliaryClass/ClassUsingAuxiliary2.out Thu Nov 01 10:48:36 2012 +0100 14.3 @@ -0,0 +1,4 @@ 14.4 +ClassUsingAuxiliary.java:33:5: compiler.warn.auxiliary.class.accessed.from.outside.of.its.source.file: AuxiliaryClass, ClassWithAuxiliary.java 14.5 +- compiler.err.warnings.and.werror 14.6 +1 error 14.7 +1 warning
15.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 15.2 +++ b/test/tools/javac/warnings/AuxiliaryClass/ClassWithAuxiliary.java Thu Nov 01 10:48:36 2012 +0100 15.3 @@ -0,0 +1,31 @@ 15.4 +/* 15.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 15.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 15.7 + * 15.8 + * This code is free software; you can redistribute it and/or modify it 15.9 + * under the terms of the GNU General Public License version 2 only, as 15.10 + * published by the Free Software Foundation. 15.11 + * 15.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 15.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 15.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15.15 + * version 2 for more details (a copy is included in the LICENSE file that 15.16 + * accompanied this code). 15.17 + * 15.18 + * You should have received a copy of the GNU General Public License version 15.19 + * 2 along with this work; if not, write to the Free Software Foundation, 15.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 15.21 + * 15.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 15.23 + * or visit www.oracle.com if you need additional information or have any 15.24 + * questions. 15.25 + */ 15.26 + 15.27 +public class ClassWithAuxiliary { 15.28 + public static class NotAnAuxiliaryClass { } 15.29 + public class NotAnAuxiliaryClassEither { } 15.30 +} 15.31 + 15.32 +// Auxiliary class that cannot be found through implicit compilation. 15.33 +class AuxiliaryClass { 15.34 +}
16.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 16.2 +++ b/test/tools/javac/warnings/AuxiliaryClass/NotAClassName.java Thu Nov 01 10:48:36 2012 +0100 16.3 @@ -0,0 +1,25 @@ 16.4 +/* 16.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 16.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 16.7 + * 16.8 + * This code is free software; you can redistribute it and/or modify it 16.9 + * under the terms of the GNU General Public License version 2 only, as 16.10 + * published by the Free Software Foundation. 16.11 + * 16.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 16.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 16.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16.15 + * version 2 for more details (a copy is included in the LICENSE file that 16.16 + * accompanied this code). 16.17 + * 16.18 + * You should have received a copy of the GNU General Public License version 16.19 + * 2 along with this work; if not, write to the Free Software Foundation, 16.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 16.21 + * 16.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 16.23 + * or visit www.oracle.com if you need additional information or have any 16.24 + * questions. 16.25 + */ 16.26 + 16.27 +class AnAuxiliaryClass { 16.28 +}
17.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 17.2 +++ b/test/tools/javac/warnings/AuxiliaryClass/SelfClassWithAux.java Thu Nov 01 10:48:36 2012 +0100 17.3 @@ -0,0 +1,44 @@ 17.4 +/* 17.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 17.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 17.7 + * 17.8 + * This code is free software; you can redistribute it and/or modify it 17.9 + * under the terms of the GNU General Public License version 2 only, as 17.10 + * published by the Free Software Foundation. 17.11 + * 17.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 17.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 17.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 17.15 + * version 2 for more details (a copy is included in the LICENSE file that 17.16 + * accompanied this code). 17.17 + * 17.18 + * You should have received a copy of the GNU General Public License version 17.19 + * 2 along with this work; if not, write to the Free Software Foundation, 17.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17.21 + * 17.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 17.23 + * or visit www.oracle.com if you need additional information or have any 17.24 + * questions. 17.25 + */ 17.26 + 17.27 +/* 17.28 + * Test that an auxiliary class referenced from its own source file, 17.29 + * does not trigger the warning. Such code does not prevent implicit 17.30 + * compilation. Also test that references to inner classes do not trigger the warning. 17.31 + */ 17.32 + 17.33 +/* 17.34 + * @test 17.35 + * @run compile -Werror -Xlint:auxiliaryclass SelfClassWithAux.java ClassWithAuxiliary.java 17.36 + * @run compile -Werror -Xlint:auxiliaryclass SelfClassWithAux.java 17.37 + */ 17.38 + 17.39 +class SelfClassWithAux { 17.40 + Aux aux; 17.41 + ClassWithAuxiliary.NotAnAuxiliaryClass alfa; 17.42 + ClassWithAuxiliary.NotAnAuxiliaryClassEither beta; 17.43 +} 17.44 + 17.45 +class Aux { 17.46 + Aux aux; 17.47 +}