项目作者: mayankrastogi

项目描述 :
An annotation processor to verify correctness of design pattern implementations in Java source code.
高级语言: Scala
项目地址: git://github.com/mayankrastogi/design-pattern-verifier.git
创建时间: 2019-11-17T19:59:42Z
项目社区:https://github.com/mayankrastogi/design-pattern-verifier

开源协议:MIT License

下载


CS 474 - Object Oriented Languages and Environments

Homework 2 - Design Pattern Verifier (Iterator Pattern)


Overview

The objective of this homework was to write an annotation processor that will verify whether a certain class, annotated with the packaged annotations, uses the said design pattern correctly.

My implementation consists of two annotation processors that verify the usage of the Iterator design pattern. Three example classes are provided that demonstrate the use of the iterator design pattern and also demonstrate the various cases that my annotation processor is able to handle. Furthermore, an exhaustive test suite is provided with the annotation processor to verify its correctness.

Instructions

Prerequisites

Running the application

  1. Clone or download this repository onto your system
  2. Open the Command Prompt (if using Windows) or the Terminal (if using Linux/Mac) and browse to the project directory
  3. Build the project using Gradle; this will run the test cases and the annotation processor on the provided examples

    1. gradlew clean build
  4. If you want detailed debug messages to be printed during annotation processing, build the project using the following command

    1. gradlew clean build -PdebugAnnotationProcessor
  5. Run the project to see the output from the examples program

    1. gradlew run

Project Structure

This project makes use of Gradle’s multi-project build and is divided into 3 sub-projects:

  1. annotations: Contains all the annotations defined in the project. Tha annotations are written in Java.
  2. annotation-processor: Contains all the annotation processors that verify correctness of classes annotated with the annotation from the annotations sub-project. Also contains test cases for testing the annotation processors themselves. The annotation processors are written in Scala.
  3. examples: Contains 3 example classes that implement the Iterator design pattern and are annotated with annotations from the annotations project. A main program, written in Scala, demonstrates how these classes may be used. The example classes are written in Java.

What is the Iterator Design Pattern?

According to the Gang Of Four Design Pattern Book, the iterator design pattern is a behavioral design pattern which “provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation”.

There are two main participants in the implementation of the iterator design pattern:

  1. Aggregate: An aggregate object which provides a way of creating an iterator.
  2. Iterator: Provides a set of methods for accessing and traversing the elements of the aggregate. At minimum, an iterator should allow the consumer to get the next element and check the availability of more elements.

The Annotations

This project provides two main annotations for the two main participants of the iterator design pattern - @IterableAggregate and @Iterator. Both of these annotations contain child annotations which can be used to annotate the methods in the classes annotated with the two main annotations. These annotations collectively help implement the iterator design pattern.

All of these annotations have a retention policy of SOURCE, meaning they are only available during compile-time annotation processing.

The @IterableAggregate Annotation

The @IterableAggregate annotation can be used to annotate a class that represents an aggregate. Although an @IterableAggregate can be applied on any TYPE declaration, the annotation processor restricts its usage to annotate only class declarations.

The value() of the @IterableAggregate annotation specifies the class of iterator object it exposes.

It provides a child annotation - @IteratorFactory, which can be used to annotate a factory method that creates a new instance of an iterator for accessing elements of the parent iterable aggregate.

The @Iterator Annotation

The @Iterator annotation can be used to annotate an iterator which provides a way to sequentially extract elements from an underlying aggregate class. Although an @Iterator can be applied on any TYPE declaration, the annotation processor restricts its usage to annotate only class declarations.

The value() of the @Iterator annotation specifies the class of the object that can be extracted from this iterator on each iteration. The treatWarningsAsErrors() property denotes whether cases, which usually generate a warning, should raise errors instead or not.

It provides three child annotations:

  1. @CurrentItem: Can be used to annotate a method that returns the current item in the current state of iteration.
  2. @IsDone: Can be used to annotate a method that tells the consumer whether the iterator has finished iterating through all its elements.
  3. @NextItem: Can be used to annotate a method that returns the next item in the current state of iteration.

The Annotation Processor

The project provides two concrete annotation processors for processing the two main participants of the iterator design pattern. These annotation processors operate on Java source files at compile-time to process elements annotated with the annotations mentioned above.

The AbstractAnnotationProcessor class

This is the abstract base class for the other two concrete annotation processors. It provides helper methods and convenience methods that can be used by child annotation processors for performing common tasks and easily print logs at the required level.

The AbstractAnnotationProcessor provides a way of switching debugging mode on and off. If the annotation processor is passed the option AnnotationProcessor.debug, irrespective of its value, debug messages are also logged during the annotation processing.

The gradle build task for the examples project has been configured to pass this option to the annotation processor if the project property debugAnnotationProcessor is specified.

  1. gradlew clean build -PdebugAnnotationProcessor

The IterableAggregateAnnotationProcessor class

This concrete annotation processor verifies the correct usage of @IterableAggregate annotation and its child annotation - @IteratorFactory.

It enforces the following rules during the processing:

  1. Only classes can be annotated with @IterableAggregate.
  2. @IterableAggregate‘s value() property must be a class annotated with @Iterator.
  3. @IterableAggregate must contain at least one method annotated with @IteratorFactory.
  4. @IteratorFactory must be enclosed within a class annotated with @IterableAggregate.
  5. The return type of a method annotated with @IteratorFactory must match the value() property of its parent class’s @IterableAggregate annotation.

