Skip to content

Commit

Permalink
Add constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasstamann committed Mar 13, 2024
1 parent 14164ee commit 95f15b5
Show file tree
Hide file tree
Showing 32 changed files with 2,439 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.toolisticon.aptk.annotationwrapper.processor;
package io.toolisticon.aptk.constraints.processor;

import io.toolisticon.aptk.annotationwrapper.api.AnnotationWrapper;
import io.toolisticon.aptk.annotationwrapper.api.CustomCodeMethod;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.toolisticon.aptk.annotationwrapper.processor;
package io.toolisticon.aptk.constraints.processor;

import io.toolisticon.aptk.annotationwrapper.api.CustomCodeMethod;
import io.toolisticon.aptk.tools.corematcher.ValidationMessage;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.toolisticon.aptk.annotationwrapper.processor;
package io.toolisticon.aptk.constraints.processor;

import io.toolisticon.aptk.cute.APTKUnitTestProcessor;
import io.toolisticon.aptk.cute.APTKUnitTestProcessorForTestingAnnotationProcessors;
Expand Down Expand Up @@ -149,27 +149,6 @@ public void aptkUnitTest(AnnotationWrapperProcessor unit, ProcessingEnvironment
.executeTest();
}

@Test
public void test_AnnotationToWrap_getSimpleName() {
CompileTestBuilder.unitTest().<TypeElement>defineTest(new APTKUnitTestProcessor<TypeElement>() {
@Override
public void aptkUnitTest(ProcessingEnvironment processingEnvironment, TypeElement typeElement) {

AnnotationWrapperProcessor.AnnotationToWrap unit = new AnnotationWrapperProcessor.AnnotationToWrap(
UnitTestAnnotation.class.getCanonicalName(),
new ArrayList<>(),
new AnnotationWrapperProcessor.AnnotationWrapperCustomCode(UnitTestAnnotation.class.getCanonicalName()),
new ArrayList<>()
);

MatcherAssert.assertThat(unit.getSimpleName(), Matchers.is(UnitTestAnnotation.class.getSimpleName()));

}
})
.compilationShouldSucceed()
.executeTest();
}

@Test
public void test_AnnotationToWrap_getQualifiedName() {
CompileTestBuilder.unitTest().<TypeElement>defineTest(new APTKUnitTestProcessor<TypeElement>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.toolisticon.aptk.annotationwrapper.processor;
package io.toolisticon.aptk.constraints.processor;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
Expand Down
85 changes: 85 additions & 0 deletions constraints/constraint-api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>aptk-constraint-api</artifactId>
<packaging>jar</packaging>

<parent>
<groupId>io.toolisticon.aptk</groupId>
<artifactId>extension-parent</artifactId>
<version>0.22.12-SNAPSHOT</version>
</parent>

<name>aptk-constraint-api</name>


<dependencies>

<!-- test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>${hamcrest.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.toolisticon.spiap</groupId>
<artifactId>spiap-api</artifactId>
</dependency>

</dependencies>


<build>

<plugins>


<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<id>enforce</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireMavenVersion>
<version>[3.0.4,)</version>
</requireMavenVersion>
<requireJavaVersion>
<version>${java.version}</version>
</requireJavaVersion>
<bannedDependencies>
<searchTransitive>false</searchTransitive>
<excludes>
<exclude>*</exclude>
</excludes>
<includes>
<include>*:*:*:*:test:*</include>
<include>*:*:*:*:provided:*</include>
</includes>
</bannedDependencies>
</rules>
</configuration>
</execution>
</executions>
</plugin>


</plugins>

</build>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.toolisticon.aptk.constraints;

import io.toolisticon.spiap.api.Spi;
import io.toolisticon.spiap.api.SpiServiceLocator;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import java.lang.annotation.Annotation;

/**
* A spi interface to process annotation constraints.
*/
@Spi
public interface AnnotationConstraintSpi {

/**
* Get the annotation supported by this spi implementation.
*
* @return the supported annotation
*/
Class<? extends Annotation> getSupportedAnnotation();



/**
* Checks constraint.
*
* @param annotatedElement The annotated element
* @param annotationMirrorToCheck the annotation which has to be validated
* @param constraintAnnotationMirror the constraint annotation used for checking
* @param attributeNameToBeCheckedByConstraint the name of the annotation attribute with constraint or null if it is placed on annotation type
*/
boolean checkConstraints(Element annotatedElement, AnnotationMirror annotationMirrorToCheck, AnnotationMirror constraintAnnotationMirror, String attributeNameToBeCheckedByConstraint);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.toolisticon.aptk.constraints;

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;

/**
*
* This annotation documents that an annotation declares a constraint.
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.ANNOTATION_TYPE})
@Documented
public @interface Constraint {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.toolisticon.aptk.constraints;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import java.lang.annotation.Annotation;

/**
* This spi is used to provide manual check for constraints. This can for example be checks for correct usage of the constraint annotations itself.
* Additionally, it can be checks that aren*t available via constraint annotations
* But
*/
public interface ManualConstraintSpi {



/**
* Get the annotation supported by this spi implementation.
*
* @return the supported annotation
*/
Class<? extends Annotation> getSupportedAnnotation();


/**
* Do some manual checks for constraints. This can be checks if annotation constraint is used correctly
* @param annotatedElement the annotated element
* @param constraintAnnotationMirror the AnnotationMirror representing the constraint annotation
*/
void checkManualConstraints(Element annotatedElement, AnnotationMirror constraintAnnotationMirror);




}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.toolisticon.aptk.constraints;


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;

/**
* The Target annotation is kind of fuzzy in some cases about where annotations might be used.
* <p>
* E.g. this is the case for TYPE, which allows you to use annotations on Classes, Interfaces, Enums and Annotations.
* Another example is METHOD, which allows you to use annotations on methods or annotation attribute declarations.
* <p>
*/
@Documented
@Constraint
@Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface On {

enum Location {
PACKAGE,
ANNOTATION_ATTRIBUTE,
ANNOTATION,
CLASS,
INTERFACE,
ENUM,
METHOD,
CONSTRUCTOR,
PARAMETER,
METHOD_PARAMETER,
CONSTRUCTOR_PARAMETER,
FIELD

}

Location[] value();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.toolisticon.aptk.constraints;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Constraint
@Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface OnAnnotationAttributeOfType {

enum AttributeType {
FLOAT,
DOUBLE,
INTEGER,
LONG,
STRING,
CLASS,
ENUM,
ANNOTATION,
FLOAT_ARRAY,
DOUBLE_ARRAY,
INTEGER_ARRAY,
LONG_ARRAY,
STRING_ARRAY,
CLASS_ARRAY,
ENUM_ARRAY,
ANNOTATION_ARRAY,
/**
* Any kind of single value.
*/
SINGLE_VALUE,
/**
* Any kind of array value.
*/
ARRAY
}

AttributeType[] value();

}
Loading

0 comments on commit 95f15b5

Please sign in to comment.