-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
174 additions
and
0 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
119 changes: 119 additions & 0 deletions
119
src/main/java/io/foldright/inspectablewrappers/utils/WrapperAdapterUtils.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,119 @@ | ||
package io.foldright.inspectablewrappers.utils; | ||
|
||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import edu.umd.cs.findbugs.annotations.Nullable; | ||
import io.foldright.inspectablewrappers.Attachable; | ||
import io.foldright.inspectablewrappers.Wrapper; | ||
import io.foldright.inspectablewrappers.WrapperAdapter; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Proxy; | ||
import java.util.Arrays; | ||
import java.util.Objects; | ||
|
||
|
||
/** | ||
* Utility class for creating {@link WrapperAdapter} instances. | ||
* | ||
* @author Jerry Lee (oldratlee at gmail dot com) | ||
*/ | ||
public final class WrapperAdapterUtils { | ||
/** | ||
* Creates a {@link WrapperAdapter} instance of the given biz interface type | ||
* by the adapted/existed wrapper instance and the underlying instance that be wrapped. | ||
* | ||
* @param bizInterface the class of instances that be wrapped | ||
* @param adaptee the adapted/existed wrapper instance, more info see {@link WrapperAdapter#adaptee()} | ||
* @param underlying the underlying instance that be wrapped, more info see {@link Wrapper#unwrap()} | ||
* @param <T> the type of instances that be wrapped | ||
* @return the {@link WrapperAdapter} instance | ||
* @see Wrapper#unwrap() | ||
* @see WrapperAdapter#adaptee() | ||
*/ | ||
@NonNull | ||
public static <T> T createWrapperAdapter(Class<T> bizInterface, T adaptee, T underlying) { | ||
return createWrapperAdapter0( | ||
Objects.requireNonNull(bizInterface, "bizInterface is null"), | ||
Objects.requireNonNull(adaptee, "adaptee is null"), | ||
Objects.requireNonNull(underlying, "underlying is null"), | ||
null); | ||
} | ||
|
||
/** | ||
* Creates a {@link WrapperAdapter} instance of the given biz interface type with attachable interface | ||
* by the adapted/existed wrapper instance, the underlying instance that be wrapped and an attachable instance. | ||
* | ||
* @param bizInterface the class of instances that be wrapped | ||
* @param adaptee the adapted/existed wrapper instance, more info see {@link WrapperAdapter#adaptee()} | ||
* @param underlying the underlying instance that be wrapped, more info see {@link Wrapper#unwrap()} | ||
* @param attachable the attachable instance, more info see {@link Attachable} | ||
* @param <T> the type of instances that be wrapped | ||
* @return the {@link WrapperAdapter} instance with attachable ability | ||
* @see Wrapper#unwrap() | ||
* @see WrapperAdapter#adaptee() | ||
* @see Attachable#getAttachment(Object) | ||
* @see Attachable#setAttachment(Object, Object) | ||
*/ | ||
@NonNull | ||
public static <T> T createWrapperAdapter(Class<T> bizInterface, T adaptee, T underlying, Attachable<?, ?> attachable) { | ||
return createWrapperAdapter0( | ||
Objects.requireNonNull(bizInterface, "bizInterface is null"), | ||
Objects.requireNonNull(adaptee, "adaptee is null"), | ||
Objects.requireNonNull(underlying, "underlying is null"), | ||
Objects.requireNonNull(attachable, "attachable is null")); | ||
} | ||
|
||
@SuppressWarnings({"unchecked", "rawtypes"}) | ||
public static <T> T createWrapperAdapter0(Class<T> bizInterface, T adaptee, T underlying, @Nullable Attachable<?, ?> attachable) { | ||
final InvocationHandler handler = (proxy, method, args) -> { | ||
if (isMethodAdaptee(method)) return adaptee; | ||
if (isMethodUnwrap(method)) return underlying; | ||
|
||
if (attachable != null && isMethodGetAttachment(method)) | ||
return ((Attachable) attachable).getAttachment(args[0]); | ||
if (attachable != null && isMethodSetAttachment(method)) { | ||
((Attachable) attachable).setAttachment(args[0], args[1]); | ||
return null; | ||
} | ||
|
||
return method.invoke(adaptee, args); | ||
}; | ||
|
||
return (T) Proxy.newProxyInstance( | ||
adaptee.getClass().getClassLoader(), | ||
attachable == null | ||
? new Class[]{bizInterface, WrapperAdapter.class} | ||
: new Class[]{bizInterface, WrapperAdapter.class, Attachable.class}, | ||
handler | ||
); | ||
} | ||
|
||
private static boolean isMethodAdaptee(Method method) { | ||
return checkMethod(method, "adaptee"); | ||
} | ||
|
||
private static boolean isMethodUnwrap(Method method) { | ||
return checkMethod(method, "unwrap"); | ||
} | ||
|
||
private static boolean isMethodGetAttachment(Method method) { | ||
return checkMethod(method, "getAttachment", Object.class); | ||
} | ||
|
||
private static boolean isMethodSetAttachment(Method method) { | ||
return checkMethod(method, "setAttachment", Object.class, Object.class); | ||
} | ||
|
||
private static boolean checkMethod(Method method, String methodName, Class<?>... parameterTypes) { | ||
return method.getName().equals(methodName) | ||
&& method.getParameterCount() == parameterTypes.length | ||
&& Arrays.equals(method.getParameterTypes(), parameterTypes); | ||
} | ||
|
||
/** | ||
* NO need to create instance at all | ||
*/ | ||
private WrapperAdapterUtils() { | ||
} | ||
} |
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