Skip to content

Commit

Permalink
Add a null-check in CanBeStatic
Browse files Browse the repository at this point in the history
JDK 12 changes the representation of labelled breaks to use an identifier for
the label (instead of just a name), and the identifier doesn't have a symbol.

Fixes #1111

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=311832221
  • Loading branch information
don-vip authored and kluever committed May 16, 2020
1 parent 0202733 commit 12e2544
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,27 @@ private CanBeStaticAnalyzer(Symbol owner, VisitorState state) {
public void visitIdent(JCTree.JCIdent tree) {
// check for unqualified references to instance members (fields and methods) declared
// in an enclosing scope
if (tree.sym.isStatic()) {
Symbol sym = tree.sym;
if (sym == null) {
// return;
}
if (sym.isStatic()) {
return;
}
switch (tree.sym.getKind()) {
switch (sym.getKind()) {
case TYPE_PARAMETER:
// declaring a class as non-static just to access a type parameter is silly -
// why not just re-declare the type parameter instead of capturing it?
// TODO(cushon): consider making the suggestion anyways, maybe with a fix?
// fall through
case FIELD:
if (!isOwnedBy(tree.sym, owner, state.getTypes())) {
if (!isOwnedBy(sym, owner, state.getTypes())) {
canPossiblyBeStatic = false;
}
break;
case METHOD:
if (!isOwnedBy(tree.sym, owner, state.getTypes())) {
outerReferences.add((MethodSymbol) tree.sym);
if (!isOwnedBy(sym, owner, state.getTypes())) {
outerReferences.add((MethodSymbol) sym);
}
break;
case CLASS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,23 @@ public void innerClassMethodReference() {
"}")
.doTest();
}

@Test
public void labelledBreak() {
compilationHelper
.addSourceLines(
"A.java",
"public class A {",
" // BUG: Diagnostic contains:",
" class Inner {",
" void f() {",
" OUTER:",
" while (true) {",
" break OUTER;",
" }",
" }",
" }",
"}")
.doTest();
}
}

0 comments on commit 12e2544

Please sign in to comment.