Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix to Issue #33 - pushing the verbose ImmutableBy...
- Loading branch information
1 parent
9aef577
commit ade8682
Showing
1 changed file
with
23 additions
and
3 deletions.
There are no files selected for viewing
26 changes: 23 additions & 3 deletions
26
...tydetector/benchmarks/ImmutableByHavingOnlyAPrivateConstructorUsingTheBuilderPattern.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,25 @@ | ||
package org.mutabilitydetector.benchmarks; | ||
|
||
public class ImmutableByHavingOnlyAPrivateConstructorUsingTheBuilderPattern { | ||
|
||
} | ||
private final String field; | ||
|
||
// usual method of making a class immutable | ||
// - make its constructor private: ref EffectiveJava | ||
private ImmutableByHavingOnlyAPrivateConstructorUsingTheBuilderPattern (String field) { | ||
this.field = field; | ||
} | ||
|
||
public String getField() { | ||
return field; | ||
} | ||
|
||
// inner Builder class | ||
public static class Builder { | ||
public ImmutableByHavingOnlyAPrivateConstructorUsingTheBuilderPattern build() { | ||
// this new OnlyPrivateConstructors() is fooling mutability detector | ||
// it thinks OnlyPrivateConstructors() is no longer immutable due to the | ||
// ability to call new to create an instance of OnlyPrivateConstructors. | ||
return new ImmutableByHavingOnlyAPrivateConstructorUsingTheBuilderPattern("hi"); | ||
} | ||
} | ||
} |