-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Inline expressions do not have access to classes loaded in Recaf…
…'s workspace
- Loading branch information
Showing
18 changed files
with
463 additions
and
293 deletions.
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
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
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
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
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
2 changes: 1 addition & 1 deletion
2
...arse/bytecode/JavassistASMTranslator.java → ...ecaf/compiler/JavassistASMTranslator.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
30 changes: 30 additions & 0 deletions
30
src/main/java/me/coley/recaf/compiler/JavassistCodeGen.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,30 @@ | ||
package me.coley.recaf.compiler; | ||
|
||
import javassist.ClassPool; | ||
import javassist.CtClass; | ||
import javassist.bytecode.Bytecode; | ||
import javassist.compiler.JvstCodeGen; | ||
import me.coley.recaf.workspace.Workspace; | ||
|
||
/** | ||
* Modified code generator for Javassist to pull information from Recaf. | ||
* | ||
* @author Matt | ||
*/ | ||
public class JavassistCodeGen extends JvstCodeGen { | ||
/** | ||
* @param workspace | ||
* Workspace to pull class information from. | ||
* @param bytecode | ||
* Target generated bytecode wrapper. | ||
* @param declaringClass | ||
* Class containing the method our expression resides in. | ||
* @param classPool | ||
* Class pool to use. | ||
*/ | ||
public JavassistCodeGen(Workspace workspace, Bytecode bytecode, CtClass declaringClass, ClassPool classPool) { | ||
super(bytecode, declaringClass, classPool); | ||
setTypeChecker(new JavassistTypeChecker(workspace, declaringClass, classPool, this)); | ||
resolver = new JavassistMemberResolver(workspace, classPool); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/me/coley/recaf/compiler/JavassistCompilationResult.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,39 @@ | ||
package me.coley.recaf.compiler; | ||
|
||
import javassist.bytecode.Bytecode; | ||
import javassist.compiler.SymbolTable; | ||
|
||
/** | ||
* Compilation results. | ||
* | ||
* @author Matt | ||
*/ | ||
public class JavassistCompilationResult { | ||
private final Bytecode bytecode; | ||
private final SymbolTable symbols; | ||
|
||
/** | ||
* @param bytecode | ||
* Generated bytecode. | ||
* @param symbols | ||
* Generated symbols, if any. | ||
*/ | ||
public JavassistCompilationResult(Bytecode bytecode, SymbolTable symbols) { | ||
this.bytecode = bytecode; | ||
this.symbols = symbols; | ||
} | ||
|
||
/** | ||
* @return Generated bytecode. | ||
*/ | ||
public Bytecode getBytecode() { | ||
return bytecode; | ||
} | ||
|
||
/** | ||
* @return Generated symbols, if any. | ||
*/ | ||
public SymbolTable getSymbols() { | ||
return symbols; | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
src/main/java/me/coley/recaf/compiler/JavassistCompiler.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,107 @@ | ||
package me.coley.recaf.compiler; | ||
|
||
import javassist.*; | ||
import javassist.bytecode.LocalVariableAttribute; | ||
import javassist.compiler.*; | ||
import me.coley.recaf.parse.bytecode.VariableNameCache; | ||
import org.objectweb.asm.tree.LocalVariableNode; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Javassist compiler utility. | ||
* | ||
* @author Matt | ||
*/ | ||
public class JavassistCompiler { | ||
|
||
/** | ||
* Compile an independent method body. | ||
* | ||
* @param declaring | ||
* Declaring type that contains the method. | ||
* @param src | ||
* Source of the method declaration. | ||
* | ||
* @return Compiled method. | ||
* | ||
* @throws CannotCompileException | ||
* When a compilation error occurred. | ||
*/ | ||
public static CtMethod compileMethod(CtClass declaring, String src) throws CannotCompileException { | ||
try { | ||
Javac compiler = new Javac(declaring); | ||
CtMember obj = compiler.compile(src); | ||
if (obj instanceof CtMethod) | ||
return (CtMethod) obj; | ||
} catch (CompileError e) { | ||
throw new CannotCompileException(e); | ||
} | ||
throw new CannotCompileException("Not a method"); | ||
} | ||
|
||
/** | ||
* Compile an independent method body. | ||
* | ||
* @param declaring | ||
* Declaring type that contains the method. | ||
* @param containerMethod | ||
* Declaring method that will contain the expression. | ||
* @param expression | ||
* Source of the expression. | ||
* @param existingVars | ||
* Variable information to populate. | ||
* @param varCache | ||
* Variable name and index information. | ||
* | ||
* @return Compiled expression. | ||
* | ||
* @throws CannotCompileException | ||
* When a compilation error occurred. | ||
*/ | ||
public static JavassistCompilationResult compileExpression(CtClass declaring, CtBehavior containerMethod, | ||
String expression, List<LocalVariableNode> existingVars, | ||
VariableNameCache varCache) | ||
throws CannotCompileException { | ||
try { | ||
JavassistExpressionJavac compiler = new JavassistExpressionJavac(declaring, varCache); | ||
populateVariables(compiler, existingVars); | ||
populateVariables(compiler, containerMethod); | ||
compiler.compileStmnt(expression); | ||
return new JavassistCompilationResult(compiler.getGeneratedBytecode(), compiler.getLastCompiledSymbols()); | ||
} catch (CompileError e) { | ||
throw new CannotCompileException(e); | ||
} | ||
} | ||
|
||
private static void populateVariables(JavassistExpressionJavac compiler, List<LocalVariableNode> variables) { | ||
JvstCodeGen gen = compiler.getGen(); | ||
SymbolTable symbolTable = compiler.getRootSTable(); | ||
for (LocalVariableNode variable : variables) { | ||
try { | ||
gen.recordVariable(variable.desc, variable.name, variable.index, symbolTable); | ||
} catch (CompileError ignored) { | ||
// ignored | ||
} | ||
} | ||
} | ||
|
||
private static void populateVariables(JavassistExpressionJavac compiler, CtBehavior containerMethod) { | ||
LocalVariableAttribute variables = (LocalVariableAttribute) | ||
containerMethod.getMethodInfo().getCodeAttribute().getAttribute(LocalVariableAttribute.tag); | ||
if (variables != null) { | ||
JvstCodeGen gen = compiler.getGen(); | ||
SymbolTable symbolTable = compiler.getRootSTable(); | ||
for (int i = 0; i < variables.tableLength(); i++) { | ||
int index = variables.index(i); | ||
String signature = variables.signature(i); | ||
String name = variables.variableName(i); | ||
try { | ||
gen.recordVariable(signature, name, index, symbolTable); | ||
} catch (CompileError ignored) { | ||
// ignored | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.