You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AffectedFilesReporter.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* @author Wes Isberg */
  2. // START-SAMPLE api-asm-listAffectedFiles Walk model to list affected files
  3. package org.aspectj.samples;
  4. import org.aspectj.tools.ajc.Main;
  5. import org.aspectj.asm.*;
  6. import org.aspectj.bridge.*;
  7. import java.io.*;
  8. import java.util.*;
  9. /**
  10. * Run ajc and write "affected.lst" file listing those files
  11. * affected by advice or inter-type declarations.
  12. *
  13. * WARNING: Does not emit info messages for uses-pointcut dependencies.
  14. * @author Wes Isberg
  15. */
  16. public class AffectedFilesReporter implements Runnable {
  17. /**
  18. * Wrapper for ajc that emits list of affected files.
  19. * @param args the String[] of args for ajc,
  20. * optionally prefixed with -to {file}
  21. * @throws IOException if unable to write to {file}
  22. */
  23. public static void main(String[] args) throws IOException {
  24. Main main = new Main();
  25. PrintStream toConfig = System.out;
  26. FileOutputStream fin = null;
  27. if ((args.length > 1) && ("-to".equals(args[0]))) {
  28. File config = new File(args[1]);
  29. fin = new FileOutputStream(config);
  30. toConfig = new PrintStream(fin, true);
  31. String[] temp = new String[args.length-2];
  32. System.arraycopy(args, 2, temp, 0, temp.length);
  33. args = temp;
  34. }
  35. Runnable runner = new AffectedFilesReporter(toConfig);
  36. main.setCompletionRunner(runner);
  37. // should add -emacssym to args if not already there
  38. main.runMain(args, false);
  39. if (null != fin) {
  40. fin.close();
  41. }
  42. }
  43. final PrintStream sink;
  44. public AffectedFilesReporter(PrintStream sink) {
  45. this.sink = (null == sink ? System.out : sink);
  46. }
  47. public void run() {
  48. IHierarchy hierarchy = AsmManager.getDefault().getHierarchy();
  49. if (null == hierarchy) {
  50. sink.println("# no structure model - use -emacssym option");
  51. return;
  52. }
  53. List /*IProgramElement*/ nodes = new ArrayList();
  54. List /*IProgramElement*/ newNodes = new ArrayList();
  55. // root could be config file or blank root - either way, use kids
  56. nodes.addAll(hierarchy.getRoot().getChildren());
  57. while (0 < nodes.size()) {
  58. for (ListIterator it = nodes.listIterator(); it.hasNext();) {
  59. IProgramElement node = (IProgramElement) it.next();
  60. if (node.getKind().isSourceFileKind()) {
  61. if (isAffected(node)) {
  62. ISourceLocation loc = node.getSourceLocation();
  63. sink.println(loc.getSourceFile().getPath());
  64. }
  65. } else {
  66. // XXX uncertain of structure - traverse all??
  67. newNodes.addAll(node.getChildren());
  68. }
  69. it.remove();
  70. }
  71. nodes.addAll(newNodes);
  72. newNodes.clear();
  73. }
  74. }
  75. /**
  76. * Return true if this file node is affected by any aspects.
  77. * Recursively search children for any effect,
  78. * and halt on first affect found.
  79. * @param node the IProgramElementNode for a source file
  80. * @return true if affected.
  81. */
  82. private boolean isAffected(final IProgramElement fileNode) {
  83. final IRelationshipMap map =
  84. AsmManager.getDefault().getRelationshipMap();
  85. List /*IProgramElement*/ nodes = new ArrayList();
  86. List /*IProgramElement*/ newNodes = new ArrayList();
  87. nodes.add(fileNode);
  88. while (0 < nodes.size()) {
  89. for (ListIterator iter = nodes.listIterator();
  90. iter.hasNext();) {
  91. IProgramElement node = (IProgramElement) iter.next();
  92. List relations = map.get(node);
  93. if (null != relations) {
  94. for (Iterator riter = relations.iterator();
  95. riter.hasNext();) {
  96. IRelationship.Kind kind =
  97. ((IRelationship) riter.next()).getKind();
  98. if ((kind == IRelationship.Kind.ADVICE)
  99. || (kind == IRelationship.Kind.DECLARE_INTER_TYPE)) {
  100. return true;
  101. }
  102. }
  103. }
  104. iter.remove();
  105. newNodes.addAll(node.getChildren());
  106. }
  107. nodes.addAll(newNodes);
  108. newNodes.clear();
  109. }
  110. return false;
  111. }
  112. }
  113. // END-SAMPLE api-asm-listAffectedFiles Walk model to list affected files