I am trying to work with MapStruct, according to the documentation here. It requires defining AnnotationProcessor
as part of the compiler plugin. when using the annotation processor the code does not compile, failing on some unrelated compilation errors. (cannot find symbol;cannot infer type arguments for...).
I was able to narrow down the problem to simple test case (github):
consider this class:
@Getter
@Setter
public class Result<D, E> {
private D data;
private E error;
}
...and this generic method that uses the class Result
:
public static <D, E> Result<D, E> createResult(D data, Class<E> type) {
Result<D, E> res = new Result<>();
res.setData(data);
return res;
}
I am using it like that:
Result<String, String> test = createResult("Test", String.class);
code works and compiles successfully, using this maven compiler plugin definition:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
... but fails when adding the annotationProcessorPaths
of MapStruct
, with error cannot find symbol
on the line res.setData(data);
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>11</source>
<target>11</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
I tried a similar generic method with generic List
, but it works ok from some reason in this example:
public static <T> List<T> createListWithItem(T item) {
List<T> res = new ArrayList<>();
res.add(item);
return res;
}
....
List<String> list = createListWithItem("BANANA");
(You can clone the repo and simply add\remove the annotationProcessorPaths
elements to toggle the problem)
I am not sure if it even related to MapStruct
, or simply something with the mention and usage of any annotationProcessor.
my questions:
- what's the problem that only adding the annotation processor makes the code not compile ?
- is it really realted to MapStruct or something in the compiler plugin ? any idea ?
- what's the difference between the
Result
example and theList
example ?