2

While I'm trying to implement HttpSession, I should override getSessionContext method. But it casues deprecated warnings because the method and its return type HttpSessionContext is deprecated.

@deprecated javadoc tag fixes a warning on the definition of getSessionContext but cannot fix a warning on the import of HttpSessionContext. And putting @SuppressWarnings before the import causes a compile error.

How can I fix both of the warnings?

Code:

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;

public class MyServletSession implements HttpSession {
    // ...

    /**
     * @deprecated
     */
    @Override
    public HttpSessionContext getSessionContext() {
        throw new UnsupportedOperationException();
    }

    // ...
}

Warning:

$ javac -Xlint:deprecation -cp /path/to/javax.servlet-api-3.0.1.jar MyServletSession.java
MyServletSession.java:5: warning: [deprecation] HttpSessionContext in javax.servlet.http has been deprecated
import javax.servlet.http.HttpSessionContext;
                                     ^
1 warning
ajduke
  • 4,991
  • 7
  • 36
  • 56
npcode
  • 1,370
  • 1
  • 14
  • 29
  • I believe you should try to avoid using `HttpSessionContext`. According to the javadoc: "Deprecated. As of Java(tm) Servlet API 2.1 for security reasons, with no replacement. This interface will be removed in a future version of this API." – Ahmad Y. Saleh Apr 01 '13 at 06:47
  • Usually deprecation is there for a good reason: to tell you that if you can afford it, you should follow whatever new implementations are supposed to replace the deprecated classes of methods. Otherwise how about tagging the entire class with the @Override annotation (which is not recommended of course) ? – b2Wc0EKKOvLPn Apr 01 '13 at 06:48
  • Why are you implementing HttpSession at all? Are you implementing a container? – user207421 Apr 01 '13 at 06:54
  • Why do you need to implement your own HttpSession? – NilsH Apr 01 '13 at 06:55
  • I am implementing Git repository hosting using jgit on PlayFramework 2.1, and jgit needs Servlet which PlayFramework 2.1 does not have. Because I think it is easier to implement Servlet than to import Servlet into PlayFramework 2.1, I should implement Servlet includes HttpSession. I have almost succeeded but only the warning left. – npcode Apr 01 '13 at 07:20

1 Answers1

1

If you really need to implement your own HttpSession, and suppress the deprecation warnings, I believe the correct compiler argument is -Xlint:-deprecation. Notice the - before deprecation

Edit: This remove all deprecation warnings, so it might not be suitable if you're trying to suppress only the warnings in that one class. Instead, I found this to work for me:

// Note, no import of javax.servlet.http.HttpSessionContext
import javax.servlet.http.HttpSession;

@SuppressWarnings("deprecation")
public class MySession implements HttpSession {

    /**
     * This javadoc comment, along with the fully qualified name of HttpSessionContex in the method signature seems to do the trick.
     * @deprecated
     */
    public javax.servlet.http.HttpSessionContext getSessionContext() {
    }

    //... All your other methods
}
NilsH
  • 13,705
  • 4
  • 41
  • 59