Make package parsing code consume annotations am: 2863e4535e
Original change: https://android-review.googlesource.com/c/platform/build/soong/+/2760406 Change-Id: I6441efbe49bd65f72d952bc8d65519dd2a286952 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
18
jar/jar.go
18
jar/jar.go
@@ -166,10 +166,23 @@ func JavaPackage(r io.Reader, src string) (string, error) {
|
|||||||
}
|
}
|
||||||
s.IsIdentRune = javaIdentRune
|
s.IsIdentRune = javaIdentRune
|
||||||
|
|
||||||
tok := s.Scan()
|
var tok rune
|
||||||
|
for {
|
||||||
|
tok = s.Scan()
|
||||||
if sErr != nil {
|
if sErr != nil {
|
||||||
return "", sErr
|
return "", sErr
|
||||||
}
|
}
|
||||||
|
// If the first token is an annotation, it could be annotating a package declaration, so consume them.
|
||||||
|
// Note that this does not support "complex" annotations with attributes, e.g. @Foo(x=y).
|
||||||
|
if tok != '@' {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
tok = s.Scan()
|
||||||
|
if tok != scanner.Ident || sErr != nil {
|
||||||
|
return "", fmt.Errorf("expected annotation identifier, got @%v", tok)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if tok == scanner.Ident {
|
if tok == scanner.Ident {
|
||||||
switch s.TokenText() {
|
switch s.TokenText() {
|
||||||
case "package":
|
case "package":
|
||||||
@@ -189,9 +202,6 @@ func JavaPackage(r io.Reader, src string) (string, error) {
|
|||||||
default:
|
default:
|
||||||
return "", fmt.Errorf(`expected first token of java file to be "package", got %q`, s.TokenText())
|
return "", fmt.Errorf(`expected first token of java file to be "package", got %q`, s.TokenText())
|
||||||
}
|
}
|
||||||
} else if tok == '@' {
|
|
||||||
// File has no package statement, first token is an annotation
|
|
||||||
return "", nil
|
|
||||||
} else if tok == scanner.EOF {
|
} else if tok == scanner.EOF {
|
||||||
// File no package statement, it has no non-whitespace non-comment tokens
|
// File no package statement, it has no non-whitespace non-comment tokens
|
||||||
return "", nil
|
return "", nil
|
||||||
|
@@ -61,6 +61,16 @@ func TestGetJavaPackage(t *testing.T) {
|
|||||||
in: "package 0foo.bar;",
|
in: "package 0foo.bar;",
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "annotations",
|
||||||
|
in: "@NonNullApi\n@X\npackage foo.bar;",
|
||||||
|
want: "foo.bar",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "complex annotation",
|
||||||
|
in: "@Foo(x=y)\n@package foo.bar;",
|
||||||
|
wantErr: true, // Complex annotation not supported yet.
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
Reference in New Issue
Block a user