Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复字节码注入类忽略mocker的逻辑synthetic #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ private void setFieldValueByFieldAccessible(DataConfig mockConfig, Object result
for (Class<?> currentClass = clazz; currentClass != Object.class; currentClass = currentClass.getSuperclass()) {
// 模拟有setter方法的字段
for (Field field :currentClass.getDeclaredFields()) {
if (field.isAnnotationPresent(MockIgnore.class)) {
if (field.isAnnotationPresent(MockIgnore.class) || field.isSynthetic()) {
continue;
}

if (field.getName() != null && field.getName().equalsIgnoreCase("serialVersionUID")) {
continue;
}

/**
* 是否配置排除这个属性
*/
Expand All @@ -87,22 +87,22 @@ private void setFieldValueByIntrospector(DataConfig mockConfig, Object result) t
// 模拟有setter方法的字段
for (Entry<Field, Method> entry : ReflectionUtils.fieldAndSetterMethod(currentClass).entrySet()) {
Field field = entry.getKey();
if (field.isAnnotationPresent(MockIgnore.class)) {
if (field.isAnnotationPresent(MockIgnore.class) || field.isSynthetic()) {
continue;
}

if (field.getName() != null && field.getName().equalsIgnoreCase("serialVersionUID")) {
continue;
}

/**
* 是否配置排除这个属性
*/
if(mockConfig.globalConfig().isConfigExcludeMock(clazz,field.getName())){
continue;
continue;
}
ReflectionUtils
.setRefValue(result, entry.getValue(), new BaseMocker(field.getGenericType()).mock(mockConfig.globalConfig().getDataConfig(currentClass,field.getName())));
.setRefValue(result, entry.getValue(), new BaseMocker(field.getGenericType()).mock(mockConfig.globalConfig().getDataConfig(currentClass,field.getName())));
}
}
}
Expand Down
34 changes: 16 additions & 18 deletions src/main/java/com/github/jsonzou/jmockdata/mocker/EnumMocker.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
package com.github.jsonzou.jmockdata.mocker;

import com.github.jsonzou.jmockdata.DataConfig;
import com.github.jsonzou.jmockdata.MockConfig;
import com.github.jsonzou.jmockdata.MockException;
import com.github.jsonzou.jmockdata.Mocker;
import com.github.jsonzou.jmockdata.util.RandomUtils;
import java.lang.reflect.Field;

/**
* Enum对象模拟器
*/
public class EnumMocker<T extends Enum> implements Mocker<Object> {

private Class<?> clazz;
private Class<?> clazz;

public EnumMocker(Class<?> clazz) {
this.clazz = clazz;
}
public EnumMocker(Class<?> clazz) {
this.clazz = clazz;
}

@Override
public T mock(DataConfig mockConfig) {
@Override
public T mock(DataConfig mockConfig) {

Enum[] enums = mockConfig.globalConfig().getcacheEnum(clazz.getName());
if (enums == null) {
// Field field = clazz.getDeclaredField("$VALUES");
// field.setAccessible(true);
enums =(Enum[]) clazz.getEnumConstants();
if (enums.length == 0) {
throw new MockException("空的enum不能模拟");
Enum[] enums = mockConfig.globalConfig().getcacheEnum(clazz.getName());
if (enums == null) {
// Field field = clazz.getDeclaredField("$VALUES");
// field.setAccessible(true);
enums =(Enum[]) clazz.getEnumConstants();
if (enums.length == 0) {
throw new MockException("空的enum不能模拟");
}
mockConfig.globalConfig().cacheEnum(clazz.getName(), enums);
}
mockConfig.globalConfig().cacheEnum(clazz.getName(), enums);
return (T) enums[RandomUtils.nextInt(0, enums.length)];
}
return (T) enums[RandomUtils.nextInt(0, enums.length)];
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.jsonzou.jmockdata.mocker;

import com.github.jsonzou.jmockdata.DataConfig;
import com.github.jsonzou.jmockdata.MockConfig;
import com.github.jsonzou.jmockdata.Mocker;
import java.lang.reflect.ParameterizedType;

Expand All @@ -12,13 +11,13 @@ public class GenericMocker implements Mocker<Object> {

private ParameterizedType type;

GenericMocker(ParameterizedType type) {
this.type = type;
}

@Override
public Object mock(DataConfig mockConfig) {
return new BaseMocker(type.getRawType(), type.getActualTypeArguments()).mock(mockConfig);
}

GenericMocker(ParameterizedType type) {
this.type = type;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public static Map<Field, Method> fieldAndSetterMethod(Class clazz) throws Intros
BeanInfo beanInfo = Introspector.getBeanInfo(clazz, Object.class);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (Field field : clazz.getDeclaredFields()) {
if (field.isSynthetic()){
continue;
}
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
if (propertyDescriptor.getName().equals(field.getName()) && propertyDescriptor.getWriteMethod() != null) {
map.put(field, propertyDescriptor.getWriteMethod());
Expand Down