Skip to content

Commit

Permalink
fix spotbugs post formatting (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
sudaraka94 authored May 5, 2024
1 parent 626efe9 commit 8d3cac0
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions content/posts/2024-04-29-handling-ei-expose-rep/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ Now you can see that we are exposing the `myList` object via the `getMyList()` m

### The Fix
We can mainly fix this using two approaches.
1. Return an unmodifiable list from the `getMyList()` method. You can change the getMyList() method as follows.
```java
public List<String> getMyList() {
return Collections.unmodifiableList(myList);
}
```
2. Return a copy of the list from the `getMyList()` method. You can change the getMyList() method as follows.
```java
public List<String> getMyList() {
return List.copyOf(myList);
}
```
- Return an unmodifiable list from the `getMyList()` method. You can change the getMyList() method as follows.
```java
public List<String> getMyList() {
return Collections.unmodifiableList(myList);
}
```
- Return a copy of the list from the `getMyList()` method. You can change the getMyList() method as follows.
```java
public List<String> getMyList() {
return List.copyOf(myList);
}
```

## EI_EXPOSE_REP2

Expand All @@ -76,18 +76,18 @@ encapsulation principle and a security risk.

The fix for this warning is slightly different from the previous warning. You can fix this by,

1. Creating a copy of the list when assigning it to the `myList` variable.
```java
public MyClass(List<String> myList) {
this.myList = List.copyOf(myList);
}
```
2. Creating an unmodifiable list when assigning it to the `myList` variable.
```java
public MyClass(List<String> myList) {
this.myList = Collections.unmodifiableList(myList);
}
```
- Creating a copy of the list when assigning it to the `myList` variable.
```java
public MyClass(List<String> myList) {
this.myList = List.copyOf(myList);
}
```
- Creating an unmodifiable list when assigning it to the `myList` variable.
```java
public MyClass(List<String> myList) {
this.myList = Collections.unmodifiableList(myList);
}
```

## Conclusion

Expand Down

0 comments on commit 8d3cac0

Please sign in to comment.