tools.analyzer

Can this code avoid reflection and be analyzed by tools.analyzer?

Details

  • Type: Defect Defect
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Declined
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None

Description

This is probably another variant of TANAL-24, but I wanted to get the example to you for two reasons: (1) for me to learn how to type-hint this case correctly, if there is a way and (2) in case there was something new in this example that you haven't already seen.

(ns eastwood.test.testcases.f09
  (:use clojure.test)
  (:import [java.io BufferedOutputStream])
  (:require [clojure.java.io :as io]))

(set! *warn-on-reflection* true)

(defmacro ^:private bytestring
  [s] `(.getBytes ~s "UTF-8"))

(def ^bytes bs-crlf (bytestring "\r\n"))

(defmacro ^:private send-crlf [out] `(.write ~out bs-crlf 0 2))

(defn send-request [out]
  (send-crlf out))

As the subject says, I would like to know your thoughts on whether there is a way to type-hint this code such that the .write method invocation does not use reflection, and tools.analyzer(.jvm) can analyze it with no exception.

This test case was boiled down from some code in the ClojureWerkz Carmine library.

Activity

Hide
Nicola Mometto added a comment -

This cannot be analyzed because of the def :tag bug I talked you about, (def ^bytes ..) will attach the clojure.core/bytes function in the :tag instead of 'bytes.

A way to fix this is to change it to (def ^{:tag 'bytes} ..)
The same holds for every primitive type hint in def.

To avoid the reflection warning ^BufferedOutputStream out in either send-request or send-crlf should work

Show
Nicola Mometto added a comment - This cannot be analyzed because of the def :tag bug I talked you about, (def ^bytes ..) will attach the clojure.core/bytes function in the :tag instead of 'bytes. A way to fix this is to change it to (def ^{:tag 'bytes} ..) The same holds for every primitive type hint in def. To avoid the reflection warning ^BufferedOutputStream out in either send-request or send-crlf should work

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: