1 package spoon.support.reflect.declaration; 2 3 import java.io.Serializable ; 4 import java.lang.annotation.Annotation ; 5 import java.util.List ; 6 import java.util.Set ; 7 import java.util.TreeSet ; 8 9 import spoon.reflect.Factory; 10 import spoon.reflect.declaration.CtAnnotation; 11 import spoon.reflect.declaration.CtElement; 12 import spoon.reflect.declaration.SourcePosition; 13 import spoon.reflect.reference.CtTypeReference; 14 import spoon.reflect.visitor.Filter; 15 import spoon.reflect.visitor.JavaPrettyPrinter; 16 import spoon.reflect.visitor.Query; 17 import spoon.support.query.AnnotationFilter; 18 import spoon.support.visitor.ElementReplacer; 19 import spoon.support.visitor.SignaturePrinter; 20 21 27 public abstract class CtElementImpl implements CtElement, Serializable { 28 29 transient Factory factory; 30 31 public Factory getFactory() { 32 return factory; 33 } 34 35 public void setFactory(Factory factory) { 36 this.factory = factory; 37 } 38 39 Set <CtAnnotation<? extends Annotation >> annotations = new TreeSet <CtAnnotation<? extends Annotation >>(); 40 41 String docComment; 42 43 CtElement parent; 44 45 SourcePosition position; 46 47 public CtElementImpl() { 48 super(); 49 } 50 51 public int compareTo(CtElement o) { 52 SignaturePrinter pr = new SignaturePrinter(); 53 pr.scan(this); 54 String current = pr.getSignature(); 55 pr.reset(); 56 pr.scan(o); 57 String other = pr.getSignature(); 58 if (current.length() <= 0 || other.length() <= 0) 59 throw new ClassCastException ("Unable to compare elements"); 60 return current.compareTo(other); 61 } 62 63 @SuppressWarnings ("unchecked") 64 public <A extends Annotation > A getAnnotation(Class <A> annotationType) { 65 for (CtAnnotation<? extends Annotation > a : getAnnotations()) { 66 if (a.getAnnotationType().toString().equals( 67 annotationType.getName())) { 68 return ((CtAnnotation<A>) a).getActualAnnotation(); 69 } 70 } 71 return null; 72 } 73 74 @SuppressWarnings ("unchecked") 76 public <A extends Annotation > CtAnnotation<A> getAnnotation( 77 CtTypeReference<A> annotationType) { 78 for (CtAnnotation<? extends Annotation > a : getAnnotations()) { 79 if (a.getAnnotationType().equals(annotationType)) { 80 return (CtAnnotation<A>) a; 81 } 82 } 83 return null; 84 } 85 86 public Set <CtAnnotation<? extends Annotation >> getAnnotations() { 87 return annotations; 88 } 89 90 public String getDocComment() { 91 return docComment; 92 } 93 94 public CtElement getParent() { 95 return parent; 96 } 97 98 @SuppressWarnings ("unchecked") 99 public <P extends CtElement> P getParent(Class <P> parentType) { 100 if (getParent() == null) 101 return null; 102 if (parentType.isAssignableFrom(getParent().getClass())) 103 return (P) getParent(); 104 return getParent().getParent(parentType); 105 } 106 107 public boolean hasParent(CtElement candidate) { 108 if (getParent() == null) 109 return false; 110 if (getParent() == candidate) 111 return true; 112 return getParent().hasParent(candidate); 113 } 114 115 public SourcePosition getPosition() { 116 return position; 117 } 118 119 @Override 120 public int hashCode() { 121 SignaturePrinter pr = new SignaturePrinter(); 122 pr.scan(this); 123 return pr.getSignature().hashCode(); 124 } 125 126 public void replace(CtElement element) { 127 ElementReplacer<CtElement> translator = new ElementReplacer<CtElement>( 128 this, element); 129 getParent().accept(translator); 130 } 131 132 public void replace(Filter<? extends CtElement> replacementPoints, 133 CtElement element) { 134 List <? extends CtElement> l = Query 135 .getElements(this, replacementPoints); 136 for (CtElement e : l) { 137 e.replace(element); 138 } 139 } 140 141 public void setAnnotations( 142 Set <CtAnnotation<? extends Annotation >> annotations) { 143 this.annotations = annotations; 144 } 145 146 public void setDocComment(String docComment) { 147 this.docComment = docComment; 148 } 149 150 public void setParent(CtElement parentElement) { 151 this.parent = parentElement; 152 } 153 154 public void setPosition(SourcePosition position) { 155 this.position = position; 156 } 157 158 @Override 159 public String toString() { 160 JavaPrettyPrinter printer = new JavaPrettyPrinter(); 161 printer.scan(this); 162 return printer.toString(); 163 } 164 165 @SuppressWarnings ("unchecked") 166 public <E extends CtElement> List <E> getAnnotatedChildren( 167 Class <? extends Annotation > annotationType) { 168 return Query.getElements(this, new AnnotationFilter(CtElement.class, 169 annotationType)); 170 } 171 } | Popular Tags |