The test suite IterableAggregateAnnotationProcessorTest tests that the IterableAggregateAnnotationProcessor is able to catch violations of the above rules. Furthermore, it validates that the annotation processor doesn’t produce any errors for a valid usage of the @IterableAggregate annotation. For each test case, a Java source file, present in the src/test/resources directory, is compiled and run through the IterableAggregateAnnotationProcessor during the compilation. Google Compile Testing library is used to perform the compilation and perform assertions based on the compilation results.

The IteratorAnnotationProcessor class

This concrete annotation processor verifies the correct usage of @Iterator annotation and its child annotations - @CurrentItem, @IsDone, and @NextItem.

It enforces the following rules during the processing:

  1. Only classes can be annotated with @Iterator.
  2. @Iterator can have only one method annotated with @CurrentItem.
  3. @Iterator can have only one method annotated with @IsDone.
  4. @Iterator can have only one method annotated with @NextItem.
  5. @CurrentItem must be enclosed within a class annotated with @Iterator.
  6. The return type of a method annotated with @CurrentItem must match the value() property of its parent class’s @Iterator annotation.
  7. A method annotated with @CurrentItem must not take any parameters.
  8. @IsDone must be enclosed within a class annotated with @Iterator.
  9. The return type of a method annotated with @IsDone must be a boolean.
  10. A method annotated with @IsDone must not take any parameters.
  11. A method annotated with @IsDone must generate a warning if it is private.
  12. @NextItem must be enclosed within a class annotated with @Iterator.
  13. The return type of a method annotated with @NextItem must match the value() property of its parent class’s @Iterator annotation.
  14. A method annotated with @NextItem must not take any parameters.
  15. A method annotated with @NextItem must generate a warning if it is private.

The test suite IteratorAnnotationProcessorTest tests that the IteratorAnnotationProcessor is able to catch violations of the above rules. Furthermore, it validates that the annotation processor doesn’t produce any errors for a valid usage of the @Iterator annotation. For each test case, a Java source file, present in the src/test/resources directory, is compiled and run through the IteratorAnnotationProcessor during the compilation. Google Compile Testing library is used to perform the compilation and perform assertions based on the compilation results.

The Example Implementations

This project provides 3 classes that demonstrate the use of the iterator pattern and are annotated with different variations of the @Iterator and @IterableAggregate (and their nested) annotations:

  1. StudentCollection: A class annotated with @IterableAggregate that implements java.lang.Iterable. It’s iterator class is annotated with @Iterator and implements java.util.Iterator. This allows the Students in this collection to be iterated using Java’s “enhanced for-loop”. The iterator returns a Student object during iteration.
  2. RangeGenerator: A class annotated with @Iterator that generates a primitive int value on each iteration within the range specified while constructing its object.
  3. Tree: A class annotated with @IterableAggregate that provides two methods annotated with @IteratorFactory. The two methods return instances of Tree.TreeIterator that allow the user to iterate the nodes of the tree in depth-first and breadth-first orders.

Output

Result of gradlew clean build

  1. > Task :annotation-processor:compileScala
  2. Pruning sources from previous analysis, due to incompatible CompileSetup.
  3. > Task :annotation-processor:compileTestScala
  4. Pruning sources from previous analysis, due to incompatible CompileSetup.
  5. > Task :annotation-processor:test
  6. com.mayankrastogi.cs474.hw2.annotations.processor.tests.IterableAggregateAnnotationProcessorTest
  7. Test IterableAggregateAnnotationProcessor should succeed without warnings on valid usage. PASSED
  8. Test @IterableAggregate must not be applicable on an interface. PASSED
  9. Test @IterableAggregate's value property must be a class annotated with @Iterator. PASSED
  10. Test @IterableAggregate must contain at least one method annotated with @IteratorFactory. PASSED
  11. Test @IteratorFactory must be enclosed within a class annotated with @IterableAggregate. PASSED
  12. Test Return type of a method annotated with @IteratorFactory must match the value property of its parent class's @IterableAggregate annotation. PASSED
  13. com.mayankrastogi.cs474.hw2.annotations.processor.tests.IteratorAnnotationProcessorTest
  14. Test IteratorAnnotationProcessor should succeed without warnings on valid usage. PASSED
  15. Test @Iterator must not be applicable on an interface. PASSED
  16. Test @Iterator must contain only one method annotated with @CurrentItem. PASSED
  17. Test @Iterator must contain only one method annotated with @IsDone. PASSED
  18. Test @Iterator must contain only one method annotated with @NextItem. PASSED
  19. Test @CurrentItem must be enclosed within a class annotated with @Iterator. PASSED
  20. Test Return type of method annotated with @CurrentItem must match the value property of its parent class's @Iterator annotation. PASSED
  21. Test Method annotated with @CurrentItem must not take any parameters. PASSED
  22. Test @IsDone must be enclosed within a class annotated with @Iterator PASSED
  23. Test Return type of method annotated with @IsDone must be boolean. PASSED
  24. Test Method annotated with @IsDone must not take any parameters. PASSED
  25. Test Method annotated with @IsDone must generate a warning if it is private PASSED
  26. Test @NextItem must be enclosed within a class annotated with @Iterator PASSED
  27. Test Return type of method annotated with @NextItem must match the value property of its parent class's @Iterator annotation. PASSED
  28. Test Method annotated with @NextItem must not take any parameters. PASSED
  29. Test Method annotated with @NextItem must generate a warning if it is private PASSED
  30. SUCCESS: Executed 22 tests in 4.3s
  31. > Task :examples:compileJava
  32. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.IterableAggregate
  33. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.IterableAggregate.IteratorFactory
  34. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator
  35. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.CurrentItem
  36. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.IsDone
  37. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.NextItem
  38. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.IterableAggregate
  39. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.IterableAggregate.IteratorFactory
  40. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator
  41. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.CurrentItem
  42. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.IsDone
  43. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.NextItem
  44. > Task :examples:compileScala
  45. Pruning sources from previous analysis, due to incompatible CompileSetup.
  46. BUILD SUCCESSFUL in 13s
  47. 17 actionable tasks: 17 executed

