Skip to content

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 7 commits into from
Aug 2, 2023

Conversation

egiptipavel
Copy link
Collaborator

@egiptipavel egiptipavel commented Jul 25, 2023

Description

Bytecode transformation of the if statement with call of the String.equals, String.startsWith or String.endsWith method with a constant string into a sequence of comparisons of each char of the string with each char of the constant string.

For example, this code

if (a.equals("abc")) {
    // block of code
}

at the bytecode level will be transformed to this

if (a.length() == 3) {
    if (a.charAt(0) == 'a') {
        if (a.charAt(1) == 'b') {
            if (a.charAt(2) == 'c') {
                // block of code
            }
        }
    }
}

How to test

Automated tests

Run org.utbot.examples.TestBytecodeTransformation in utbot-instrumentation-tests module

Manual tests

public class Transformation {
    public boolean test1(String a) {
        if (a.equals(null)) {
            return true;
        }
        return false;
    }

    public boolean test2(String a) {
        if (a.equals("")) {
            return true;
        }
        return false;
    }

    public boolean test3(String a) {
        if (a.equals("abc")) {
            return true;
        }
        return false;
    }

    public boolean test4(String a) {
        if (a.startsWith("abc")) {
            return true;
        }
        return false;
    }

    public boolean test5(String a) {
        if (a.endsWith("def")) {
            return true;
        }
        return false;
    }

    public boolean test6(String a) {
        if (a.startsWith("")) {
            return true;
        }
        return false;
    }

    public boolean test7(String a) {
        if (a.endsWith("")) {
            return true;
        }
        return false;
    }
}

Self-check list

Check off the item if the statement is true. Hint: [x] is a marked item.

Please do not delete the list or its items.

  • I've set the proper labels for my PR (at least, for category and component).
  • PR title and description are clear and intelligible.
  • I've added enough comments to my code, particularly in hard-to-understand areas.
  • The functionality I've repaired, changed or added is covered with automated tests.
  • Manual tests have been provided optionally.
  • The documentation for the functionality I've been working on is up-to-date.

@egiptipavel egiptipavel added ctg-enhancement New feature, improvement or change request comp-instrumented-process Issue is related to Instrumented process labels Jul 25, 2023
@egiptipavel egiptipavel requested a review from Markoutte July 25, 2023 11:11
@egiptipavel egiptipavel self-assigned this Jul 25, 2023
@egiptipavel egiptipavel marked this pull request as draft July 25, 2023 11:25
@egiptipavel egiptipavel changed the title Add bytecode transformation for if statement with call of String.equals Add bytecode transformation for if statement with call of String methods Jul 25, 2023
@egiptipavel egiptipavel marked this pull request as ready for review July 26, 2023 15:22

override fun build(writer: ClassWriter): BytecodeTransformer = BytecodeTransformer(writer)
})
if (UtSettings.useBytecodeTransformation) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instrumentation has no access to UtSettings. Because of this it fails to create any test

@@ -19,6 +20,7 @@ private val logger = getLogger<DynamicClassTransformer>()
class DynamicClassTransformer : ClassFileTransformer {
lateinit var transformer: ClassFileTransformer

var useBytecodeTransformation by Delegates.notNull<Boolean>()
Copy link
Collaborator

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

@egiptipavel egiptipavel merged commit a5eb0a6 into main Aug 2, 2023
@egiptipavel egiptipavel deleted the egiptipavel/code-transformation branch August 2, 2023 07:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp-instrumented-process Issue is related to Instrumented process ctg-enhancement New feature, improvement or change request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants