1 /* 2 * Copyright 2018 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.google.auto.value; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import java.lang.annotation.ElementType; 21 import java.lang.annotation.Retention; 22 import java.lang.annotation.RetentionPolicy; 23 import java.lang.annotation.Target; 24 import java.lang.reflect.AnnotatedType; 25 import java.lang.reflect.Method; 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 import org.junit.runners.JUnit4; 29 30 /** 31 * Tests for Java8-specific {@code @AutoOneOf} behaviour. 32 * 33 * @author emcmanus@google.com (Éamonn McManus) 34 */ 35 @RunWith(JUnit4.class) 36 public class AutoOneOfJava8Test { 37 @AutoOneOf(EqualsNullable.Kind.class) 38 public abstract static class EqualsNullable { 39 40 @Target(ElementType.TYPE_USE) 41 @Retention(RetentionPolicy.RUNTIME) 42 public @interface Nullable {} 43 44 public enum Kind { 45 THING 46 } 47 kind()48 public abstract Kind kind(); 49 thing()50 public abstract String thing(); 51 ofThing(String thing)52 public static EqualsNullable ofThing(String thing) { 53 return AutoOneOf_AutoOneOfJava8Test_EqualsNullable.thing(thing); 54 } 55 56 @Override equals(@ullable Object x)57 public abstract boolean equals(@Nullable Object x); 58 59 @Override hashCode()60 public abstract int hashCode(); 61 } 62 63 /** 64 * Tests that a type annotation on the parameter of {@code equals(Object)} is copied into the 65 * implementation class. 66 */ 67 @Test equalsNullable()68 public void equalsNullable() throws ReflectiveOperationException { 69 EqualsNullable x = EqualsNullable.ofThing("foo"); 70 Class<? extends EqualsNullable> c = x.getClass(); 71 Method equals = c.getMethod("equals", Object.class); 72 assertThat(equals.getDeclaringClass()).isNotSameInstanceAs(EqualsNullable.class); 73 AnnotatedType parameterType = equals.getAnnotatedParameterTypes()[0]; 74 assertThat(parameterType.isAnnotationPresent(EqualsNullable.Nullable.class)).isTrue(); 75 } 76 } 77