use of com.baidu.disconf.client.common.annotations.DisconfItem in project disconf by knightliao.
the class MethodUtils method getFieldFromMethod.
/**
* 对于一个 get/is 方法,返回其相对应的Field
*/
public static Field getFieldFromMethod(Method method, Field[] expectedFields, DisConfigTypeEnum disConfigTypeEnum) {
String fieldName;
if (disConfigTypeEnum.equals(DisConfigTypeEnum.FILE)) {
DisconfFileItem disconfFileItem = method.getAnnotation(DisconfFileItem.class);
// 根据用户设定的注解来获取
fieldName = disconfFileItem.associateField();
} else {
DisconfItem disItem = method.getAnnotation(DisconfItem.class);
// 根据用户设定的注解来获取
fieldName = disItem.associateField();
}
//
if (StringUtils.isEmpty(fieldName)) {
// 从方法名 获取其 Field 名
fieldName = ClassUtils.getFieldNameByGetMethodName(method.getName());
}
// 确认此Field名是正确的
for (Field field : expectedFields) {
if (field.getName().equals(fieldName)) {
return field;
}
}
LOGGER.error(method.toString() + " cannot get its related field name. ");
return null;
}
use of com.baidu.disconf.client.common.annotations.DisconfItem in project disconf by knightliao.
the class ReflectionScanStatic method scanBasicInfo.
/**
* 扫描基本信息
*/
private ScanStaticModel scanBasicInfo(List<String> packNameList) {
ScanStaticModel scanModel = new ScanStaticModel();
//
// 扫描对象
//
Reflections reflections = getReflection(packNameList);
scanModel.setReflections(reflections);
//
// 获取DisconfFile class
//
Set<Class<?>> classdata = reflections.getTypesAnnotatedWith(DisconfFile.class);
scanModel.setDisconfFileClassSet(classdata);
//
// 获取DisconfFileItem method
//
Set<Method> af1 = reflections.getMethodsAnnotatedWith(DisconfFileItem.class);
scanModel.setDisconfFileItemMethodSet(af1);
//
// 获取DisconfItem method
//
af1 = reflections.getMethodsAnnotatedWith(DisconfItem.class);
scanModel.setDisconfItemMethodSet(af1);
//
// 获取DisconfActiveBackupService
//
classdata = reflections.getTypesAnnotatedWith(DisconfActiveBackupService.class);
scanModel.setDisconfActiveBackupServiceClassSet(classdata);
//
// 获取DisconfUpdateService
//
classdata = reflections.getTypesAnnotatedWith(DisconfUpdateService.class);
scanModel.setDisconfUpdateService(classdata);
// update pipeline
Set<Class<? extends IDisconfUpdatePipeline>> iDisconfUpdatePipeline = reflections.getSubTypesOf(IDisconfUpdatePipeline.class);
if (iDisconfUpdatePipeline != null && iDisconfUpdatePipeline.size() != 0) {
scanModel.setiDisconfUpdatePipeline((Class<IDisconfUpdatePipeline>) iDisconfUpdatePipeline.toArray()[0]);
}
return scanModel;
}
use of com.baidu.disconf.client.common.annotations.DisconfItem in project disconf by knightliao.
the class StaticScannerItemMgrImpl method transformScanItem.
/**
* 转换配置项
*/
private static DisconfCenterItem transformScanItem(Method method) {
DisconfCenterItem disconfCenterItem = new DisconfCenterItem();
// class
Class<?> cls = method.getDeclaringClass();
// fields
Field[] expectedFields = cls.getDeclaredFields();
// field
Field field = MethodUtils.getFieldFromMethod(method, expectedFields, DisConfigTypeEnum.ITEM);
if (field == null) {
return null;
}
// 获取标注
DisconfItem disconfItem = method.getAnnotation(DisconfItem.class);
// 去掉空格
String key = disconfItem.key().replace(" ", "");
// get setter method
Method setterMethod = MethodUtils.getSetterMethodFromField(cls, field);
disconfCenterItem.setSetMethod(setterMethod);
// field
disconfCenterItem.setField(field);
// key
disconfCenterItem.setKey(key);
// access
field.setAccessible(true);
// object
disconfCenterItem.setObject(null);
// value
if (Modifier.isStatic(field.getModifiers())) {
try {
disconfCenterItem.setValue(field.get(null));
} catch (Exception e) {
LOGGER.error(e.toString());
disconfCenterItem.setValue(null);
}
} else {
disconfCenterItem.setValue(null);
}
//
// disConfCommonModel
DisConfCommonModel disConfCommonModel = makeDisConfCommonModel(disconfItem.app(), disconfItem.env(), disconfItem.version());
disconfCenterItem.setDisConfCommonModel(disConfCommonModel);
// Disconf-web url
String url = DisconfWebPathMgr.getRemoteUrlParameter(DisClientSysConfig.getInstance().CONF_SERVER_STORE_ACTION, disConfCommonModel.getApp(), disConfCommonModel.getVersion(), disConfCommonModel.getEnv(), key, DisConfigTypeEnum.ITEM);
disconfCenterItem.setRemoteServerUrl(url);
return disconfCenterItem;
}
use of com.baidu.disconf.client.common.annotations.DisconfItem in project disconf by knightliao.
the class ScanPrinterUtils method printItem.
/**
*/
public static void printItem(Set<Field> af1) {
for (Field item : af1) {
LOGGER.info(item.toString());
DisconfItem disconfItem = item.getAnnotation(DisconfItem.class);
LOGGER.info("\tkey: " + disconfItem.key());
LOGGER.info("\tenv: " + disconfItem.env());
LOGGER.info("\tversion: " + disconfItem.version());
}
}
Aggregations