GeezerVison is showing github.com — Compiler error when mapping an object named "Override" · Issue #3905 · mapstruct/mapstruct
Compiler error when mapping an object named "Override" #3905DescriptionThe following mapper:
import org.mapstruct.Mapper;
class Override {}
class OverrideDto {}
class Car {
public Override override;
}
class CarDto {
public OverrideDto override;
}
@Mapper
interface Issue3904Mapper {
CarDto map(Car car);
OverrideDto map(Override override);
}
generates:
import javax.annotation.processing.Generated;
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2025-07-26T10:41:47+0200",
comments = "version: 1.6.0, compiler: javac, environment: Java 21.0.1 (Oracle Corporation)"
)
class Issue3904MapperImpl implements Issue3904Mapper {
@Override
public CarDto map(Car car) {
if ( car == null ) {
return null;
}
CarDto carDto = new CarDto();
carDto.override = map( car.override );
return carDto;
}
@Override
public OverrideDto map(Override override) { // Incompatible types
if ( override == null ) {
return null;
}
OverrideDto overrideDto = new OverrideDto();
return overrideDto;
}
}
Override needs to get a fully qualified name, which it currently does not.
Interesting note: I also tried this with Generated (which could also clash) but this got a qully qualified name.
Another option would be to write @java.lang.Override.
Discussed in #3904
Originally posted by trico-tillster July 23, 2025
I've 1 object to be mapped to another, if one of them contains a object called Override compiler fails.
This happens because generated code adds the @Override annotation to the implementation method, and compiler gets confused with the import class.
I've tried to search for a config to manipulate the generated annotation, something like updating it to @java.lang.Override, but I was not able.
I'm also not able to change the model Override class name to other, because this is a generated code from an external json schema.
Thought it would be helpful to mention this here.
Any clues how to solve this? Reacted by Luís Tiago Barreto Rico Activitythunderhook commented on Jul 26, 2025 ContributorAuthor More actions I just had a quick look. Even when we change it to a fully qualified name, there is an error:
@Override // <-- Annotation type expected
public OverrideDto map(example.package.Override override) {
if ( override == null ) {
return null;
}
OverrideDto overrideDto = new OverrideDto();
return overrideDto;
}
Even if it is not imported via import it shows Annotation type expected.
So to fix this, we would need to declare @java.lang.Override instead of @Override. Adding this by default might cause some issues with some code quality tools, but I'm not 100% sure. filiphr commented on Jul 27, 2025 I am not sure that I am following @thunderhook. Can you provide the entire class?
If the example.package.Override is not in the import statements, then the code you shared should compile. I do not see a reason why it wouldn't compile? thunderhook commented on Jul 28, 2025 ContributorAuthor More actions I'm not sure myself why Java behaves like that. I stumbled accross this while trying to fix it in a unit test. Just copy those classes to one of your projects (with the given packages) and have a look:
package org.example._3904.inner;
public class Override {
public String name;
}
package org.example._3904.inner;
public class OverrideDto {
public String name;
}
package org.example._3904;
import org.example._3904.inner.Override;
import org.example._3904.inner.OverrideDto;
import org.mapstruct.Mapper;
@Mapper
interface Issue3904Mapper {
OverrideDto map(Override override);
}
package org.example._3904;
import javax.annotation.processing.Generated;
import org.example._3904.inner.Override;
import org.example._3904.inner.OverrideDto;
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2025-07-28T07:37:57+0200",
comments = "version: 1.6.3, compiler: javac, environment: Java 21.0.1 (Oracle Corporation)"
)
class Issue3904MapperImpl implements Issue3904Mapper {
@Override // <- Annotation type expected
public OverrideDto map(Override override) {
if ( override == null ) {
return null;
}
OverrideDto overrideDto = new OverrideDto();
overrideDto.name = override.name;
return overrideDto;
}
}
In the generated class you can play around with the imports and fully qualified names. The only way I found was to give the Override a FQN. Maybe you have a better idea. filiphr commented on Jul 31, 2025 The reason why you need an FQN in your example is because import org.example._3904.inner.Override is imported. If instead if importing that, we ise an FQQ in the map method then it works properly.
e.g.
public OverrideDto map(org.example._3904.inner.Override override) {Reacted by Luís Tiago Barreto Rico added 2 commits that reference this issue on Aug 29, 2025#3905: Using custom class Override should compile
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|