Skip to content

Crash on annotated array type #374

Description

@mernst

Here are three files, all of which compile with javac:

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
@interface MyTypeAnno {}

public class GjfFailure {
  void m(int a @MyTypeAnno []) {}
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
@interface MyTypeAnno {}

public class GjfSuccess {
  void m(int @MyTypeAnno [] a) {}
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
@interface MyTypeAnno {}

public class GjfSuccess2 {
  void m(int a[]) {}
}

Running google-java-format (version 1.7) on the first file yields this stack trace:

GjfFailure.java:8:11: error: com.google.common.base.VerifyException
	at com.google.common.base.Verify.verify(Verify.java:100)
	at com.google.googlejavaformat.java.JavaInputAstVisitor.visitAnnotatedArrayType(JavaInputAstVisitor.java:580)
	at com.google.googlejavaformat.java.JavaInputAstVisitor.visitAnnotatedType(JavaInputAstVisitor.java:1322)
	at com.google.googlejavaformat.java.JavaInputAstVisitor.visitAnnotatedType(JavaInputAstVisitor.java:156)
	at org.openjdk.tools.javac.tree.JCTree$JCAnnotatedType.accept(JCTree.java:2637)
	at org.openjdk.source.util.TreePathScanner.scan(TreePathScanner.java:82)
	at com.google.googlejavaformat.java.JavaInputAstVisitor.scan(JavaInputAstVisitor.java:328)
	at com.google.googlejavaformat.java.JavaInputAstVisitor.declareOne(JavaInputAstVisitor.java:3253)
	at com.google.googlejavaformat.java.JavaInputAstVisitor.visitToDeclare(JavaInputAstVisitor.java:2536)
	at com.google.googlejavaformat.java.JavaInputAstVisitor.visitFormals(JavaInputAstVisitor.java:2361)
	at com.google.googlejavaformat.java.JavaInputAstVisitor.visitMethod(JavaInputAstVisitor.java:1419)
	at com.google.googlejavaformat.java.JavaInputAstVisitor.visitMethod(JavaInputAstVisitor.java:156)
	at org.openjdk.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:898)
	at org.openjdk.source.util.TreePathScanner.scan(TreePathScanner.java:82)
	at com.google.googlejavaformat.java.JavaInputAstVisitor.scan(JavaInputAstVisitor.java:328)
	at com.google.googlejavaformat.java.JavaInputAstVisitor.addBodyDeclarations(JavaInputAstVisitor.java:3464)
	at com.google.googlejavaformat.java.JavaInputAstVisitor.visitClassDeclaration(JavaInputAstVisitor.java:1962)
	at com.google.googlejavaformat.java.JavaInputAstVisitor.visitClass(JavaInputAstVisitor.java:400)
	at com.google.googlejavaformat.java.JavaInputAstVisitor.visitClass(JavaInputAstVisitor.java:156)
	at org.openjdk.tools.javac.tree.JCTree$JCClassDecl.accept(JCTree.java:808)
	at org.openjdk.source.util.TreePathScanner.scan(TreePathScanner.java:82)
	at com.google.googlejavaformat.java.JavaInputAstVisitor.scan(JavaInputAstVisitor.java:328)
	at com.google.googlejavaformat.java.JavaInputAstVisitor.visitCompilationUnit(JavaInputAstVisitor.java:372)
	at com.google.googlejavaformat.java.JavaInputAstVisitor.visitCompilationUnit(JavaInputAstVisitor.java:156)
	at org.openjdk.tools.javac.tree.JCTree$JCCompilationUnit.accept(JCTree.java:591)
	at org.openjdk.source.util.TreePathScanner.scan(TreePathScanner.java:82)
	at com.google.googlejavaformat.java.JavaInputAstVisitor.scan(JavaInputAstVisitor.java:328)
	at com.google.googlejavaformat.java.Formatter.format(Formatter.java:155)
	at com.google.googlejavaformat.java.Formatter.getFormatReplacements(Formatter.java:258)
	at com.google.googlejavaformat.java.Formatter.formatSource(Formatter.java:234)
	at com.google.googlejavaformat.java.FormatFileCallable.call(FormatFileCallable.java:45)
	at com.google.googlejavaformat.java.FormatFileCallable.call(FormatFileCallable.java:26)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

The code is in bad style as it uses a deprecated placement of array brackets in a variable declaration. However, it's legal code and google-java-format probably shouldn't crash.

Activity

  1. cushon commented on Dec 19, 2019

    @cushon
    Collaborator

    Thanks, I'll fix the crash.

    If we get around to making non-whitespace changes, it might be nice to automatically rewrite the c-style notation.

    -int a @MyTypeAnno []
    +int @MyTypeAnno [] a

    For my understanding, is int[] a @MyTypeAnno [] supposed to be equivalent to this:

    -int[] a @MyTypeAnno []
    +int[] @MyTypeAnno [] a

    or to this?

    -int[] a @MyTypeAnno []
    +int @MyTypeAnno [][] a
  2. self-assigned this
    on Dec 19, 2019
  3. mernst commented on Dec 20, 2019

    @mernst
    ContributorAuthor

    @cushon
    To interpret a mixed-style array acces, read right-to left from the variable, then right-to-left from the first array, then the element type:

    @A int @B[] @C [] variablename @D[] @E[];
    

    is the same as

    @A int @D[] @E[] @B[] @C [] variablename;
    

    namely a @D array of @E arrays of @B arrays of @C arrays of @A integers.

  4. added a commit that references this issue on Dec 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions