pojo-agent
A tool which enhances your pojo, powered by java-agent.
maven
<dependency>
<groupId>io.vproxygroupId>
<artifactId>pojo-agent-apiartifactId>
<version>1.0.0version>
dependency>
gradle
implementation group: 'io.vproxy', name: 'pojo-agent-api', version: '1.0.0'
current supported functions
- Helps you know that a setter is invoked or not.
build
./gradlew clean jar
The agent is in build/libs/pojo-agent.jar
.
You can also get the pre-built agent in the release pages.
how to use
Works on java 11, 17, and kotlin.
step 1
Add @Pojo
annotation on you pojo:
@Pojo
public class MyEntity {
private String id;
// getters ...
// setters ...
}
or you can use data class
if you are using kotlin:
@Pojo
data class MyEntity(
var id: String = ""
)
// getters and setters are generated by kotlin compiler
step 2
Add @PojoCaller
on the method which requires enhancement:
@PojoCaller
public void updateMyEntity(MyEntity entity) {
// ...
}
step 3
Use PojoAgent
helper class in the @PojoCaller
functions:
// check whether a field is set
PojoAgent.fieldIsSet(entity.getId())
// unset the field
PojoAgent.unsetField(entity.getId())
// then you will get `false` from `fieldIsSet` call after calling `unsetField`
// However note that the value of this property will not be modified, you can still get correct result from `getId()`
step 4
Add -javaagent:${path-to-pojo-agent.jar}
to your java
command.
If you are using java 17, you may need to add extra arguments:
--add-opens java.base/jdk.internal.org.objectweb.asm=ALL-UNNAMED --add-opens java.base/jdk.internal.org.objectweb.asm.tree=ALL-UNNAMED
sample
The sample code is in ./sample/src/main/
Run:
./gradlew clean sample
./gradlew clean ktsample
to see the output.