-
Notifications
You must be signed in to change notification settings - Fork 46
Add bytecode transformation for if statement with call of String methods #2446
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2448a52
Add bytecode transformation for if statement with call of String.equa…
egiptipavel 57054c7
Merge branch 'main' into egiptipavel/code-transformation
egiptipavel eefe129
Add bytecode transformation for String.startsWith and String.endsWith…
egiptipavel b1d49ea
Fix bug and add tests
egiptipavel 4ec2b2b
Fix bug with computing maximum stack size and the maximum number of l…
egiptipavel e0e2ffe
Add flag to enable bytecode transformation
egiptipavel 4a57b76
Fix bug
egiptipavel 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 hidden or 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
45 changes: 45 additions & 0 deletions
45
...ion-tests/src/test/java/org/utbot/examples/samples/transformation/StringMethodsCalls.java
This file contains hidden or 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,45 @@ | ||
package org.utbot.examples.samples.transformation; | ||
|
||
public class StringMethodsCalls { | ||
public static boolean equalsWithEmptyString(String strToCompare) { | ||
if (strToCompare.equals("")) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public static boolean equalsWithNotEmptyString(String strToCompare) { | ||
if (strToCompare.equals("abc")) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public static boolean startsWithWithEmptyString(String strToCompare) { | ||
if (strToCompare.startsWith("")) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public static boolean startsWithWithNotEmptyString(String strToCompare) { | ||
if (strToCompare.startsWith("abc")) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public static boolean endsWithWithEmptyString(String strToCompare) { | ||
if (strToCompare.endsWith("")) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public static boolean endsWithWithNotEmptyString(String strToCompare) { | ||
if (strToCompare.endsWith("abc")) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
130 changes: 130 additions & 0 deletions
130
utbot-instrumentation-tests/src/test/kotlin/org/utbot/examples/TestBytecodeTransformation.kt
This file contains hidden or 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,130 @@ | ||
package org.utbot.examples | ||
|
||
import org.junit.jupiter.api.Assertions.assertFalse | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.Test | ||
import org.utbot.examples.samples.transformation.StringMethodsCalls | ||
import org.utbot.instrumentation.execute | ||
import org.utbot.instrumentation.instrumentation.transformation.BytecodeTransformation | ||
import org.utbot.instrumentation.withInstrumentation | ||
|
||
class TestBytecodeTransformation { | ||
lateinit var utContext: AutoCloseable | ||
|
||
@Test | ||
fun testStringEqualsWithEmptyStringCall() { | ||
withInstrumentation( | ||
BytecodeTransformation.Factory, | ||
StringMethodsCalls::class.java.protectionDomain.codeSource.location.path | ||
) { executor -> | ||
val res1 = executor.execute(StringMethodsCalls::equalsWithEmptyString, arrayOf("")) | ||
assertTrue(res1.getOrNull() as Boolean) | ||
|
||
val res2 = executor.execute(StringMethodsCalls::equalsWithEmptyString, arrayOf("abc")) | ||
assertFalse(res2.getOrNull() as Boolean) | ||
|
||
val res3 = executor.execute(StringMethodsCalls::equalsWithEmptyString, arrayOf(null)) | ||
assertTrue(res3.exceptionOrNull() is NullPointerException) | ||
} | ||
} | ||
|
||
@Test | ||
fun testStringEqualsWithNotEmptyStringCall() { | ||
withInstrumentation( | ||
BytecodeTransformation.Factory, | ||
StringMethodsCalls::class.java.protectionDomain.codeSource.location.path | ||
) { executor -> | ||
val res1 = executor.execute(StringMethodsCalls::equalsWithNotEmptyString, arrayOf("")) | ||
assertFalse(res1.getOrNull() as Boolean) | ||
|
||
val res2 = executor.execute(StringMethodsCalls::equalsWithNotEmptyString, arrayOf("abc")) | ||
assertTrue(res2.getOrNull() as Boolean) | ||
|
||
val res3 = executor.execute(StringMethodsCalls::equalsWithNotEmptyString, arrayOf("abcd")) | ||
assertFalse(res3.getOrNull() as Boolean) | ||
|
||
val res4 = executor.execute(StringMethodsCalls::equalsWithNotEmptyString, arrayOf(null)) | ||
assertTrue(res4.exceptionOrNull() is NullPointerException) | ||
} | ||
} | ||
|
||
@Test | ||
fun testStringStartsWithWithEmptyStringCall() { | ||
withInstrumentation( | ||
BytecodeTransformation.Factory, | ||
StringMethodsCalls::class.java.protectionDomain.codeSource.location.path | ||
) { executor -> | ||
val res1 = executor.execute(StringMethodsCalls::startsWithWithEmptyString, arrayOf("")) | ||
assertTrue(res1.getOrNull() as Boolean) | ||
|
||
val res2 = executor.execute(StringMethodsCalls::startsWithWithEmptyString, arrayOf("abc")) | ||
assertTrue(res2.getOrNull() as Boolean) | ||
|
||
val res3 = executor.execute(StringMethodsCalls::startsWithWithEmptyString, arrayOf(null)) | ||
assertTrue(res3.exceptionOrNull() is NullPointerException) | ||
} | ||
} | ||
|
||
@Test | ||
fun testStringStartsWithWithNotEmptyStringCall() { | ||
withInstrumentation( | ||
BytecodeTransformation.Factory, | ||
StringMethodsCalls::class.java.protectionDomain.codeSource.location.path | ||
) { executor -> | ||
val res1 = executor.execute(StringMethodsCalls::startsWithWithNotEmptyString, arrayOf("")) | ||
assertFalse(res1.getOrNull() as Boolean) | ||
|
||
val res2 = executor.execute(StringMethodsCalls::startsWithWithNotEmptyString, arrayOf("abc")) | ||
assertTrue(res2.getOrNull() as Boolean) | ||
|
||
val res3 = executor.execute(StringMethodsCalls::startsWithWithNotEmptyString, arrayOf("abcd")) | ||
assertTrue(res3.getOrNull() as Boolean) | ||
|
||
val res4 = executor.execute(StringMethodsCalls::startsWithWithNotEmptyString, arrayOf("aabc")) | ||
assertFalse(res4.getOrNull() as Boolean) | ||
|
||
val res5 = executor.execute(StringMethodsCalls::startsWithWithNotEmptyString, arrayOf(null)) | ||
assertTrue(res5.exceptionOrNull() is NullPointerException) | ||
} | ||
} | ||
|
||
@Test | ||
fun testStringEndsWithWithEmptyString() { | ||
withInstrumentation( | ||
BytecodeTransformation.Factory, | ||
StringMethodsCalls::class.java.protectionDomain.codeSource.location.path | ||
) { executor -> | ||
val res1 = executor.execute(StringMethodsCalls::endsWithWithEmptyString, arrayOf("")) | ||
assertTrue(res1.getOrNull() as Boolean) | ||
|
||
val res2 = executor.execute(StringMethodsCalls::endsWithWithEmptyString, arrayOf("abc")) | ||
assertTrue(res2.getOrNull() as Boolean) | ||
|
||
val res3 = executor.execute(StringMethodsCalls::endsWithWithEmptyString, arrayOf(null)) | ||
assertTrue(res3.exceptionOrNull() is NullPointerException) | ||
} | ||
} | ||
|
||
@Test | ||
fun testStringEndsWithWithNotEmptyString() { | ||
withInstrumentation( | ||
BytecodeTransformation.Factory, | ||
StringMethodsCalls::class.java.protectionDomain.codeSource.location.path | ||
) { executor -> | ||
val res1 = executor.execute(StringMethodsCalls::endsWithWithNotEmptyString, arrayOf("")) | ||
assertFalse(res1.getOrNull() as Boolean) | ||
|
||
val res2 = executor.execute(StringMethodsCalls::endsWithWithNotEmptyString, arrayOf("abc")) | ||
assertTrue(res2.getOrNull() as Boolean) | ||
|
||
val res3 = executor.execute(StringMethodsCalls::endsWithWithNotEmptyString, arrayOf("aabc")) | ||
assertTrue(res3.getOrNull() as Boolean) | ||
|
||
val res4 = executor.execute(StringMethodsCalls::endsWithWithNotEmptyString, arrayOf("abcd")) | ||
assertFalse(res4.getOrNull() as Boolean) | ||
|
||
val res5 = executor.execute(StringMethodsCalls::endsWithWithNotEmptyString, arrayOf(null)) | ||
assertTrue(res5.exceptionOrNull() is NullPointerException) | ||
} | ||
} | ||
} |
This file contains hidden or 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
This file contains hidden or 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
44 changes: 44 additions & 0 deletions
44
...kotlin/org/utbot/instrumentation/instrumentation/transformation/BytecodeTransformation.kt
This file contains hidden or 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,44 @@ | ||
package org.utbot.instrumentation.instrumentation.transformation | ||
|
||
import org.utbot.framework.plugin.api.FieldId | ||
import org.utbot.instrumentation.instrumentation.ArgumentList | ||
import org.utbot.instrumentation.instrumentation.Instrumentation | ||
import org.utbot.instrumentation.instrumentation.InvokeInstrumentation | ||
import org.utbot.instrumentation.instrumentation.instrumenter.Instrumenter | ||
import java.security.ProtectionDomain | ||
|
||
/** | ||
* This instrumentation transforms bytecode and delegates invoking a given function to [InvokeInstrumentation]. | ||
*/ | ||
class BytecodeTransformation : Instrumentation<Result<*>> { | ||
private val invokeInstrumentation = InvokeInstrumentation() | ||
|
||
override fun invoke( | ||
clazz: Class<*>, | ||
methodSignature: String, | ||
arguments: ArgumentList, | ||
parameters: Any? | ||
): Result<*> = invokeInstrumentation.invoke(clazz, methodSignature, arguments, parameters) | ||
|
||
override fun getStaticField(fieldId: FieldId): Result<*> = invokeInstrumentation.getStaticField(fieldId) | ||
|
||
override fun transform( | ||
loader: ClassLoader?, | ||
className: String?, | ||
classBeingRedefined: Class<*>?, | ||
protectionDomain: ProtectionDomain?, | ||
classfileBuffer: ByteArray | ||
): ByteArray { | ||
val instrumenter = Instrumenter(classfileBuffer, loader) | ||
|
||
instrumenter.visitClass { writer -> | ||
BytecodeTransformer(writer) | ||
} | ||
|
||
return instrumenter.classByteCode | ||
} | ||
|
||
object Factory : Instrumentation.Factory<Result<*>, BytecodeTransformation> { | ||
override fun create(): BytecodeTransformation = BytecodeTransformation() | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...in/kotlin/org/utbot/instrumentation/instrumentation/transformation/BytecodeTransformer.kt
This file contains hidden or 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,23 @@ | ||
package org.utbot.instrumentation.instrumentation.transformation | ||
|
||
import org.objectweb.asm.ClassVisitor | ||
import org.objectweb.asm.MethodVisitor | ||
import org.utbot.instrumentation.Settings | ||
import org.utbot.instrumentation.instrumentation.transformation.adapters.StringMethodsAdapter | ||
|
||
/** | ||
* Main class for the transformation. | ||
* Bytecode transformations will be combined in this class. | ||
*/ | ||
class BytecodeTransformer(classVisitor: ClassVisitor) : ClassVisitor(Settings.ASM_API, classVisitor) { | ||
override fun visitMethod( | ||
access: Int, | ||
name: String?, | ||
descriptor: String?, | ||
signature: String?, | ||
exceptions: Array<out String>? | ||
): MethodVisitor { | ||
val methodVisitor = cv.visitMethod(access, name, descriptor, signature, exceptions) | ||
return StringMethodsAdapter(api, access, descriptor, methodVisitor) | ||
} | ||
} |
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.
You can use
lateinit var
here instead of delegate