-
Notifications
You must be signed in to change notification settings - Fork 411
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 Kotlin simple enums in optimization #350
Open
mrjameshamilton
wants to merge
2
commits into
master
Choose a base branch
from
349-verifyerror-with-compose-multiplatform-project
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
base/src/test/kotlin/proguard/optimize/EnumSimplifierTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package proguard.optimize | ||
|
||
import io.kotest.core.spec.style.FreeSpec | ||
import io.kotest.matchers.shouldBe | ||
import proguard.classfile.ClassPool | ||
import proguard.classfile.ProgramClass | ||
import proguard.classfile.ProgramMember | ||
import proguard.classfile.attribute.visitor.AllAttributeVisitor | ||
import proguard.classfile.editor.MemberReferenceFixer | ||
import proguard.classfile.util.ClassPoolClassLoader | ||
import proguard.classfile.visitor.AllMemberVisitor | ||
import proguard.classfile.visitor.AllMethodVisitor | ||
import proguard.classfile.visitor.MemberNameFilter | ||
import proguard.classfile.visitor.MemberVisitor | ||
import proguard.optimize.evaluation.SimpleEnumClassSimplifier | ||
import proguard.optimize.evaluation.SimpleEnumDescriptorSimplifier | ||
import proguard.optimize.evaluation.SimpleEnumUseSimplifier | ||
import proguard.optimize.info.ProgramClassOptimizationInfoSetter | ||
import proguard.optimize.info.ProgramMemberOptimizationInfoSetter | ||
import proguard.optimize.info.SimpleEnumFilter | ||
import proguard.optimize.info.SimpleEnumMarker | ||
import proguard.testutils.ClassPoolBuilder | ||
import proguard.testutils.JavaSource | ||
import proguard.testutils.KotlinSource | ||
import proguard.testutils.and | ||
import proguard.testutils.match | ||
|
||
class EnumSimplifierTest : FreeSpec({ | ||
"Test simplification of simple Kotlin enum (#349)" { | ||
val (programClassPool, libraryClassPool) = ClassPoolBuilder.fromSource( | ||
KotlinSource( | ||
"TestEnum.kt", "enum class TestEnum { A, B, C }" | ||
) | ||
) | ||
|
||
simplifyEnum(libraryClassPool, programClassPool) | ||
} | ||
|
||
"Test simplification of simple Java enum" { | ||
val (programClassPool, libraryClassPool) = ClassPoolBuilder.fromSource( | ||
JavaSource( | ||
"TestEnum.java", "public enum TestEnum { A, B, C, }" | ||
) | ||
) | ||
|
||
simplifyEnum(libraryClassPool, programClassPool) | ||
} | ||
}) | ||
|
||
private fun simplifyEnum(libraryClassPool: ClassPool, programClassPool: ClassPool) { | ||
// Setup the OptimizationInfo on the classes | ||
val keepMarker = KeepMarker() | ||
libraryClassPool.classesAccept(keepMarker) | ||
libraryClassPool.classesAccept(AllMemberVisitor(keepMarker)) | ||
programClassPool.classesAccept(ProgramClassOptimizationInfoSetter()) | ||
programClassPool.classesAccept(AllMemberVisitor(ProgramMemberOptimizationInfoSetter())) | ||
|
||
programClassPool.classAccept("TestEnum", SimpleEnumMarker(true)) | ||
|
||
val enumClazz = programClassPool.getClass("TestEnum") | ||
|
||
SimpleEnumMarker.isSimpleEnum(enumClazz) shouldBe true | ||
|
||
// Application of enum simplification similar to the Optimizer. | ||
|
||
// Simplify the use of the enum classes in code. | ||
programClassPool.classesAccept( | ||
AllMethodVisitor( | ||
AllAttributeVisitor( | ||
SimpleEnumUseSimplifier() | ||
) | ||
) | ||
) | ||
|
||
// Simplify the static initializers of simple enum classes. | ||
programClassPool.classesAccept( | ||
SimpleEnumFilter( | ||
SimpleEnumClassSimplifier() | ||
) | ||
) | ||
|
||
// Simplify the use of the enum classes in descriptors. | ||
programClassPool.classesAccept( | ||
SimpleEnumDescriptorSimplifier() | ||
) | ||
|
||
// Update references to class members with simple enum classes. | ||
programClassPool.classesAccept(MemberReferenceFixer(false)) | ||
|
||
// Trigger class loading, that would previously result in a verify error. | ||
val classPoolClassLoader = ClassPoolClassLoader(programClassPool) | ||
val clazz = classPoolClassLoader.findClass("TestEnum") | ||
clazz.fields.single { it.name.startsWith("A") }.get(null) shouldBe 1 | ||
|
||
enumClazz.methodsAccept( | ||
MemberNameFilter( | ||
"values*", | ||
object : MemberVisitor { | ||
override fun visitProgramMember(programClass: ProgramClass, programMember: ProgramMember) { | ||
with(programClass and programMember) { | ||
match { | ||
invokevirtual("[I", "clone", "()Ljava/lang/Object;") | ||
} shouldBe true | ||
} | ||
} | ||
} | ||
) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can this also trigger when we have a reguler
Object.clone
call that has nothing to do with Kotlin? We shouldn't apply this replacement in that case then? Could be useful to also add a test for that.