eclipse-jdt / eclipse-jdt/eclipse.jdt.ui

FR extract to lamda-function

Open
#40 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Java
Stars
59
Forks
127
Avg merge
22h 47m
Merged PRs (30d)
28

Description

Having a class like this:

```
@Transactional
@RestController("check/health")
public class ServerHealthIcons {
/**
* The entitymanager to use, never null.
*/
@PersistenceContext
private final EntityManager entityManager = null;

@GetMapping("bad/{shcid}.png")
public void bad(@PathVariable long shcid, HttpServletRequest request, HttpServletResponse response)
throws IOException {
ServerHealthCheck shc = entityManager.find(ServerHealthCheck.class, shcid);
byte[] iconPng = shc.getBadIconPng();
response.setContentLength(iconPng.length);
response.setContentType("image/png");
ServletOutputStream out = response.getOutputStream();
out.write(iconPng);
out.close();
}

@GetMapping("warn/{shcid}.png")
public void warn(@PathVariable long shcid, HttpServletRequest request, HttpServletResponse response)
throws IOException {
ServerHealthCheck shc = entityManager.find(ServerHealthCheck.class, shcid);
byte[] iconPng = shc.getBadIconPng();
response.setContentLength(iconPng.length);
response.setContentType("image/png");
ServletOutputStream out = response.getOutputStream();
out.write(iconPng);
out.close();
}

@GetMapping("good/{shcid}.png")
public void good(@PathVariable long shcid, HttpServletRequest request, HttpServletResponse response)
throws IOException {
ServerHealthCheck shc = entityManager.find(ServerHealthCheck.class, shcid);
byte[] iconPng = shc.getGoodIconPng();
response.setContentLength(iconPng.length);
response.setContentType("image/png");
ServletOutputStream out = response.getOutputStream();
out.write(iconPng);
out.close();
}
}
```
We have about 3 times the mostly exact same method body.

The only difference is the initialization of the iconPng-variable. So we have to extract the iconPng initialization first.

This is the Menu:
![grafik](https://user-images.githubusercontent.com/6880636/167257772-e8f42cc4-a293-4d53-9988-7c83c62599b3.png)

Unfortunately we can only extract as
1. Local variable
2. Method

What I miss here is the possibility to extract the variable to a lamda-function.

The extraction should only be available for one input and one output (two input for BiFunction if you like, consumer, supplier respectievly).

This should the extraction create:

This Line:

`byte[] iconPng = shc.getBadIconPng();`

Should be extracted to this line:

```
Function supply = ServerHealthCheck::getBadIconPng;
byte[] iconPng = supply.apply(shc);
```

As soon as the FR is implemented, it will be an ease to transform the code above to this:

```
@Transactional
@RestController("check/health")
public class ServerHealthIcons {
/**
* The entitymanager to use, never null.
*/
@PersistenceContext
private final EntityManager entityManager = null;

@GetMapping("bad/{shcid}.png")
public void bad(@PathVariable long shcid, HttpServletRequest request, HttpServletResponse response)
throws IOException {
extracted(shcid, response, ServerHealthCheck::getBadIconPng);
}

@GetMapping("warn/{shcid}.png")
public void warn(@PathVariable long shcid, HttpServletRequest request, HttpServletResponse response)
throws IOException {
extracted(shcid, response, ServerHealthCheck::getWarningIconPng);
}

@GetMapping("good/{shcid}.png")
public void good(@PathVariable long shcid, HttpServletRequest request, HttpServletResponse response)
throws IOException {
extracted(shcid, response, ServerHealthCheck::getGoodIconPng);
}

private void extracted(long shcid, HttpServletResponse response, Function supply)
throws IOException {
ServerHealthCheck shc = entityManager.find(ServerHealthCheck.class, shcid);
byte[] iconPng = supply.apply(shc);
response.setContentLength(iconPng.length);
response.setContentType("image/png");
ServletOutputStream out = response.getOutputStream();
out.write(iconPng);
out.close();
}
}

```

Thanks.

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.