1 /* 2 * Copyright 2013 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.factory.processor; 17 18 import com.google.common.collect.ImmutableList; 19 import com.google.common.collect.Iterables; 20 import java.util.List; 21 import javax.lang.model.element.AnnotationValue; 22 import javax.lang.model.element.TypeElement; 23 import javax.lang.model.type.DeclaredType; 24 import javax.lang.model.type.TypeMirror; 25 import javax.lang.model.util.ElementFilter; 26 import javax.lang.model.util.SimpleAnnotationValueVisitor6; 27 import javax.lang.model.util.SimpleTypeVisitor6; 28 29 final class AnnotationValues { AnnotationValues()30 private AnnotationValues() {} 31 asBoolean(AnnotationValue value)32 static boolean asBoolean(AnnotationValue value) { 33 return value.accept( 34 new SimpleAnnotationValueVisitor6<Boolean, Void>() { 35 @Override protected Boolean defaultAction(Object o, Void p) { 36 throw new IllegalArgumentException(); 37 } 38 39 @Override public Boolean visitBoolean(boolean b, Void p) { 40 return b; 41 } 42 }, null); 43 } 44 45 static TypeElement asType(AnnotationValue value) { 46 return value.accept( 47 new SimpleAnnotationValueVisitor6<TypeElement, Void>() { 48 @Override protected TypeElement defaultAction(Object o, Void p) { 49 throw new IllegalArgumentException(); 50 } 51 52 @Override public TypeElement visitType(TypeMirror t, Void p) { 53 return t.accept( 54 new SimpleTypeVisitor6<TypeElement, Void>() { 55 @Override 56 protected TypeElement defaultAction(TypeMirror e, Void p) { 57 throw new AssertionError(); 58 } 59 60 @Override 61 public TypeElement visitDeclared(DeclaredType t, Void p) { 62 return Iterables.getOnlyElement(ElementFilter.typesIn( 63 ImmutableList.of(t.asElement()))); 64 } 65 }, null); 66 } 67 }, null); 68 } 69 70 static ImmutableList<? extends AnnotationValue> asList(AnnotationValue value) { 71 return value.accept( 72 new SimpleAnnotationValueVisitor6<ImmutableList<? extends AnnotationValue>, Void>() { 73 @Override 74 protected ImmutableList<? extends AnnotationValue> defaultAction(Object o, Void p) { 75 throw new IllegalArgumentException(); 76 } 77 78 @Override 79 public ImmutableList<? extends AnnotationValue> visitArray( 80 List<? extends AnnotationValue> vals, Void p) { 81 return ImmutableList.copyOf(vals); 82 } 83 }, null); 84 } 85 } 86