1 /* 2 * Copyright 2019 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.Truth8.assertThat; 19 20 import java.util.Optional; 21 import javax.annotation.Nullable; 22 import org.junit.Test; 23 import org.junit.runner.RunWith; 24 import org.junit.runners.JUnit4; 25 26 /** 27 * Like {@link AutoValueTest}, but with code that doesn't build with at least some versions of 28 * Eclipse, and should therefore not be included in {@link CompileWithEclipseTest}. (The latter is 29 * not currently present in the open-source build.) 30 */ 31 @RunWith(JUnit4.class) 32 public class AutoValueNotEclipseTest { 33 // This produced the following error with JDT 4.6: 34 // Internal compiler error: java.lang.Exception: java.lang.IllegalArgumentException: element 35 // public abstract B setOptional(T) is not a member of the containing type 36 // com.google.auto.value.AutoValueTest.ConcreteOptional.Builder nor any of its superclasses at 37 // org.eclipse.jdt.internal.compiler.apt.dispatch.RoundDispatcher.handleProcessor(RoundDispatcher.java:169) 38 interface AbstractOptional<T> { optional()39 Optional<T> optional(); 40 41 interface Builder<T, B extends Builder<T, B>> { setOptional(@ullable T t)42 B setOptional(@Nullable T t); 43 } 44 } 45 46 @AutoValue 47 abstract static class ConcreteOptional implements AbstractOptional<String> { builder()48 static Builder builder() { 49 return new AutoValue_AutoValueNotEclipseTest_ConcreteOptional.Builder(); 50 } 51 52 @AutoValue.Builder 53 interface Builder extends AbstractOptional.Builder<String, Builder> { build()54 ConcreteOptional build(); 55 } 56 } 57 58 @Test genericOptionalOfNullable()59 public void genericOptionalOfNullable() { 60 ConcreteOptional empty = ConcreteOptional.builder().build(); 61 assertThat(empty.optional()).isEmpty(); 62 ConcreteOptional notEmpty = ConcreteOptional.builder().setOptional("foo").build(); 63 assertThat(notEmpty.optional()).hasValue("foo"); 64 } 65 } 66