1 /* 2 * Copyright (C) 2016 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.javapoet; 18 19 import static com.google.common.base.Preconditions.checkArgument; 20 21 import com.google.common.collect.ImmutableList; 22 import com.google.common.collect.ImmutableSet; 23 import com.google.common.collect.Lists; 24 import com.squareup.javapoet.AnnotationSpec; 25 26 /** Static factories to create {@link AnnotationSpec}s. */ 27 public final class AnnotationSpecs { 28 /** Values for an {@link SuppressWarnings} annotation. */ 29 public enum Suppression { 30 RAWTYPES("rawtypes"), 31 UNCHECKED("unchecked"), 32 FUTURE_RETURN_VALUE_IGNORED("FutureReturnValueIgnored"), 33 KOTLIN_INTERNAL("KotlinInternal", "KotlinInternalInJava"), 34 CAST("cast") 35 ; 36 37 private final ImmutableList<String> values; 38 Suppression(String... values)39 Suppression(String... values) { 40 this.values = ImmutableList.copyOf(values); 41 } 42 } 43 44 /** Creates an {@link AnnotationSpec} for {@link SuppressWarnings}. */ suppressWarnings(Suppression first, Suppression... rest)45 public static AnnotationSpec suppressWarnings(Suppression first, Suppression... rest) { 46 return suppressWarnings(ImmutableSet.copyOf(Lists.asList(first, rest))); 47 } 48 49 /** Creates an {@link AnnotationSpec} for {@link SuppressWarnings}. */ suppressWarnings(ImmutableSet<Suppression> suppressions)50 public static AnnotationSpec suppressWarnings(ImmutableSet<Suppression> suppressions) { 51 checkArgument(!suppressions.isEmpty()); 52 AnnotationSpec.Builder builder = AnnotationSpec.builder(SuppressWarnings.class); 53 suppressions.stream() 54 .flatMap(suppression -> suppression.values.stream()) 55 .forEach(value -> builder.addMember("value", "$S", value)); 56 return builder.build(); 57 } 58 AnnotationSpecs()59 private AnnotationSpecs() {} 60 } 61