Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
3ccb0c2
#1574: AnnotateWith implementation (Work in progress)
Mar 7, 2022
39836d7
#1574: Support added for all singular forms of parameters.
Mar 8, 2022
70f89e9
#1574: Support added for array form of parameters.
Mar 9, 2022
0fe8edb
#1574: added an error for multiple values while only 1 value expected…
Mar 9, 2022
2931d7f
#1574: added checks on annotations being placed on correct target types.
Mar 15, 2022
80933ae
#1574: forgot to add additional test classes.
Mar 15, 2022
14a6abe
#1574 added comment to something that needs to be changed before this…
Mar 15, 2022
072a2b5
#1574: added support for meta-annotations for
Mar 15, 2022
01083a9
#1574: moved meta annotation behavior to util class to make it re-usa…
Mar 15, 2022
141ff6e
#1574: Changed behavior to match '@Mapping' and '@SubclassMapping' fo…
Mar 15, 2022
1777f98
#1574: refactoring to make it all fit together better.
Mar 15, 2022
609f3b1
#1574: fix build.
Mar 15, 2022
56d4665
#1574: replaced manual formatting with .ftl files.
Mar 18, 2022
b9299b3
#1574: add missing license header.
Mar 18, 2022
90bb493
#1574: Rename variables to better match java spec.
Jun 1, 2022
5878b6a
#1574: more renames and support for enums.
Jun 3, 2022
86268b8
#1574: missing javadoc and fixed long lines.
Jun 4, 2022
4f64413
#1574: documentation, javadoc and adjust GemGenerator to not import t…
Jun 4, 2022
ea688a3
Changes in the additional annotations builder
filiphr Jun 4, 2022
648dc91
Use ClassAssert from AssertJ for checking for annotations on a class
filiphr Jun 4, 2022
0faac4d
Reformat AnnotateWithTest so Checkstyle doesn't complain
filiphr Jun 4, 2022
a7a2fd2
Remove extra annotation
filiphr Jun 4, 2022
ae330cb
#1574: Merged all AnnotationElement ftl's and corresponding classes. …
Jun 4, 2022
48c7ac6
#1574: removed unnecessary test.
Jun 4, 2022
eb422e2
#1574: moved annotations above '@Override'.
Jun 4, 2022
5ff023c
#1574: rewritten enum structure. Moved missing enum value, unknown el…
Jun 4, 2022
f417fbb
#1574: moved not allowed error locations.
Jun 4, 2022
669fff6
#1475: annotateWith now also possible without specifying the element …
Jun 5, 2022
a1fcfd2
#1427: AnnotateWith now also usable for Spring "Component" annotation.
Jun 5, 2022
1b45a1d
#1574: Checkstyle and license header fixes.
Jun 5, 2022
9c4ce7d
#1574: Added compiler error for wrong type without specifying annotat…
Jun 5, 2022
8bc4aa8
#1574: Added compiler error for missing name parameter in AnnotateWit…
Jun 5, 2022
e62a948
#1574: added compiler error for situation with multiple elements but …
Jun 5, 2022
241eceb
Revert "#1427: AnnotateWith now also usable for Spring "Component" an…
Jun 5, 2022
905fb7f
#1574: add compiler errors for wrong usage of enum structure.
Jun 5, 2022
93857cd
#1574: always use default name 'value' in case it is missing.
Jun 5, 2022
3abaa29
#1574: update messages and fix test name
Jun 5, 2022
fc81cb8
#1574: Add compiler error for repeating not repeatable annotations.
Jun 5, 2022
2cb8605
#1574: added compiler error for defining same parameter twice using A…
Jun 5, 2022
8bfba39
#1574: Added compiler error for completely identical annotations.
Jun 5, 2022
9f727f3
Required parameter should be reported on the annotate with line
filiphr Jun 6, 2022
60c8755
Change error message for duplicate and unknown parameters
filiphr Jun 6, 2022
c7eec33
Duplicated identical annotation should be a warning + remove not used…
filiphr Jun 6, 2022
7e76cd6
#1574: added examples to javadoc. fixed line length.
Jun 6, 2022
8dbb1f7
#1574: remove paragraph tags
Jun 6, 2022
f9ba5c1
#1574: converted the more complex tests into fixture tests.
Jun 6, 2022
5c60c3d
#1574: introducing a NullEnum util class as default class for annotat…
Jun 6, 2022
b8045a0
#1574: fix javadoc
Jun 6, 2022
9117141
#1574: fix line ending
Jun 6, 2022
5e834d6
#1574: add missing license header.
Jun 6, 2022
f823f79
Move NullEnum to be package protected in the org.mapstruct package
filiphr Jun 18, 2022
2ebdc60
Documentation and javadoc typo changes
filiphr Jun 18, 2022
070b650
Simplify ConvertToProperty a bit
filiphr Jun 18, 2022
4eb98a1
Simplify some creation of the annotation element in the annotation ba…
filiphr Jun 18, 2022
701709f
#1574: Too many element parameters then specific error message.
Jul 31, 2022
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
176 changes: 176 additions & 0 deletions core/src/main/java/org/mapstruct/AnnotateWith.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct;

import java.lang.annotation.Annotation;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.CLASS;

