How to solve provide parameterized type for this generic in SonarLint
Asked Answered
H

1

6

I am trying to refactor my code using the IntelliJ idea extension called sonarLint. In there, sonarLint suggests further modification like this picture below(I have marked the issue in red)

SonarLint error alert

here is my code

 public class AbcController extends BaseController{   

    private final PermissionService permissionService;

    public PermissionController(PermissionService permissionService) {
        this.permissionService = permissionService;
    }

    @GetMapping("/groups")
    public ResponseEntity<List<GroupResponse>> getGroups( String accessToken) {
        List<GroupResponse> groupResponses = permissionService.getGroups(accessToken);       
        return getResponseEntity(groupResponses);
    }
}



public abstract class BaseController <T>{


    protected ResponseEntity<T> getResponseEntity(T t, HttpHeaders httpHeaders) {
        return new ResponseEntity<> (t, httpHeaders, HttpStatus.OK);

    }

    protected ResponseEntity<T> getResponseEntity(T t) {
        return new ResponseEntity<> (t, HttpStatus.OK);

    }
}

I tried to make modifications according to the sonarLint alert but it is not working. Please help me with this problem. Thank you all.

Hamiltonian answered 10/12, 2021 at 15:38 Comment(3)
Where is the ResponseEntityTransformer in the code? I cannot find itMaleeny
Specify the generic type, e.g.: public class AbcController extends BaseController<ActualTypeGoesHere>Milo
thank you @AndrewS do you mean like this public class AbcController extends BaseController<ResponseEntity>. but it's not supported for getGroups API return statementHamiltonian
K
0

This is not that complicated. When you give List<GroupResponse> as a type parameter to BaseController, all of the warnings are going away.

import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;

public class AbcController extends BaseController<List<GroupResponse>>{   

    private final PermissionService permissionService;

    public AbcController(PermissionService permissionService) {
        this.permissionService = permissionService;
    }

    @GetMapping("/groups")
    public ResponseEntity<List<GroupResponse>> getGroups( String accessToken) {
        List<GroupResponse> groupResponses = permissionService.getGroups(accessToken);       
        return getResponseEntity(groupResponses);
    }
}

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

public abstract class BaseController <T>{


    protected ResponseEntity<T> getResponseEntity(T t, HttpHeaders httpHeaders) {
        return new ResponseEntity<> (t, httpHeaders, HttpStatus.OK);

    }

    protected ResponseEntity<T> getResponseEntity(T t) {
        return new ResponseEntity<> (t, HttpStatus.OK);

    }
}

Kaolin answered 14/7, 2024 at 20:38 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.