Result of gradlew clean build -PdebugAnnotationProcessor (Debug mode ON)

  1. > Configure project :examples
  2. Debugging mode for annotation processor has been switched on.
  3. > Task :annotation-processor:compileScala
  4. Pruning sources from previous analysis, due to incompatible CompileSetup.
  5. > Task :annotation-processor:compileTestScala
  6. Pruning sources from previous analysis, due to incompatible CompileSetup.
  7. > Task :annotation-processor:test
  8. com.mayankrastogi.cs474.hw2.annotations.processor.tests.IterableAggregateAnnotationProcessorTest
  9. Test IterableAggregateAnnotationProcessor should succeed without warnings on valid usage. PASSED
  10. Test @IterableAggregate must not be applicable on an interface. PASSED
  11. Test @IterableAggregate's value property must be a class annotated with @Iterator. PASSED
  12. Test @IterableAggregate must contain at least one method annotated with @IteratorFactory. PASSED
  13. Test @IteratorFactory must be enclosed within a class annotated with @IterableAggregate. PASSED
  14. Test Return type of a method annotated with @IteratorFactory must match the value property of its parent class's @IterableAggregate annotation. PASSED
  15. com.mayankrastogi.cs474.hw2.annotations.processor.tests.IteratorAnnotationProcessorTest
  16. Test IteratorAnnotationProcessor should succeed without warnings on valid usage. PASSED
  17. Test @Iterator must not be applicable on an interface. PASSED
  18. Test @Iterator must contain only one method annotated with @CurrentItem. PASSED
  19. Test @Iterator must contain only one method annotated with @IsDone. PASSED
  20. Test @Iterator must contain only one method annotated with @NextItem. PASSED
  21. Test @CurrentItem must be enclosed within a class annotated with @Iterator. PASSED
  22. Test Return type of method annotated with @CurrentItem must match the value property of its parent class's @Iterator annotation. PASSED
  23. Test Method annotated with @CurrentItem must not take any parameters. PASSED
  24. Test @IsDone must be enclosed within a class annotated with @Iterator PASSED
  25. Test Return type of method annotated with @IsDone must be boolean. PASSED
  26. Test Method annotated with @IsDone must not take any parameters. PASSED
  27. Test Method annotated with @IsDone must generate a warning if it is private PASSED
  28. Test @NextItem must be enclosed within a class annotated with @Iterator PASSED
  29. Test Return type of method annotated with @NextItem must match the value property of its parent class's @Iterator annotation. PASSED
  30. Test Method annotated with @NextItem must not take any parameters. PASSED
  31. Test Method annotated with @NextItem must generate a warning if it is private PASSED
  32. SUCCESS: Executed 22 tests in 4.5s
  33. > Task :examples:compileJava
  34. Note: [DEBUG]process(annotations: [com.mayankrastogi.cs474.hw2.annotations.IterableAggregate, com.mayankrastogi.cs474.hw2.annotations.IterableAggregate.IteratorFactory], roundEnv: [errorRaised=false, rootElements=[com.mayankrastogi.cs474.hw2.examples.RangeGenerator, com.mayankrastogi.cs474.hw2.examples.Degree, com.mayankrastogi.cs474.hw2.examples.StudentCollection, com.mayankrastogi.cs474.hw2.examples.Student, com.mayankrastogi.cs474.hw2.examples.Tree, com.mayankrastogi.cs474.hw2.examples.Node], processingOver=false])
  35. Note: [DEBUG]iterableAggregates: Set(com.mayankrastogi.cs474.hw2.examples.StudentCollection, com.mayankrastogi.cs474.hw2.examples.Tree)
  36. Note: [DEBUG]iteratorFactories: Set(iterator(), dfsIterator(), bfsIterator())
  37. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.IterableAggregate
  38. examples\src\main\java\com\mayankrastogi\cs474\hw2\examples\StudentCollection.java:25: Note: [DEBUG]Processing iterableAggregateElement
  39. public class StudentCollection implements Iterable<Student> {
  40. ^
  41. Note: [DEBUG]assertIterableAggregateElementIsAppliedOnClass...
  42. Note: [DEBUG]assertIterableAggregateElementIsAppliedOnClass: true
  43. Note: [DEBUG]assertIterableAggregateAnnotationValueIsAnnotatedWithIterator...
  44. Note: [DEBUG]assertIterableAggregateAnnotationValueIsAnnotatedWithIterator: true
  45. Note: [DEBUG]assertIterableAggregateElementContainsAtLeastOneIteratorFactory...
  46. Note: [DEBUG]Found 1 element(s) annotated with @com.mayankrastogi.cs474.hw2.annotations.IterableAggregate.IteratorFactory within iterable aggregate
  47. Note: [DEBUG]Processing successful: true
  48. examples\src\main\java\com\mayankrastogi\cs474\hw2\examples\Tree.java:20: Note: [DEBUG]Processing iterableAggregateElement
  49. public class Tree<T> {
  50. ^
  51. Note: [DEBUG]assertIterableAggregateElementIsAppliedOnClass...
  52. Note: [DEBUG]assertIterableAggregateElementIsAppliedOnClass: true
  53. Note: [DEBUG]assertIterableAggregateAnnotationValueIsAnnotatedWithIterator...
  54. Note: [DEBUG]assertIterableAggregateAnnotationValueIsAnnotatedWithIterator: true
  55. Note: [DEBUG]assertIterableAggregateElementContainsAtLeastOneIteratorFactory...
  56. Note: [DEBUG]Found 2 element(s) annotated with @com.mayankrastogi.cs474.hw2.annotations.IterableAggregate.IteratorFactory within iterable aggregate
  57. Note: [DEBUG]Processing successful: true
  58. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.IterableAggregate.IteratorFactory
  59. examples\src\main\java\com\mayankrastogi\cs474\hw2\examples\StudentCollection.java:59: Note: [DEBUG]Processing iteratorFactoryMethod
  60. public java.util.Iterator<Student> iterator() {
  61. ^
  62. Note: [DEBUG]assertIteratorFactoryMethodIsEnclosedWithinIterableAggregate...
  63. Note: [DEBUG]Iterable factory method is enclosed within an iterable aggregate
  64. Note: [DEBUG]assertIteratorFactoryMethodReturnsTypeSpecifiedInIterableAggregateAnnotationValue...
  65. Note: [DEBUG]iterableAggregateAnnotationValue: com.mayankrastogi.cs474.hw2.examples.StudentCollection.StudentCollectionIterator
  66. Note: [DEBUG]The return type of the iterator factory method is assignable to the type specified as value on its enclosing @com.mayankrastogi.cs474.hw2.annotations.IterableAggregate
  67. Note: [DEBUG]Processing successful: true
  68. examples\src\main\java\com\mayankrastogi\cs474\hw2\examples\Tree.java:42: Note: [DEBUG]Processing iteratorFactoryMethod
  69. public TreeIterator dfsIterator() {
  70. ^
  71. Note: [DEBUG]assertIteratorFactoryMethodIsEnclosedWithinIterableAggregate...
  72. Note: [DEBUG]Iterable factory method is enclosed within an iterable aggregate
  73. Note: [DEBUG]assertIteratorFactoryMethodReturnsTypeSpecifiedInIterableAggregateAnnotationValue...
  74. Note: [DEBUG]iterableAggregateAnnotationValue: com.mayankrastogi.cs474.hw2.examples.Tree.TreeIterator
  75. Note: [DEBUG]The return type of the iterator factory method is assignable to the type specified as value on its enclosing @com.mayankrastogi.cs474.hw2.annotations.IterableAggregate
  76. Note: [DEBUG]Processing successful: true
  77. examples\src\main\java\com\mayankrastogi\cs474\hw2\examples\Tree.java:52: Note: [DEBUG]Processing iteratorFactoryMethod
  78. public TreeIterator bfsIterator() {
  79. ^
  80. Note: [DEBUG]assertIteratorFactoryMethodIsEnclosedWithinIterableAggregate...
  81. Note: [DEBUG]Iterable factory method is enclosed within an iterable aggregate
  82. Note: [DEBUG]assertIteratorFactoryMethodReturnsTypeSpecifiedInIterableAggregateAnnotationValue...
  83. Note: [DEBUG]iterableAggregateAnnotationValue: com.mayankrastogi.cs474.hw2.examples.Tree.TreeIterator
  84. Note: [DEBUG]The return type of the iterator factory method is assignable to the type specified as value on its enclosing @com.mayankrastogi.cs474.hw2.annotations.IterableAggregate
  85. Note: [DEBUG]Processing successful: true
  86. Note: [DEBUG]process(annotations: [com.mayankrastogi.cs474.hw2.annotations.Iterator.CurrentItem, com.mayankrastogi.cs474.hw2.annotations.Iterator, com.mayankrastogi.cs474.hw2.annotations.Iterator.NextItem, com.mayankrastogi.cs474.hw2.annotations.Iterator.IsDone], roundEnv: [errorRaised=false, rootElements=[com.mayankrastogi.cs474.hw2.examples.RangeGenerator, com.mayankrastogi.cs474.hw2.examples.Degree, com.mayankrastogi.cs474.hw2.examples.StudentCollection, com.mayankrastogi.cs474.hw2.examples.Student, com.mayankrastogi.cs474.hw2.examples.Tree, com.mayankrastogi.cs474.hw2.examples.Node], processingOver=false])
  87. Note: [DEBUG]iterators: Set(com.mayankrastogi.cs474.hw2.examples.RangeGenerator, com.mayankrastogi.cs474.hw2.examples.StudentCollection.StudentCollectionIterator, com.mayankrastogi.cs474.hw2.examples.Tree.TreeIterator)
  88. Note: [DEBUG]currentItems: Set(currentValue(), current(), currentNode())
  89. Note: [DEBUG]isDones: Set(isDone(), hasNext(), isDone())
  90. Note: [DEBUG]nextItems: Set(next(), next(), next())
  91. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator
  92. examples\src\main\java\com\mayankrastogi\cs474\hw2\examples\RangeGenerator.java:11: Note: [DEBUG]Processing iteratorElement
  93. public class RangeGenerator {
  94. ^
  95. Note: [DEBUG]assertIteratorElementIsValid...
  96. Note: [DEBUG]assertIteratorElementIsValid: true
  97. Note: [DEBUG]assertIteratorElementContainsOnlyOneMethodAnnotatedWith...
  98. Note: [DEBUG]Found 1 element(s) annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.CurrentItem within iterator
  99. Note: [DEBUG]assertIteratorElementContainsOnlyOneMethodAnnotatedWith: true
  100. Note: [DEBUG]assertIteratorElementContainsOnlyOneMethodAnnotatedWith...
  101. Note: [DEBUG]Found 1 element(s) annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.IsDone within iterator
  102. Note: [DEBUG]assertIteratorElementContainsOnlyOneMethodAnnotatedWith: true
  103. Note: [DEBUG]assertIteratorElementContainsOnlyOneMethodAnnotatedWith...
  104. Note: [DEBUG]Found 1 element(s) annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.NextItem within iterator
  105. Note: [DEBUG]assertIteratorElementContainsOnlyOneMethodAnnotatedWith: true
  106. Note: [DEBUG]Processing successful: true
  107. examples\src\main\java\com\mayankrastogi\cs474\hw2\examples\StudentCollection.java:70: Note: [DEBUG]Processing iteratorElement
  108. public class StudentCollectionIterator implements java.util.Iterator<Student> {
  109. ^
  110. Note: [DEBUG]assertIteratorElementIsValid...
  111. Note: [DEBUG]assertIteratorElementIsValid: true
  112. Note: [DEBUG]assertIteratorElementContainsOnlyOneMethodAnnotatedWith...
  113. Note: [DEBUG]Found 1 element(s) annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.CurrentItem within iterator
  114. Note: [DEBUG]assertIteratorElementContainsOnlyOneMethodAnnotatedWith: true
  115. Note: [DEBUG]assertIteratorElementContainsOnlyOneMethodAnnotatedWith...
  116. Note: [DEBUG]Found 1 element(s) annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.IsDone within iterator
  117. Note: [DEBUG]assertIteratorElementContainsOnlyOneMethodAnnotatedWith: true
  118. Note: [DEBUG]assertIteratorElementContainsOnlyOneMethodAnnotatedWith...
  119. Note: [DEBUG]Found 1 element(s) annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.NextItem within iterator
  120. Note: [DEBUG]assertIteratorElementContainsOnlyOneMethodAnnotatedWith: true
  121. Note: [DEBUG]Processing successful: true
  122. examples\src\main\java\com\mayankrastogi\cs474\hw2\examples\Tree.java:65: Note: [DEBUG]Processing iteratorElement
  123. public class TreeIterator {
  124. ^
  125. Note: [DEBUG]assertIteratorElementIsValid...
  126. Note: [DEBUG]assertIteratorElementIsValid: true
  127. Note: [DEBUG]assertIteratorElementContainsOnlyOneMethodAnnotatedWith...
  128. Note: [DEBUG]Found 1 element(s) annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.CurrentItem within iterator
  129. Note: [DEBUG]assertIteratorElementContainsOnlyOneMethodAnnotatedWith: true
  130. Note: [DEBUG]assertIteratorElementContainsOnlyOneMethodAnnotatedWith...
  131. Note: [DEBUG]Found 1 element(s) annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.IsDone within iterator
  132. Note: [DEBUG]assertIteratorElementContainsOnlyOneMethodAnnotatedWith: true
  133. Note: [DEBUG]assertIteratorElementContainsOnlyOneMethodAnnotatedWith...
  134. Note: [DEBUG]Found 1 element(s) annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.NextItem within iterator
  135. Note: [DEBUG]assertIteratorElementContainsOnlyOneMethodAnnotatedWith: true
  136. Note: [DEBUG]Processing successful: true
  137. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.CurrentItem
  138. examples\src\main\java\com\mayankrastogi\cs474\hw2\examples\RangeGenerator.java:28: Note: [DEBUG]Processing currentItemMethod
  139. private int currentValue() {
  140. ^
  141. Note: [DEBUG]assertMethodIsEnclosedWithinIterator...
  142. Note: [DEBUG]CurrentItem is enclosed within an iterator
  143. Note: [DEBUG]assertMethodReturnsTypeSpecifiedInIteratorAnnotationValue...
  144. Note: [DEBUG]iteratorAnnotationValue: int
  145. Note: [DEBUG]The return type of the method annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.CurrentItem is assignable to the type specified as value on its enclosing @com.mayankrastogi.cs474.hw2.annotations.Iterator
  146. Note: [DEBUG]assertMethodTakesNoParameters...
  147. Note: [DEBUG]assertMethodTakesNoParameters: true
  148. Note: [DEBUG]Processing successful: true
  149. examples\src\main\java\com\mayankrastogi\cs474\hw2\examples\StudentCollection.java:85: Note: [DEBUG]Processing currentItemMethod
  150. public Student current() {
  151. ^
  152. Note: [DEBUG]assertMethodIsEnclosedWithinIterator...
  153. Note: [DEBUG]CurrentItem is enclosed within an iterator
  154. Note: [DEBUG]assertMethodReturnsTypeSpecifiedInIteratorAnnotationValue...
  155. Note: [DEBUG]iteratorAnnotationValue: com.mayankrastogi.cs474.hw2.examples.Student
  156. Note: [DEBUG]The return type of the method annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.CurrentItem is assignable to the type specified as value on its enclosing @com.mayankrastogi.cs474.hw2.annotations.Iterator
  157. Note: [DEBUG]assertMethodTakesNoParameters...
  158. Note: [DEBUG]assertMethodTakesNoParameters: true
  159. Note: [DEBUG]Processing successful: true
  160. examples\src\main\java\com\mayankrastogi\cs474\hw2\examples\Tree.java:90: Note: [DEBUG]Processing currentItemMethod
  161. public Node<T> currentNode() {
  162. ^
  163. Note: [DEBUG]assertMethodIsEnclosedWithinIterator...
  164. Note: [DEBUG]CurrentItem is enclosed within an iterator
  165. Note: [DEBUG]assertMethodReturnsTypeSpecifiedInIteratorAnnotationValue...
  166. Note: [DEBUG]iteratorAnnotationValue: com.mayankrastogi.cs474.hw2.examples.Node
  167. Note: [DEBUG]The return type of the method annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.CurrentItem is assignable to the type specified as value on its enclosing @com.mayankrastogi.cs474.hw2.annotations.Iterator
  168. Note: [DEBUG]assertMethodTakesNoParameters...
  169. Note: [DEBUG]assertMethodTakesNoParameters: true
  170. Note: [DEBUG]Processing successful: true
  171. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.IsDone
  172. examples\src\main\java\com\mayankrastogi\cs474\hw2\examples\RangeGenerator.java:38: Note: [DEBUG]Processing isDoneMethod
  173. public boolean isDone() {
  174. ^
  175. Note: [DEBUG]assertMethodIsEnclosedWithinIterator...
  176. Note: [DEBUG]IsDone is enclosed within an iterator
  177. Note: [DEBUG]assertIsDoneMethodReturnsBooleanType...
  178. Note: [DEBUG]assertIsDoneMethodReturnsBooleanType: true
  179. Note: [DEBUG]assertMethodTakesNoParameters...
  180. Note: [DEBUG]assertMethodTakesNoParameters: true
  181. Note: [DEBUG]warnIfMethodIsPrivate...
  182. Note: [DEBUG]shouldTreatWarningsAsErrors...
  183. Note: [DEBUG]shouldTreatWarningsAsErrors: false
  184. Note: [DEBUG]isPrivate: false
  185. Note: [DEBUG]operationSuccessful: true
  186. Note: [DEBUG]Processing successful: true
  187. examples\src\main\java\com\mayankrastogi\cs474\hw2\examples\StudentCollection.java:96: Note: [DEBUG]Processing isDoneMethod
  188. public boolean hasNext() {
  189. ^
  190. Note: [DEBUG]assertMethodIsEnclosedWithinIterator...
  191. Note: [DEBUG]IsDone is enclosed within an iterator
  192. Note: [DEBUG]assertIsDoneMethodReturnsBooleanType...
  193. Note: [DEBUG]assertIsDoneMethodReturnsBooleanType: true
  194. Note: [DEBUG]assertMethodTakesNoParameters...
  195. Note: [DEBUG]assertMethodTakesNoParameters: true
  196. Note: [DEBUG]warnIfMethodIsPrivate...
  197. Note: [DEBUG]shouldTreatWarningsAsErrors...
  198. Note: [DEBUG]shouldTreatWarningsAsErrors: false
  199. Note: [DEBUG]isPrivate: false
  200. Note: [DEBUG]operationSuccessful: true
  201. Note: [DEBUG]Processing successful: true
  202. examples\src\main\java\com\mayankrastogi\cs474\hw2\examples\Tree.java:100: Note: [DEBUG]Processing isDoneMethod
  203. public boolean isDone() {
  204. ^
  205. Note: [DEBUG]assertMethodIsEnclosedWithinIterator...
  206. Note: [DEBUG]IsDone is enclosed within an iterator
  207. Note: [DEBUG]assertIsDoneMethodReturnsBooleanType...
  208. Note: [DEBUG]assertIsDoneMethodReturnsBooleanType: true
  209. Note: [DEBUG]assertMethodTakesNoParameters...
  210. Note: [DEBUG]assertMethodTakesNoParameters: true
  211. Note: [DEBUG]warnIfMethodIsPrivate...
  212. Note: [DEBUG]shouldTreatWarningsAsErrors...
  213. Note: [DEBUG]shouldTreatWarningsAsErrors: false
  214. Note: [DEBUG]isPrivate: false
  215. Note: [DEBUG]operationSuccessful: true
  216. Note: [DEBUG]Processing successful: true
  217. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.NextItem
  218. examples\src\main\java\com\mayankrastogi\cs474\hw2\examples\RangeGenerator.java:50: Note: [DEBUG]Processing nextItemMethod
  219. public int next() {
  220. ^
  221. Note: [DEBUG]assertMethodIsEnclosedWithinIterator...
  222. Note: [DEBUG]NextItem is enclosed within an iterator
  223. Note: [DEBUG]assertMethodReturnsTypeSpecifiedInIteratorAnnotationValue...
  224. Note: [DEBUG]iteratorAnnotationValue: int
  225. Note: [DEBUG]The return type of the method annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.NextItem is assignable to the type specified as value on its enclosing @com.mayankrastogi.cs474.hw2.annotations.Iterator
  226. Note: [DEBUG]assertMethodTakesNoParameters...
  227. Note: [DEBUG]assertMethodTakesNoParameters: true
  228. Note: [DEBUG]warnIfMethodIsPrivate...
  229. Note: [DEBUG]shouldTreatWarningsAsErrors...
  230. Note: [DEBUG]shouldTreatWarningsAsErrors: false
  231. Note: [DEBUG]isPrivate: false
  232. Note: [DEBUG]operationSuccessful: true
  233. Note: [DEBUG]Processing successful: true
  234. examples\src\main\java\com\mayankrastogi\cs474\hw2\examples\StudentCollection.java:110: Note: [DEBUG]Processing nextItemMethod
  235. public Student next() {
  236. ^
  237. Note: [DEBUG]assertMethodIsEnclosedWithinIterator...
  238. Note: [DEBUG]NextItem is enclosed within an iterator
  239. Note: [DEBUG]assertMethodReturnsTypeSpecifiedInIteratorAnnotationValue...
  240. Note: [DEBUG]iteratorAnnotationValue: com.mayankrastogi.cs474.hw2.examples.Student
  241. Note: [DEBUG]The return type of the method annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.NextItem is assignable to the type specified as value on its enclosing @com.mayankrastogi.cs474.hw2.annotations.Iterator
  242. Note: [DEBUG]assertMethodTakesNoParameters...
  243. Note: [DEBUG]assertMethodTakesNoParameters: true
  244. Note: [DEBUG]warnIfMethodIsPrivate...
  245. Note: [DEBUG]shouldTreatWarningsAsErrors...
  246. Note: [DEBUG]shouldTreatWarningsAsErrors: false
  247. Note: [DEBUG]isPrivate: false
  248. Note: [DEBUG]operationSuccessful: true
  249. Note: [DEBUG]Processing successful: true
  250. examples\src\main\java\com\mayankrastogi\cs474\hw2\examples\Tree.java:113: Note: [DEBUG]Processing nextItemMethod
  251. public Node<T> next() {
  252. ^
  253. Note: [DEBUG]assertMethodIsEnclosedWithinIterator...
  254. Note: [DEBUG]NextItem is enclosed within an iterator
  255. Note: [DEBUG]assertMethodReturnsTypeSpecifiedInIteratorAnnotationValue...
  256. Note: [DEBUG]iteratorAnnotationValue: com.mayankrastogi.cs474.hw2.examples.Node
  257. Note: [DEBUG]The return type of the method annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.NextItem is assignable to the type specified as value on its enclosing @com.mayankrastogi.cs474.hw2.annotations.Iterator
  258. Note: [DEBUG]assertMethodTakesNoParameters...
  259. Note: [DEBUG]assertMethodTakesNoParameters: true
  260. Note: [DEBUG]warnIfMethodIsPrivate...
  261. Note: [DEBUG]shouldTreatWarningsAsErrors...
  262. Note: [DEBUG]shouldTreatWarningsAsErrors: false
  263. Note: [DEBUG]isPrivate: false
  264. Note: [DEBUG]operationSuccessful: true
  265. Note: [DEBUG]Processing successful: true
  266. Note: [DEBUG]process(annotations: [], roundEnv: [errorRaised=false, rootElements=[], processingOver=true])
  267. Note: [DEBUG]iterableAggregates: Set()
  268. Note: [DEBUG]iteratorFactories: Set()
  269. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.IterableAggregate
  270. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.IterableAggregate.IteratorFactory
  271. Note: [DEBUG]process(annotations: [], roundEnv: [errorRaised=false, rootElements=[], processingOver=true])
  272. Note: [DEBUG]iterators: Set()
  273. Note: [DEBUG]currentItems: Set()
  274. Note: [DEBUG]isDones: Set()
  275. Note: [DEBUG]nextItems: Set()
  276. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator
  277. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.CurrentItem
  278. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.IsDone
  279. Note: Processing elements annotated with @com.mayankrastogi.cs474.hw2.annotations.Iterator.NextItem
  280. > Task :examples:compileScala
  281. Pruning sources from previous analysis, due to incompatible CompileSetup.
  282. BUILD SUCCESSFUL in 13s
  283. 17 actionable tasks: 17 executed

Result of gradlew run

  1. > Task :examples:run
  2. =================================================================================================================
  3. Design Pattern Verifier - Iterator Design Pattern Example Programs
  4. =================================================================================================================
  5. The iterator design pattern provides a way to access the elements of an aggregate object sequentially without
  6. exposing its underlying representation.
  7. This application demonstrates 3 classes that use the iterator pattern and are annotated with different variations
  8. of the @Iterator and @IterableAggregate (and their nested) annotations:
  9. 1. StudentCollection: A class annotated with @IterableAggregate that implements `java.lang.Iterable`. It's
  10. iterator class is annotated with @Iterator and implements `java.util.Iterator`. The iterator returns a
  11. `Student` object during iteration.
  12. 2. RangeGenerator: A class annotated with @Iterator that generates a primitive `int` value on each iteration
  13. within the range specified while constructing its object.
  14. 3. Tree: A class annotated with @IterableAggregate that provides two methods annotated with @IteratorFactory.
  15. The two methods return instances of `Tree.TreeIterator` that allow the user to iterate the nodes of the
  16. tree in depth-first and breadth-first orders.
  17. -----------------------------------------------------------------------------------------------------------------
  18. Student Collection Iteration Example
  19. -----------------------------------------------------------------------------------------------------------------
  20. 23:13:35.800 [main] DEBUG com.mayankrastogi.cs474.hw2.examples.IteratorExamplesMain$ - Creating StudentCollection...
  21. 23:13:35.803 [main] DEBUG com.mayankrastogi.cs474.hw2.examples.IteratorExamplesMain$ - Iterating StudentCollection...
  22. Name: Alex
  23. Age: 20
  24. Degree: Bachelors
  25. GPA: 3.2
  26. Name: Bob
  27. Age: 25
  28. Degree: Masters
  29. GPA: 3.6
  30. Name: Chuck
  31. Age: 28
  32. Degree: PhD
  33. GPA: 3.5
  34. 23:13:35.815 [main] DEBUG com.mayankrastogi.cs474.hw2.examples.IteratorExamplesMain$ - StudentCollection Iteration Example Finished.
  35. -----------------------------------------------------------------------------------------------------------------
  36. RangeGenerator Iteration Example
  37. -----------------------------------------------------------------------------------------------------------------
  38. 23:13:35.823 [main] DEBUG com.mayankrastogi.cs474.hw2.examples.IteratorExamplesMain$ - Creating a RangeGenerator(from: 1, to: 10)...
  39. 23:13:35.823 [main] DEBUG com.mayankrastogi.cs474.hw2.examples.IteratorExamplesMain$ - Iterating RangeGenerator...
  40. 1
  41. 2
  42. 3
  43. 4
  44. 5
  45. 6
  46. 7
  47. 8
  48. 9
  49. 10
  50. 23:13:35.823 [main] DEBUG com.mayankrastogi.cs474.hw2.examples.IteratorExamplesMain$ - RangeGenerator Iteration Example Finished.
  51. -----------------------------------------------------------------------------------------------------------------
  52. Tree Iteration Example
  53. -----------------------------------------------------------------------------------------------------------------
  54. Sample Tree:
  55. -----------
  56. 1
  57. _______________________________|_______________________________
  58. | | |
  59. 2 3 4
  60. _______|_______ _______|_______ _______|_______
  61. | | | | | |
  62. 5 6 7 8 9 10
  63. 23:13:35.824 [main] DEBUG com.mayankrastogi.cs474.hw2.examples.IteratorExamplesMain$ - Creating 10 nodes...
  64. 23:13:35.828 [main] DEBUG com.mayankrastogi.cs474.hw2.examples.IteratorExamplesMain$ - allNodes: Vector(Node(data: 1, children: []), Node(data: 2, children: []), Node(data: 3, children: []), Node(data: 4, children: []), Node(data: 5, children: []), Node(data: 6, children: []), Node(data: 7, children: []), Node(data: 8, children: []), Node(data: 9, children: []), Node(data: 10, children: []))
  65. 23:13:35.828 [main] DEBUG com.mayankrastogi.cs474.hw2.examples.IteratorExamplesMain$ - Creating Sample Tree...
  66. 23:13:35.841 [main] DEBUG com.mayankrastogi.cs474.hw2.examples.IteratorExamplesMain$ - tree: Tree(root: Node(data: 1, children: [Node(data: 2, children: [Node(data: 5, children: []), Node(data: 6, children: [])]), Node(data: 3, children: [Node(data: 7, children: []), Node(data: 8, children: [])]), Node(data: 4, children: [Node(data: 9, children: []), Node(data: 10, children: [])])]))
  67. 23:13:35.841 [main] DEBUG com.mayankrastogi.cs474.hw2.examples.IteratorExamplesMain$ - Iterating tree using `dfsIterator()`...
  68. Depth-first tree traversal:
  69. 1 2 5 6 3 7 8 4 9 10
  70. 23:13:35.842 [main] DEBUG com.mayankrastogi.cs474.hw2.examples.IteratorExamplesMain$ - Iterating tree using `bfsIterator()`...
  71. Breadth-first tree traversal:
  72. 1 2 3 4 5 6 7 8 9 10
  73. 23:13:35.843 [main] DEBUG com.mayankrastogi.cs474.hw2.examples.IteratorExamplesMain$ - Tree Iteration Example Finished.
  74. BUILD SUCCESSFUL in 1s
  75. 8 actionable tasks: 1 executed, 7 up-to-date