1 package spoon.aval.processing; 2 3 import java.awt.Color ; 4 import java.awt.Component ; 5 import java.util.ArrayList ; 6 import java.util.HashMap ; 7 import java.util.List ; 8 import java.util.Map ; 9 10 import javax.swing.JFrame ; 11 import javax.swing.JLabel ; 12 import javax.swing.JScrollPane ; 13 import javax.swing.JTree ; 14 import javax.swing.event.TreeModelListener ; 15 import javax.swing.tree.TreeCellRenderer ; 16 import javax.swing.tree.TreeModel ; 17 import javax.swing.tree.TreePath ; 18 19 import spoon.processing.AbstractProcessor; 20 import spoon.processing.Severity; 21 import spoon.reflect.declaration.CtAnnotationType; 22 import spoon.reflect.declaration.CtElement; 23 import spoon.reflect.declaration.CtExecutable; 24 import spoon.reflect.declaration.CtField; 25 import spoon.reflect.declaration.CtPackage; 26 import spoon.reflect.declaration.CtSimpleType; 27 import spoon.reflect.declaration.SourcePosition; 28 import spoon.reflect.reference.CtExecutableReference; 29 import spoon.reflect.reference.CtReference; 30 import spoon.reflect.reference.CtTypeReference; 31 import spoon.reflect.visitor.CtStackScanner; 32 33 40 public class AnnotationProcessingMapProcessor extends 41 AbstractProcessor<CtAnnotationType<?>> { 42 43 private List <CtPackage> framework; 44 45 private List <APPoint> report; 46 47 @Override 48 public void init() { 49 super.init(); 50 framework = new ArrayList <CtPackage>(); 51 framework.addAll(getFactory().Package().getAll()); 52 framework.remove(getFactory().Package() 53 .get("org.hibernate.annotations")); 54 framework.remove(getFactory().Package().get("org.hibernate.validator")); 55 report = new ArrayList <APPoint>(); 59 } 60 61 public void process(CtAnnotationType<?> element) { 62 APPoint point = new APPoint(); 63 point.ap = element; 64 point.apPosition = checkTypeUse(element); 65 point.attribsPosition = new HashMap <CtField<?>, List <CtElement>>(); 66 List <CtField<?>> fields = element.getFields(); 67 68 for (CtField<?> field : fields) { 69 point.attribsPosition.put(field, checkAccess(field)); 70 } 71 report.add(point); 72 } 73 74 private List <CtElement> checkAccess(final CtField<?> field) { 75 final boolean[] found = new boolean[1]; 76 final List <CtElement> users = new ArrayList <CtElement>(); 77 found[0] = false; 78 for (CtPackage pack : framework) { 79 new CtStackScanner() { 80 @Override 81 public void scan(CtReference reference) { 82 if (reference instanceof CtExecutableReference) { 83 CtExecutableReference ref = (CtExecutableReference) reference; 84 if (ref.getSimpleName().equals(field.getSimpleName()) 85 && ref.getDeclaringType() 86 .equals( 87 field.getDeclaringType() 88 .getReference()) 89 && this.elementStack.peek().getParent( 90 CtExecutable.class) != null) { 91 users.add(this.elementStack.peek()); 92 } 93 } 94 super.scan(reference); 95 } 96 }.scan(pack); 97 } 98 99 return users; 100 } 101 102 private List <CtElement> checkTypeUse(final CtAnnotationType<?> element) { 103 final List <CtElement> users = new ArrayList <CtElement>(); 104 for (CtPackage pack : framework) { 105 new CtStackScanner() { 106 @Override 107 public void scan(CtReference reference) { 108 if (reference instanceof CtTypeReference<?>) { 109 CtTypeReference<?> ref = (CtTypeReference<?>) reference; 110 if (ref.equals(element.getReference()) 111 && this.elementStack.peek().getParent( 112 CtExecutable.class) != null) { 113 if (!users.contains(this.elementStack.peek())) 114 users.add(this.elementStack.peek()); 115 } 116 } 117 super.scan(reference); 118 } 119 120 }.scan(pack); 121 } 122 return users; 123 } 124 125 @Override 126 public void processingDone() { 127 super.processingDone(); 128 for (APPoint point : report) { 130 point.ap.getFactory().getEnvironment().report(this, 131 Severity.MESSAGE, point.ap, 132 "Annotation " + point.ap.getSimpleName()); 133 for (CtElement p : point.apPosition) { 134 p.getFactory().getEnvironment().report(this, Severity.MESSAGE, 135 p, "is processed "); 136 } 137 } 138 } 139 } 140 141 class APPoint { 142 CtAnnotationType ap; 143 144 List <CtElement> apPosition; 145 146 Map <CtField<?>, List <CtElement>> attribsPosition; 147 } 148 149 class ReportFrame extends JFrame { 150 153 private static final long serialVersionUID = -5118768893183502357L; 154 155 class Renderer implements TreeCellRenderer { 156 157 JLabel lbl = new JLabel (); 158 159 public Component getTreeCellRendererComponent(JTree tree, Object value, 160 boolean selected, boolean expanded, boolean leaf, int row, 161 boolean hasFocus) { 162 163 if (selected) { 164 lbl.setBackground(Color.green); 165 } else { 166 lbl.setBackground(Color.white); 167 } 168 169 if (value instanceof CtSimpleType) { 170 CtSimpleType stV = (CtSimpleType) value; 171 lbl.setText(stV.getSimpleName()); 172 } else if (value instanceof CtElement) { 173 SourcePosition sp = (SourcePosition) ((CtElement) value) 174 .getPosition(); 175 lbl.setText(sp.getFile() + " : " + sp.getLine()); 176 } else if (value instanceof List ) { 177 lbl.setText("Report"); 178 } else 179 lbl.setText(value.toString()); 180 return lbl; 181 } 182 183 } 184 185 JTree tree; 186 187 public ReportFrame(List <APPoint> r) { 188 tree = new JTree (); 189 tree.setModel(new ReportModel(r)); 190 tree.setCellRenderer(new Renderer ()); 191 JScrollPane jsp = new JScrollPane (tree); 192 tree.setScrollsOnExpand(true); 193 this.getContentPane().add(jsp); 195 this.setSize(640, 480); 196 } 197 198 class ReportModel implements TreeModel { 199 List <APPoint> rep; 200 201 List <TreeModelListener > list; 202 203 public ReportModel(List <APPoint> r) { 204 super(); 205 rep = r; 206 list = new ArrayList <TreeModelListener >(); 207 208 } 209 210 public void addTreeModelListener(TreeModelListener l) { 211 list.add(l); 212 } 213 214 public Object getChild(Object parent, int index) { 215 if (parent instanceof List ) { 216 return rep.get(index).ap; 217 } 218 for (APPoint point : rep) { 219 if (point.ap.equals(parent)) { 220 return point.apPosition.get(index); 221 } 222 } 226 return null; 227 } 228 229 public int getChildCount(Object parent) { 230 if (parent instanceof List ) { 231 return rep.size(); 232 } 233 for (APPoint point : rep) { 234 if (point.ap.equals(parent)) { 235 return point.apPosition.size(); 236 } 237 } 241 return 0; 242 } 243 244 public int getIndexOfChild(Object parent, Object child) { 245 for (APPoint point : rep) { 246 if (point.ap.equals(parent)) { 247 } 248 } 252 return 0; 253 } 254 255 public Object getRoot() { 256 return rep; 258 } 259 260 public boolean isLeaf(Object node) { 261 return node instanceof SourcePosition; 262 } 263 264 public void removeTreeModelListener(TreeModelListener l) { 265 list.remove(l); 266 } 267 268 public void valueForPathChanged(TreePath path, Object newValue) { 269 271 } 272 273 } 274 } 275 | Popular Tags |