Java Code Examples for com.jstarcraft.core.common.reflection.ReflectionUtility#doWithFields()

The following examples show how to use com.jstarcraft.core.common.reflection.ReflectionUtility#doWithFields() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResourceAccessorProcessor.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public boolean postProcessAfterInstantiation(final Object object, String name) throws BeansException {
    ReflectionUtility.doWithFields(object.getClass(), (field) -> {
        ResourceAccessor annotation = field.getAnnotation(ResourceAccessor.class);
        if (annotation == null) {
            return;
        }
        if (ResourceManager.class.isAssignableFrom(field.getType())) {
            // 装配仓储
            assembleStorage(object, field, annotation);
        } else {
            // 装配实例
            assembleInstance(object, field, annotation);
        }
    });
    return super.postProcessAfterInstantiation(object, name);
}
 
Example 2
Source File: CacheAccessorProcessor.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public boolean postProcessAfterInstantiation(final Object instance, final String name) throws BeansException {
    ReflectionUtility.doWithFields(instance.getClass(), (field) -> {
        CacheAccessor annotation = field.getAnnotation(CacheAccessor.class);
        if (annotation == null) {
            return;
        }
        if (field.getType().equals(EntityManager.class)) {
            // 注入实体单位缓存服务
            assembleEntityManager(instance, name, field);
        } else if (field.getType().equals(RegionManager.class)) {
            // 注入区域单位缓存服务
            assembleRegionManager(instance, name, field);
        } else {
            String message = StringUtility.format("无法装配Bean[{}]的属性[{}]", name, field.getName());
            LOGGER.error(message);
            throw new CacheConfigurationException(message);
        }
    });
    return super.postProcessAfterInstantiation(instance, name);
}
 
Example 3
Source File: MongoMetadata.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
/**
 * 构造方法
 * 
 * @param metadata
 */
MongoMetadata(Class<?> clazz) {
    Document document = clazz.getAnnotation(Document.class);
    if (document == null) {
        throw new IllegalArgumentException();
    }
    ormName = document.collection();
    if (StringUtility.isBlank(ormName)) {
        ormName = clazz.getSimpleName();
        ormName = ormName.substring(0, 1).toLowerCase() + ormName.substring(1, ormName.length());
    }
    ormClass = clazz;
    ReflectionUtility.doWithFields(ormClass, (field) -> {
        if (Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers())) {
            return;
        }
        if (field.isAnnotationPresent(Version.class)) {
            versionName = field.getName();
            return;
        }
        Class<?> type = field.getType();
        fields.put(field.getName(), type);
        if (field.isAnnotationPresent(Id.class)) {
            primaryName = field.getName();
            primaryClass = type;
        }
        if (field.isAnnotationPresent(Indexed.class)) {
            indexNames.add(field.getName());
        }
    });
}
 
Example 4
Source File: HibernateMetadata.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
/**
 * 构造方法
 * 
 * @param metadata
 */
HibernateMetadata(Class<?> clazz) {
    ormClass = clazz;
    ormName = clazz.getName();
    ReflectionUtility.doWithFields(ormClass, (field) -> {
        if (Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers())) {
            return;
        }
        if (field.isAnnotationPresent(Version.class)) {
            versionName = field.getName();
            return;
        }
        Class<?> type = ClassUtility.primitiveToWrapper(field.getType());
        if (String.class == type) {
            fields.put(field.getName(), type);
        } else if (type.isEnum()) {
            fields.put(field.getName(), type);
        } else if (Collection.class.isAssignableFrom(type) || type.isArray()) {
            fields.put(field.getName(), List.class);
        } else if (Date.class.isAssignableFrom(type)) {
            fields.put(field.getName(), Date.class);
        } else {
            fields.put(field.getName(), Map.class);
        }
        if (field.isAnnotationPresent(Id.class) || field.isAnnotationPresent(EmbeddedId.class)) {
            primaryName = field.getName();
            primaryClass = type;
        }
    });
    Table table = clazz.getAnnotation(Table.class);
    if (table != null) {
        for (Index index : table.indexes()) {
            indexNames.add(index.columnList());
        }
    }
}
 
Example 5
Source File: CodecDefinition.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
private static void findDependentClasses(Class<?> clazz, Collection<Class<?>> classes) {
    ReflectionUtility.doWithFields(clazz, (field) -> {
        if (!classes.contains(field.getGenericType())) {
            findDependentClasses(field.getGenericType(), classes);
        }
    }, (field) -> {
        return !(Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers()));
    });
}
 
Example 6
Source File: MyBatisMetadata.java    From jstarcraft-core with Apache License 2.0 4 votes vote down vote up
/**
 * 构造方法
 * 
 * @param metadata
 */
MyBatisMetadata(Class<? extends BaseMapper<?>> clazz) {
    ormClass = Class.class.cast(ParameterizedType.class.cast(clazz.getGenericInterfaces()[0]).getActualTypeArguments()[0]);
    ormName = ormClass.getName();
    ReflectionUtility.doWithFields(ormClass, (field) -> {
        if (Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers())) {
            return;
        }
        TableField annotation = field.getAnnotation(TableField.class);
        if (annotation != null) {
            if (!annotation.exist()) {
                return;
            }
            String columnName = annotation.value();
            if (!StringUtility.isEmpty(columnName)) {
                columnNames.put(field.getName(), columnName);
            }
        }
        if (field.isAnnotationPresent(Version.class)) {
            versionName = field.getName();
            return;
        }
        Class<?> type = ClassUtility.primitiveToWrapper(field.getType());
        if (String.class == type) {
            fields.put(field.getName(), type);
        } else if (type.isEnum()) {
            fields.put(field.getName(), type);
        } else if (Collection.class.isAssignableFrom(type) || type.isArray()) {
            fields.put(field.getName(), List.class);
        } else if (Date.class.isAssignableFrom(type)) {
            fields.put(field.getName(), Date.class);
        } else {
            fields.put(field.getName(), Map.class);
        }
        if (field.isAnnotationPresent(TableId.class)) {
            primaryName = field.getName();
            primaryClass = type;
        }
    });
    mapperClass = clazz;
}