Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasstamann committed Oct 27, 2023
2 parents 6945e5a + b62b962 commit 0112245
Show file tree
Hide file tree
Showing 24 changed files with 94 additions and 30 deletions.
41 changes: 30 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ compile time and runtime model by shading common pitfalls behind it's api.
# Features

- provides a processor for generating wrapper classes for accessing annotation attributes
- provides wrappers for _Elements_ and _TypeMirror_ that provide a lot of useful utility functions
- provides support for Class conversion from runtime to compile time model (Class / FQN to Element and TypeMirror)
- provides support for accessing the compile time element tree
- provides generic Element based filters, validator and matchers
Expand All @@ -49,9 +50,16 @@ This project provides the abstract base class _io.toolisticon.annotationprocesso
which extends the AbstractProcessor class provided by java. Your annotation processor needs to extends this class to be
able to use the utilities offered by this project and to build your annotation processor.

Since your annotation processor later mostly will be bound as a provided dependency you should use the maven shade
plugin to embed the annotation-processor-toolkit and all other 3rd party dependency classes into your annotation
processor artifact. This can be done by adding the following to your annotation processors pom.xml:
## Manually init ToolingProvider if you don't use the AbstractAnnotationProcessor
Nevertheless, you can even use this library if your processor doesn't extend the _io.toolisticon.annotationprocessortoolkit.AbstractAnnotationProcessor_. You need to initialize the _ToolingProvider_ manually in your processor - best place to do this is either in your processors _init_ or _process_ method:

```java
ToolingProvider.setTooling(processingEnv);
```

## Delivering your processor
In general, you should consider to have as few external dependencies as possible used by your processor.
It's a good approach to use the maven shade plugin to repackage and embed the annotation-processor-toolkit and all other 3rd party dependencies into your annotation processor artifact. This can be done by adding the following to your annotation processors pom.xml:

```xml

Expand All @@ -60,14 +68,14 @@ processor artifact. This can be done by adding the following to your annotation
<dependency>
<groupId>io.toolisticon.aptk</groupId>
<artifactId>aptk-tools</artifactId>
<version>0.18.0</version>
<version>0.22.5</version>
</dependency>

<!-- recommended for testing your annotation processor -->
<dependency>
<groupId>io.toolisticon.cute</groupId>
<artifactId>cute</artifactId>
<version>0.11.1</version>
<version>0.12.1</version>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -116,10 +124,10 @@ Please check our example provided in the github.
## Annotation Wrapper

Reading attribute values can be very complicated if it comes to annotation type or Class based attributes. In this case
you are often forced to read the attribute values via the AnnotationMirror api. Additionally, you usually have to create
you are often forced to read the attribute values via the _AnnotationMirror_ api. Additionally, you usually have to create
some kind of class to store those annotation configurations of the annotation.

The APTK provides an annotation processor that generates wrapper classes that allow you to access like if you are
The APTK provides an annotation processor that generates wrapper classes that allow you to access the annotation attribiute like if you are
accessing the annotation directly. Only difference is that Class type based attributes will be accessible as FQN String,
TypeMirror or TypeMirrorWrapper. Annotation type based attributes will be also wrapped to ease access.

Expand Down Expand Up @@ -195,6 +203,14 @@ Optional<ExecutableElementWrapper> method = TypeElementWrapper.wrap(typeElement)

```

## TypeMirror wrapper
There is a wrapper for TypeMirrors as well. It's called _TypeMirrorWrapper_ and provides a lot of usefull tools like checking Assignability.
Usage is similar to the Element wrapper:

```java
TypeMirrorWrapper.wrap(typeMirror);
```


## Enhanced utility support

Expand All @@ -210,6 +226,7 @@ There are some more helpful utility classes:

- _AnnotationUtils_ : provides support for reading annotation attribute values
- _AnnotationValueUtils_ : provides support for handling _AnnotationValue_;
- _InterfaceUtils_ : provides support for handling generic interfaces and superclasses, for example to determine concrete types of type variables in super classes or parent interfaces (still experimental)

Example:

Expand All @@ -231,14 +248,14 @@ List<?extends Element> enclosedElements=ElementUtils.AccessEnclosedElements.getE

These are just a few examples of the provided tools. Please check the javadoc for more information.

## Characteristic matching, validation and filtering of Elements with core mathcers and fluent API
## Characteristic matching, validation and filtering of Elements with core matchers and fluent API

The framework provides a set of core matchers that can be used to check if an Element matches a specific characteristic.

Those core matchers can also be used for validation - validators allow you to check if an element matches none, one, at
least one or all of the passed characteristics.

