Skip to content

Commit

Permalink
code(PropertyOperator): optimize code and comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Createsequence committed Jul 31, 2023
1 parent 878c1ef commit 214b8f6
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import cn.crane4j.annotation.ContainerEnum;
import cn.crane4j.core.support.AnnotationFinder;
import cn.crane4j.core.support.SimpleAnnotationFinder;
import cn.crane4j.core.support.converter.SimpleConverterManager;
import cn.crane4j.core.support.reflect.PropertyOperator;
import cn.crane4j.core.support.reflect.ReflectivePropertyOperator;
import cn.crane4j.core.util.StringUtils;
Expand All @@ -28,7 +27,7 @@ public class EnumContainerBuilder<K, T extends Enum<?>> {

private static final Function<? super Enum<?>, ?> DEFAULT_KEY_GETTER = Enum::name;
private static final Function<? super Enum<?>, ?> DEFAULT_VALUE_GETTER = Function.identity();
private static final PropertyOperator DEFAULT_PROPERTY_OPERATOR = new ReflectivePropertyOperator(new SimpleConverterManager());
private static final PropertyOperator DEFAULT_PROPERTY_OPERATOR = new ReflectivePropertyOperator();

/**
* The enum type to create a container for.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public AsmReflectivePropertyOperator(@Nullable ConverterManager converterManager
* @return {@link MethodInvoker}
*/
@Override
protected MethodInvoker createInvoker(Class<?> targetType, String propertyName, Method method) {
protected MethodInvoker createInvokerForMethod(Class<?> targetType, String propertyName, Method method) {
MethodAccess access = CollectionUtils.computeIfAbsent(methodAccessCaches, targetType, MethodAccess::get);
int methodIndex = access.getIndex(method.getName(), method.getParameterTypes());
return new ReflectAsmMethodInvoker(methodIndex, access);
Expand Down Expand Up @@ -81,30 +81,30 @@ public Object invoke(@Nullable Object target, @Nullable Object... args) {
}

@Override
protected MethodInvoker createInvokerForSetter(Class<?> targetType, String propertyName, Field field) {
protected MethodInvoker createSetterInvokerForField(Class<?> targetType, String propertyName, Field field) {
if (Modifier.isPrivate(field.getModifiers())) {
return super.createInvokerForSetter(targetType, propertyName, field);
return super.createSetterInvokerForField(targetType, propertyName, field);
}
FieldAccess access = CollectionUtils.computeIfAbsent(fieldAccessCaches, targetType, FieldAccess::get);
try {
int fieldIndex = access.getIndex(field.getName());
return new ReflectAsmFieldAdapterSetterInvoker(access, fieldIndex);
} catch (IllegalArgumentException e) {
return super.createInvokerForSetter(targetType, propertyName, field);
return super.createSetterInvokerForField(targetType, propertyName, field);
}
}

@Override
protected MethodInvoker createInvokerForGetter(Class<?> targetType, String propertyName, Field field) {
protected MethodInvoker createGetterInvokerForField(Class<?> targetType, String propertyName, Field field) {
if (Modifier.isPrivate(field.getModifiers())) {
return super.createInvokerForGetter(targetType, propertyName, field);
return super.createGetterInvokerForField(targetType, propertyName, field);
}
FieldAccess access = CollectionUtils.computeIfAbsent(fieldAccessCaches, targetType, FieldAccess::get);
try {
int fieldIndex = access.getIndex(field.getName());
return new ReflectAsmFieldAdapterGetterInvoker(access, fieldIndex);
} catch (IllegalArgumentException e) {
return super.createInvokerForGetter(targetType, propertyName, field);
return super.createGetterInvokerForField(targetType, propertyName, field);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ public ReflectivePropertyOperator(@Nullable ConverterManager converterManager) {
this.converterManager = converterManager;
}

/**
* Create a property operator.
*/
public ReflectivePropertyOperator() {
this(null);
}

/**
* Get getter method.
*
Expand All @@ -80,13 +87,13 @@ public ReflectivePropertyOperator(@Nullable ConverterManager converterManager) {
@Override
public MethodInvoker findGetter(Class<?> targetType, String propertyName) {
Method method = findGetterMethod(targetType, propertyName);
MethodInvoker methodInvoker = resolveInvoker(targetType, propertyName, method);
MethodInvoker methodInvoker = resolveInvokerForMethod(targetType, propertyName, method);
if (methodInvoker != null) {
return methodInvoker;
}

Field field = ReflectUtils.getField(targetType, propertyName);
MethodInvoker methodInvokerForGetter = resolveInvokerForGetter(targetType, propertyName, field);
MethodInvoker methodInvokerForGetter = resolveGetterInvokerForField(targetType, propertyName, field);
if (methodInvokerForGetter != null) {
return methodInvokerForGetter;
}
Expand All @@ -106,13 +113,13 @@ public MethodInvoker findGetter(Class<?> targetType, String propertyName) {
@Override
public MethodInvoker findSetter(Class<?> targetType, String propertyName) {
Method method = findSetterMethod(targetType, propertyName);
MethodInvoker methodInvoker = resolveInvoker(targetType, propertyName, method);
MethodInvoker methodInvoker = resolveInvokerForMethod(targetType, propertyName, method);
if (methodInvoker != null) {
return methodInvoker;
}

Field field = ReflectUtils.getField(targetType, propertyName);
MethodInvoker methodInvokerForSetter = resolveInvokerForSetter(targetType, propertyName, field);
MethodInvoker methodInvokerForSetter = resolveSetterInvokerForField(targetType, propertyName, field);
if (methodInvokerForSetter != null) {
return methodInvokerForSetter;
}
Expand All @@ -121,55 +128,71 @@ public MethodInvoker findSetter(Class<?> targetType, String propertyName) {
return null;
}

// region ========= find by method =========

/**
* Find setter method by given type and field name.
* Resolve the invoker which finally to used.
*
* @param targetType target type
* @param propertyName property name
* @return setter method
* @param method method
* @return method invoker comparator
* @see #createInvokerForMethod
* @see ParameterConvertibleMethodInvoker
*/
@Nullable
protected Method findSetterMethod(Class<?> targetType, String propertyName) {
Field field = ReflectUtils.getField(targetType, propertyName);
if (Objects.isNull(field)) {
return ReflectUtils.findSetterMethod(targetType, propertyName)
.orElse(null);
} else {
return ReflectUtils.findSetterMethod(targetType, field)
.orElse(null);
protected final MethodInvoker resolveInvokerForMethod(Class<?> targetType, String propertyName, @Nullable Method method) {
if (Objects.isNull(method)) {
return null;
}
MethodInvoker invoker = createInvokerForMethod(targetType, propertyName, method);
if (Objects.isNull(invoker)) {
return null;
}
if (Objects.isNull(converterManager)) {
return invoker;
}
return ParameterConvertibleMethodInvoker.create(invoker, converterManager, method.getParameterTypes());
}

/**
* Find getter method by given type and field name.
* Create {@link MethodInvoker} according to the specified method.
*
* @param targetType target type
* @param targetType target type
* @param propertyName property name
* @return getter method
* @param method getter method or setter method
* @return {@link MethodInvoker}
*/
@Nullable
protected Method findGetterMethod(Class<?> targetType, String propertyName) {
Field field = ReflectUtils.getField(targetType, propertyName);
if (Objects.isNull(field)) {
return ReflectUtils.findGetterMethod(targetType, propertyName)
.orElse(null);
} else {
return ReflectUtils.findGetterMethod(targetType, field)
.orElse(null);
}
protected MethodInvoker createInvokerForMethod(Class<?> targetType, String propertyName, Method method) {
return ReflectiveMethodInvoker.create(null, method, false);
}

// endregion

// region ========= find by field =========

/**
* Create {@link MethodInvoker} according to the specified method.
* Resolve invoker which finally be used to set the value of the specified field.
*
* @param targetType target type
* @param propertyName property name
* @param method getter method or setter method
* @return {@link MethodInvoker}
* @param field field to be set.
* @return the {@link MethodInvoker} instance for setting the value of the specified field.
*/
@Nullable
protected MethodInvoker createInvoker(Class<?> targetType, String propertyName, Method method) {
return ReflectiveMethodInvoker.create(null, method, false);
protected final MethodInvoker resolveSetterInvokerForField(Class<?> targetType, String propertyName, @Nullable Field field) {
if (Objects.isNull(field)) {
return null;
}
MethodInvoker invoker = createSetterInvokerForField(targetType, propertyName, field);
if (Objects.isNull(invoker)) {
return null;
}
if (Objects.isNull(converterManager)) {
return invoker;
}
return ParameterConvertibleMethodInvoker.create(invoker, converterManager, new Class[] {field.getType()});
}

/**
Expand All @@ -180,82 +203,76 @@ protected MethodInvoker createInvoker(Class<?> targetType, String propertyName,
* @param field field to be set.
* @return The {@link MethodInvoker} instance for setting the value of the specified field.
*/
protected MethodInvoker createInvokerForSetter(Class<?> targetType, String propertyName, Field field) {
protected MethodInvoker createSetterInvokerForField(Class<?> targetType, String propertyName, Field field) {
return ReflectiveFieldAdapterMethodInvoker.createSetter(field);
}

/**
* Creates a {@link MethodInvoker} for getting the value of the specified field.
* Resolve invoker which finally be used to get the value of the specified field.
*
* @param targetType target type
* @param propertyName property name
* @param field field to be got.
* @return The {@link MethodInvoker} instance for getting the value of the specified field.
* @return the {@link MethodInvoker} instance for getting the value of the specified field.
*/
protected MethodInvoker createInvokerForGetter(Class<?> targetType, String propertyName, Field field) {
return ReflectiveFieldAdapterMethodInvoker.createGetter(field);
protected final MethodInvoker resolveGetterInvokerForField(Class<?> targetType, String propertyName, @Nullable Field field) {
if (Objects.isNull(field)) {
return null;
}
return createGetterInvokerForField(targetType, propertyName, field);
}

/**
* Resolve the invoker which finally to used.
* Creates a {@link MethodInvoker} for getting the value of the specified field.
*
* @param targetType target type
* @param targetType target type
* @param propertyName property name
* @param method method
* @return method invoker comparator
* @see #createInvoker
* @see ParameterConvertibleMethodInvoker
* @param field field to be got.
* @return The {@link MethodInvoker} instance for getting the value of the specified field.
*/
@Nullable
protected final MethodInvoker resolveInvoker(Class<?> targetType, String propertyName, @Nullable Method method) {
if (Objects.isNull(method)) {
return null;
}
MethodInvoker invoker = createInvoker(targetType, propertyName, method);
if (Objects.isNull(invoker)) {
return null;
}
if (Objects.isNull(converterManager)) {
return invoker;
}
return ParameterConvertibleMethodInvoker.create(invoker, converterManager, method.getParameterTypes());
protected MethodInvoker createGetterInvokerForField(Class<?> targetType, String propertyName, Field field) {
return ReflectiveFieldAdapterMethodInvoker.createGetter(field);
}

// ================== static methods ==================

// endregion

/**
* Resolve invoker which finally be used to set the value of the specified field.
* Find setter method by given type and field name.
*
* @param targetType target type
* @param targetType target type
* @param propertyName property name
* @param field field to be set.
* @return the {@link MethodInvoker} instance for setting the value of the specified field.
* @return setter method
*/
@Nullable
protected final MethodInvoker resolveInvokerForSetter(Class<?> targetType, String propertyName, @Nullable Field field) {
private static Method findSetterMethod(Class<?> targetType, String propertyName) {
Field field = ReflectUtils.getField(targetType, propertyName);
if (Objects.isNull(field)) {
return null;
}
MethodInvoker invoker = createInvokerForSetter(targetType, propertyName, field);
if (Objects.isNull(invoker)) {
return null;
}
if (Objects.isNull(converterManager)) {
return invoker;
return ReflectUtils.findSetterMethod(targetType, propertyName)
.orElse(null);
} else {
return ReflectUtils.findSetterMethod(targetType, field)
.orElse(null);
}
return ParameterConvertibleMethodInvoker.create(invoker, converterManager, new Class[] {field.getType()});
}

/**
* Resolve invoker which finally be used to get the value of the specified field.
* Find getter method by given type and field name.
*
* @param targetType target type
* @param targetType target type
* @param propertyName property name
* @param field field to be got.
* @return the {@link MethodInvoker} instance for getting the value of the specified field.
* @return getter method
*/
protected final MethodInvoker resolveInvokerForGetter(Class<?> targetType, String propertyName, @Nullable Field field) {
@Nullable
private static Method findGetterMethod(Class<?> targetType, String propertyName) {
Field field = ReflectUtils.getField(targetType, propertyName);
if (Objects.isNull(field)) {
return null;
return ReflectUtils.findGetterMethod(targetType, propertyName)
.orElse(null);
} else {
return ReflectUtils.findGetterMethod(targetType, field)
.orElse(null);
}
return createInvokerForGetter(targetType, propertyName, field);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,19 @@ public CustomReflectivePropertyOperator() {
}

@Override
protected @Nullable MethodInvoker createInvoker(Class<?> targetType, String propertyName, Method method) {
protected @Nullable MethodInvoker createInvokerForMethod(Class<?> targetType, String propertyName, Method method) {
if (immutableFields.contains(propertyName)) {
return null;
}
return super.createInvoker(targetType, propertyName, method);
return super.createInvokerForMethod(targetType, propertyName, method);
}

@Override
protected MethodInvoker createInvokerForSetter(Class<?> targetType, String propertyName, Field field) {
protected MethodInvoker createSetterInvokerForField(Class<?> targetType, String propertyName, Field field) {
if (immutableFields.contains(propertyName)) {
return null;
}
return super.createInvokerForSetter(targetType, propertyName, field);
return super.createSetterInvokerForField(targetType, propertyName, field);
}
}

Expand Down

0 comments on commit 214b8f6

Please sign in to comment.