1 20 21 package spoon.aval.processing; 22 23 import java.util.ArrayList ; 24 import java.util.List ; 25 26 import spoon.processing.AbstractProcessor; 27 import spoon.processing.Property; 28 import spoon.processing.Severity; 29 import spoon.reflect.Factory; 30 import spoon.reflect.declaration.CtAnnotationType; 31 import spoon.reflect.declaration.CtField; 32 import spoon.reflect.declaration.CtPackage; 33 import spoon.reflect.reference.CtExecutableReference; 34 import spoon.reflect.reference.CtPackageReference; 35 import spoon.reflect.reference.CtReference; 36 import spoon.reflect.reference.CtTypeReference; 37 import spoon.reflect.visitor.CtScanner; 38 39 55 56 public class CompletenessCheckerProcessor extends 57 AbstractProcessor<CtAnnotationType<?>> { 58 59 private List <CtPackage> framework; 60 61 private List <CtPackageReference> annotations; 62 63 @Property 64 String [] checkPackages; 65 66 @Property 67 String [] skipAnnotationsIn; 68 69 @Property 70 boolean checkAll = false; 71 72 77 @Override 78 public void init() { 79 super.init(); 80 framework = new ArrayList <CtPackage>(); 81 annotations = new ArrayList <CtPackageReference>(); 82 configurePackages(); 83 } 84 85 89 protected void configurePackages() { 90 94 Factory fac = getFactory(); 95 if (checkAll) { 96 framework.addAll(getFactory().Package().getAll()); 97 } else { 98 for (String check : checkPackages) { 99 framework.add(fac.Package().getOrCreate(check)); 100 } 101 } 102 103 for (String skip : skipAnnotationsIn) { 104 annotations.add(fac.Package().createReference(skip)); 105 } 106 107 } 108 109 114 public void process(CtAnnotationType<?> element) { 115 116 if (shouldSkip(element)) 117 return; 118 119 checkTypeUse(element); 120 List <CtField<?>> fields = element.getFields(); 121 122 for (CtField<?> field : fields) { 123 checkAccess(field); 124 } 125 126 } 127 128 private boolean shouldSkip(CtAnnotationType<?> element) { 129 CtPackageReference elPackRef = element.getPackage().getReference(); 130 for (CtPackageReference ref : annotations) { 131 if (elPackRef.getSimpleName().startsWith(ref.getSimpleName())) { 132 return true; 133 } 134 } 135 return false; 136 } 137 138 private void checkAccess(final CtField<?> field) { 139 final boolean[] found = new boolean[1]; 140 found[0] = false; 141 for (CtPackage pack : framework) { 142 new CtScanner() { 143 @Override 144 public void scan(CtReference reference) { 145 if (reference instanceof CtExecutableReference) { 146 CtExecutableReference ref = (CtExecutableReference) reference; 147 if (ref.getSimpleName().equals(field.getSimpleName()) 148 && ref.getDeclaringType() 149 .equals( 150 field.getDeclaringType() 151 .getReference())) { 152 found[0] = true; 153 } 154 } 155 super.scan(reference); 156 } 157 }.scan(pack); 158 } 159 160 if (!found[0]) 161 getFactory().getEnvironment().report(this, Severity.WARNING, field, 162 "This Field is never processed!"); 163 164 } 165 166 private void checkTypeUse(final CtAnnotationType<?> element) { 167 final boolean[] found = new boolean[1]; 168 found[0] = false; 169 for (CtPackage pack : framework) { 170 new CtScanner() { 171 @Override 172 public void scan(CtReference reference) { 173 if (reference instanceof CtTypeReference<?>) { 174 CtTypeReference<?> ref = (CtTypeReference<?>) reference; 175 if (ref.equals(element.getReference())) { 176 found[0] = true; 177 } 178 } 179 super.scan(reference); 180 } 181 }.scan(pack); 182 } 183 184 if (!found[0]) 185 getFactory().getEnvironment().report(this, Severity.WARNING, 186 element, "This annotation is never processed!"); 187 188 } 189 190 } 191 | Popular Tags |