1 package type; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 /** 7 * A representation of a Java type. Handles type parameters, bounded types, arrays 8 * and inner types. 9 */ 10 public abstract class Type { 11 12 /** 13 * The different kinds of {@link Type}s. 14 */ 15 public enum Kind { 16 ARRAY, 17 BOUNDED, 18 DECLARED 19 } 20 21 /** 22 * The annotations on the outer type. Empty if there are none. 23 */ 24 private List<String> annotations; 25 26 /** 27 * Constructs a new type with no outer annotations. 28 */ Type()29 public Type() { 30 annotations = new ArrayList<String>(); 31 } 32 33 /** 34 * Adds an outer annotation to this type. 35 * @param annotation the annotation to add 36 */ addAnnotation(String annotation)37 public void addAnnotation(String annotation) { 38 annotations.add(annotation); 39 } 40 41 /** 42 * Replaces the annotations on this type with the given annotations. 43 * @param annotations the new annotations to be placed on this type 44 */ setAnnotations(List<String> annotations)45 public void setAnnotations(List<String> annotations) { 46 this.annotations = annotations; 47 } 48 49 /** 50 * Gets an outer annotation on this type at the given index. 51 * @param index the index 52 * @return the annotation 53 */ getAnnotation(int index)54 public String getAnnotation(int index) { 55 return annotations.get(index); 56 } 57 58 /** 59 * Gets a copy of the outer annotations on this type. This 60 * will be empty if there are none. 61 * @return the annotations 62 */ getAnnotations()63 public List<String> getAnnotations() { 64 return new ArrayList<String>(annotations); 65 } 66 67 /** 68 * Removes the annotations from this type. 69 */ clearAnnotations()70 public void clearAnnotations() { 71 annotations.clear(); 72 } 73 74 /** 75 * Gets the {@link Kind} of this {@link Type}. 76 * @return the kind 77 */ getKind()78 public abstract Kind getKind(); 79 } 80