In order to prevent SonarQube false positives for your DTOs you could use AutoValue as a library to create DTOs:
@AutoValue
@AutoGson(autoValueClass = AutoValue_DTO.class)
public abstract class DTO
{
public abstract String getA();
}
with AutoGson defined as:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface AutoGson
{
Class autoValueClass();
}
and a Gson type adapter defined as:
public class AutoValueTypeAdapterFactory implements TypeAdapterFactory
{
@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type)
{
Class<? super T> rawType = type.getRawType();
AutoGson annotation = rawType.getAnnotation(AutoGson.class);
if (annotation == null)
{
return null;
}
return (TypeAdapter<T>) gson.getAdapter(annotation.autoValueClass());
}
}
which is used by Gson as:
Gson gson = new GsonBuilder().registerTypeAdapterFactory(new AutoValueTypeAdapterFactory())