1 /* 2 * Copyright 2014 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.processor; 17 18 import static java.util.stream.Collectors.joining; 19 20 import java.util.Collections; 21 import java.util.List; 22 import java.util.Map; 23 import java.util.Optional; 24 import javax.lang.model.element.AnnotationMirror; 25 import javax.lang.model.element.AnnotationValue; 26 import javax.lang.model.element.ExecutableElement; 27 import javax.lang.model.element.Name; 28 import javax.lang.model.element.TypeElement; 29 30 class GwtCompatibility { 31 private final Optional<AnnotationMirror> gwtCompatibleAnnotation; 32 GwtCompatibility(TypeElement type)33 GwtCompatibility(TypeElement type) { 34 Optional<AnnotationMirror> gwtCompatibleAnnotation = Optional.empty(); 35 List<? extends AnnotationMirror> annotations = type.getAnnotationMirrors(); 36 for (AnnotationMirror annotation : annotations) { 37 Name name = annotation.getAnnotationType().asElement().getSimpleName(); 38 if (name.contentEquals("GwtCompatible")) { 39 gwtCompatibleAnnotation = Optional.of(annotation); 40 } 41 } 42 this.gwtCompatibleAnnotation = gwtCompatibleAnnotation; 43 } 44 gwtCompatibleAnnotation()45 Optional<AnnotationMirror> gwtCompatibleAnnotation() { 46 return gwtCompatibleAnnotation; 47 } 48 49 // Get rid of the misconceived <? extends ExecutableElement, ? extends AnnotationValue> 50 // in the return type of getElementValues(). getElementValues(AnnotationMirror annotation)51 static Map<ExecutableElement, AnnotationValue> getElementValues(AnnotationMirror annotation) { 52 return Collections.<ExecutableElement, AnnotationValue>unmodifiableMap( 53 annotation.getElementValues()); 54 } 55 gwtCompatibleAnnotationString()56 String gwtCompatibleAnnotationString() { 57 if (gwtCompatibleAnnotation.isPresent()) { 58 AnnotationMirror annotation = gwtCompatibleAnnotation.get(); 59 TypeElement annotationElement = (TypeElement) annotation.getAnnotationType().asElement(); 60 String annotationArguments; 61 if (annotation.getElementValues().isEmpty()) { 62 annotationArguments = ""; 63 } else { 64 annotationArguments = 65 getElementValues(annotation) 66 .entrySet() 67 .stream() 68 .map(e -> e.getKey().getSimpleName() + " = " + e.getValue()) 69 .collect(joining(", ", "(", ")")); 70 } 71 return "@" + annotationElement.getQualifiedName() + annotationArguments; 72 } else { 73 return ""; 74 } 75 } 76 } 77