Skip to content

Commit 8e5d78c

Browse files
authored
Fix for shapeless @@ types passed on as args (#219)
1 parent d1f250f commit 8e5d78c

File tree

2 files changed

+123
-10
lines changed

2 files changed

+123
-10
lines changed

src/main/scala/com/typesafe/scalalogging/LoggerMacro.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private[scalalogging] object LoggerMacro {
4141
val underlying = q"${c.prefix}.underlying"
4242
val anyRefArgs = formatArgs(c)(args: _*)
4343
if (args.length == 2)
44-
q"if ($underlying.isErrorEnabled) $underlying.error($message, _root_.scala.Array(${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
44+
q"if ($underlying.isErrorEnabled) $underlying.error($message, _root_.scala.Array[AnyRef](${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
4545
else
4646
q"if ($underlying.isErrorEnabled) $underlying.error($message, ..$anyRefArgs)"
4747
}
@@ -62,7 +62,7 @@ private[scalalogging] object LoggerMacro {
6262
val underlying = q"${c.prefix}.underlying"
6363
val anyRefArgs = formatArgs(c)(args: _*)
6464
if (args.length == 2)
65-
q"if ($underlying.isErrorEnabled($marker)) $underlying.error($marker, $message, _root_.scala.Array(${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
65+
q"if ($underlying.isErrorEnabled($marker)) $underlying.error($marker, $message, _root_.scala.Array[AnyRef](${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
6666
else
6767
q"if ($underlying.isErrorEnabled($marker)) $underlying.error($marker, $message, ..$anyRefArgs)"
6868
}
@@ -91,7 +91,7 @@ private[scalalogging] object LoggerMacro {
9191
val underlying = q"${c.prefix}.underlying"
9292
val anyRefArgs = formatArgs(c)(args: _*)
9393
if (args.length == 2)
94-
q"if ($underlying.isWarnEnabled) $underlying.warn($message, _root_.scala.Array(${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
94+
q"if ($underlying.isWarnEnabled) $underlying.warn($message, _root_.scala.Array[AnyRef](${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
9595
else
9696
q"if ($underlying.isWarnEnabled) $underlying.warn($message, ..$anyRefArgs)"
9797
}
@@ -112,7 +112,7 @@ private[scalalogging] object LoggerMacro {
112112
val underlying = q"${c.prefix}.underlying"
113113
val anyRefArgs = formatArgs(c)(args: _*)
114114
if (args.length == 2)
115-
q"if ($underlying.isWarnEnabled($marker)) $underlying.warn($marker, $message, _root_.scala.Array(${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
115+
q"if ($underlying.isWarnEnabled($marker)) $underlying.warn($marker, $message, _root_.scala.Array[AnyRef](${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
116116
else
117117
q"if ($underlying.isWarnEnabled($marker)) $underlying.warn($marker, $message, ..$anyRefArgs)"
118118
}
@@ -141,7 +141,7 @@ private[scalalogging] object LoggerMacro {
141141
val underlying = q"${c.prefix}.underlying"
142142
val anyRefArgs = formatArgs(c)(args: _*)
143143
if (args.length == 2)
144-
q"if ($underlying.isInfoEnabled) $underlying.info($message, _root_.scala.Array(${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
144+
q"if ($underlying.isInfoEnabled) $underlying.info($message, _root_.scala.Array[AnyRef](${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
145145
else
146146
q"if ($underlying.isInfoEnabled) $underlying.info($message, ..$anyRefArgs)"
147147
}
@@ -162,7 +162,7 @@ private[scalalogging] object LoggerMacro {
162162
val underlying = q"${c.prefix}.underlying"
163163
val anyRefArgs = formatArgs(c)(args: _*)
164164
if (args.length == 2)
165-
q"if ($underlying.isInfoEnabled($marker)) $underlying.info($marker, $message, _root_.scala.Array(${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
165+
q"if ($underlying.isInfoEnabled($marker)) $underlying.info($marker, $message, _root_.scala.Array[AnyRef](${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
166166
else
167167
q"if ($underlying.isInfoEnabled($marker)) $underlying.info($marker, $message, ..$anyRefArgs)"
168168
}
@@ -191,7 +191,7 @@ private[scalalogging] object LoggerMacro {
191191
val underlying = q"${c.prefix}.underlying"
192192
val anyRefArgs = formatArgs(c)(args: _*)
193193
if (args.length == 2)
194-
q"if ($underlying.isDebugEnabled) $underlying.debug($message, _root_.scala.Array(${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
194+
q"if ($underlying.isDebugEnabled) $underlying.debug($message, _root_.scala.Array[AnyRef](${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
195195
else
196196
q"if ($underlying.isDebugEnabled) $underlying.debug($message, ..$anyRefArgs)"
197197
}
@@ -212,7 +212,7 @@ private[scalalogging] object LoggerMacro {
212212
val underlying = q"${c.prefix}.underlying"
213213
val anyRefArgs = formatArgs(c)(args: _*)
214214
if (args.length == 2)
215-
q"if ($underlying.isDebugEnabled($marker)) $underlying.debug($marker, $message, _root_.scala.Array(${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
215+
q"if ($underlying.isDebugEnabled($marker)) $underlying.debug($marker, $message, _root_.scala.Array[AnyRef](${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
216216
else
217217
q"if ($underlying.isDebugEnabled($marker)) $underlying.debug($marker, $message, ..$anyRefArgs)"
218218
}
@@ -241,7 +241,7 @@ private[scalalogging] object LoggerMacro {
241241
val underlying = q"${c.prefix}.underlying"
242242
val anyRefArgs = formatArgs(c)(args: _*)
243243
if (args.length == 2)
244-
q"if ($underlying.isTraceEnabled) $underlying.trace($message, _root_.scala.Array(${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
244+
q"if ($underlying.isTraceEnabled) $underlying.trace($message, _root_.scala.Array[AnyRef](${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
245245
else
246246
q"if ($underlying.isTraceEnabled) $underlying.trace($message, ..$anyRefArgs)"
247247
}
@@ -262,7 +262,7 @@ private[scalalogging] object LoggerMacro {
262262
val underlying = q"${c.prefix}.underlying"
263263
val anyRefArgs = formatArgs(c)(args: _*)
264264
if (args.length == 2)
265-
q"if ($underlying.isTraceEnabled($marker)) $underlying.trace($marker, $message, _root_.scala.Array(${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
265+
q"if ($underlying.isTraceEnabled($marker)) $underlying.trace($marker, $message, _root_.scala.Array[AnyRef](${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
266266
else
267267
q"if ($underlying.isTraceEnabled($marker)) $underlying.trace($marker, $message, ..$anyRefArgs)"
268268
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.typesafe.scalalogging
2+
3+
import org.scalatest.{ Matchers, WordSpec }
4+
5+
import java.lang.ClassCastException
6+
import org.slf4j.{ Logger => Underlying }
7+
import org.scalatestplus.mockito.MockitoSugar
8+
import org.mockito.ArgumentMatchers._
9+
import org.mockito.Mockito._
10+
11+
object tag {
12+
13+
def apply[U] = new Tagger[U]
14+
15+
trait Tagged[U]
16+
type @@[+T, U] = T with Tagged[U]
17+
18+
class Tagger[U] {
19+
def apply[T](t: T): T @@ U = t.asInstanceOf[T @@ U]
20+
}
21+
}
22+
23+
class LoggerWithTaggedAargsSpec extends WordSpec with MockitoSugar with Matchers with Varargs {
24+
25+
trait Tag
26+
27+
import tag._
28+
29+
"Calling error with tagged args" should {
30+
31+
"not throw ClassCastException when varargs are passed" in {
32+
val f = fixture(_.isErrorEnabled)
33+
import f._
34+
noException shouldBe thrownBy {
35+
logger.error("This should not throw: {}, {}", arg1, arg2)
36+
}
37+
}
38+
39+
"not throw ClassCastException when interpolated message is passed" in {
40+
val f = fixture(_.isErrorEnabled)
41+
import f._
42+
noException shouldBe thrownBy {
43+
logger.error(s"This should not throw: $arg1, $arg2")
44+
}
45+
}
46+
}
47+
48+
"Calling trace with tagged args" should {
49+
50+
"not throw ClassCastException when varargs are passed" in {
51+
val f = fixture(_.isTraceEnabled)
52+
import f._
53+
noException shouldBe thrownBy {
54+
logger.trace("This should not throw: {}, {}", arg1, arg2)
55+
}
56+
}
57+
58+
"not throw ClassCastException when interpolated message is passed" in {
59+
val f = fixture(_.isTraceEnabled)
60+
import f._
61+
noException shouldBe thrownBy {
62+
logger.trace(s"This should not throw: $arg1, $arg2")
63+
}
64+
}
65+
}
66+
67+
"Calling debug with tagged args" should {
68+
69+
"not throw ClassCastException when varargs are passed" in {
70+
val f = fixture(_.isTraceEnabled)
71+
import f._
72+
noException shouldBe thrownBy {
73+
logger.debug("This should not throw: {}, {}", arg1, arg2)
74+
}
75+
}
76+
77+
"not throw ClassCastException when interpolated message is passed" in {
78+
val f = fixture(_.isTraceEnabled)
79+
import f._
80+
noException shouldBe thrownBy {
81+
logger.debug(s"This should not throw: $arg1, $arg2")
82+
}
83+
}
84+
}
85+
86+
"Calling info with tagged args" should {
87+
88+
"not throw ClassCastException when varargs are passed" in {
89+
val f = fixture(_.isInfoEnabled)
90+
import f._
91+
noException shouldBe thrownBy {
92+
logger.info("This should not throw: {}, {}", arg1, arg2)
93+
}
94+
}
95+
96+
"not throw ClassCastException when interpolated message is passed" in {
97+
val f = fixture(_.isInfoEnabled)
98+
import f._
99+
noException shouldBe thrownBy {
100+
logger.info(s"This should not throw: $arg1, $arg2")
101+
}
102+
}
103+
}
104+
105+
def fixture(p: Underlying => Boolean, isEnabled: Boolean = true) =
106+
new {
107+
val arg1 = tag[Tag][String]("arg1")
108+
val arg2 = tag[Tag][Integer](Integer.valueOf(1))
109+
val underlying = mock[org.slf4j.Logger]
110+
when(p(underlying)).thenReturn(isEnabled)
111+
val logger = Logger(underlying)
112+
}
113+
}

0 commit comments

Comments
 (0)