Function inlining
- Dominant language
- Java
- Stars
- 2k
- Forks
- 392
- Avg merge
- 11h 24m
- Merged PRs (30d)
- 36
Description
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):
CtMethod targetMethodDeclaration;
CtMethod extractedMethodDeclaration;
CtInvocation invocation;
Launcher launcher
It will look like:
```
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.
_Originally posted by @lyriccoder in https://github.com/INRIA/spoon/issues/3251#issuecomment-784334568_
Contributor guide
Research direction
Start with the Spoon entry points named in the report: Launcher, CtModel, CtMethod, CtInvocation, CtBlock, and CtPackage.clone(). Trace how model mutations are printed or persisted instead of relying on getOriginalSourceFragment(). Done would require a defined approach for inlining return-valued and void methods, cloning models, and saving independent transformed Java files.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- devtools, tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100