Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code cleanup #240

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions gradle/license.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apply plugin: 'com.github.hierynomus.license'

license {
header = rootProject.file('config/HEADER')
strictCheck = true
ignoreFailures = true
mapping {
kt = 'SLASHSTAR_STYLE'
java = 'SLASHSTAR_STYLE'
groovy = 'SLASHSTAR_STYLE'
}
ext.year = '2017-2023'

exclude "**/transaction/**"
exclude '**/*.txt'
exclude '**/*.html'
exclude '**/*.xml'
exclude '**/*.json'
exclude '**/build-info.properties'
exclude '**/git.properties'
exclude '**/othergit.properties'
}
2 changes: 1 addition & 1 deletion src/main/docs/guide/dataClassesValidation.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ To validate data classes, e.g. POJOs (typically used in JSON interchange), the c
.POJO Validation Example
snippet::io.micronaut.docs.validation.Person[tags="class"]

TIP: The {coreDocsUrl}core/annotation/Introspected[@Introspected] annotation can be used as a meta-annotation; common annotations like `@javax.persistence.Entity` are treated as `@Introspected`
TIP: The {coreDocsUrl}core/annotation/Introspected[@Introspected] annotation can be used as a meta-annotation; common annotations like `@jakarta.persistence.Entity` are treated as `@Introspected`

The above example defines a `Person` class that has two properties (`name` and `age`) that have constraints applied. Note that in Java the annotations can be on the field or the getter, and with Kotlin data classes, the annotation should target the field.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.docs.validation.custom;
package io.micronaut.docs.validation.custom

// tag::imports[]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import spock.lang.Specification
class BookInfoSpec extends Specification {

@Inject
BookInfoService bookInfoService;
BookInfoService bookInfoService

// tag::validate-iterables[]
void testAuthorNamesAreValidated() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.docs.validation.custom;
package io.micronaut.docs.validation.custom

// tag::imports[]
import io.micronaut.docs.validation.custom.DurationPattern
import io.micronaut.core.annotation.AnnotationValue
import io.micronaut.core.annotation.Introspected
import io.micronaut.validation.validator.constraints.ConstraintValidator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.micronaut.context.annotation.Property;
import io.micronaut.context.annotation.Requires;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import jakarta.validation.Valid;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,9 @@ public ProtocolDescription getDefaultProtocol() {
}

private static JavaArchive buildSupportLibrary() {
JavaArchive supportLib = ShrinkWrap.create(JavaArchive.class, "micronaut-validation-tck-support.jar")
return ShrinkWrap.create(JavaArchive.class, "micronaut-validation-tck-support.jar")
.addAsManifestResource("META-INF/services/io.micronaut.inject.visitor.TypeElementVisitor")
.addPackage(TestClassVisitor.class.getPackage());
return supportLib;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package io.micronaut.validation
package io.micronaut.validation.visitor

import io.micronaut.annotation.processing.test.AbstractTypeElementSpec
import io.micronaut.inject.BeanDefinition
import io.micronaut.inject.ValidatedBeanDefinition
import io.micronaut.inject.validation.RequiresValidation
import io.micronaut.inject.writer.BeanDefinitionVisitor
import jakarta.validation.Valid

import java.time.LocalDate

class ValidatedParseSpec extends AbstractTypeElementSpec {
final static String VALIDATED_ANN = "io.micronaut.validation.Validated";
final static String VALIDATED_ANN = "io.micronaut.validation.Validated"

void "test constraints on beans make them @Validated"() {
given:
Expand Down Expand Up @@ -77,7 +74,7 @@ class Test {
}
''')
when:
def method = definition.getRequiredMethod("setList", List<String>);
def method = definition.getRequiredMethod("setList", List<String>)

then:
method.hasStereotype(VALIDATED_ANN)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package io.micronaut.validation.visitor

import io.micronaut.aop.Around
import io.micronaut.ast.transform.test.AbstractBeanDefinitionSpec
import io.micronaut.core.beans.BeanIntrospection
import io.micronaut.inject.ProxyBeanDefinition
import io.micronaut.inject.writer.BeanDefinitionVisitor
import io.micronaut.inject.writer.BeanDefinitionWriter
import io.micronaut.validation.ValidatedParseSpec

import java.time.LocalDate

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,13 @@ public ValidationCloseable validating(Object obj) {
public ValidationCloseable withExecutableParameterValues(Object[] executableParameterValues) {
Object[] prevExecutableParameterValues = this.executableParameterValues;
this.executableParameterValues = executableParameterValues;
return () -> DefaultConstraintValidatorContext.this.executableParameterValues = prevExecutableParameterValues;
return () -> this.executableParameterValues = prevExecutableParameterValues;
}

public ValidationCloseable withExecutableReturnValue(Object executableReturnValue) {
Object prevExecutableReturnValue = this.executableReturnValue;
this.executableReturnValue = executableReturnValue;
return () -> DefaultConstraintValidatorContext.this.executableReturnValue = prevExecutableReturnValue;
return () -> this.executableReturnValue = prevExecutableReturnValue;
}

public GroupsValidation withGroupSequence(@NonNull ValidationGroup validationGroup) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import jakarta.validation.metadata.ConstraintDescriptor;

/**
* The default implementation {@link jakarta.validation.ConstraintValidatorContext.ConstraintViolationBuilder}.
* The default implementation {@link ConstraintValidatorContext.ConstraintViolationBuilder}.
*
* @param <R> The result type
* @author Denis Stepnov
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ public <T> Set<ConstraintViolation<T>> validate(@NonNull T object, @Nullable Cla
* @return The constraint violations
*/
@Override
@SuppressWarnings("ConstantConditions")
@NonNull
public <T> Set<ConstraintViolation<T>> validate(@NonNull BeanIntrospection<T> introspection,
@NonNull T object,
Expand Down Expand Up @@ -511,7 +510,6 @@ public <T> Set<ConstraintViolation<T>> validateConstructorParameters(Class<? ext
@NonNull Class<?>[] groups) {
requireNonNull("groups", groups);

//noinspection ConstantConditions
parameterValues = parameterValues != null ? parameterValues : ArrayUtils.EMPTY_OBJECT_ARRAY;
final int argLength = constructorArguments.length;
if (parameterValues.length != argLength) {
Expand Down Expand Up @@ -730,7 +728,6 @@ public <T> void validateBean(@NonNull BeanResolutionContext resolutionContext,
@Nullable
protected <T> BeanIntrospection<T> getBeanIntrospection(@NonNull T object,
@NonNull Class<T> definedClass) {
//noinspection ConstantConditions
if (object == null) {
return null;
}
Expand All @@ -748,7 +745,6 @@ protected <T> BeanIntrospection<T> getBeanIntrospection(@NonNull T object,
@SuppressWarnings({"WeakerAccess", "unchecked"})
@Nullable
protected <T> BeanIntrospection<T> getBeanIntrospection(@NonNull T object) {
//noinspection ConstantConditions
if (object == null) {
return null;
}
Expand Down Expand Up @@ -1471,10 +1467,7 @@ private <R> List<DefaultConstraintDescriptor<Annotation>> getConstraints(Default
(AnnotationValue<Annotation>) annotationValue,
annotationMetadata
))
.filter(annotationValue -> {
boolean constraintIncluded = isConstraintIncluded(context, annotationValue);
return constraintIncluded;
});
.filter(annotationValue -> isConstraintIncluded(context, annotationValue));
})
.toList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.util.Set;

/**
* Extended version of {@link ExecutableValidator} that operates on {@link io.micronaut.inject.ExecutableMethod} instances.
* Extended version of {@link ExecutableValidator} that operates on {@link ExecutableMethod} instances.
*
* @author graemerocher
* @since 1.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,11 @@ public Set<ConstraintDescriptor<?>> getConstraintDescriptors() {
return beanProperty.getAnnotationTypesByStereotype(Constraint.class)
.stream().flatMap(type -> beanProperty.getAnnotationValuesByType(type).stream().map(annotationValue -> {
AnnotationValue<? extends Annotation> annotation = beanProperty.getAnnotation(type);
DefaultConstraintDescriptor<?> descriptor = new DefaultConstraintDescriptor(
return (DefaultConstraintDescriptor<?>) new DefaultConstraintDescriptor(
type,
annotation,
beanProperty
);
return descriptor;
})).collect(Collectors.toSet());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public String toString() {
ContextualPath withContainerContext(ContainerContext containerContext) {
ContainerContext prevContainerContext = this.containerContext;
this.containerContext = containerContext;
return () -> ValidationPath.this.containerContext = prevContainerContext;
return () -> this.containerContext = prevContainerContext;
}

ContextualPath addBeanNode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.util.Set;

/**
* Extended version of the {@link jakarta.validation.Valid} interface for Micronaut's implementation.
* Extended version of the {@link Valid} interface for Micronaut's implementation.
*
* <p>The {@link #getConstraintsForClass(Class)} method is not supported by the implementation.</p>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

/**
* Constraint validator that can be used at either runtime or compilation time and
* is capable of validation {@link jakarta.validation.Constraint} instances. Allows defining validators that work with both Hibernate validator and Micronaut's validator.
* is capable of validation {@link Constraint} instances. Allows defining validators that work with both Hibernate validator and Micronaut's validator.
*
* <p>Unlike the specification's interface this one can uses as a functional interface. Implementor should not implement the {@link #initialize(Annotation)} method and should instead read the passed {@link AnnotationValue}.</p>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

/**
* A factory bean that contains implementation for many of the default validations.
* This approach is preferred as it generates less classes and smaller byte code than defining a
* This approach is preferred as it generates fewer classes and smaller byte code than defining a
* validator class for each case.
*
* @author graemerocher
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@ public final class DomainNameUtil {
private static final String IP_V6_DOMAIN = "(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))";

/**
* Regular expression for the domain part of an URL
* Regular expression for the domain part of a URL
* <p>
* A host string must be a domain string, an IPv4 address string, or "[", followed by an IPv6 address string,
* followed by "]".
*/
private static final Pattern DOMAIN_PATTERN = Pattern.compile(
DOMAIN + "|\\[" + IP_V6_DOMAIN + "\\]", CASE_INSENSITIVE
DOMAIN + "|\\[" + IP_V6_DOMAIN + "]", CASE_INSENSITIVE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was the regex incorrect?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just redundant escape ] symbol:
изображение

);

/**
* Regular expression for the domain part of an email address (everything after '@').
*/
private static final Pattern EMAIL_DOMAIN_PATTERN = Pattern.compile(
DOMAIN + "|\\[" + IP_DOMAIN + "\\]|" + "\\[IPv6:" + IP_V6_DOMAIN + "\\]", CASE_INSENSITIVE
DOMAIN + "|\\[" + IP_DOMAIN + "]|" + "\\[IPv6:" + IP_V6_DOMAIN + "]", CASE_INSENSITIVE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was the regex incorrect?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just redundant escape ] symbol:
изображение

);

private DomainNameUtil() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.micronaut.validation


import io.micronaut.annotation.processing.TypeElementVisitorProcessor
import io.micronaut.annotation.processing.test.AbstractTypeElementSpec
import io.micronaut.annotation.processing.test.JavaParser
import io.micronaut.context.annotation.Replaces
import io.micronaut.context.annotation.Requires
import io.micronaut.context.visitor.ConfigurationReaderVisitor
import io.micronaut.core.beans.BeanIntrospection
import io.micronaut.core.beans.BeanIntrospectionReference
Expand Down Expand Up @@ -692,7 +692,7 @@ interface GroupTwo {}
interface GroupThree {}
''')
def clazz = context.classLoader.loadClass('test.$Address$IntrospectionRef')
BeanIntrospectionReference reference = clazz.newInstance()
BeanIntrospectionReference reference = clazz.getDeclaredConstructor().newInstance()


expect:
Expand Down Expand Up @@ -767,7 +767,7 @@ class Book {

@Singleton
@Replaces(BeanIntrospectionModule)
@io.micronaut.context.annotation.Requires(property = "bean.introspection.test")
@Requires(property = "bean.introspection.test")
static class StaticBeanIntrospectionModule extends BeanIntrospectionModule {
Map<Class<?>, BeanIntrospection> introspectionMap = [:]
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package io.micronaut.validation;

import io.micronaut.context.annotation.Executable;
import io.micronaut.core.annotation.Introspected;
import jakarta.inject.Singleton;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import java.util.Optional;
@ConfigurationProperties("foo.bar")
@Executable
interface MyConfig {
@javax.annotation.Nullable
@jakarta.annotation.Nullable
String getHost();

@jakarta.validation.constraints.Min(10L)
Expand Down Expand Up @@ -278,7 +278,7 @@ interface MyConfig {
''')
then:
def e = thrown(RuntimeException)
e.message.contains('Only getter methods are allowed on @ConfigurationProperties interfaces: junk(java.lang.String). You can change the accessors using @AccessorsStyle annotation');
e.message.contains('Only getter methods are allowed on @ConfigurationProperties interfaces: junk(java.lang.String). You can change the accessors using @AccessorsStyle annotation')
}

void "test getter that returns void method"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ class NullablePrimitiveSpec extends AbstractTypeElementSpec {

where:
deprecated | annotation
false | 'javax.annotation.Nullable'
false | 'jakarta.annotation.Nullable'
false | 'io.micronaut.core.annotation.Nullable'
false | 'edu.umd.cs.findbugs.annotations.Nullable'
false | 'javax.annotation.Nonnull'
false | 'jakarta.annotation.Nonnull'
false | 'io.micronaut.core.annotation.NonNull'
false | 'edu.umd.cs.findbugs.annotations.NonNull'
}
Expand Down Expand Up @@ -81,7 +81,9 @@ class NullablePrimitiveSpec extends AbstractTypeElementSpec {
output.contains(warning)

where:
annotation << ['@javax.annotation.Nullable', '@io.micronaut.core.annotation.Nullable', '@edu.umd.cs.findbugs.annotations.Nullable']
annotation << ['@jakarta.annotation.Nullable',
'@io.micronaut.core.annotation.Nullable',
'@edu.umd.cs.findbugs.annotations.Nullable']
}

@Unroll
Expand Down Expand Up @@ -109,7 +111,9 @@ class NullablePrimitiveSpec extends AbstractTypeElementSpec {
!output.contains(warning)

where:
annotation << ['@javax.annotation.Nullable', '@io.micronaut.core.annotation.Nullable', '@edu.umd.cs.findbugs.annotations.Nullable']
annotation << ['@jakarta.annotation.Nullable',
'@io.micronaut.core.annotation.Nullable',
'@edu.umd.cs.findbugs.annotations.Nullable']
}

@Unroll
Expand All @@ -132,7 +136,9 @@ class NullablePrimitiveSpec extends AbstractTypeElementSpec {
!output.contains(warning)

where:
annotation << ['@javax.annotation.Nullable', '@io.micronaut.core.annotation.Nullable', '@edu.umd.cs.findbugs.annotations.Nullable']
annotation << ['@jakarta.annotation.Nullable',
'@io.micronaut.core.annotation.Nullable',
'@edu.umd.cs.findbugs.annotations.Nullable']
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.micronaut.validation.properties;
package io.micronaut.validation.properties

import io.micronaut.context.ApplicationContext
import io.micronaut.context.exceptions.BeanInstantiationException
Expand Down
Loading
Loading