1 20 21 package spoon.aval.processing; 22 23 import java.lang.annotation.Annotation ; 24 import java.util.List ; 25 26 import spoon.aval.annotation.ReplacesAnnotationInPackage; 27 import spoon.processing.AbstractManualProcessor; 28 import spoon.processing.Property; 29 import spoon.processing.Severity; 30 import spoon.reflect.Factory; 31 import spoon.reflect.declaration.CtAnnotation; 32 import spoon.reflect.declaration.CtAnnotationType; 33 import spoon.reflect.declaration.CtPackage; 34 import spoon.reflect.visitor.Filter; 35 import spoon.reflect.visitor.Query; 36 import spoon.support.query.AnnotationFilter; 37 38 46 public class DummyPreProcessor extends AbstractManualProcessor { 47 48 52 @Property 53 String [] packs; 54 55 @Override 56 public void init() { 57 super.init(); 58 } 59 60 66 public void process() { 67 68 Factory fac = getFactory(); 69 if (packs == null) { 70 List <CtAnnotationType<? extends Annotation >> types = Query 71 .getElements( 72 fac, 73 new Filter<CtAnnotationType<? extends Annotation >>() { 74 75 @SuppressWarnings ("unchecked") 76 public Class <CtAnnotationType<? extends Annotation >> getType() { 77 return (Class ) CtAnnotationType.class; 79 } 80 81 public boolean matches( 82 CtAnnotationType<? extends Annotation > element) { 83 return element 84 .getAnnotation(ReplacesAnnotationInPackage.class) != null; 85 } 86 87 }); 88 89 for (CtAnnotationType<? extends Annotation > type : types) { 90 replaceAnnotationImpl(fac, type); 91 } 92 } else { 93 for (String packag : packs) { 94 processDummyPackage(fac, packag); 95 } 96 } 97 } 98 99 private void processDummyPackage(Factory fac, String packag) { 100 List <CtAnnotationType<? extends Annotation >> dummys = getPackages(fac, 101 packag); 102 103 for (CtAnnotationType<? extends Annotation > type : dummys) { 104 replaceAnnotationImpl(fac, type); 105 } 106 } 107 108 private void replaceAnnotationImpl(Factory fac, 109 CtAnnotationType<? extends Annotation > type) { 110 CtAnnotation<ReplacesAnnotationInPackage> rep = type.getAnnotation(fac 111 .Type().createReference(ReplacesAnnotationInPackage.class)); 112 String repPackName = (String ) rep.getElementValue("value"); 113 CtPackage repPack = fac.Package().get(repPackName); 114 if (repPack == null) { 115 fac.getEnvironment().report( 116 this, 117 Severity.MESSAGE, 118 rep, 119 "Replaces annotation refers to unknown package " 120 + repPackName); 121 repPack = fac.Package().getOrCreate(repPackName); 122 } 123 type.getParent(CtPackage.class).getTypes().remove(type); 124 repPack.getTypes().add(type); 125 type.setParent(repPack); 126 } 128 129 private List <CtAnnotationType<? extends Annotation >> getPackages( 130 Factory fac, String packag) { 131 CtPackage dummyP = fac.Package().get(packag); 132 List <CtAnnotationType<? extends Annotation >> dummys = Query 133 .getElements( 134 dummyP, 135 new AnnotationFilter<CtAnnotationType<? extends Annotation >>( 136 ReplacesAnnotationInPackage.class)); 137 return dummys; 138 } 139 } 140 | Popular Tags |