Skip to content

Commit 0c40128

Browse files
committedAug 12, 2022
7194212: NPE in Flow.visitIdent
Reviewed-by: vromero
1 parent 6eb7c3a commit 0c40128

File tree

6 files changed

+119
-10
lines changed

6 files changed

+119
-10
lines changed
 

‎src/jdk.compiler/share/classes/com/sun/tools/javac/code/Flags.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ public static EnumSet<Flag> asFlagSet(long flags) {
127127
*/
128128
public static final int BLOCK = 1<<20;
129129

130-
/** Flag bit 21 is available. (used earlier to tag compiler-generated abstract methods that implement
131-
* an interface method (Miranda methods)).
130+
/** Flag is set for ClassSymbols that are being compiled from source.
132131
*/
132+
public static final int FROM_SOURCE = 1<<21; //ClassSymbols
133133

134134
/** Flag is set for nested classes that do not access instance members
135135
* or `this' of an outer class and therefore don't need to be passed
@@ -482,6 +482,7 @@ public enum Flag {
482482
DEPRECATED(Flags.DEPRECATED),
483483
HASINIT(Flags.HASINIT),
484484
BLOCK(Flags.BLOCK),
485+
FROM_SOURCE(Flags.FROM_SOURCE),
485486
ENUM(Flags.ENUM),
486487
MANDATED(Flags.MANDATED),
487488
NOOUTERTHIS(Flags.NOOUTERTHIS),

‎src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symtab.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -673,7 +673,8 @@ public ClassSymbol enterClass(ModuleSymbol msym, Name name, TypeSymbol owner) {
673673
if (c == null) {
674674
c = defineClass(name, owner);
675675
doEnterClass(msym, c);
676-
} else if ((c.name != name || c.owner != owner) && owner.kind == TYP && c.owner.kind == PCK) {
676+
} else if ((c.name != name || c.owner != owner) && owner.kind == TYP &&
677+
c.owner.kind == PCK && ((c.flags_field & FROM_SOURCE) == 0)) {
677678
// reassign fields of classes that might have been loaded with
678679
// their flat names.
679680
c.owner.members().remove(c);

‎src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Enter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -494,7 +494,7 @@ public void visitClassDef(JCClassDecl tree) {
494494

495495
// Fill out class fields.
496496
c.completer = Completer.NULL_COMPLETER; // do not allow the initial completer linger on.
497-
c.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, c, tree);
497+
c.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, c, tree) | FROM_SOURCE;
498498
c.classfile = c.sourcefile = env.toplevel.sourcefile;
499499
c.members_field = WriteableScope.create(c);
500500
c.clearAnnotationMetadata();

‎src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -2591,7 +2591,7 @@ void readInnerClasses(ClassSymbol c) {
25912591
if (member.erasure_field != null)
25922592
((ClassType)member.erasure_field).setEnclosingType(types.erasure(outer.type));
25932593
}
2594-
if (c == outer) {
2594+
if (c == outer && member.owner == c) {
25952595
member.flags_field = flags;
25962596
enterMember(c, member);
25972597
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 7194212
27+
* @summary Ensure InnerClasses attribute does not overwrite flags
28+
* for source based classes
29+
* @library /tools/lib
30+
* @modules
31+
* jdk.compiler/com.sun.tools.javac.api
32+
* jdk.compiler/com.sun.tools.javac.main
33+
* @build toolbox.ToolBox toolbox.JavacTask
34+
* @run main T7194212
35+
*/
36+
37+
import toolbox.JavacTask;
38+
import toolbox.TestRunner;
39+
import toolbox.ToolBox;
40+
41+
import java.nio.file.Files;
42+
import java.nio.file.Path;
43+
import java.nio.file.Paths;
44+
45+
public class T7194212 extends TestRunner {
46+
47+
protected ToolBox tb;
48+
49+
T7194212() {
50+
super(System.err);
51+
tb = new ToolBox();
52+
}
53+
54+
public static void main(String... args) throws Exception {
55+
T7194212 t = new T7194212();
56+
t.runTests();
57+
}
58+
59+
/**
60+
* Run all methods annotated with @Test, and throw an exception if any
61+
* errors are reported..
62+
*
63+
* @throws Exception if any errors occurred
64+
*/
65+
protected void runTests() throws Exception {
66+
runTests(m -> new Object[] { Paths.get(m.getName()) });
67+
}
68+
69+
@Test
70+
public void testSourceClassFileClash(Path base) throws Exception {
71+
Path src = base.resolve("src");
72+
tb.writeJavaFiles(src, """
73+
public class Outer {
74+
public class Inner { }
75+
}
76+
""");
77+
78+
Path classes = base.resolve("classes");
79+
80+
Files.createDirectories(classes);
81+
82+
new JavacTask(tb)
83+
.outdir(classes)
84+
.files(tb.findJavaFiles(src))
85+
.run()
86+
.writeAll();
87+
88+
Path test = base.resolve("test");
89+
tb.writeJavaFiles(test, """
90+
public class Outer$Inner extends Outer { }
91+
""",
92+
"""
93+
public class Test extends Outer { }
94+
""");
95+
96+
Path testClasses = base.resolve("test-classes");
97+
98+
Files.createDirectories(testClasses);
99+
100+
new JavacTask(tb)
101+
.options("-classpath", classes.toString())
102+
.outdir(testClasses)
103+
.files(tb.findJavaFiles(test))
104+
.run()
105+
.writeAll();
106+
}
107+
}

‎test/langtools/tools/lib/toolbox/ToolBox.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -752,7 +752,7 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) {
752752
private final static Pattern packagePattern =
753753
Pattern.compile("package\\s+(((?:\\w+\\.)*)\\w+)");
754754
private final static Pattern classPattern =
755-
Pattern.compile("(?:public\\s+)?(?:class|enum|interface|record)\\s+(\\w+)");
755+
Pattern.compile("(?:public\\s+)?(?:class|enum|interface|record)\\s+((\\w|\\$)+)");
756756

757757
/**
758758
* Extracts the Java file name from the class declaration.

0 commit comments

Comments
 (0)
Please sign in to comment.