/**
* This can be used to have mapstruct generate additional annotations on classes/methods.
* <p>
* Examples based on the spring framework annotations.
* </p>
* Marking a class as `Lazy`:
*
* <pre><code>
* &#64;AnnotateWith( value = Lazy.class )
* &#64;Mapper
* public interface FooMapper {
* // mapper code
* }
* </code></pre>
*
* The following code would be generated:
*
* <pre><code>
* &#64;Lazy
* public class FooMapperImpl implements FooMapper {
* // mapper code
* }
* </code></pre>
* Setting the profile on the generated implementation:
*
* <pre><code>
* &#64;AnnotateWith( value = Profile.class, elements = @AnnotateWith.Element( strings = "prod" ) )
* &#64;Mapper
* public interface FooMapper {
* // mapper code
* }
* </code></pre>
*
* The following code would be generated:
*
* <pre><code>
* &#64;Profile( value = "prod" )
* public class FooMapperImpl implements FooMapper {
* // mapper code
* }
* </code></pre>
*
* @author Ben Zegveld
* @since 1.6
*/
@Repeatable( AnnotateWiths.class )
@Retention( CLASS )
@Target( { TYPE, METHOD, ANNOTATION_TYPE } )
public @interface AnnotateWith {
/**
* @return the annotation class that needs to be added.
*/
Class<? extends Annotation> value();

/**
* @return the annotation elements that are to be applied to this annotation.
*/
Element[] elements() default {};

/**
* Used in combination with {@link AnnotateWith} to configure the annotation elements. Only 1 value type may be used
* within the same annotation at a time. For example mixing shorts and ints is not allowed.
*
* @author Ben Zegveld
* @since 1.6
*/
@interface Element {
/**
* @return name of the annotation element.
*/
String name() default "value";

/**
* cannot be used in conjunction with other value fields.
*
* @return short value(s) for the annotation element.
*/
short[] shorts() default {};

/**
* cannot be used in conjunction with other value fields.
*
* @return byte value(s) for the annotation element.
*/
byte[] bytes() default {};

/**
* cannot be used in conjunction with other value fields.
*
* @return int value(s) for the annotation element.
*/
int[] ints() default {};

/**
* cannot be used in conjunction with other value fields.
*
* @return long value(s) for the annotation element.
*/
long[] longs() default {};

/**
* cannot be used in conjunction with other value fields.
*
* @return float value(s) for the annotation element.
*/
float[] floats() default {};

/**
* cannot be used in conjunction with other value fields.
*
* @return double value(s) for the annotation element.
*/
double[] doubles() default {};

/**
* cannot be used in conjunction with other value fields.
*
* @return char value(s) for the annotation element.
*/
char[] chars() default {};

/**
* cannot be used in conjunction with other value fields.
*
* @return boolean value(s) for the annotation element.
*/
boolean[] booleans() default {};

/**
* cannot be used in conjunction with other value fields.
*
* @return string value(s) for the annotation element.
*/
String[] strings() default {};

/**
* cannot be used in conjunction with other value fields.
*
* @return class value(s) for the annotation element.
*/
Class<?>[] classes() default {};

/**
* only used in conjunction with the {@link #enums()} annotation element.
*
* @return the class of the enum.
*/
Class<? extends Enum<?>> enumClass() default NullEnum.class;

/**
* cannot be used in conjunction with other value fields. {@link #enumClass()} is also required when using
* {@link #enums()}
*
* @return enum value(s) for the annotation element.
*/
String[] enums() default {};

}
}
31 changes: 31 additions & 0 deletions core/src/main/java/org/mapstruct/AnnotateWiths.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct;

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

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.CLASS;

/**
* This can be used to have mapstruct generate additional annotations on classes/methods.
*
* @author Ben Zegveld
* @since 1.6
*/
@Retention( CLASS )
@Target( { TYPE, METHOD } )
public @interface AnnotateWiths {

/**
* The configuration of the additional annotations.
*
* @return The configuration of the additional annotations.
*/
AnnotateWith[] value();
}
15 changes: 15 additions & 0 deletions core/src/main/java/org/mapstruct/NullEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct;

/**
* To be used as a default value for enum class annotation elements.
*
* @author Ben Zegveld
* @since 1.6
*/
enum NullEnum {
}
Original file line number Diff line number Diff line change
Expand Up @@ -720,3 +720,43 @@ i.e. You can map from `Map<String, Integer>` where for each property a conversio
When a raw map or a map that does not have a String as a key is used, then a warning will be generated.
The warning is not generated if the map itself is mapped into some other target property directly as is.
====

[[adding-annotations]]
=== Adding annotations

Other frameworks sometimes requires you to add annotations to certain classes so that they can easily detect the mappers.
Using the `@AnnotateWith` annotation you can generate an annotation at the specified location.

For example Apache Camel has a `@Converter` annotation which you can apply to generated mappers using the `@AnnotateWith` annotation.

.AnnotateWith source example
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
@Mapper
@AnnotateWith(
value = Converter.class,
elements = @AnnotateWith.Element( name = "generateBulkLoader", booleans = true )
)
public interface MyConverter {
@AnnotateWith( Converter.class )
DomainObject map( DtoObject dto );
}
----
====

.AnnotateWith generated mapper
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
@Converter( generateBulkLoader = true )
public class MyConverterImpl implements MyConverter {
@Converter
public DomainObject map( DtoObject dto ) {
// default mapping behaviour
}
}
----
====
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import javax.xml.bind.annotation.XmlElementRef;

import org.mapstruct.AfterMapping;
import org.mapstruct.AnnotateWith;
import org.mapstruct.AnnotateWiths;
import org.mapstruct.BeanMapping;
import org.mapstruct.BeforeMapping;
import org.mapstruct.Builder;
Expand Down Expand Up @@ -42,6 +44,9 @@
*
* @author Gunnar Morling
*/
@GemDefinition(AnnotateWith.class)
@GemDefinition(AnnotateWith.Element.class)
@GemDefinition(AnnotateWiths.class)
@GemDefinition(Mapper.class)
@GemDefinition(Mapping.class)
@GemDefinition(Mappings.class)
Expand Down
Loading