1 /* 2 * Copyright (C) 2014 Google, Inc. 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 dagger.internal.codegen.writer; 17 18 import com.google.common.collect.Iterables; 19 import com.google.common.collect.Lists; 20 import dagger.internal.codegen.writer.Writable.Context; 21 import java.io.IOException; 22 import java.lang.annotation.Annotation; 23 import java.util.EnumSet; 24 import java.util.List; 25 import java.util.Set; 26 import javax.lang.model.element.Modifier; 27 28 public abstract class Modifiable { 29 final Set<Modifier> modifiers; 30 final List<AnnotationWriter> annotations; 31 Modifiable()32 Modifiable() { 33 this.modifiers = EnumSet.noneOf(Modifier.class); 34 this.annotations = Lists.newArrayList(); 35 } 36 addModifiers(Modifier first, Modifier... rest)37 public void addModifiers(Modifier first, Modifier... rest) { 38 addModifiers(Lists.asList(first, rest)); 39 } 40 addModifiers(Iterable<Modifier> modifiers)41 public void addModifiers(Iterable<Modifier> modifiers) { 42 Iterables.addAll(this.modifiers, modifiers); 43 } 44 annotate(ClassName annotation)45 public AnnotationWriter annotate(ClassName annotation) { 46 AnnotationWriter annotationWriter = new AnnotationWriter(annotation); 47 this.annotations.add(annotationWriter); 48 return annotationWriter; 49 } 50 annotate(Class<? extends Annotation> annotation)51 public AnnotationWriter annotate(Class<? extends Annotation> annotation) { 52 return annotate(ClassName.fromClass(annotation)); 53 } 54 writeModifiers(Appendable appendable)55 Appendable writeModifiers(Appendable appendable) throws IOException { 56 for (Modifier modifier : modifiers) { 57 appendable.append(modifier.toString()).append(' '); 58 } 59 return appendable; 60 } 61 writeAnnotations(Appendable appendable, Context context)62 Appendable writeAnnotations(Appendable appendable, Context context) throws IOException { 63 for (AnnotationWriter annotationWriter : annotations) { 64 annotationWriter.write(appendable, context).append('\n'); 65 } 66 return appendable; 67 } 68 } 69