Skip to content

Commit

Permalink
Add special case support for single line generated java files to the …
Browse files Browse the repository at this point in the history
…package statement parser.

Since the parser is also used for generated source files in some cases, we can't assume that the input is a "normal" source file with a package statement on a line by itself. Generated source files sometimes have the entire content on a single line, so add support for that.

We do it this way to avoid using a full java parser which add extra complexity (though maybe not too much?).

PiperOrigin-RevId: 585650766
  • Loading branch information
Googler authored and copybara-github committed Nov 29, 2023
1 parent d3c0a3f commit 2046e85
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
1 change: 1 addition & 0 deletions querysync/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ test_suite(
"//querysync/javatests/com/google/idea/blaze/qsync:AffectedPackagesTest",
"//querysync/javatests/com/google/idea/blaze/qsync:GeneratedSourceProjectUpdaterTest",
"//querysync/javatests/com/google/idea/blaze/qsync:GraphToProjectConverterTest",
"//querysync/javatests/com/google/idea/blaze/qsync:PackageStatementParserTest",
"//querysync/javatests/com/google/idea/blaze/qsync:PartialProjectRefreshTest",
"//querysync/javatests/com/google/idea/blaze/qsync:ProjectRefresherTest",
"//querysync/javatests/com/google/idea/blaze/qsync:QuerySyncTestUtilsTest",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
public class PackageStatementParser implements PackageReader {

private static final Pattern PACKAGE_PATTERN = Pattern.compile("^\\s*package\\s+([\\w\\.]+)");
private static final Pattern SINGLE_LINE_PACKAGE_PATTERN =
Pattern.compile("\\bpackage\\s+([^;]+);");

@Override
public String readPackage(Path path) throws IOException {
Expand All @@ -40,13 +42,26 @@ public String readPackage(Path path) throws IOException {

public String readPackage(InputStream in) throws IOException {
BufferedReader javaReader = new BufferedReader(new InputStreamReader(in, UTF_8));
String firstLine = null;
String javaLine;
int linesRead = 0;
while ((javaLine = javaReader.readLine()) != null) {
if (firstLine == null) {
firstLine = javaLine;
}
linesRead++;
Matcher packageMatch = PACKAGE_PATTERN.matcher(javaLine);
if (packageMatch.find()) {
return packageMatch.group(1);
}
}
// A special case for generated sources files with no newlines in them:
if (linesRead == 1) {
Matcher packageMatch = SINGLE_LINE_PACKAGE_PATTERN.matcher(firstLine);
if (packageMatch.find()) {
return packageMatch.group(1);
}
}
return "";
}
}
12 changes: 12 additions & 0 deletions querysync/javatests/com/google/idea/blaze/qsync/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,15 @@ java_test(
"@truth//jar",
],
)

java_test(
name = "PackageStatementParserTest",
size = "small",
srcs = ["PackageStatementParserTest.java"],
deps = [
"//querysync/java/com/google/idea/blaze/qsync",
"@com_google_guava_guava//jar",
"@junit//jar",
"@truth//jar",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright 2023 The Bazel Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.idea.blaze.qsync;

import static com.google.common.truth.Truth.assertThat;

import com.google.common.base.Joiner;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class PackageStatementParserTest {

@Test
public void basic_package_statement() throws IOException {
PackageStatementParser psp = new PackageStatementParser();
assertThat(
psp.readPackage(
new ByteArrayInputStream(
Joiner.on("\n")
.join("package com.myorg.somepackage;", "", "public class MyClass {}")
.getBytes(StandardCharsets.UTF_8))))
.isEqualTo("com.myorg.somepackage");
}

@Test
public void comment_before_package_statement() throws IOException {
PackageStatementParser psp = new PackageStatementParser();
assertThat(
psp.readPackage(
new ByteArrayInputStream(
Joiner.on("\n")
.join(
"/**",
" * Copyright statement!",
" * Another line",
" */",
"package com.myorg.otherpackage;",
"",
"public class MyClass {}")
.getBytes(StandardCharsets.UTF_8))))
.isEqualTo("com.myorg.otherpackage");
}

@Test
public void single_line_generated_file() throws IOException {
PackageStatementParser psp = new PackageStatementParser();
assertThat(
psp.readPackage(
new ByteArrayInputStream(
("/* This is a generated file */package com.myorg.package.generated;public"
+ " final class SomeClass {public final static boolean GENERATED_THING ="
+ " true;}")
.getBytes(StandardCharsets.UTF_8))))
.isEqualTo("com.myorg.package.generated");
}

@Test
public void basic_kotlin() throws IOException {
PackageStatementParser psp = new PackageStatementParser();
assertThat(
psp.readPackage(
new ByteArrayInputStream(
Joiner.on("\n")
.join(
"package com.myorg.kotlinpackage",
"",
"import kotlin.text.*",
"",
"fun main() {",
" println(\"Hello world!\")",
"}")
.getBytes(StandardCharsets.UTF_8))))
.isEqualTo("com.myorg.kotlinpackage");
}

@Test
public void kotlin_package_annotation() throws IOException {
PackageStatementParser psp = new PackageStatementParser();
assertThat(
psp.readPackage(
new ByteArrayInputStream(
Joiner.on("\n")
.join(
"@file:JvmName(\"MyFile\")",
"package com.myorg.kotlinpackage",
"",
"object MyObject {",
"}")
.getBytes(StandardCharsets.UTF_8))))
.isEqualTo("com.myorg.kotlinpackage");
}

@Test
public void kotlin_shebang() throws IOException {
PackageStatementParser psp = new PackageStatementParser();
assertThat(
psp.readPackage(
new ByteArrayInputStream(
Joiner.on("\n")
.join(
"#!/bin/interpreter",
"package com.myorg.kotlinpackage",
"",
"object MyObject {",
"}")
.getBytes(StandardCharsets.UTF_8))))
.isEqualTo("com.myorg.kotlinpackage");
}
}

0 comments on commit 2046e85

Please sign in to comment.