1 package spoon.support.reflect.declaration; 2 3 import java.lang.annotation.Annotation ; 4 import java.lang.annotation.Inherited ; 5 import java.util.ArrayList ; 6 import java.util.List ; 7 import java.util.Set ; 8 import java.util.TreeSet ; 9 10 import spoon.reflect.declaration.CtAnnotation; 11 import spoon.reflect.declaration.CtAnonymousExecutable; 12 import spoon.reflect.declaration.CtClass; 13 import spoon.reflect.declaration.CtConstructor; 14 import spoon.reflect.declaration.CtField; 15 import spoon.reflect.declaration.CtMethod; 16 import spoon.reflect.declaration.CtSimpleType; 17 import spoon.reflect.declaration.CtType; 18 import spoon.reflect.reference.CtTypeReference; 19 import spoon.reflect.visitor.CtVisitor; 20 21 26 public class CtClassImpl<T extends Object > extends CtTypeImpl<T> implements 27 CtClass<T> { 28 private static final long serialVersionUID = 1L; 29 30 List <CtAnonymousExecutable> anonymousExecutable = new ArrayList <CtAnonymousExecutable>(); 31 32 Set <CtConstructor> constructors = new TreeSet <CtConstructor>(); 33 34 CtTypeReference superClass; 35 36 public void accept(CtVisitor v) { 37 v.visitCtClass(this); 38 } 39 40 51 public Set <CtMethod<?>> getAllMethods() { 52 Set <CtMethod<?>> ret = new TreeSet <CtMethod<?>>(); 53 ret.addAll(getMethods()); 54 55 if (getSuperclass() != null && getSuperclass().getDeclaration() != null) { 56 CtType<?> t = (CtType<?>) getSuperclass().getDeclaration(); 57 ret.addAll(t.getMethods()); 58 } 59 return ret; 60 } 61 62 public List <CtAnonymousExecutable> getAnonymousExecutables() { 63 return anonymousExecutable; 64 } 65 66 public CtConstructor<T> getConstructor(CtTypeReference<?>... parameterTypes) { 67 for (CtConstructor<T> c : constructors) { 68 boolean cont = c.getParameters().size() == parameterTypes.length; 69 for (int i = 0; cont && i < c.getParameters().size() 70 && i < parameterTypes.length; i++) { 71 if (!c.getParameters().get(i).getType().getQualifiedName() 72 .equals(parameterTypes[i].getQualifiedName())) { 73 cont = false; 74 } 75 } 76 if (cont) { 77 return c; 78 } 79 } 80 return null; 81 } 82 83 public Set <CtConstructor> getConstructors() { 84 return constructors; 85 } 86 87 @Override 88 @SuppressWarnings ("unchecked") 89 public List <CtField<?>> getFields() { 90 return (List <CtField<?>>) super.getFields(); 91 } 92 93 public CtTypeReference<?> getSuperclass() { 94 return superClass; 95 } 96 97 public void setAnonymousExecutables(List <CtAnonymousExecutable> e) { 98 anonymousExecutable.clear(); 99 anonymousExecutable.addAll(e); 100 } 101 102 public void setConstructors(Set <CtConstructor> constructors) { 103 this.constructors = constructors; 104 } 105 106 public void setSuperclass(CtTypeReference<?> superClass) { 107 this.superClass = superClass; 108 } 109 110 @Override 111 public Set <CtAnnotation<? extends Annotation >> getAnnotations() { 112 Set <CtAnnotation<? extends Annotation >> annot = super.getAnnotations(); 113 114 if (getSuperclass() != null) { 115 CtSimpleType sup = getSuperclass().getDeclaration(); 116 if (sup != null) { 117 for (CtAnnotation<? extends Annotation > a : sup 118 .getAnnotations()) { 119 if (a.getAnnotationType().getAnnotation(Inherited .class) != null) { 120 annot.add(a); 121 } 122 } 123 } 124 } 125 return annot; 126 } 127 128 public boolean isSubtypeOf(CtTypeReference<?> type) { 129 if (getSuperclass() != null && getSuperclass().isSubtypeOf(type)) 130 return true; 131 for (CtTypeReference<?> ref : getSuperInterfaces()) { 132 if (ref.isSubtypeOf(type)) 133 return true; 134 } 135 return false; 136 } 137 } 138 | Popular Tags |