INRIA / INRIA/spoon

Ho to add variable in class using spoon

Open
#4,579 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2k
Forks
392
Avg merge
11h 24m
Merged PRs (30d)
36

Description

I want to remove some code from the class, identify the classname from it and create a variable for same class.
I am able to do the first 2 parts but not able to add the code in CtClass as a variable. Please help.

**Current code:**
```
public class BankProcessor {
public Collection getAccountInformation(String customerId){
Context context = new InitialContext();
ArrayList accountBeanList = new ArrayList();
CustomerHomeLocal customerHome = (CustomerHomeLocal)context.lookup(CustomerHomeLocal.JNDI_NAME);
CustomerLocal customer = customerHome.findByPrimaryKey(customerId);
Iterator accountIterator = customer.getAccounts().iterator();
}
}
```

**Expected Code**
```
public class BankProcessor {
@Autowired
private CustomerHomeLocal customerHome;

public Collection getAccountInformation(String customerId){
ArrayList accountBeanList = new ArrayList();
CustomerLocal customer = customerHome.findByPrimaryKey(customerId);
Iterator accountIterator = customer.getAccounts().iterator();
}
}
```
I have tried creating codeSnippet from
> classfactory.Code().createCodeSnippetStatement("CustomerHomeLocal customerHome;")

but not able to add it to
> CtClass

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.