diff options
author | cushon <cushon@google.com> | 2019-08-07 10:12:52 -0700 |
---|---|---|
committer | David P. Baker <dpb@google.com> | 2019-08-19 12:23:56 -0400 |
commit | 12436ccd31725220fc90fe3775a58e39930508b5 (patch) | |
tree | 4510c0e29610fa7fb875c486700e0a2912f18d3b /factory | |
parent | 4e3366badb50fcef3c881694dedcf0ea61ba7ecc (diff) | |
download | auto-12436ccd31725220fc90fe3775a58e39930508b5.tar.gz |
Don't crash processing classes in the default packages
Fixes https://github.com/google/auto/issues/745
RELNOTES=n/a
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=262160859
Diffstat (limited to 'factory')
4 files changed, 63 insertions, 1 deletions
diff --git a/factory/src/main/java/com/google/auto/factory/processor/FactoryWriter.java b/factory/src/main/java/com/google/auto/factory/processor/FactoryWriter.java index 8037a831..e084eb0e 100644 --- a/factory/src/main/java/com/google/auto/factory/processor/FactoryWriter.java +++ b/factory/src/main/java/com/google/auto/factory/processor/FactoryWriter.java @@ -257,7 +257,7 @@ final class FactoryWriter { private static String getPackage(CharSequence fullyQualifiedName) { int lastDot = lastIndexOf(fullyQualifiedName, '.'); - return fullyQualifiedName.subSequence(0, lastDot).toString(); + return lastDot == -1 ? "" : fullyQualifiedName.subSequence(0, lastDot).toString(); } private static int lastIndexOf(CharSequence charSequence, char c) { diff --git a/factory/src/test/java/com/google/auto/factory/processor/AutoFactoryProcessorTest.java b/factory/src/test/java/com/google/auto/factory/processor/AutoFactoryProcessorTest.java index 671ddd3f..853be12c 100644 --- a/factory/src/test/java/com/google/auto/factory/processor/AutoFactoryProcessorTest.java +++ b/factory/src/test/java/com/google/auto/factory/processor/AutoFactoryProcessorTest.java @@ -433,6 +433,17 @@ public class AutoFactoryProcessorTest { .generatesSources(loadExpectedFile("expected/OnlyPrimitivesFactory.java")); } + @Test + public void defaultPackage() { + JavaFileObject file = JavaFileObjects.forResource("good/DefaultPackage.java"); + assertAbout(javaSource()) + .that(file) + .processedWith(new AutoFactoryProcessor()) + .compilesWithoutError() + .and() + .generatesSources(loadExpectedFile("expected/DefaultPackageFactory.java")); + } + private JavaFileObject loadExpectedFile(String resourceName) { try { List<String> sourceLines = Resources.readLines(Resources.getResource(resourceName), UTF_8); diff --git a/factory/src/test/resources/expected/DefaultPackageFactory.java b/factory/src/test/resources/expected/DefaultPackageFactory.java new file mode 100644 index 00000000..d1d4cc85 --- /dev/null +++ b/factory/src/test/resources/expected/DefaultPackageFactory.java @@ -0,0 +1,31 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +import javax.annotation.processing.Generated; +import javax.inject.Inject; + +@Generated( + value = "com.google.auto.factory.processor.AutoFactoryProcessor", + comments = "https://github.com/google/auto/tree/master/factory" + ) +public final class DefaultPackageFactory { + @Inject + public DefaultPackageFactory() {} + + public DefaultPackage create() { + return new DefaultPackage(); + } +} diff --git a/factory/src/test/resources/good/DefaultPackage.java b/factory/src/test/resources/good/DefaultPackage.java new file mode 100644 index 00000000..f4efee68 --- /dev/null +++ b/factory/src/test/resources/good/DefaultPackage.java @@ -0,0 +1,20 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +import com.google.auto.factory.AutoFactory; + +@AutoFactory +public final class DefaultPackage {} |