Additionally, the Core matchers can be used to filter a List of Elements by specific characteristics.
Additionally, the core matchers can be used to filter a List of Elements by specific characteristics.

The framework provides a _FluentElementValidator_ and a _FluentElementFilter_ class that allow you to combine multiple
filters and validations by providing a simple and powerful fluent api.
Expand Down Expand Up @@ -332,16 +349,18 @@ The FilerUtils utility class also provides a way to write Strings to Source and
builder classes for bean classes
- [SPIAP](https://github.com/toolisticon/SPI-Annotation-Processor) : An annotation processor that helps you to generate
SPI configuration files and service locator classes
- [FluApiGen](https://github.com/toolisticon/FluApiGen) : An annotation processor that generates fluent api implementation based on annotated interfaces
- APTK itself provides some annotation processors based on the APTK for generating wrappers for annotations or compiler messages

# Useful links

## Compile time testing of annotation processors

- [Toolisticon CUTE](https://github.com/toolisticon/cute) : A simple compile testing framework that allows you to test
annotation processors. It was extracted from this project is a great help to unit test your annotation processor code.
annotation processors. It was extracted from this project is a great help to unit test your annotation processor code. It supports both unit and black box testing of the processors code.
- [google compile-testing](https://github.com/google/compile-testing) : Another compile testing framework which was used
in the past by this framework. It has some flaws like missing compatibility with different Java versions, is binding a
lot of common 3rd party libraries, and has almost no documentation
lot of common 3rd party libraries, and has almost no documentation.

# Contributing

Expand Down
2 changes: 1 addition & 1 deletion annotationwrapper/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.toolisticon.aptk</groupId>
<artifactId>aptk-annotationwrapper-parent</artifactId>
<version>0.22.5</version>
<version>0.22.6</version>
</parent>

<name>aptk-annotationwrapper-api</name>
Expand Down
2 changes: 1 addition & 1 deletion annotationwrapper/integrationtest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>io.toolisticon.aptk</groupId>
<artifactId>aptk-annotationwrapper-parent</artifactId>
<version>0.22.5</version>
<version>0.22.6</version>
</parent>

<name>aptk-annotationwrapper-integrationTest</name>
Expand Down
2 changes: 1 addition & 1 deletion annotationwrapper/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>io.toolisticon.aptk</groupId>
<artifactId>aptk-parent</artifactId>
<version>0.22.5</version>
<version>0.22.6</version>
</parent>


Expand Down
2 changes: 1 addition & 1 deletion annotationwrapper/processor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>io.toolisticon.aptk</groupId>
<artifactId>aptk-annotationwrapper-parent</artifactId>
<version>0.22.5</version>
<version>0.22.6</version>
</parent>

<name>aptk-annotationwrapper-processor</name>
Expand Down
2 changes: 1 addition & 1 deletion common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.toolisticon.aptk</groupId>
<artifactId>aptk-parent</artifactId>
<version>0.22.5</version>
<version>0.22.6</version>
</parent>

<name>aptk-common</name>
Expand Down
2 changes: 1 addition & 1 deletion compilermessages/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.toolisticon.aptk</groupId>
<artifactId>aptk-compilermessages-parent</artifactId>
<version>0.22.5</version>
<version>0.22.6</version>
</parent>

<name>aptk-compilermessages-api</name>
Expand Down
2 changes: 1 addition & 1 deletion compilermessages/integrationtest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>io.toolisticon.aptk</groupId>
<artifactId>aptk-compilermessages-parent</artifactId>
<version>0.22.5</version>
<version>0.22.6</version>
</parent>

<name>aptk-compilermessages-integrationTest</name>
Expand Down
2 changes: 1 addition & 1 deletion compilermessages/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>io.toolisticon.aptk</groupId>
<artifactId>aptk-parent</artifactId>
<version>0.22.5</version>
<version>0.22.6</version>
</parent>


Expand Down
2 changes: 1 addition & 1 deletion compilermessages/processor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>io.toolisticon.aptk</groupId>
<artifactId>aptk-compilermessages-parent</artifactId>
<version>0.22.5</version>
<version>0.22.6</version>
</parent>

<name>aptk-compilermessages-processor</name>
Expand Down
2 changes: 1 addition & 1 deletion cute/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.toolisticon.aptk</groupId>
<artifactId>aptk-parent</artifactId>
<version>0.22.5</version>
<version>0.22.6</version>
</parent>

<name>aptk-cute</name>
Expand Down
2 changes: 1 addition & 1 deletion example/example-annotationprocessor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.toolisticon.aptk</groupId>
<artifactId>aptk-example-parent</artifactId>
<version>0.22.5</version>
<version>0.22.6</version>
</parent>

<name>aptk-example-annotationprocessor</name>
Expand Down
2 changes: 1 addition & 1 deletion example/example-annotations/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.toolisticon.aptk</groupId>
<artifactId>aptk-example-parent</artifactId>
<version>0.22.5</version>
<version>0.22.6</version>
</parent>

<name>aptk-example-annotations</name>
Expand Down
2 changes: 1 addition & 1 deletion example/example-usecase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.toolisticon.aptk</groupId>
<artifactId>aptk-example-parent</artifactId>
<version>0.22.5</version>
<version>0.22.6</version>
</parent>

<name>aptk-example-usecase</name>
Expand Down
2 changes: 1 addition & 1 deletion example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.toolisticon.aptk</groupId>
<artifactId>aptk-parent</artifactId>
<version>0.22.5</version>
<version>0.22.6</version>
</parent>

<name>aptk-example-parent</name>
Expand Down
2 changes: 1 addition & 1 deletion extensions/java9/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.toolisticon.aptk</groupId>
<artifactId>extension-parent</artifactId>
<version>0.22.5</version>
<version>0.22.6</version>
</parent>

<name>aptk-tools-java9</name>
Expand Down
2 changes: 1 addition & 1 deletion extensions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.toolisticon.aptk</groupId>
<artifactId>aptk-parent</artifactId>
<version>0.22.5</version>
<version>0.22.6</version>
</parent>

<name>extension-parent</name>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>io.toolisticon.aptk</groupId>
<artifactId>aptk-parent</artifactId>
<version>0.22.5</version>
<version>0.22.6</version>
<packaging>pom</packaging>

<name>aptk-parent</name>
Expand Down
2 changes: 1 addition & 1 deletion templating/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.toolisticon.aptk</groupId>
<artifactId>aptk-parent</artifactId>
<version>0.22.5</version>
<version>0.22.6</version>
</parent>

<name>aptk-templating</name>
Expand Down
2 changes: 1 addition & 1 deletion tools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.toolisticon.aptk</groupId>
<artifactId>aptk-parent</artifactId>
<version>0.22.5</version>
<version>0.22.6</version>
</parent>

<name>aptk-tools</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,16 @@ public static Optional<TypeElementWrapper> getTypeElement(TypeMirror typeMirror)
return Optional.empty();
}

/**
* Gets the TypeElements wrapped TypeMirror.
*
* @param typeElement the TypeElement to get the TypeMirror for
* @return The wrapped TypeMirror of the TypeElement
*/
public static TypeMirrorWrapper wrap(TypeElement typeElement) {
return TypeElementWrapper.wrap(typeElement).asType();
}

/**
* Wraps a TypeMirror instance to provide some convenience methods
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.type.DeclaredType;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.Optional;
Expand Down Expand Up @@ -38,6 +39,24 @@ public AnnotationMirror unwrap() {
return annotationMirror;
}

/**
* Get DeclaredType of wrapped annotation.
* @return the annotations declared type
*/
DeclaredType getAnnotationType() {
return this.annotationMirror.getAnnotationType();
}

/**
* Get wrapped DeclaredType of wrapped annotation.
* @return the annotations declared type
*/
TypeMirrorWrapper getWrappedAnnotationType() {
return TypeMirrorWrapper.wrap(getAnnotationType());
}



/**
* Returns the "value" attribute
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ public FluentElementFilter<Element> filterFlattenedEnclosedElementTree(boolean i
return FluentElementFilter.createFluentElementFilter(getFlattenedEnclosedElementTree(includeSelf, maxDepth).stream().map(ElementWrapper::unwrap).collect(Collectors.toList()));
}

/**
* Gets all wrapped annotations of element.
* @return a list containing all annotations of the element
*/
public List<AnnotationMirrorWrapper> getAnnotations() {
return this.element.getAnnotationMirrors().stream().map(AnnotationMirrorWrapper::wrap).collect(Collectors.toList());
}

/**
* Gets the annotation
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,15 @@ public void test_validate() {

}

@Test
public void test_getAnnotations() {

CompileTestBuilder.unitTest().<TypeElement>defineTestWithPassedInElement(TestClass.class, (processingEnvironment, element) -> {
ElementWrapper<TypeElement> unit = ElementWrapper.wrap(element);
MatcherAssert.assertThat("Should find annotation", unit.getAnnotations(),Matchers.hasSize(1));
}).executeTest();

}
@Test
public void test_getAnnotation_byFqnString() {

Expand Down

0 comments on commit 0112245

Please sign in to comment.