6 Replies Latest reply: Jan 28, 2009 6:10 AM by 843810 RSS

    @SuppressWarnings(value="unchecked") & javac <identifier> expected messages

    843810
      Hi folks
      I thought I'd add a suppress warning annotation to my code to stop getting this compiler message:
      javac -Xlint LegacyAdapterRequestWrapper.java
      LegacyAdapterRequestWrapper.java:16: warning: [unchecked] unchecked call to put(
      K,V) as a member of the raw type java.util.Map
          tmpMap.put("DEPT", myDept);
                    ^
      1 warning
      Here is my modified code:
      package com.apress.projsp;
      
      import java.util.Map;
      import javax.servlet.http.*;
      
      public class LegacyAdapterRequestWrapper extends HttpServletRequestWrapper {
        String myDept = null;
      
        public LegacyAdapterRequestWrapper(HttpServletRequest inReq, String deptString) {
          super(inReq);
          myDept = deptString;
        }
      
        public Map getParameterMap() {
          Map tmpMap = super.getParameterMap();
          @SuppressWarnings(value="unchecked")
          tmpMap.put("DEPT", myDept);
          return tmpMap;
        }
      
        public String[] getParameterValues(String paramName) {
          if (paramName.equalsIgnoreCase("DEPT")) {
            String[] tpAry = new String[1];
            tpAry[0] = myDept;
            return tpAry;
          } else {
            return super.getParameterValues(paramName);
          }
        }
      
        public String getParameter(String paramName) {
          if (paramName.equalsIgnoreCase("DEPT")) {
            return myDept;
          } else {
            return super.getParameter(paramName);
          }
        }
      }
      Can't quite understand what the compiler is telling me with these message. So was wondering if someone can clarify for me??

      Compiler spews out following messages:
      LegacyAdapterRequestWrapper.java:17: <identifier> expected
          tmpMap.put("DEPT", myDept);
                    ^
      LegacyAdapterRequestWrapper.java:17: not a statement
          tmpMap.put("DEPT", myDept);
                     ^
      LegacyAdapterRequestWrapper.java:17: ';' expected
          tmpMap.put("DEPT", myDept);
                           ^
      LegacyAdapterRequestWrapper.java:17: not a statement
          tmpMap.put("DEPT", myDept);
                             ^
      LegacyAdapterRequestWrapper.java:17: ';' expected
          tmpMap.put("DEPT", myDept);
                                   ^
      5 errors
      BTW: java -version gives me this:
      java version "1.6.0_05"
      Java(TM) SE Runtime Environment (build 1.6.0_05-b13)
      Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode, sharing)
        • 1. Re: @SuppressWarnings(value="unchecked") & javac <identifier> expected messages
          843810
          I believe I found the answer here.
          Quote: "Annotations can not be attached to statements, expressions, or blocks, only to program entities with a definition like types, variables, etc."
          I generally don't like putting suppress warnings on method as it's not granular enough for my liking..
          But I'm guessing the part about "definition" applies to the fact where a "new" statement appears. You can put annotation above new statement I think...
          In the example I've given super.getParameterMap returns an instance.
          The put then causes the unchecked call warning, so I think I've got no choice to put suppresswarnings at the method level.
          If there is a better way of doing this, I'd appreciate a response.
          Cheers..
          • 2. Re: @SuppressWarnings(value="unchecked") & javac <identifier> expected mess
            608410
            You can only apply that annotation to methods, constructors and whole classes.

            In order to get the fine granularity, replace the statement you would like to (but can't) annotate, with a call to a new private method, and annotate that method.


            in this particular case, I think getParameterMap() is small enought that you could just annotate that. If your opinion varies (excuse the ugly method name)
             public Map getParameterMap() {
                Map tmpMap = super.getParameterMap();
                suppressWarningMapPut(tmpMap,"DEPT", myDept);
                return tmpMap;
              }
            
            @SuppressWarnings(value="unchecked")
            private void suppressWarningMapPut(Map m, String key, Object value) {
                m.put(key,key,value);
            }
            Alternatively you can use generics to tell the compiler what sort of map it is - maybe like this.
              public Map getParameterMap() {
                Map<String,String[]> tmpMap = super.getParameterMap();
                tmpMap.put("DEPT", myDept);
                return tmpMap;
              }
            But bear in mind that a ServletRequest's parameter map is immutable!!

            [http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/ServletRequest.html#getParameterMap()|http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/ServletRequest.html#getParameterMap()]
            • 3. Re: @SuppressWarnings(value="unchecked") & javac <identifier> expected mess
              843810
              Hi Bruce,
              Since writing the post I had thought about refactoring method as you describe with first example to gain the granularity.
              Thanks for feedback.
              BTW: I had tried <String, Object> first but still got warnings on put as you describe in second example. So this still doesn't work either..
              See below:
              javac -Xlint LegacyAdapterRequestWrapper.java
              
              LegacyAdapterRequestWrapper.java:16: warning: [unchecked] unchecked conversion
              found   : java.util.Map
              required: java.util.Map<java.lang.String,java.lang.Object>
                  Map<String, Object> tmpMap = super.getParameterMap();
                                                                    ^
              1 warning
              • 4. Re: @SuppressWarnings(value="unchecked") & javac <identifier> expected mess
                843810
                Can you not cast it?
                    Map<String, Object> tmpMap = (Map<String, Object>)super.getParameterMap();
                or, as brucechapman pointed out ;as ServletRequest's parameter map is immutable;
                    Map<String, Object> tmpMap = new HashMap<String, Object>(super.getParameterMap());
                • 5. Re: @SuppressWarnings(value="unchecked") & javac <identifier> expected mess
                  843810
                  tschodt wrote:
                  Can you not cast it?
                  Map<String, Object> tmpMap = (Map<String, Object>)super.getParameterMap();
                  or, as brucechapman pointed out ;as ServletRequest's parameter map is immutable;
                  Map<String, Object> tmpMap = new HashMap<String, Object>(super.getParameterMap());
                  You end up going down a slippery slope here. tmpMap is returned from method.
                  That method inherits from a base class that doesn't have generics, so you'd get a problem on the return then!
                  This is what first option you gave gives:
                  javac -Xlint LegacyAdapterRequestWrapper.java
                  
                  LegacyAdapterRequestWrapper.java:16: warning: [unchecked] unchecked cast
                  found   : java.util.Map
                  required: java.util.Map<java.lang.String,java.lang.Object>
                      Map<String, Object> tmpMap = (Map<String, Object>) super.getParameterMap();
                                                                                              ^
                  1 warning
                  Second gives:
                  LegacyAdapterRequestWrapper.java:17: warning: [unchecked] unchecked conversion
                  found   : java.util.Map
                  required: java.util.Map<? extends java.lang.String,? extends java.lang.Object>
                      Map<String, Object> tmpMap = new HashMap<String, Object>(super.getParameterM
                  ap());
                  
                    ^
                  1 warning
                  • 6. Re: @SuppressWarnings(value="unchecked") & javac <identifier> expected messages
                    843810
                    Hi JGF,

                    You do not have the opening curly brace '{' for the method
                    public LegacyAdapterRequestWrapper(HttpServletRequest inReq, String deptString)