1 /* 2 * Copyright (C) 2017 The Dagger Authors. 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 17 package dagger.internal.codegen; 18 19 import static com.google.common.base.Preconditions.checkArgument; 20 21 import com.squareup.javapoet.AnnotationSpec; 22 import java.util.Optional; 23 import javax.lang.model.element.AnnotationMirror; 24 import javax.lang.model.element.Element; 25 import javax.lang.model.element.Name; 26 27 final class GwtCompatibility { 28 29 /** 30 * Returns a {@code @GwtIncompatible} annotation that is applied to {@code binding}'s {@link 31 * Binding#bindingElement()} or any enclosing type. 32 */ gwtIncompatibleAnnotation(Binding binding)33 static Optional<AnnotationSpec> gwtIncompatibleAnnotation(Binding binding) { 34 checkArgument(binding.bindingElement().isPresent()); 35 Element element = binding.bindingElement().get(); 36 while (element != null) { 37 Optional<AnnotationSpec> gwtIncompatible = 38 element 39 .getAnnotationMirrors() 40 .stream() 41 .filter(annotation -> isGwtIncompatible(annotation)) 42 .map(AnnotationSpec::get) 43 .findFirst(); 44 if (gwtIncompatible.isPresent()) { 45 return gwtIncompatible; 46 } 47 element = element.getEnclosingElement(); 48 } 49 return Optional.empty(); 50 } 51 isGwtIncompatible(AnnotationMirror annotation)52 private static boolean isGwtIncompatible(AnnotationMirror annotation) { 53 Name simpleName = annotation.getAnnotationType().asElement().getSimpleName(); 54 return simpleName.contentEquals("GwtIncompatible"); 55 } 56 } 57