-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy type annotations to the parameter of equals(Object).
If equals(Object) is being implemented because there is abstract redeclaration of it in the AutoValue class or an ancestor, and if that redeclaration has a type annotation such as @nullable on its parameter, then the generated implementation will also have that annotation. The same applies to AutoOneOf. We could go further and also copy annotations on the return types of equals/hashCode/toString, but for now there's no perceived need for that. Fixes #579. RELNOTES=In `@AutoValue`, copy `@Nullable` from `equals(@nullable Object)` to its implementation. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=188044994
- Loading branch information
1 parent
9ebbeb4
commit 1561e2c
Showing
10 changed files
with
279 additions
and
19 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
value/src/it/functional/src/test/java/com/google/auto/value/AutoOneOfJava8Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (C) 2018 Google, Inc. | ||
* | ||
* 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; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import java.lang.reflect.AnnotatedType; | ||
import java.lang.reflect.Method; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** | ||
* Tests for Java8-specific {@code @AutoOneOf} behaviour. | ||
* | ||
* @author [email protected] (Éamonn McManus) | ||
*/ | ||
@RunWith(JUnit4.class) | ||
public class AutoOneOfJava8Test { | ||
@AutoOneOf(EqualsNullable.Kind.class) | ||
public abstract static class EqualsNullable { | ||
|
||
@Target(ElementType.TYPE_USE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Nullable {} | ||
|
||
public enum Kind {THING} | ||
public abstract Kind kind(); | ||
public abstract String thing(); | ||
|
||
public static EqualsNullable ofThing(String thing) { | ||
return AutoOneOf_AutoOneOfJava8Test_EqualsNullable.thing(thing); | ||
} | ||
|
||
@Override | ||
public abstract boolean equals(@Nullable Object x); | ||
|
||
@Override | ||
public abstract int hashCode(); | ||
} | ||
|
||
/** | ||
* Tests that a type annotation on the parameter of {@code equals(Object)} is copied into the | ||
* implementation class. | ||
*/ | ||
@Test | ||
public void equalsNullable() throws ReflectiveOperationException { | ||
if (BugDetector.typeVisitorDropsAnnotations()) { | ||
System.err.println("TYPE VISITORS DO NOT SEE TYPE ANNOTATIONS, SKIPPING TEST"); | ||
return; | ||
} | ||
EqualsNullable x = EqualsNullable.ofThing("foo"); | ||
Class<? extends EqualsNullable> c = x.getClass(); | ||
Method equals = c.getMethod("equals", Object.class); | ||
assertThat(equals.getDeclaringClass()).isNotSameAs(EqualsNullable.class); | ||
AnnotatedType parameterType = equals.getAnnotatedParameterTypes()[0]; | ||
assertThat(parameterType.isAnnotationPresent(EqualsNullable.Nullable.class)) | ||
.isTrue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
value/src/it/functional/src/test/java/com/google/auto/value/BugDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright (C) 2018 Google, Inc. | ||
* | ||
* 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; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static com.google.testing.compile.CompilationSubject.assertThat; | ||
|
||
import com.google.testing.compile.Compilation; | ||
import com.google.testing.compile.Compiler; | ||
import com.google.testing.compile.JavaFileObjects; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Set; | ||
import javax.annotation.processing.AbstractProcessor; | ||
import javax.annotation.processing.RoundEnvironment; | ||
import javax.annotation.processing.SupportedAnnotationTypes; | ||
import javax.annotation.processing.SupportedSourceVersion; | ||
import javax.lang.model.SourceVersion; | ||
import javax.lang.model.element.AnnotationMirror; | ||
import javax.lang.model.element.ExecutableElement; | ||
import javax.lang.model.element.TypeElement; | ||
import javax.lang.model.type.DeclaredType; | ||
import javax.lang.model.type.TypeMirror; | ||
import javax.lang.model.util.ElementFilter; | ||
import javax.lang.model.util.SimpleTypeVisitor8; | ||
import javax.tools.JavaFileObject; | ||
|
||
/** | ||
* Detects javac bugs that might prevent tests from working. | ||
* | ||
* @author [email protected] (Éamonn McManus) | ||
*/ | ||
final class BugDetector { | ||
private BugDetector() {} | ||
|
||
/** | ||
* Returns true if {@link TypeMirror#accept} gives the unannotated type to the type visitor. It | ||
* should obviously receive the type that {@code accept} was called on, but in at least some | ||
* Java 8 versions it ends up being the unannotated one. | ||
*/ | ||
// I have not been able to find a reference for this bug. | ||
static boolean typeVisitorDropsAnnotations() { | ||
JavaFileObject testClass = JavaFileObjects.forSourceLines( | ||
"com.example.Test", | ||
"package com.example;", | ||
"", | ||
"import java.lang.annotation.*;", | ||
"", | ||
"abstract class Test {", | ||
" @Target(ElementType.TYPE_USE)", | ||
" @interface Nullable {}", | ||
"", | ||
" @Override public abstract boolean equals(@Nullable Object x);", | ||
"}"); | ||
BugDetectorProcessor bugDetectorProcessor = new BugDetectorProcessor(); | ||
Compilation compilation = | ||
Compiler.javac().withProcessors(bugDetectorProcessor).compile(testClass); | ||
assertThat(compilation).succeeded(); | ||
return bugDetectorProcessor.typeAnnotationsNotReturned; | ||
} | ||
|
||
@SupportedAnnotationTypes("*") | ||
@SupportedSourceVersion(SourceVersion.RELEASE_8) | ||
private static class BugDetectorProcessor extends AbstractProcessor { | ||
volatile boolean typeAnnotationsNotReturned; | ||
|
||
@Override | ||
public boolean process( | ||
Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { | ||
if (!roundEnv.processingOver()) { | ||
TypeElement test = processingEnv.getElementUtils().getTypeElement("com.example.Test"); | ||
ExecutableElement equals = ElementFilter.methodsIn(test.getEnclosedElements()).get(0); | ||
assertThat(equals.getSimpleName().toString()).isEqualTo("equals"); | ||
TypeMirror parameterType = equals.getParameters().get(0).asType(); | ||
List<AnnotationMirror> annotationsFromVisitor = | ||
parameterType.accept(new BugDetectorVisitor(), null); | ||
typeAnnotationsNotReturned = annotationsFromVisitor.isEmpty(); | ||
} | ||
return false; | ||
} | ||
|
||
private static class BugDetectorVisitor | ||
extends SimpleTypeVisitor8<List<AnnotationMirror>, Void> { | ||
@Override | ||
public List<AnnotationMirror> visitDeclared(DeclaredType t, Void p) { | ||
return Collections.unmodifiableList(t.getAnnotationMirrors()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters