-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Since the code to recursively analyze annotation parameters is complicated, error prone and duplicated, it makes sense to introduce a more sophisticated parameter handling within `JavaAnnotation` itself. This way we can a) separate traversal from actual handling (say what we want to do, not how), b) unify and test the traversal in isolation and c) offer a nicer end user API for handling annotation parameters for `JavaAnnotations` that are not used via Reflection in tests (so far there was pretty much no choice but to use instanceof's and casts requiring some knowledge about the imported annotation type). Parameter handling is implemented as a Visitor pattern, which is well known and established for complex tree structures (with various types of nodes), where conditional traversal happens in various places. Signed-off-by: Peter Gafert <[email protected]>
- Loading branch information
1 parent
edbff5a
commit bf0ac80
Showing
5 changed files
with
695 additions
and
61 deletions.
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
151 changes: 151 additions & 0 deletions
151
...rc/main/java/com/tngtech/archunit/core/domain/JavaAnnotationParameterVisitorAcceptor.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,151 @@ | ||
/* | ||
* Copyright 2014-2020 TNG Technology Consulting GmbH | ||
* | ||
* 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. | ||
*/ | ||
package com.tngtech.archunit.core.domain; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Sole purpose is to keep all the ugly boring fluff out of {@link JavaAnnotation}. | ||
* The order of the conditionals is based on an educated guess about the frequency of annotation parameter types. | ||
*/ | ||
class JavaAnnotationParameterVisitorAcceptor { | ||
static void accept(Map<String, Object> properties, JavaAnnotation.ParameterVisitor parameterVisitor) { | ||
for (Map.Entry<String, Object> property : properties.entrySet()) { | ||
if (property.getValue() instanceof Boolean) { | ||
parameterVisitor.visitBoolean(property.getKey(), (Boolean) property.getValue()); | ||
continue; | ||
} | ||
if (property.getValue() instanceof Integer) { | ||
parameterVisitor.visitInteger(property.getKey(), (Integer) property.getValue()); | ||
continue; | ||
} | ||
if (property.getValue() instanceof String) { | ||
parameterVisitor.visitString(property.getKey(), (String) property.getValue()); | ||
continue; | ||
} | ||
if (property.getValue() instanceof JavaClass) { | ||
parameterVisitor.visitClass(property.getKey(), (JavaClass) property.getValue()); | ||
continue; | ||
} | ||
if (property.getValue() instanceof JavaEnumConstant) { | ||
parameterVisitor.visitEnumConstant(property.getKey(), (JavaEnumConstant) property.getValue()); | ||
continue; | ||
} | ||
if (property.getValue() instanceof JavaAnnotation<?>) { | ||
parameterVisitor.visitAnnotation(property.getKey(), (JavaAnnotation<?>) property.getValue()); | ||
continue; | ||
} | ||
if (property.getValue() instanceof String[]) { | ||
for (String value : ((String[]) property.getValue())) { | ||
parameterVisitor.visitString(property.getKey(), value); | ||
} | ||
continue; | ||
} | ||
if (property.getValue() instanceof JavaClass[]) { | ||
for (JavaClass value : ((JavaClass[]) property.getValue())) { | ||
parameterVisitor.visitClass(property.getKey(), value); | ||
} | ||
continue; | ||
} | ||
if (property.getValue() instanceof JavaEnumConstant[]) { | ||
for (JavaEnumConstant value : ((JavaEnumConstant[]) property.getValue())) { | ||
parameterVisitor.visitEnumConstant(property.getKey(), value); | ||
} | ||
continue; | ||
} | ||
if (property.getValue() instanceof JavaAnnotation<?>[]) { | ||
for (JavaAnnotation<?> value : ((JavaAnnotation<?>[]) property.getValue())) { | ||
parameterVisitor.visitAnnotation(property.getKey(), value); | ||
} | ||
continue; | ||
} | ||
if (property.getValue() instanceof Long) { | ||
parameterVisitor.visitLong(property.getKey(), (Long) property.getValue()); | ||
continue; | ||
} | ||
if (property.getValue() instanceof int[]) { | ||
for (int value : ((int[]) property.getValue())) { | ||
parameterVisitor.visitInteger(property.getKey(), value); | ||
} | ||
continue; | ||
} | ||
if (property.getValue() instanceof boolean[]) { | ||
for (boolean value : ((boolean[]) property.getValue())) { | ||
parameterVisitor.visitBoolean(property.getKey(), value); | ||
} | ||
continue; | ||
} | ||
if (property.getValue() instanceof long[]) { | ||
for (long value : ((long[]) property.getValue())) { | ||
parameterVisitor.visitLong(property.getKey(), value); | ||
} | ||
continue; | ||
} | ||
if (property.getValue() instanceof Byte) { | ||
parameterVisitor.visitByte(property.getKey(), (Byte) property.getValue()); | ||
continue; | ||
} | ||
if (property.getValue() instanceof byte[]) { | ||
for (byte value : ((byte[]) property.getValue())) { | ||
parameterVisitor.visitByte(property.getKey(), value); | ||
} | ||
continue; | ||
} | ||
if (property.getValue() instanceof Character) { | ||
parameterVisitor.visitCharacter(property.getKey(), (Character) property.getValue()); | ||
continue; | ||
} | ||
if (property.getValue() instanceof char[]) { | ||
for (char value : ((char[]) property.getValue())) { | ||
parameterVisitor.visitCharacter(property.getKey(), value); | ||
} | ||
continue; | ||
} | ||
if (property.getValue() instanceof Double) { | ||
parameterVisitor.visitDouble(property.getKey(), (Double) property.getValue()); | ||
continue; | ||
} | ||
if (property.getValue() instanceof double[]) { | ||
for (double value : ((double[]) property.getValue())) { | ||
parameterVisitor.visitDouble(property.getKey(), value); | ||
} | ||
continue; | ||
} | ||
if (property.getValue() instanceof Float) { | ||
parameterVisitor.visitFloat(property.getKey(), (Float) property.getValue()); | ||
continue; | ||
} | ||
if (property.getValue() instanceof float[]) { | ||
for (float value : ((float[]) property.getValue())) { | ||
parameterVisitor.visitFloat(property.getKey(), value); | ||
} | ||
continue; | ||
} | ||
if (property.getValue() instanceof Short) { | ||
parameterVisitor.visitShort(property.getKey(), (Short) property.getValue()); | ||
continue; | ||
} | ||
if (property.getValue() instanceof short[]) { | ||
for (short value : ((short[]) property.getValue())) { | ||
parameterVisitor.visitShort(property.getKey(), value); | ||
} | ||
// The danger of missing the continue when resorting is bigger than the "nuisance" of the obsolete continue | ||
//noinspection UnnecessaryContinue | ||
continue; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.