2

在 Tomcat 7 中使用 Scala 时,HttpServletRequest.getHeaderNames() 显然返回 java.util.Collection 而不是 java.util.Enumeration,如docs 中所示

这里是否发生了一些隐式转换?

更具体地说,以下 servlet:

import java.io._
import javax.servlet._
import javax.servlet.http._

class TomcatScalaExample extends HttpServlet {
    override def doGet (req: HttpServletRequest, res: HttpServletResponse): Unit = {
        val out = res.getWriter()
        out.println(res.getHeaderNames.getClass.getCanonicalName)
        out.close()
    }
}

在浏览器中打印以下内容:

java.util.ArrayList

虽然我希望有一些与 java.util.Enumeration 相关的东西。

作为参考,下面是使用 -Xprint:typer 编译的代码:

   [scalac] package pl.kamituel.tomcatscalaexample {
   [scalac]   import java.io._;
   [scalac]   import javax.servlet._;
   [scalac]   import javax.servlet.http._;
   [scalac]   class TomcatScalaExample extends javax.servlet.http.HttpServlet {
   [scalac]     def <init>(): pl.kamituel.tomcatscalaexample.TomcatScalaExample = {
   [scalac]       TomcatScalaExample.super.<init>();
   [scalac]       ()
   [scalac]     };
   [scalac]     override def doGet(req: javax.servlet.http.HttpServletRequest, res:     javax.servlet.http.HttpServletResponse): Unit = {
   [scalac]       val out: java.io.PrintWriter = res.getWriter();
   [scalac]       out.println(res.getHeaderNames().getClass().getCanonicalName());
   [scalac]       out.close()
   [scalac]     }
   [scalac]   }
   [scalac] }
   [scalac]
4

0 回答 0