Skip to content

Commit

Permalink
Unpickle "privateWith", aka "package private"
Browse files Browse the repository at this point in the history
Anything which is package private is now excluded from the problem
analysis MiMa does.  This includes:

* classes/traits/objects,
* methods/fields,
* method overloads,
* as well as classes becoming private/public.
* case classes, or more generally class/object companions
* nested classes/objects (tested up to 3 levels)
  • Loading branch information
dwijnand committed Dec 23, 2020
1 parent 3e9f383 commit 8d93547
Show file tree
Hide file tree
Showing 97 changed files with 1,464 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@ private[core] sealed class BytesReader(buf: Array[Byte]) {
final def getDouble(idx: Int): Double = longBitsToDouble(getLong(idx))

final def getString(idx: Int, len: Int): String = new String(buf, idx, len, StandardCharsets.UTF_8)

final def getBytes(idx: Int, bytes: Array[Byte]): Unit = System.arraycopy(buf, idx, bytes, 0, bytes.length)
}

/** A BytesReader which also holds a mutable pointer to where it will read next. */
private[core] final class BufferReader(buf: Array[Byte]) extends BytesReader(buf) {
private[core] final class BufferReader(buf: Array[Byte], val path: String) extends BytesReader(buf) {
/** the buffer pointer */
var bp: Int = 0

def nextByte: Byte = { val b = getByte(bp); bp += 1; b }
def nextChar: Char = { val c = getChar(bp); bp += 2; c } // Char = unsigned 2-bytes, aka u16
def nextInt: Int = { val i = getInt(bp); bp += 4; i }

def acceptByte(exp: Byte, ctx: => String = "") = { val obt = nextByte; assert(obt == exp, s"Expected $exp, obtained $obt$ctx"); obt }
def acceptChar(exp: Char, ctx: => String = "") = { val obt = nextChar; assert(obt == exp, s"Expected $exp, obtained $obt$ctx"); obt }

def skip(n: Int): Unit = bp += n
}
125 changes: 125 additions & 0 deletions core/src/main/scala/com/typesafe/tools/mima/core/ByteCodecs.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package com.typesafe.tools.mima.core

/**
* Helper methods to serialize a byte array as String
* that can be written as "modified" UTF-8 to classfiles.
*
* Modified UTF-8 is the same as UTF-8, except for 0x00,
* which is represented as the "overlong" 0xC0 0x80.
* Constant strings in classfiles use this encoding.
*
* Encoding (according to SID-10):
* - The 8-bit bytes are split into 7-bit bytes, e.g., 0xff 0x0f becomes 0x7f 0x1f 0x00
* - Every bit is incremented by 1 (modulo 0x80), in the example we get 0x00, 0x20 0x01
* - 0x00 is mapped to the overlong encoding, so we get 0xC0 0x80 0x20 0x01
*
* The +1 increment should reduce the number of (overlong) zeros in the resulting string,
* as 0x7f is (hoped to be) more common than 0x00.
*/
object ByteCodecs {
/** Map 0xC0 0x80 to 0x00, then subtract 1 from each element. In-place. */
def regenerateZero(src: Array[Byte]): Int = {
var i = 0
val srclen = src.length
var j = 0
while (i < srclen) {
val in: Int = src(i) & 0xff
if (in == 0xc0 && (src(i + 1) & 0xff) == 0x80) {
src(j) = 0x7f
i += 2
} else if (in == 0) {
src(j) = 0x7f
i += 1
} else {
src(j) = (in - 1).toByte
i += 1
}
j += 1
}
j
}

def decode7to8(src: Array[Byte], srclen: Int): Int = {
var i = 0
var j = 0
val dstlen = (srclen * 7 + 7) / 8
while (i + 7 < srclen) {
var out: Int = src(i).toInt
var in: Byte = src(i + 1)
src(j) = (out | (in & 0x01) << 7).toByte
out = in >>> 1
in = src(i + 2)
src(j + 1) = (out | (in & 0x03) << 6).toByte
out = in >>> 2
in = src(i + 3)
src(j + 2) = (out | (in & 0x07) << 5).toByte
out = in >>> 3
in = src(i + 4)
src(j + 3) = (out | (in & 0x0f) << 4).toByte
out = in >>> 4
in = src(i + 5)
src(j + 4) = (out | (in & 0x1f) << 3).toByte
out = in >>> 5
in = src(i + 6)
src(j + 5) = (out | (in & 0x3f) << 2).toByte
out = in >>> 6
in = src(i + 7)
src(j + 6) = (out | in << 1).toByte
i += 8
j += 7
}
if (i < srclen) {
var out: Int = src(i).toInt
if (i + 1 < srclen) {
var in: Byte = src(i + 1)
src(j) = (out | (in & 0x01) << 7).toByte; j += 1
out = in >>> 1
if (i + 2 < srclen) {
in = src(i + 2)
src(j) = (out | (in & 0x03) << 6).toByte; j += 1
out = in >>> 2
if (i + 3 < srclen) {
in = src(i + 3)
src(j) = (out | (in & 0x07) << 5).toByte; j += 1
out = in >>> 3
if (i + 4 < srclen) {
in = src(i + 4)
src(j) = (out | (in & 0x0f) << 4).toByte; j += 1
out = in >>> 4
if (i + 5 < srclen) {
in = src(i + 5)
src(j) = (out | (in & 0x1f) << 3).toByte; j += 1
out = in >>> 5
if (i + 6 < srclen) {
in = src(i + 6)
src(j) = (out | (in & 0x3f) << 2).toByte; j += 1
out = in >>> 6
}
}
}
}
}
}
if (j < dstlen) src(j) = out.toByte
}
dstlen
}

/**
* Destructively decodes array xs and returns the length of the decoded array.
*
* Sometimes returns (length+1) of the decoded array. Example:
*
* scala> val enc = scala.reflect.internal.pickling.ByteCodecs.encode(Array(1,2,3))
* enc: Array[Byte] = Array(2, 5, 13, 1)
*
* scala> scala.reflect.internal.pickling.ByteCodecs.decode(enc)
* res43: Int = 4
*
* scala> enc
* res44: Array[Byte] = Array(1, 2, 3, 0)
*
* However, this does not always happen.
*/
def decode(xs: Array[Byte]): Int = decode7to8(xs, regenerateZero(xs))
}
35 changes: 20 additions & 15 deletions core/src/main/scala/com/typesafe/tools/mima/core/ClassInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ private[core] final class ConcreteClassInfo(owner: PackageInfo, val file: AbsFil
private var loaded: Boolean = false

protected def afterLoading[A](x: => A) = {
if (!loaded)
try {
ConsoleLogging.verbose(s"parsing $file")
ClassfileParser.parseInPlace(this, file)
} finally {
loaded = true
}
if (!loaded) {
loaded = true
ConsoleLogging.verbose(s"parsing $file")
ClassfileParser.parseInPlace(this, file)
}
x
}
}
Expand All @@ -60,10 +58,14 @@ private[mima] sealed abstract class ClassInfo(val owner: PackageInfo) extends In
final var _fields: Members[FieldInfo] = NoMembers
final var _methods: Members[MethodInfo] = NoMembers
final var _flags: Int = 0
final var _scopedPrivate: Boolean = false
final var _implClass: ClassInfo = NoClass
final var _moduleClass: ClassInfo = NoClass
final var _module: ClassInfo = NoClass

protected def afterLoading[A](x: => A): A

final def forceLoad: this.type = afterLoading(this)
final def innerClasses: Seq[String] = afterLoading(_innerClasses)
final def isLocalClass: Boolean = afterLoading(_isLocalClass)
final def isTopLevel: Boolean = afterLoading(_isTopLevel)
Expand All @@ -72,20 +74,23 @@ private[mima] sealed abstract class ClassInfo(val owner: PackageInfo) extends In
final def fields: Members[FieldInfo] = afterLoading(_fields)
final def methods: Members[MethodInfo] = afterLoading(_methods)
final def flags: Int = afterLoading(_flags)
final def isScopedPrivate: Boolean = afterLoading(_scopedPrivate)
final def implClass: ClassInfo = { owner.setImplClasses; _implClass } // returns NoClass if this is not a trait
final def moduleClass: ClassInfo = { owner.setModules; if (_moduleClass == NoClass) this else _moduleClass }
final def module: ClassInfo = { owner.setModules; if (_module == NoClass) this else _module }

final def isTrait: Boolean = implClass ne NoClass // trait with some concrete methods or fields
final def isModule: Boolean = bytecodeName.endsWith("$") // super scuffed
final def isImplClass: Boolean = bytecodeName.endsWith("$class")
final def isInterface: Boolean = ClassfileParser.isInterface(flags) // java interface or trait w/o impl methods
final def isClass: Boolean = !isTrait && !isInterface // class, object or trait's impl class
final def isTrait: Boolean = implClass ne NoClass // trait with some concrete methods or fields
final def isModuleClass: Boolean = bytecodeName.endsWith("$") // super scuffed
final def isImplClass: Boolean = bytecodeName.endsWith("$class")
final def isInterface: Boolean = ClassfileParser.isInterface(flags) // java interface or trait w/o impl methods
final def isClass: Boolean = !isTrait && !isInterface // class, object or trait's impl class

final def accessModifier: String = if (isProtected) "protected" else if (isPrivate) "private" else ""
final def declarationPrefix: String = if (isModule) "object" else if (isTrait) "trait" else if (isInterface) "interface" else "class"
final def declarationPrefix: String = if (isModuleClass) "object" else if (isTrait) "trait" else if (isInterface) "interface" else "class"
final lazy val fullName: String = if (owner.isRoot) bytecodeName else s"${owner.fullName}.$bytecodeName"
final def formattedFullName: String = formatClassName(if (isModule) fullName.init else fullName)
final def formattedFullName: String = formatClassName(if (isModuleClass) fullName.init else fullName)
final def description: String = s"$declarationPrefix $formattedFullName"
final def classString: String = s"$accessModifier $declarationPrefix $formattedFullName".trim
final def classString: String = s"$accessModifier $description".trim

lazy val superClasses: Set[ClassInfo] = {
if (this == ClassInfo.ObjectClass) Set.empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ private[core] object ClassfileConstants {
final val JAVA_ACC_PROTECTED = 0x0004
final val JAVA_ACC_STATIC = 0x0008
final val JAVA_ACC_FINAL = 0x0010
final val JAVA_ACC_BRIDGE = 0x0040
final val JAVA_ACC_INTERFACE = 0x0200
final val JAVA_ACC_ABSTRACT = 0x0400
final val JAVA_ACC_SYNTHETIC = 0x1000
Expand Down Expand Up @@ -43,4 +44,8 @@ private[core] object ClassfileConstants {
final val VOID_TAG = 'V'
final val OBJECT_TAG = 'L'
final val ANNOTATION_TAG = '@'

final val STRING_TAG = 's'
final val ENUM_TAG = 'e'
final val CLASS_TAG = 'c'
}
Loading

0 comments on commit 8d93547

Please sign in to comment.