How to add statements to a PsiMethod object?
Hi, I'm trying to create a method object using a PsiElementFactory.
I've been able to create the method, and add parameters and modifiers but I can't find a way to add PsiExpression statements.
Here's the code thus far:
PsiMethod exportMethod(GUID id) { //create method object PsiType type = factory.createTypeFromText(store.getName(getInstance(id,'TypeElement')),null); PsiMethod m = factory.createMethod(store.getName(id),type); //assign modifiers m.getModifierList().setModifierProperty('private', false); Object modifier = getInstance(id,'ModifierList'); if(modifier!=null) {String text = store.getName(modifier); String[] modifiers = text.split(' '); for (String mod : modifiers) m.getModifierList().setModifierProperty(mod, true); //I add the modifiers through setModifierProperty() } //set the parameterList for the method PsiParameterList parameters = exportParameters(id); m.getParameterList().replace(parameters); //I add the parameters using replace() //add statements to the method (in progress) PsiCodeBlock block = m.getBody(); PsiStatement[] blockExpressions = block.getStatements(); PsiStatement[] expressions = exportStatements(id); return m; }
The problem I'm finding is that
a) There is no setExpressions() method.
b) I can't replace() a PsiStatement[] object.
c) While I can replace() the PsiCodeBlock, I can't assign expressions to a newly created one either.
Can anyone point me in the right direction?
Thanks in advance.
Please sign in to leave a comment.
But the code block in Java consists of statements, not expressions.
For adding a set of statements you may use body.addRangeAfter() method (body.getStatements() should be empty - so replace() not needed).