java.lang.AssertionError thrown by compiler when adding generated method with parameters
I've managed to get generated, functional parameters by creating a MethodSymbol, using that with TreeMaker::MethodDef(MethodSymbol, JCTree.JCBlock) to create a new JCTree.JCMethodDecl, and them iterating over the parameters to set their adr to 0 (which is accepted, and nothing in the spec mentioned by @Piotr Wilkin leads me to believe this shouldn't be valid).
// maker is a TreeMaker instance
// sym is a Symbol.MethodSymbol instance
// body is a JCTree.JCBlock instance
JCTree.JCMethodDecl decl = maker.MethodDef(sym, body);
decl.params.forEach(p -> {
p.sym.adr = 0;
});
I suppose the reason this works, is because javac will not attempt to create new symbols (since one already exists), and the VarSymbols (sym field of JCTree.JCVariableDecl) are mutable. Moreover, searching the javac code shows little attempt to override this anyway.
Actually, the JavaDoc for the Symbol
class tells you what the adr
field is used for:
The variable's address. Used for different purposes during flow analysis, translation and code generation. Flow analysis: If this is a blank final or local variable, its sequence number. Translation: If this is a private field, its access number. Code generation: If this is a local variable, its logical slot number.
Its default value is -1
, which is basically the base-types equivalent of null
, so what you're getting is semantically (although not technically) a NullPointerException
. You need to fill in the parameter position according to the above documentation (and probably some reverse engineering as well, since you're using internal APIs).