0

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:

  1. what's the problem that only adding the annotation processor makes the code not compile ?
  2. is it really realted to MapStruct or something in the compiler plugin ? any idea ?
  3. what's the difference between the Result example and the List example ?
aviad cohen
  • 637
  • 1
  • 6
  • 16
  • The problem is the use of Lombok. See https://stackoverflow.com/questions/47676369/mapstruct-and-lombok-not-working-together – Filip Feb 08 '23 at 03:05
  • Indeed this is a duplicate. For some reason, I missed the original question with the correct answer. The problem was in the order of applying the annotation processors, as described in the attached question – aviad cohen Feb 08 '23 at 05:57

0 Answers0