-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #796 from stbischof/confann
[cm] use Config Annotations on custom Annotations
- Loading branch information
Showing
5 changed files
with
463 additions
and
69 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
org.osgi.test.junit5.cm/src/main/java/org/osgi/test/junit5/cm/AnnotationUtil.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,93 @@ | ||
/******************************************************************************* | ||
* Copyright (c) Contributors to the Eclipse Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*******************************************************************************/ | ||
|
||
package org.osgi.test.junit5.cm; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.AnnotatedElement; | ||
import java.util.AbstractMap; | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map.Entry; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
|
||
import org.osgi.test.common.annotation.config.WithConfiguration; | ||
import org.osgi.test.common.annotation.config.WithConfigurations; | ||
import org.osgi.test.common.annotation.config.WithFactoryConfiguration; | ||
import org.osgi.test.common.annotation.config.WithFactoryConfigurations; | ||
|
||
class AnnotationUtil { | ||
|
||
private AnnotationUtil() {} | ||
|
||
private static Predicate<Annotation> ANY_WITH_CONF_ANNNOTATIONS = t -> t.annotationType() | ||
.equals(WithFactoryConfiguration.class) | ||
|| t.annotationType() | ||
.equals(WithFactoryConfigurations.class) | ||
|| t.annotationType() | ||
.equals(WithConfiguration.class) | ||
|| t.annotationType() | ||
.equals(WithConfigurations.class); | ||
|
||
public static List<Annotation> findAllConfigAnnotations(AnnotatedElement annotatedElement) { | ||
return findAllAnnotationsMatching(annotatedElement, ANY_WITH_CONF_ANNNOTATIONS); | ||
} | ||
|
||
private static List<Annotation> findAllAnnotationsMatching(AnnotatedElement annotatedElement, | ||
Predicate<Annotation> test) { | ||
final ArrayList<Annotation> found = new ArrayList<>(); | ||
findAllAnnotationsMatching(annotatedElement, test, found, new HashSet<>()); | ||
return found; | ||
} | ||
|
||
private static void findAllAnnotationsMatching(AnnotatedElement annotatedElement, Predicate<Annotation> test, | ||
ArrayList<? super Annotation> found, Set<Entry<AnnotatedElement, Annotation>> visited) { | ||
Annotation[] declaredAnnotations = annotatedElement.getDeclaredAnnotations(); | ||
|
||
for (Annotation ann : declaredAnnotations) { | ||
if (isJavaLangAnnotation(ann) || isJunitAnnotation(ann)) { | ||
continue; | ||
} | ||
|
||
if (visited.add(new AbstractMap.SimpleEntry<>(annotatedElement, ann))) { | ||
findAllAnnotationsMatching(ann.annotationType(), test, found, visited); | ||
} | ||
|
||
if (test.test(ann)) { | ||
found.add(ann); | ||
} | ||
} | ||
} | ||
|
||
private static boolean isJavaLangAnnotation(Annotation annotation) { | ||
|
||
return annotation.annotationType() | ||
.getName() | ||
.startsWith("java.lang.annotation"); | ||
} | ||
|
||
private static boolean isJunitAnnotation(Annotation annotation) { | ||
|
||
return annotation.annotationType() | ||
.getName() | ||
.startsWith("org.junit"); | ||
} | ||
|
||
} |
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
Oops, something went wrong.