-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d162ee6
commit aa78b75
Showing
3 changed files
with
212 additions
and
1 deletion.
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleToJavaCodeBuilder.java
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,111 @@ | ||
package sootup.jimple.frontend; | ||
|
||
import java.util.*; | ||
|
||
public class JimpleToJavaCodeBuilder { | ||
|
||
private final List<JavaCode> jimpleToJavaCodeObjects = new ArrayList<>(); | ||
|
||
public static JimpleToJavaCodeBuilder builder() { | ||
return new JimpleToJavaCodeBuilder(); | ||
} | ||
|
||
public List<JavaCode> build() { | ||
return jimpleToJavaCodeObjects; | ||
} | ||
|
||
public JimpleToJavaCodeBuilder initObjects(String thisRefClassType) { | ||
InitObjects initObjects = new InitObjects(thisRefClassType); | ||
jimpleToJavaCodeObjects.add(initObjects); | ||
return this; | ||
} | ||
|
||
public JimpleToJavaCodeBuilder addLocal(String name, String type) { | ||
Local local = new Local(name, type); | ||
jimpleToJavaCodeObjects.add(local); | ||
return this; | ||
} | ||
|
||
public JimpleToJavaCodeBuilder addAssignStmt(String localName, String value) { | ||
AssignStmt assignStmt = new AssignStmt(localName, value); | ||
jimpleToJavaCodeObjects.add(assignStmt); | ||
return this; | ||
} | ||
|
||
public JimpleToJavaCodeBuilder addIdentityStmt(String localName, String type) { | ||
IdentityStmt identityStmt = new IdentityStmt(localName, type); | ||
jimpleToJavaCodeObjects.add(identityStmt); | ||
return this; | ||
} | ||
|
||
// Base JavaObject interface for all elements | ||
public interface JavaCode { | ||
String generateCode(); | ||
} | ||
|
||
// Represents a local variable | ||
public static class Local implements JavaCode { | ||
private final String name; | ||
private final String type; | ||
|
||
public Local(String name, String type) { | ||
this.name = name; | ||
this.type = type; | ||
} | ||
|
||
@Override | ||
public String generateCode() { | ||
return "Local " + name + " = JavaJimple.newLocal(\"" + name + "\", factory.getClassType(\"" + type + "\"));"; | ||
} | ||
} | ||
|
||
// Represents an identity statement | ||
public static class IdentityStmt implements JavaCode { | ||
private final String localName; | ||
private final String type; | ||
|
||
public IdentityStmt(String localName, String type) { | ||
this.localName = localName; | ||
this.type = type; | ||
} | ||
|
||
@Override | ||
public String generateCode() { | ||
return "FallsThroughStmt startingStmt = JavaJimple.newIdentityStmt(" + | ||
localName + ", JavaJimple.newThisRef(factory.getClassType(\"" + type + "\")), noStmtPositionInfo);"; | ||
} | ||
} | ||
|
||
// Represents an assignment statement | ||
public static class AssignStmt implements JavaCode { | ||
private final String localName; | ||
private final String value; | ||
|
||
public AssignStmt(String localName, String value) { | ||
this.localName = localName; | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public String generateCode() { | ||
return "FallsThroughStmt stmt = JavaJimple.newAssignStmt(" + | ||
localName + ", " + value + ", noStmtPositionInfo);"; | ||
} | ||
} | ||
|
||
private static class InitObjects implements JavaCode { | ||
|
||
private final String thisRefClassType; | ||
|
||
private InitObjects(String thisRefClassType) { | ||
this.thisRefClassType = thisRefClassType; | ||
} | ||
|
||
@Override | ||
public String generateCode() { | ||
return "JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance();\n" + | ||
"StmtPositionInfo noStmtPositionInfo = StmtPositionInfo.getNoStmtPositionInfo();\n" + | ||
"IdentityRef identityRef = JavaJimple.newThisRef(factory.getClassType(\"" + thisRefClassType + "\");"; | ||
} | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleToJavaCodeBuilderTest.java
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,43 @@ | ||
package sootup.jimple.frontend; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
public class JimpleToJavaCodeBuilderTest { | ||
|
||
@Test | ||
public void testJimpleToJavaCode() { | ||
// Example Input | ||
String input = "r0 := @this: Test;\n" + | ||
"i1 = 5;\n" + | ||
"i2 = 0;"; | ||
|
||
// Using the builder | ||
List<JimpleToJavaCodeBuilder.JavaCode> javaObjects = JimpleToJavaCodeBuilder.builder() | ||
.initObjects("Test") | ||
.addLocal("r0", "Test") | ||
.addIdentityStmt("r0", "Test") | ||
.addLocal("i1", "int") | ||
.addAssignStmt("i1", "IntConstant.getInstance(5)") | ||
.addLocal("i2", "int") | ||
.addAssignStmt("i2", "IntConstant.getInstance(0)") | ||
.build(); | ||
|
||
// Generate the code | ||
for (JimpleToJavaCodeBuilder.JavaCode javaCode : javaObjects) { | ||
System.out.println(javaCode.generateCode()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testJimpleToJavaCode2() { | ||
String input = "\"r0 := @this: AbstractClass\",\n" + | ||
" \"r1 = new AbstractClass\",\n" + | ||
" \"specialinvoke r1.<AbstractClass: void <init>()>()\",\n" + | ||
" \"virtualinvoke r1.<A: void a()>()\",\n" + | ||
" \"return\""; | ||
|
||
|
||
} | ||
} |