-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support diffing Jar bytecode versions #168
base: trunk
Are you sure you want to change the base?
Changes from all commits
d029d8d
518b586
6d00f64
2dcda91
199a3a8
2521808
ac601d4
7e403a5
fc45678
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package com.jakewharton.diffuse.format | ||
|
||
interface BinaryFormat { | ||
sealed interface BinaryFormat { | ||
val filename: String? | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import com.jakewharton.diffuse.io.Input | |
|
||
class Jar private constructor( | ||
override val filename: String?, | ||
val bytecodeVersion: Short?, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately I think we're going to drown in nuance here. Jars don't have bytecode versions, classes do. And multiple classes within a jar can have different versions. You can conditionally classload classes based on capabilities and those classes could be compiled for two different versions of Java. There's also the multi-version jar standard which puts classes of different versions into I'm happy to discuss strategies for surfacing this information, but as-is I think this is too simple of an implementation. Note that it probably works for 99.99% of jars, but when it fails it fails in a spectacularly wrong way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now I count all the byte code versions in a Jar and filter the most one. |
||
val files: ArchiveFiles, | ||
val classes: List<Class>, | ||
override val declaredMembers: List<Member>, | ||
|
@@ -20,17 +21,27 @@ class Jar private constructor( | |
fun Input.toJar(): Jar { | ||
toZip().use { zip -> | ||
val files = zip.toArchiveFiles { it.toJarFileType() } | ||
val bcVersions = mutableMapOf<Short, Long>() | ||
|
||
val classes = zip.entries | ||
.asSequence() | ||
.filter { it.path.endsWith(".class") } | ||
.map { it.asInput().toClass() } | ||
.map { entry -> | ||
entry.asInput().toClass().also { cls -> | ||
// Count all the byte code versions in a Jar. | ||
bcVersions.merge(cls.bytecodeVersion, 1L, Long::plus) | ||
} | ||
} | ||
.toList() | ||
|
||
val mostBytecodeVersion = bcVersions.maxByOrNull { it.value }?.key | ||
|
||
val declaredMembers = classes.flatMap { it.declaredMembers } | ||
val referencedMembers = classes.flatMapTo(LinkedHashSet()) { it.referencedMembers } | ||
// Declared methods are likely to reference other declared members. Ensure all are removed. | ||
referencedMembers -= declaredMembers | ||
|
||
return Jar(name, files, classes, declaredMembers.sorted(), referencedMembers.sorted()) | ||
return Jar(name, mostBytecodeVersion, files, classes, declaredMembers.sorted(), referencedMembers.sorted()) | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how to implement this for Dex files.