-
Notifications
You must be signed in to change notification settings - Fork 297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enable @AnalyzeClasses
annotation to be used as meta annotation
#1300
Open
mathze
wants to merge
1
commit into
TNG:main
Choose a base branch
from
mathze:feature/GH-182-allow-analyzeclass-to-be-used-as-meta-annotation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...ch/archunit/junit/internal/testexamples/TestClassWithMetaAnnotationForAnalyzeClasses.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.tngtech.archunit.junit.internal.testexamples; | ||
|
||
import com.tngtech.archunit.junit.AnalyzeClasses; | ||
import com.tngtech.archunit.junit.ArchTest; | ||
import com.tngtech.archunit.lang.ArchRule; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@TestClassWithMetaAnnotationForAnalyzeClasses.MetaAnalyzeCls | ||
public class TestClassWithMetaAnnotationForAnalyzeClasses { | ||
|
||
@ArchTest | ||
public static final ArchRule rule_in_class_with_meta_analyze_class_annotation = RuleThatFails.on(UnwantedClass.class); | ||
|
||
@Retention(RUNTIME) | ||
@Target(TYPE) | ||
@AnalyzeClasses(wholeClasspath = true) | ||
public @interface MetaAnalyzeCls { | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...unit/internal/testexamples/wrong/WrongTestClassWithMultipleAnalyzeClassesAnnotations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.tngtech.archunit.junit.internal.testexamples.wrong; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import com.tngtech.archunit.junit.AnalyzeClasses; | ||
import com.tngtech.archunit.junit.ArchTest; | ||
import com.tngtech.archunit.junit.internal.testexamples.RuleThatFails; | ||
import com.tngtech.archunit.junit.internal.testexamples.UnwantedClass; | ||
import com.tngtech.archunit.lang.ArchRule; | ||
|
||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@AnalyzeClasses(packages = "dummy") | ||
@WrongTestClassWithMultipleAnalyzeClassesAnnotations.MetaAnalyzeCls | ||
public class WrongTestClassWithMultipleAnalyzeClassesAnnotations { | ||
|
||
@ArchTest | ||
public static final ArchRule dummy_rule = RuleThatFails.on(UnwantedClass.class); | ||
|
||
@Retention(RUNTIME) | ||
@Target(TYPE) | ||
@AnalyzeClasses(wholeClasspath = true) | ||
public @interface MetaAnalyzeCls { | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
archunit-junit/src/main/java/com/tngtech/archunit/junit/internal/AnnotationFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.tngtech.archunit.junit.internal; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.util.HashSet; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
class AnnotationFinder<T extends Annotation> { | ||
|
||
private final Class<T> annotationClass; | ||
|
||
public AnnotationFinder(final Class<T> annotationClass) { | ||
this.annotationClass = annotationClass; | ||
} | ||
|
||
/** | ||
* Recursively retrieve all {@link T} annotations from a given element. | ||
* | ||
* @param clazz The clazz from which to retrieve the annotation. | ||
* @return List of all found annotation instance or empty list. | ||
*/ | ||
public List<T> findAnnotationsOn(final Class<?> clazz) { | ||
return findAnnotations(clazz.getAnnotations(), new HashSet<>()); | ||
} | ||
|
||
private List<T> findAnnotations(final Annotation[] annotations, final HashSet<Annotation> visited) { | ||
final List<T> result = new LinkedList<>(); | ||
for (Annotation annotation : annotations) { | ||
if (visited.contains(annotation)) { | ||
continue; | ||
} else { | ||
visited.add(annotation); | ||
} | ||
if (annotationClass.isInstance(annotation)) { | ||
result.add(annotationClass.cast(annotation)); | ||
} else { | ||
result.addAll(findAnnotations(annotation.annotationType().getAnnotations(), visited)); | ||
} | ||
} | ||
return result; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure how to reach this from test.