Skip to content

Commit

Permalink
Remove the requirement for @AutoAnnotation methods to be static.
Browse files Browse the repository at this point in the history
In Java, people will typically want to keep using static methods. But in Kotlin, which doesn't have static methods as such, they can use instance methods instead. (AutoAnnotation is less useful in Kotlin since you can instantiate annotations there as if they were classes. But it still has some use cases.)

RELNOTES=`@AutoAnnotation` methods no longer need to be static. This makes it easier to use AutoAnnotation with Kotlin.
PiperOrigin-RevId: 421426975
  • Loading branch information
eamonnmcmanus authored and Google Java Core Libraries committed Jan 13, 2022
1 parent 8de959a commit cf55dc6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2022 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.
*/
package com.google.auto.value.annotations;

/** Test annotation for AutoAnnotation and Kotlin. */
public @interface TestAnnotation {
String value() default "default";
int integer() default 23;
String[] values() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.ArrayType;
Expand Down Expand Up @@ -153,14 +152,9 @@ private void process(RoundEnvironment roundEnv) {
}

private void processMethod(ExecutableElement method) {
if (!method.getModifiers().contains(Modifier.STATIC)) {
throw abortWithError(method, "@AutoAnnotation method must be static");
}

TypeElement annotationElement = getAnnotationReturnType(method);

Set<Class<?>> wrapperTypesUsedInCollections = wrapperTypesUsedInCollections(method);

ImmutableSet<Class<?>> wrapperTypesUsedInCollections = wrapperTypesUsedInCollections(method);
ImmutableMap<String, ExecutableElement> memberMethods = getMemberMethods(annotationElement);
TypeElement methodClass = MoreElements.asType(method.getEnclosingElement());
String pkg = TypeSimplifier.packageNameOf(methodClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,6 @@ public void testCorrect() {
assertThat(compilation).succeededWithoutWarnings();
}

@Test
public void testNotStatic() {
JavaFileObject testSource =
JavaFileObjects.forSourceLines(
"com.foo.Test",
"package com.foo;",
"",
"import com.example.TestAnnotation;",
"import com.google.auto.value.AutoAnnotation;",
"",
"class Test {",
" @AutoAnnotation TestAnnotation newTestAnnotation(int value) {",
" return new AutoAnnotation_Test_newTestAnnotation(value);",
" }",
"}");
Compilation compilation =
javac().withProcessors(new AutoAnnotationProcessor()).compile(TEST_ANNOTATION, testSource);
assertThat(compilation)
.hadErrorContaining("must be static")
.inFile(testSource)
.onLineContaining("TestAnnotation newTestAnnotation(int value)");
}

@Test
public void testDoesNotReturnAnnotation() {
JavaFileObject testSource =
Expand Down
22 changes: 22 additions & 0 deletions value/userguide/howto.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,28 @@ public class Names {
}
```

In Java the method will usually be static. In Kotlin, which doesn't have static
methods as such, a normal function can be used:
```kotlin
public class Names {
@AutoAnnotation public fun named(value: String): Named {
return AutoAnnotation_Names_named(value);
}
}
```
Kotlin also allows you to instantiate annotations directly, so you may not need
AutoAnnotation:
```kotlin
public class Names {
public fun named(value: String): Named {
return Named(value = value)
}
}
```
For more details, see the [`AutoAnnotation`
javadoc](http://github.com/google/auto/blob/master/value/src/main/java/com/google/auto/value/AutoAnnotation.java#L24).
Expand Down

0 comments on commit cf55dc6

Please sign in to comment.