org.tomitribe.crest.val.BuiltInValidation Maven / Gradle / Ivy
/*
* 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.tomitribe.crest.val;
import org.tomitribe.crest.api.validation.Validation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.IntConsumer;
import java.util.stream.Stream;
import static java.util.Optional.ofNullable;
public class BuiltInValidation implements BeanValidationImpl {
private final BeanValidationImpl sibling;
private final Map> validatorPerExecutable = new ConcurrentHashMap<>();
private final Function, Object> lookup;
public BuiltInValidation(final BeanValidationImpl sibling, final Function, Object> lookup) {
this.sibling = sibling;
this.lookup = lookup;
}
@Override
public void validateParameters(final Object instanceOrClass, final Method method, final Object[] parameters) {
doValidateParameters(method, parameters);
if (sibling != null) {
sibling.validateParameters(instanceOrClass, method, parameters);
}
}
@Override
public void validateParameters(final Constructor constructor, final Object[] parameters) {
doValidateParameters(constructor, parameters);
if (sibling != null) {
sibling.validateParameters(constructor, parameters);
}
}
@Override
public Optional> messages(final Throwable exception) {
return Optional.of(exception)
.filter(ValidationMessages.class::isInstance)
.map(ValidationMessages.class::cast)
.map(e -> e.messages);
}
private Object createInstance(final Class> value) {
return ofNullable(lookup.apply(value)).orElseGet(() -> {
try {
return value.getConstructor().newInstance();
} catch (final InstantiationException | IllegalAccessException | NoSuchMethodException e) {
throw new IllegalArgumentException(e);
} catch (final InvocationTargetException e) {
throw new IllegalStateException(e.getTargetException());
}
});
}
private void doValidateParameters(final Executable executable, final Object[] parameters) {
validatorPerExecutable.computeIfAbsent(executable, e -> {
if (executable.getParameterCount() == 0) {
return a -> {
};
}
final Consumer[] validators = Stream.of(executable.getParameters())
.map(this::toValidator)
.toArray(Consumer[]::new);
return args -> executeValidations(validators.length, i -> validators[i].accept(args[i]));
}).accept(parameters);
}
private Consumer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy