support annotaiton on constraint annotation

This commit is contained in:
Albert Shift 2015-06-10 16:46:57 -07:00
parent 52c3f19ea2
commit 1eb3c17d6b
3 changed files with 70 additions and 18 deletions

View file

@ -55,26 +55,14 @@ public final class MappingUtil {
for (Annotation constraintAnnotation : getterMethod.getDeclaredAnnotations()) {
list = addValidators(constraintAnnotation, list);
Class<? extends Annotation> annotationType = constraintAnnotation.annotationType();
Constraint constraint = annotationType.getDeclaredAnnotation(Constraint.class);
if (constraint == null) {
continue;
}
for (Class<? extends ConstraintValidator<?, ?>> clazz : constraint.validatedBy()) {
ConstraintValidator<? extends Annotation, ?> validator = ReflectionInstantiator.instantiateClass(clazz);
for (Annotation possibleConstraint : annotationType.getDeclaredAnnotations()) {
((ConstraintValidator) validator).initialize(constraintAnnotation);
list = addValidators(possibleConstraint, list);
if (list == null) {
list = new ArrayList<ConstraintValidator<? extends Annotation, ?>>();
}
list.add(validator);
}
}
@ -87,6 +75,38 @@ public final class MappingUtil {
}
}
private static List<ConstraintValidator<? extends Annotation, ?>> addValidators(Annotation constraintAnnotation, List<ConstraintValidator<? extends Annotation, ?>> list) {
Class<? extends Annotation> annotationType = constraintAnnotation.annotationType();
for (Annotation possibleConstraint : annotationType.getDeclaredAnnotations()) {
if (possibleConstraint instanceof Constraint) {
Constraint constraint = (Constraint) possibleConstraint;
for (Class<? extends ConstraintValidator<?, ?>> clazz : constraint.validatedBy()) {
ConstraintValidator<? extends Annotation, ?> validator = ReflectionInstantiator.instantiateClass(clazz);
((ConstraintValidator) validator).initialize(constraintAnnotation);
if (list == null) {
list = new ArrayList<ConstraintValidator<? extends Annotation, ?>>();
}
list.add(validator);
}
}
}
return list;
}
public static Optional<IdentityName> getIndexName(Method getterMethod) {
String indexName = null;

View file

@ -16,7 +16,6 @@
package com.noorq.casser.test.integration.core.simple;
import com.noorq.casser.mapping.annotation.Column;
import com.noorq.casser.mapping.annotation.Constraints;
import com.noorq.casser.mapping.annotation.PartitionKey;
import com.noorq.casser.mapping.annotation.Table;
@ -26,7 +25,7 @@ public interface User {
@PartitionKey
Long id();
@Constraints.LowerCase
@Username
@Column("override_name")
String name();

View file

@ -0,0 +1,33 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* 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.noorq.casser.test.integration.core.simple;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import com.noorq.casser.mapping.annotation.Constraints;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value = { ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Constraints.LowerCase
public @interface Username {
}