parse contraint annotations and initilize validators
This commit is contained in:
parent
9919fd9e25
commit
49cb219849
5 changed files with 110 additions and 0 deletions
|
@ -15,15 +15,19 @@
|
|||
*/
|
||||
package com.noorq.casser.core.reflect;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
|
||||
import com.noorq.casser.core.SessionRepository;
|
||||
import com.noorq.casser.mapping.CasserEntity;
|
||||
import com.noorq.casser.mapping.CasserProperty;
|
||||
import com.noorq.casser.mapping.ColumnType;
|
||||
import com.noorq.casser.mapping.IdentityName;
|
||||
import com.noorq.casser.mapping.MappingUtil;
|
||||
import com.noorq.casser.mapping.OrderingDirection;
|
||||
import com.noorq.casser.mapping.type.AbstractDataType;
|
||||
import com.noorq.casser.support.CasserMappingException;
|
||||
|
@ -98,4 +102,8 @@ public final class CasserNamedProperty implements CasserProperty {
|
|||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConstraintValidator<? extends Annotation, ?>[] getValidators() {
|
||||
return MappingUtil.EMPTY_VALIDATORS;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* 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.core.reflect;
|
||||
|
||||
import com.noorq.casser.support.CasserMappingException;
|
||||
|
||||
public final class ReflectionInstantiator {
|
||||
|
||||
private ReflectionInstantiator() {
|
||||
}
|
||||
|
||||
public static <T> T instantiateClass(Class<T> clazz) {
|
||||
|
||||
try {
|
||||
return clazz.newInstance();
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
throw new CasserMappingException("invalid class " + clazz, e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -15,11 +15,14 @@
|
|||
*/
|
||||
package com.noorq.casser.mapping;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
|
||||
import com.noorq.casser.core.SessionRepository;
|
||||
import com.noorq.casser.mapping.javatype.AbstractJavaType;
|
||||
import com.noorq.casser.mapping.javatype.MappingJavaTypes;
|
||||
|
@ -43,6 +46,8 @@ public final class CasserMappingProperty implements CasserProperty {
|
|||
private volatile Optional<Function<Object, Object>> readConverter = null;
|
||||
private volatile Optional<Function<Object, Object>> writeConverter = null;
|
||||
|
||||
private final ConstraintValidator<? extends Annotation, ?>[] validators;
|
||||
|
||||
public CasserMappingProperty(CasserMappingEntity entity, Method getter) {
|
||||
this.entity = entity;
|
||||
this.getter = getter;
|
||||
|
@ -57,6 +62,8 @@ public final class CasserMappingProperty implements CasserProperty {
|
|||
this.abstractJavaType = MappingJavaTypes.resolveJavaType(this.javaType);
|
||||
|
||||
this.dataType = abstractJavaType.resolveDataType(this.getter, this.genericJavaType, this.columnInfo.getColumnType());
|
||||
|
||||
this.validators = MappingUtil.getValidators(getter);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -129,4 +136,10 @@ public final class CasserMappingProperty implements CasserProperty {
|
|||
return writeConverter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConstraintValidator<? extends Annotation, ?>[] getValidators() {
|
||||
return validators;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -15,10 +15,13 @@
|
|||
*/
|
||||
package com.noorq.casser.mapping;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
|
||||
import com.noorq.casser.core.SessionRepository;
|
||||
import com.noorq.casser.mapping.type.AbstractDataType;
|
||||
|
||||
|
@ -48,4 +51,6 @@ public interface CasserProperty {
|
|||
|
||||
Optional<Function<Object, Object>> getWriteConverter(SessionRepository repository);
|
||||
|
||||
ConstraintValidator<? extends Annotation, ?>[] getValidators();
|
||||
|
||||
}
|
||||
|
|
|
@ -15,9 +15,15 @@
|
|||
*/
|
||||
package com.noorq.casser.mapping;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.ConstraintValidator;
|
||||
|
||||
import com.noorq.casser.core.Casser;
|
||||
import com.noorq.casser.core.Getter;
|
||||
import com.noorq.casser.core.reflect.CasserPropertyNode;
|
||||
|
@ -25,6 +31,7 @@ import com.noorq.casser.core.reflect.DslExportable;
|
|||
import com.noorq.casser.core.reflect.ListDsl;
|
||||
import com.noorq.casser.core.reflect.MapDsl;
|
||||
import com.noorq.casser.core.reflect.MapExportable;
|
||||
import com.noorq.casser.core.reflect.ReflectionInstantiator;
|
||||
import com.noorq.casser.core.reflect.SetDsl;
|
||||
import com.noorq.casser.mapping.annotation.Index;
|
||||
import com.noorq.casser.mapping.annotation.Table;
|
||||
|
@ -33,11 +40,53 @@ import com.noorq.casser.mapping.annotation.UDT;
|
|||
import com.noorq.casser.support.CasserMappingException;
|
||||
import com.noorq.casser.support.DslPropertyException;
|
||||
|
||||
|
||||
public final class MappingUtil {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static final ConstraintValidator<? extends Annotation, ?>[] EMPTY_VALIDATORS = new ConstraintValidator[0];
|
||||
|
||||
private MappingUtil() {
|
||||
}
|
||||
|
||||
public static ConstraintValidator<? extends Annotation, ?>[] getValidators(Method getterMethod) {
|
||||
|
||||
List<ConstraintValidator<? extends Annotation, ?>> list = null;
|
||||
|
||||
for (Annotation constraintAnnotation : getterMethod.getDeclaredAnnotations()) {
|
||||
|
||||
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);
|
||||
|
||||
((ConstraintValidator) validator).initialize(constraintAnnotation);
|
||||
|
||||
if (list == null) {
|
||||
list = new ArrayList<ConstraintValidator<? extends Annotation, ?>>();
|
||||
}
|
||||
|
||||
list.add(validator);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (list == null) {
|
||||
return EMPTY_VALIDATORS;
|
||||
}
|
||||
else {
|
||||
return list.toArray(EMPTY_VALIDATORS);
|
||||
}
|
||||
}
|
||||
|
||||
public static Optional<IdentityName> getIndexName(Method getterMethod) {
|
||||
|
||||
String indexName = null;
|
||||
|
|
Loading…
Reference in a new issue