|
17 | 17 | package org.spockframework.runtime.extension; |
18 | 18 |
|
19 | 19 | import java.lang.annotation.Annotation; |
| 20 | +import java.util.List; |
20 | 21 |
|
| 22 | +import org.spockframework.runtime.InvalidSpecException; |
21 | 23 | import org.spockframework.runtime.model.*; |
22 | 24 |
|
23 | 25 | /** |
24 | | - * |
25 | 26 | * @author Peter Niederwieser |
26 | 27 | */ |
27 | 28 | public interface IAnnotationDrivenExtension<T extends Annotation> { |
28 | | - void visitSpecAnnotation(T annotation, SpecInfo spec); |
29 | | - void visitFeatureAnnotation(T annotation, FeatureInfo feature); |
30 | | - void visitFixtureAnnotation(T annotation, MethodInfo fixtureMethod); |
31 | | - void visitFieldAnnotation(T annotation, FieldInfo field); |
32 | | - void visitSpec(SpecInfo spec); |
| 29 | + /** |
| 30 | + * Handles the annotation when applied to a specification one or multiple times. |
| 31 | + * The default implementation of this method calls {@link #visitSpecAnnotation(Annotation, SpecInfo)} |
| 32 | + * once for each given annotation. |
| 33 | + * |
| 34 | + * @since 2.0 |
| 35 | + * @param annotations the annotations found on the specification |
| 36 | + * @param spec the annotated specification |
| 37 | + */ |
| 38 | + default void visitSpecAnnotations(List<T> annotations, SpecInfo spec) { |
| 39 | + for (T annotation : annotations) { |
| 40 | + visitSpecAnnotation(annotation, spec); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Handles the annotation when applied to a specification. |
| 46 | + * The default implementation of this method throws an {@link InvalidSpecException}. |
| 47 | + * This method is not called by Spock directly, but only by the default implementation of |
| 48 | + * {@link #visitSpecAnnotations(List, SpecInfo)}, so if you have overwritten that method, |
| 49 | + * there is no need to overwrite this one too except if you want to call it yourself. |
| 50 | + * |
| 51 | + * @param annotation the annotation found on the specification |
| 52 | + * @param spec the annotated specification |
| 53 | + */ |
| 54 | + default void visitSpecAnnotation(T annotation, SpecInfo spec) { |
| 55 | + throw new InvalidSpecException("@%s may not be applied to Specs") |
| 56 | + .withArgs(annotation.annotationType().getSimpleName()); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Handles the annotation when applied to a field of a specification one or multiple times. |
| 61 | + * The default implementation of this method calls {@link #visitFieldAnnotation(Annotation, FieldInfo)} |
| 62 | + * once for each given annotation. |
| 63 | + * |
| 64 | + * @since 2.0 |
| 65 | + * @param annotations the annotations found on the field |
| 66 | + * @param field the annotated field |
| 67 | + */ |
| 68 | + default void visitFieldAnnotations(List<T> annotations, FieldInfo field) { |
| 69 | + for (T annotation : annotations) { |
| 70 | + visitFieldAnnotation(annotation, field); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Handles the annotation when applied to a field of a specification. |
| 76 | + * The default implementation of this method throws an {@link InvalidSpecException}. |
| 77 | + * This method is not called by Spock directly, but only by the default implementation of |
| 78 | + * {@link #visitFieldAnnotations(List, FieldInfo)}, so if you have overwritten that method, |
| 79 | + * there is no need to overwrite this one too except if you want to call it yourself. |
| 80 | + * |
| 81 | + * @param annotation the annotation found on the field |
| 82 | + * @param field the annotated field |
| 83 | + */ |
| 84 | + default void visitFieldAnnotation(T annotation, FieldInfo field) { |
| 85 | + throw new InvalidSpecException("@%s may not be applied to fields") |
| 86 | + .withArgs(annotation.annotationType().getSimpleName()); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Handles the annotation when applied to a fixture method of a specification one or multiple times. |
| 91 | + * The default implementation of this method calls {@link #visitFixtureAnnotation(Annotation, MethodInfo)} |
| 92 | + * once for each given annotation. |
| 93 | + * |
| 94 | + * @since 2.0 |
| 95 | + * @param annotations the annotations found on the fixture method |
| 96 | + * @param fixtureMethod the annotated fixture method |
| 97 | + */ |
| 98 | + default void visitFixtureAnnotations(List<T> annotations, MethodInfo fixtureMethod) { |
| 99 | + for (T annotation : annotations) { |
| 100 | + visitFixtureAnnotation(annotation, fixtureMethod); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Handles the annotation when applied to a fixture method of a specification. |
| 106 | + * The default implementation of this method throws an {@link InvalidSpecException}. |
| 107 | + * This method is not called by Spock directly, but only by the default implementation of |
| 108 | + * {@link #visitFixtureAnnotations(List, MethodInfo)}, so if you have overwritten that method, |
| 109 | + * there is no need to overwrite this one too except if you want to call it yourself. |
| 110 | + * |
| 111 | + * @param annotation the annotation found on the fixture method |
| 112 | + * @param fixtureMethod the annotated fixture method |
| 113 | + */ |
| 114 | + default void visitFixtureAnnotation(T annotation, MethodInfo fixtureMethod) { |
| 115 | + throw new InvalidSpecException("@%s may not be applied to fixture methods") |
| 116 | + .withArgs(annotation.annotationType().getSimpleName()); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Handles the annotation when applied to a feature method of a specification one or multiple times. |
| 121 | + * The default implementation of this method calls {@link #visitFeatureAnnotation(Annotation, FeatureInfo)} |
| 122 | + * once for each given annotation. |
| 123 | + * |
| 124 | + * @since 2.0 |
| 125 | + * @param annotations the annotations found on the feature method |
| 126 | + * @param feature the annotated feature method |
| 127 | + */ |
| 128 | + default void visitFeatureAnnotations(List<T> annotations, FeatureInfo feature) { |
| 129 | + for (T annotation : annotations) { |
| 130 | + visitFeatureAnnotation(annotation, feature); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Handles the annotation when applied to a feature method of a specification. |
| 136 | + * The default implementation of this method throws an {@link InvalidSpecException}. |
| 137 | + * This method is not called by Spock directly, but only by the default implementation of |
| 138 | + * {@link #visitFeatureAnnotations(List, FeatureInfo)}, so if you have overwritten that method, |
| 139 | + * there is no need to overwrite this one too except if you want to call it yourself. |
| 140 | + * |
| 141 | + * @param annotation the annotation found on the feature method |
| 142 | + * @param feature the annotated feature method |
| 143 | + */ |
| 144 | + default void visitFeatureAnnotation(T annotation, FeatureInfo feature) { |
| 145 | + throw new InvalidSpecException("@%s may not be applied to feature methods") |
| 146 | + .withArgs(annotation.annotationType().getSimpleName()); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Does concluding actions after the single annotations were handled. |
| 151 | + * This method is called after all the other {@code visit...} methods of this interface to do any concluding |
| 152 | + * or combining actions that are necessary. It is called once for each extension that has at least one |
| 153 | + * of its annotation somewhere on the given specification. |
| 154 | + * |
| 155 | + * @param spec the annotated specification |
| 156 | + */ |
| 157 | + default void visitSpec(SpecInfo spec) { |
| 158 | + } |
33 | 159 | } |
0 commit comments