Like Collections#unmodifiableCollection, but for objects.
Unmodifiable-wrapper is an annotation processor for generating an unmodifiable wrapper.
The unmodifiable wrapper overrides all setter-methods - throws UnsupportedOperationException
.
Required:
- Class must be not-abstract
- Class must be not-final.
- Class must be top-level.
- Class must have not-private no-arguments constructor.
Add this code to dependencies
section in your build.gradle
:
compileOnly 'dev.alexengrig:metter:0.1.1'
annotationProcessor 'dev.alexengrig:metter:0.1.1'
Add @UnmodifiableWrapper
to your class:
@UnmodifiableWrapper
// not-abstract and not-final
class Domain {
// fields
// factory-method
static Domain unmodifiable(Domain domain) {
// generated class
return new UnmodifiableDomain(domain);
}
// not-private no-args constructor
Domain() {
}
// getters and setters
}
Usage:
class Main {
public static void main(String[] args) {
Domain domain = ...
Domain unmodifiableDomain = Domain.unmodifiable(domain);
mutate(unmodifiableDomain);
}
static void mutate(Domain domain) {
domain.setXXX(...)// throw UnsupportedOperationException
}
}
This project is licensed under Apache License, version 2.0.