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 36 protected Boolean defaultAction(Object o, Void p) { 37 throw new IllegalArgumentException(); 38 } 39 40 @Override 41 public Boolean visitBoolean(boolean b, Void p) { 42 return b; 43 } 44 }, 45 null); 46 } 47 48 static TypeElement asType(AnnotationValue value) { 49 return value.accept( 50 new SimpleAnnotationValueVisitor6<TypeElement, Void>() { 51 @Override 52 protected TypeElement defaultAction(Object o, Void p) { 53 throw new IllegalArgumentException(); 54 } 55 56 @Override 57 public TypeElement visitType(TypeMirror t, Void p) { 58 return t.accept( 59 new SimpleTypeVisitor6<TypeElement, Void>() { 60 @Override 61 protected TypeElement defaultAction(TypeMirror e, Void p) { 62 throw new AssertionError(); 63 } 64 65 @Override 66 public TypeElement visitDeclared(DeclaredType t, Void p) { 67 return Iterables.getOnlyElement( 68 ElementFilter.typesIn(ImmutableList.of(t.asElement()))); 69 } 70 }, 71 null); 72 } 73 }, 74 null); 75 } 76 77 static ImmutableList<? extends AnnotationValue> asList(AnnotationValue value) { 78 return value.accept( 79 new SimpleAnnotationValueVisitor6<ImmutableList<? extends AnnotationValue>, Void>() { 80 @Override 81 protected ImmutableList<? extends AnnotationValue> defaultAction(Object o, Void p) { 82 throw new IllegalArgumentException(); 83 } 84 85 @Override 86 public ImmutableList<? extends AnnotationValue> visitArray( 87 List<? extends AnnotationValue> vals, Void p) { 88 return ImmutableList.copyOf(vals); 89 } 90 }, 91 null); 92 } 93 } 94