-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Refactor ReflectivePropertyOperator to support unencapsulat…
…ed field
- Loading branch information
1 parent
cbfed7f
commit f74da77
Showing
8 changed files
with
562 additions
and
43 deletions.
There are no files selected for viewing
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
111 changes: 111 additions & 0 deletions
111
...re/src/main/java/cn/crane4j/core/support/reflect/ReflectiveFieldAdapterMethodInvoker.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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package cn.crane4j.core.support.reflect; | ||
|
||
import cn.crane4j.core.support.MethodInvoker; | ||
import cn.crane4j.core.util.ReflectUtils; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
/** | ||
* Adapter class that implements the {@link MethodInvoker} interface with fields | ||
* without explicit getter and setter methods. | ||
* | ||
* @author tangcent | ||
*/ | ||
@RequiredArgsConstructor | ||
public abstract class ReflectiveFieldAdapterMethodInvoker implements MethodInvoker { | ||
|
||
private final Field field; | ||
|
||
/** | ||
* Create a getter {@link ReflectiveFieldAdapterMethodInvoker} from the given field. | ||
* | ||
* @param field the field to access | ||
* @return the FieldAdapterMethodInvoker for getting the field value | ||
*/ | ||
public static ReflectiveFieldAdapterMethodInvoker createGetter(Field field) { | ||
ReflectUtils.setAccessible(field); | ||
return new ReflectiveFieldGetterInvoker(field); | ||
} | ||
|
||
/** | ||
* Create a setter {@link ReflectiveFieldAdapterMethodInvoker} from the given field. | ||
* | ||
* @param field the field to access | ||
* @return the FieldAdapterMethodInvoker for setting the field value | ||
*/ | ||
public static ReflectiveFieldAdapterMethodInvoker createSetter(Field field) { | ||
ReflectUtils.setAccessible(field); | ||
return new ReflectiveFieldSetterInvoker(field); | ||
} | ||
|
||
@Override | ||
public Object invoke(Object target, Object... args) { | ||
return accessField(target, field, args); | ||
} | ||
|
||
/** | ||
* Get or set the field value using reflection. | ||
* | ||
* @param target the object on which to invoke the method | ||
* @param field the field to access | ||
* @param args the arguments to pass to the method | ||
* @return the result of the method invocation | ||
*/ | ||
protected abstract Object accessField(Object target, Field field, Object... args); | ||
|
||
/** | ||
* An implementation of the {@link ReflectiveFieldAdapterMethodInvoker} for getter. | ||
*/ | ||
private static class ReflectiveFieldGetterInvoker extends ReflectiveFieldAdapterMethodInvoker { | ||
|
||
public ReflectiveFieldGetterInvoker(Field field) { | ||
super(field); | ||
} | ||
|
||
/** | ||
* Gets the value of the field using reflection. | ||
* | ||
* @param target the object on which to invoke the method | ||
* @param field the field to access | ||
* @param args the arguments to pass to the method (ignored) | ||
* @return the value of the field | ||
*/ | ||
@Override | ||
protected Object accessField(Object target, Field field, Object... args) { | ||
try { | ||
return field.get(target); | ||
} catch (IllegalAccessException e) { | ||
throw new IllegalStateException("Failed to access field: " + field.getName(), e); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* An implementation of the {@link ReflectiveFieldAdapterMethodInvoker} for setter. | ||
*/ | ||
private static class ReflectiveFieldSetterInvoker extends ReflectiveFieldAdapterMethodInvoker { | ||
|
||
public ReflectiveFieldSetterInvoker(Field field) { | ||
super(field); | ||
} | ||
|
||
/** | ||
* Sets the value of the field using reflection. | ||
* | ||
* @param target the object on which to invoke the method | ||
* @param field the field to access | ||
* @param args the arguments to pass to the method (one argument, the value to set the field to) | ||
* @return null (the result of the method invocation) | ||
*/ | ||
@Override | ||
protected Object accessField(Object target, Field field, Object... args) { | ||
try { | ||
field.set(target, args[0]); | ||
return null; | ||
} catch (IllegalAccessException e) { | ||
throw new IllegalStateException("Failed to access field: " + field.getName(), e); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.