This plugin was inspired by the project Immutables.
- If you can use annotations in your project, then I recommend you to take a look at Immutables.
- If you need something really simple and rather generating cource code in place, that you can easily modify, then take a look at this plugin.
This plugin makes a class immutable by applying the following changes:
- Class modifier
finalis added if not present. - Fields modifier
finalis added if not present. - Constructor/s visibility changed to
private. The class must have at least one constructor. - Generation of static constructor (method name:
of) for each private constructor. - Generation of
getters. - Generation of
withers(withXYZMethods).
This plugin can be downloaded from the JetBrains Plugins Repository: Make Immutable
- Precondition: The class must have at least one constructor.
- Expectation:
- The methods are generated only if there is no methods matching the signature.
gettersandwithersare generated based on the class fields and constructors arguments (see example below)
- Usage: Code | Generate (Alt + Insert) | Make Immutable
Let's assume you have the following class (no getters and setters):
public class SomeClass<T, R> {
private String value1;
private T value2;
private R value3;
private boolean value4;
public SomeClass(String value1a, String value1b, T value2, R value3, boolean value4) {
this.value1 = value1a + value1b;
this.value2 = value2;
this.value3 = value3;
this.value4 = value4;
}
}Then, if you run the plugin 'Make Immutable' you would obtain the following result:
public final class SomeClass<T, R> {
private final String value1;
private final T value2;
private final R value3;
private final boolean value4;
private SomeClass(String value1a, String value1b, T value2, R value3, boolean value4) {
this.value1 = value1a + value1b;
this.value2 = value2;
this.value3 = value3;
this.value4 = value4;
}
public static <T, R> SomeClass of(String value1a, String value1b, T value2, R value3, boolean value4) {
return new SomeClass<>(value1a, value1b, value2, value3, value4);
}
public String getValue1() {
return this.value1;
}
public T getValue2() {
return this.value2;
}
public R getValue3() {
return this.value3;
}
public boolean isValue4() {
return this.value4;
}
public String getValue1a() {
throw new RuntimeException("This method must be implemented."); // TODO
}
public String getValue1b() {
throw new RuntimeException("This method must be implemented."); // TODO
}
public SomeClass withValue1a(String value1a) {
return of(value1a, getValue1b(), getValue2(), getValue3(), isValue4());
}
public SomeClass withValue1b(String value1b) {
return of(getValue1a(), value1b, getValue2(), getValue3(), isValue4());
}
public SomeClass withValue2(T value2) {
return of(getValue1a(), getValue1b(), value2, getValue3(), isValue4());
}
public SomeClass withValue3(R value3) {
return of(getValue1a(), getValue1b(), getValue2(), value3, isValue4());
}
public SomeClass withValue4(boolean value4) {
return of(getValue1a(), getValue1b(), getValue2(), getValue3(), value4);
}
}Copyright 2018 by Grebiel José Ifill Brito
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.