Skip to content

Commit d04de01

Browse files
committed
Pattern matching on ClassBType extracts the inernalName
1 parent ddc0f44 commit d04de01

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

src/compiler/scala/tools/nsc/backend/jvm/BTypes.scala

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ abstract class BTypes[G <: Global](val __global_dont_use: G) {
4343
case FLOAT => "F"
4444
case LONG => "J"
4545
case DOUBLE => "D"
46-
case c @ ClassBType(_, _) => "L" + c.internalName + ";"
47-
case ArrayBType(component) => "[" + component
48-
case MethodBType(args, res) => "(" + args.mkString + ")" + res
46+
case ClassBType(internalName) => "L" + internalName + ";"
47+
case ArrayBType(component) => "[" + component
48+
case MethodBType(args, res) => "(" + args.mkString + ")" + res
4949
}
5050

5151
/**
@@ -160,9 +160,9 @@ abstract class BTypes[G <: Global](val __global_dont_use: G) {
160160
case FLOAT => asm.Type.FLOAT_TYPE
161161
case LONG => asm.Type.LONG_TYPE
162162
case DOUBLE => asm.Type.DOUBLE_TYPE
163-
case c @ ClassBType(_, _) => asm.Type.getObjectType(c.internalName) // (*)
164-
case a @ ArrayBType(_) => asm.Type.getObjectType(a.descriptor)
165-
case m @ MethodBType(_, _) => asm.Type.getMethodType(m.descriptor)
163+
case ClassBType(internalName) => asm.Type.getObjectType(internalName) // see (*) above
164+
case a: ArrayBType => asm.Type.getObjectType(a.descriptor)
165+
case m: MethodBType => asm.Type.getMethodType(m.descriptor)
166166
}
167167

168168
def asRefBType : RefBType = this.asInstanceOf[RefBType]
@@ -227,8 +227,8 @@ abstract class BTypes[G <: Global](val __global_dont_use: G) {
227227
* This can be verified for example using javap or ASMifier.
228228
*/
229229
def classOrArrayType: String = this match {
230-
case c: ClassBType => c.internalName
231-
case a: ArrayBType => a.descriptor
230+
case ClassBType(internalName) => internalName
231+
case a: ArrayBType => a.descriptor
232232
}
233233
}
234234

@@ -458,21 +458,23 @@ abstract class BTypes[G <: Global](val __global_dont_use: G) {
458458
*/
459459
class ClassBType private(val offset: Int, val length: Int) extends RefBType {
460460
/**
461-
* Construct a ClassBType for a given (intenred) class name.
461+
* Construct a ClassBType from the (intenred) internal name of a class.
462462
*
463-
* @param n The class name as a slice of the `chrs` array, without the surrounding 'L' and ';'.
464-
* Note that `classSymbol.javaBinaryName` returns exactly such a name.
463+
* @param internalName The internal name as a slice of the `chrs` array. The internal name does
464+
* not have the surrounding 'L' and ';'. Note that
465+
* `classSymbol.javaBinaryName` returns exactly such a name.
465466
*/
466-
def this(n: BTypeName) = this(n.start, n.length)
467+
def this(internalName: BTypeName) = this(internalName.start, internalName.length)
467468

468469
/**
469-
* Construct a ClassBType for a given java class name.
470+
* Construct a ClassBType from the internal name of a class.
470471
*
471-
* @param s A class name of the form "java/lang/String", without the surrounding 'L' and ';'.
472+
* @param internalName The internal name of a class has the form "java/lang/String", without the
473+
* surrounding 'L' and ';'.
472474
*/
473-
def this(s: String) = this({
474-
assert(!(s.head == 'L' && s.last == ';'), s"Descriptor instead of internal name: $s")
475-
createNewName(s)
475+
def this(internalName: String) = this({
476+
assert(!(internalName.head == 'L' && internalName.last == ';'), s"Descriptor instead of internal name: $internalName")
477+
createNewName(internalName)
476478
})
477479

478480
/**
@@ -490,7 +492,7 @@ abstract class BTypes[G <: Global](val __global_dont_use: G) {
490492
* Custom equals / hashCode are needed because this is not a case class.
491493
*/
492494
override def equals(o: Any): Boolean = (this eq o.asInstanceOf[Object]) || (o match {
493-
case ClassBType(`offset`, `length`) => true
495+
case c: ClassBType => c.offset == this.offset && c.length == this.length
494496
case _ => false
495497
})
496498

@@ -504,12 +506,15 @@ abstract class BTypes[G <: Global](val __global_dont_use: G) {
504506
}
505507

506508
object ClassBType {
507-
def apply(n: BTypeName): ClassBType = new ClassBType(n)
508-
def apply(s: String): ClassBType = new ClassBType(s)
509+
def apply(internalName: BTypeName): ClassBType = new ClassBType(internalName)
510+
def apply(internalName: String): ClassBType = new ClassBType(internalName)
509511

510-
def unapply(c: ClassBType): Option[(Int, Int)] =
512+
/**
513+
* Pattern matching on a ClassBType extracts the `internalName` of the class.
514+
*/
515+
def unapply(c: ClassBType): Option[String] =
511516
if (c == null) None
512-
else Some((c.offset, c.length))
517+
else Some(c.internalName)
513518
}
514519

515520
case class ArrayBType(componentType: BType) extends RefBType {

0 commit comments

Comments
 (0)