You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class Test {
void targetMethodDeclaration() {
int e = 5;
int res = extractedMethodDeclaration(e);
return res;
}
int extractedMethodDeclaration(int b) {
int a = 0;
++a;
return a;
}
}
Is it possible to inline extractedMethodDeclaration into targetMethodDeclaration:
class Test {
void targetMethodDeclaration() {
int e = 0;
int a = 0;
++a;
int res = a;
return res ;
}
void extractedMethodDeclaration() {
int a = 0;
++a;
return a;
}
}
Or at least can I inline it with void function? Then I can add functionlity which can insert function with return types.
I see that I can add statements, but I can't apply the changes. Also, it is possible to apply changes on the cloned model, but in different places to get different Java files?
public class InlineOpportunity {
protected final CtModel astTree;
private CtMethod targetMethodDeclaration;
private CtMethod extractedMethodDeclaration;
private CtInvocation invocation;
...
}
Suppose I have 1 model and all methods (actually, each opportunity has a link to a single model):
for (InlineOpportunity opp: opportunities) {
// Do i need to copy it? How can i Do it?
// CtPackage cloneOfAstTree = opp.getAstTree().getRootPackage().clone();
CtBlock block = opp.getTargetMethodDeclaration().getBody();
int counter = 1;
int statementsSize = block.getStatements().size();
for (CtStatement st : block.getStatements()) {
while ((st != opp.getInvocation()) && (counter < statementsSize)) {
++counter;
}
if (counter == statementsSize) {
break;
}
CtBlock block_to_insert = opp.getExtractedMethodDeclaration().getBody();
for (CtStatement inserted_st: block_to_insert.getStatements()) {
st.insertAfter(inserted_st);
}
st.delete(); // If I look at opp.getTargetMethodDeclaration().getBody line, i see that change have been made
String s = opp.getAstTree().getRootPackage().getOriginalSourceFragment().getSourceCode(); // Here I see that the changes are not applied for the whole model (class)
break;
}
}
How can I apply chages? How can I make a clone of a model to apply different changes (I have lots of invocations)? I need to save them into a different file.
Hello @lyriccoder, if each element in a list of InlineOpportunity should represent a separate file, a rough implementation of inlining in assignment statements is:
for (InlineOpportunityopp: opportunities) {
if(opp.getInvocation().getParent() instanceofCtRHSReceiver) {
// stash original elementCtInvocationclonedInvocation = opp.getInvocation().clone();
// clone replacing elementsList<CtStatement> statements = opp.getExtractedMethodDeclaration().getBody().getStatements();
List<CtStatement> clonedStatements = newArrayList<>();
for (CtStatements : statements) {
clonedStatements.add(s.clone());
}
// make the inliningintsize = clonedStatements.size();
for(inti = 0; i<size-1;i++){
((CtStatement)opp.getInvocation().getParent()).insertBefore(clonedStatements.get(i));
}
CtExpressionreturnExpression = ((CtReturn)clonedStatements.get(size-1)).getReturnedExpression();
opp.getInvocation().replace(returnExpression);
// print the resulting classSystem.out.println(ctClass);
// reset modelreturnExpression.replace(clonedInvocation);
for (CtStatements : clonedStatements) {
s.delete();
}
}
}
To reset the model back to its original state, element clones are inserted into the model and deleted at the end of each loop and the stashed original element is put back. You get the prettyprint of the changed model by calling toString on it.
Hi SPOON contributors. How can I make inline of the function?
I found a way to search for the folllowing data (so, I can find all those objects via your framework):
It will look like:
Is it possible to inline extractedMethodDeclaration into targetMethodDeclaration:
Or at least can I inline it with void function? Then I can add functionlity which can insert function with return types.
I see that I can add statements, but I can't apply the changes. Also, it is possible to apply changes on the cloned model, but in different places to get different Java files?
Suppose I have 1 model and all methods (actually, each opportunity has a link to a single model):
How can I apply chages? How can I make a clone of a model to apply different changes (I have lots of invocations)? I need to save them into a different file.
Originally posted by @lyriccoder in #3251 (comment)
The text was updated successfully, but these errors were encountered: