Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code in IntelliJ IDEA marked with compiler error where it works fine in Eclipse

I'm trying to setup my IntelliJ workspace to do development on an eclipse project. One of the things I've run into is rather confusing:

Error:(24, 8) java: SomeClass.java:24: getHeader(java.lang.String) in org.springframework.mock.web.MockHttpServletResponse cannot implement getHeader(java.lang.String) in javax.servlet.http.HttpServletResponse; attempting to use incompatible return type
found   : java.lang.Object
required: java.lang.String

The problem is the following class definition:

public class SomeClass extends MockHttpServletResponse {

The problem seems to be because MockHttpServletResponse implements Collection<String> getHeaders(String) as public List getHeaders(String name). Here, I can see that the implementing method uses a raw List where the parent asks for a generic Collection typed with String. Aside from being potentially not type-safe, why would IntelliJ mark this as a complier error instead of a warning?

I have no option of changing any of these libraries. I'm simply trying to make work in IntellJ 14 what already works without complaints in Eclipse 4.3+.

EDIT:

I have since updated to IntelliJ 15.0, and the project is using Java 1.7 now instead of 1.6. I am still running into this issue with IntelliJ, but the issue is not presenting itself at all in Eclipse. I can compile the project using existing Ant scripts via IntelliJ, but I cannot debug through the IDE.

Here is my class definition

public class ExecutableServletResponse extends MockHttpServletResponse {
  ...

Here is the error showing in my 'Messages' pane:

Error:(24, 8) java: getHeader(java.lang.String) in org.springframework.mock.web.MockHttpServletResponse cannot implement getHeader(java.lang.String) in javax.servlet.http.HttpServletResponse
                return type java.lang.Object is not compatible with java.lang.String

The project SDK is using version 1.7 (1.7.0_79 to be exact). Language level is 7. Module SDK and Language Levels match the project.

I've tried using the eclipse compiler, but the app still doesn't fully compile, and will fail to run presumably because it fails to compile this class, and a whole part of the webapp doesn't compile as a result.

Here's a screenshot of my error, FWIW:

enter image description here

like image 629
Mirrana Avatar asked Mar 16 '23 11:03

Mirrana


1 Answers

You are seeing the error in your class, but the real issue is that the Spring mock library is not compatible with the Servlet Specification you are using. This may happen if you upgraded to the Servlet 3.0 Spec (or added a dependency that pulled it in transitively). Check your dependencies and ensure that either:

  • Only Servlet 2.5 is provided, or
  • You are using a version of Spring that is compatible with Servlet 3.0. Also ensure that all of your Spring dependencies are using the same version.

This combination should work:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-mock</artifactId>
    <version>2.0.8</version>
</dependency>

as should this:

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-web-api</artifactId>
    <version>6.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>3.1.0.RELEASE</version>
    <scope>test</scope>
</dependency>

But this will fail:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.2.2.RELEASE</version>
    <scope>test</scope>
</dependency>

The fact that it works in Eclipse but not in IntelliJ suggests that you have multiple dependencies that provide the same classes. There is no guarantee as to which jar the system will use to load the class. This may either be because you have both servlet-api and javaee-web-api on your classpath or because you have both spring-mock and spring-test on your classpath. After version 2.0.8, the classes in spring-mock were moved to spring-test and only version 3.1.0.RELEASE and higher of spring-test are compatible with Servlet 3.0.

like image 145
Carlos Macasaet Avatar answered Apr 06 '23 04:04

Carlos Macasaet