-
-
Notifications
You must be signed in to change notification settings - Fork 5
CModifyConstant
The CModifyConstant
annotation can be used to modify a constant value in a method.
The original constant value is passed as an optional parameter and a new value has to be returned.
A target can be made optional by setting the optional
field to true
. If the target is not found, the injection will be skipped instead of throwing an exception.
Constant Type | Parameter/Return Type |
---|---|
null |
Any Object
|
int |
int |
long |
long |
float |
float |
double |
double |
String |
String |
Type |
Any Class
|
The method signature of the transformer method depends on the target method.
When injecting into a static method, the transformer method also needs to be static and vice versa.
The parameter of the transformer method can be the target constant value, but can also be left out.
The return type always has to be the same as the target constant type.
//Injecting into a static method
@CModifyConstant(method = "method", ...)
public static int transform(final int constantValue)
//Injecting into a non-static method
@CModifyConstant(method = "method", ...)
public int transform(final int constantValue)
//Leaving out the parameter
@CModifyConstant(method = "method", ...)
public int transform()
To target a constant, use the wanted Value
field.
Constant Type | Annotation Name | Example |
---|---|---|
null |
nullValue |
@CModifyConstant(nullValue = true, ...) |
int |
intValue |
@CModifyConstant(intValue = 1, ...) |
long |
longValue |
@CModifyConstant(longValue = 5L, ...) |
float |
floatValue |
@CModifyConstant(floatValue = 7.5F, ...) |
double |
doubleValue |
@CModifyConstant(doubleValue = 12.5D, ...) |
String |
stringValue |
@CModifyConstant(stringValue = "test", ...) |
Type |
typeValue |
@CModifyConstant(typeValue = String.class, ...) |
Only one of the Value
fields can be used at a time.
If multiple Value
fields are used, a runtime exception will be thrown.
When a constant is found multiple times, the ordinal
field can be used to choose the target.
If the ordinal
field is not set, all targets will be used.
Check out CSlice for more information about how slices work.
The value
field can be an array of strings to target multiple methods.
The transformer method needs to be compatible with all targeted methods.
@CModifyConstant(method = {"method1", "method2"}, ...)
public void transform()
Original method:
public void method() {
System.out.println("Hello World");
}
Transformer method:
@CModifyConstant(method = "method", stringValue = "Hello World")
public void transform(final String constant) {
return "Hello World!
}
Injected code:
public void method() {
System.out.println(transform("Hello World"));
}