BVAL-150 special AnnotationLiteral for empty annotations
Project: http://git-wip-us.apache.org/repos/asf/bval/repo Commit: http://git-wip-us.apache.org/repos/asf/bval/commit/aad8498a Tree: http://git-wip-us.apache.org/repos/asf/bval/tree/aad8498a Diff: http://git-wip-us.apache.org/repos/asf/bval/diff/aad8498a Branch: refs/heads/master Commit: aad8498a3ab59e120dc24b4e94f4ce6566875c0e Parents: e36f14d Author: Mark Struberg <[email protected]> Authored: Fri Oct 21 18:48:54 2016 +0000 Committer: Mark Struberg <[email protected]> Committed: Fri Oct 21 18:48:54 2016 +0000 ---------------------------------------------------------------------- bval-core/pom.xml | 4 + .../java/org/apache/bval/cdi/AnyLiteral.java | 3 +- .../org/apache/bval/cdi/BValAnnotatedType.java | 7 +- .../org/apache/bval/cdi/DefaultLiteral.java | 2 +- .../apache/bval/cdi/EmptyAnnotationLiteral.java | 123 +++++++++++++++++++ .../bval/jsr/xml/AnnotationProxyBuilder.java | 3 +- 6 files changed, 136 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/bval/blob/aad8498a/bval-core/pom.xml ---------------------------------------------------------------------- diff --git a/bval-core/pom.xml b/bval-core/pom.xml index 485e3f0..f56ee89 100644 --- a/bval-core/pom.xml +++ b/bval-core/pom.xml @@ -41,11 +41,14 @@ <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> + + <!-- we only support validation in DynaBeans, but do not use c-beanutils in BVal itself --> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils-core</artifactId> <optional>true</optional> </dependency> + <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-weaver-privilizer-api</artifactId> @@ -130,6 +133,7 @@ build.timestamp=${timestamp} <artifactSet> <includes> <include>org.apache.commons:commons-lang3</include> + <include>org.apache.commons:commons-lang3</include> </includes> </artifactSet> <filters> http://git-wip-us.apache.org/repos/asf/bval/blob/aad8498a/bval-jsr/src/main/java/org/apache/bval/cdi/AnyLiteral.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/cdi/AnyLiteral.java b/bval-jsr/src/main/java/org/apache/bval/cdi/AnyLiteral.java index 5e87660..2b62c1e 100644 --- a/bval-jsr/src/main/java/org/apache/bval/cdi/AnyLiteral.java +++ b/bval-jsr/src/main/java/org/apache/bval/cdi/AnyLiteral.java @@ -21,7 +21,7 @@ package org.apache.bval.cdi; import javax.enterprise.inject.Any; import javax.enterprise.util.AnnotationLiteral; -public class AnyLiteral extends AnnotationLiteral<Any> implements Any { +public class AnyLiteral extends EmptyAnnotationLiteral<Any> implements Any { private static final long serialVersionUID = 1L; /** @@ -33,4 +33,5 @@ public class AnyLiteral extends AnnotationLiteral<Any> implements Any { public String toString() { return "@javax.enterprise.inject.Any()"; } + } http://git-wip-us.apache.org/repos/asf/bval/blob/aad8498a/bval-jsr/src/main/java/org/apache/bval/cdi/BValAnnotatedType.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/cdi/BValAnnotatedType.java b/bval-jsr/src/main/java/org/apache/bval/cdi/BValAnnotatedType.java index fa932f0..a143a5b 100644 --- a/bval-jsr/src/main/java/org/apache/bval/cdi/BValAnnotatedType.java +++ b/bval-jsr/src/main/java/org/apache/bval/cdi/BValAnnotatedType.java @@ -98,14 +98,15 @@ public class BValAnnotatedType<A> implements AnnotatedType<A> { return false; } - public static class BValBindingLiteral extends AnnotationLiteral<BValBinding> implements BValBinding { + public static class BValBindingLiteral extends EmptyAnnotationLiteral<BValBinding> implements BValBinding { private static final long serialVersionUID = 1L; public static final Annotation INSTANCE = new BValBindingLiteral(); @Override - public Class<? extends Annotation> annotationType() { - return BValBinding.class; + public String toString() { + return "@org.apache.bval.cdi.BValBinding()"; } + } } http://git-wip-us.apache.org/repos/asf/bval/blob/aad8498a/bval-jsr/src/main/java/org/apache/bval/cdi/DefaultLiteral.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/cdi/DefaultLiteral.java b/bval-jsr/src/main/java/org/apache/bval/cdi/DefaultLiteral.java index db12240..07b2246 100644 --- a/bval-jsr/src/main/java/org/apache/bval/cdi/DefaultLiteral.java +++ b/bval-jsr/src/main/java/org/apache/bval/cdi/DefaultLiteral.java @@ -21,7 +21,7 @@ package org.apache.bval.cdi; import javax.enterprise.inject.Default; import javax.enterprise.util.AnnotationLiteral; -public class DefaultLiteral extends AnnotationLiteral<Default> implements Default { +public class DefaultLiteral extends EmptyAnnotationLiteral<Default> implements Default { private static final long serialVersionUID = 1L; public static final DefaultLiteral INSTANCE = new DefaultLiteral(); http://git-wip-us.apache.org/repos/asf/bval/blob/aad8498a/bval-jsr/src/main/java/org/apache/bval/cdi/EmptyAnnotationLiteral.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/cdi/EmptyAnnotationLiteral.java b/bval-jsr/src/main/java/org/apache/bval/cdi/EmptyAnnotationLiteral.java new file mode 100644 index 0000000..bb75193 --- /dev/null +++ b/bval-jsr/src/main/java/org/apache/bval/cdi/EmptyAnnotationLiteral.java @@ -0,0 +1,123 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.bval.cdi; + +import java.lang.annotation.Annotation; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + +import javax.enterprise.util.AnnotationLiteral; + +/** + * Base class for AnnotationLiterals which have no members. + * Taken from Apache OpenWebBeans. + * @param <T> + */ +public abstract class EmptyAnnotationLiteral<T extends Annotation> extends AnnotationLiteral<T> +{ + private Class<T> annotationType; + + protected EmptyAnnotationLiteral() + { + // Leave this constructor protected, because an EmptyAnnotationLiteral may never directly be instantiated + } + + /** + * Implemented for compatibility reasons with other cdi-api jar's. + * See OWB-802. + */ + @Override + public Class<? extends Annotation> annotationType() + { + if (annotationType == null) + { + annotationType = getAnnotationType(getClass()); + } + return annotationType; + } + + /** + * Implemented for performance reasons. + * This is needed because an Annotation always returns 0 as hashCode + * if there is no method in it. + * Contrary to this the generic {@link AnnotationLiteral#hashCode()} + * always does search for methods via reflection and only then returns 0. + * Not very well performing ... + * @return always 0 + */ + @Override + public int hashCode() + { + return 0; + } + + /** + * Just checks whether the 2 classes have the same annotationType. + * We do not need to dynamically evaluate the member values via reflection + * as there are no members in this annotation at all. + */ + @Override + public boolean equals(final Object other) + { + // implemented for performance reasons + return Annotation.class.isInstance(other) && + Annotation.class.cast(other).annotationType().equals(annotationType()); + } + + private Class<T> getAnnotationType(Class<?> definedClazz) + { + Type superClazz = definedClazz.getGenericSuperclass(); + + Class<T> clazz = null; + + if (superClazz.equals(Object.class)) + { + throw new RuntimeException("Super class must be parametrized type!"); + } + else if (superClazz instanceof ParameterizedType) + { + ParameterizedType paramType = (ParameterizedType) superClazz; + Type[] actualArgs = paramType.getActualTypeArguments(); + + if (actualArgs.length == 1) + { + //Actual annotation type + Type type = actualArgs[0]; + + if (type instanceof Class) + { + clazz = (Class<T>) type; + return clazz; + } + else + { + throw new RuntimeException("Not class type!"); + } + } + else + { + throw new RuntimeException("More than one parametric type!"); + } + } + else + { + return getAnnotationType((Class<?>) superClazz); + } + } +} http://git-wip-us.apache.org/repos/asf/bval/blob/aad8498a/bval-jsr/src/main/java/org/apache/bval/jsr/xml/AnnotationProxyBuilder.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/jsr/xml/AnnotationProxyBuilder.java b/bval-jsr/src/main/java/org/apache/bval/jsr/xml/AnnotationProxyBuilder.java index 564b92f..dedfabc 100644 --- a/bval-jsr/src/main/java/org/apache/bval/jsr/xml/AnnotationProxyBuilder.java +++ b/bval-jsr/src/main/java/org/apache/bval/jsr/xml/AnnotationProxyBuilder.java @@ -16,6 +16,7 @@ */ package org.apache.bval.jsr.xml; +import org.apache.bval.cdi.EmptyAnnotationLiteral; import org.apache.bval.jsr.ConstraintAnnotationAttributes; import org.apache.bval.util.reflection.Reflection; import org.apache.commons.weaver.privilizer.Privileged; @@ -217,7 +218,7 @@ public final class AnnotationProxyBuilder<A extends Annotation> { } } - public static final class ValidAnnotation extends AnnotationLiteral<Valid> implements Valid { + public static final class ValidAnnotation extends EmptyAnnotationLiteral<Valid> implements Valid { private static final long serialVersionUID = 1L; public static final ValidAnnotation INSTANCE = new ValidAnnotation();
