本文整理汇总了Java中org.apache.commons.lang3.reflect.FieldUtils.getFieldsWithAnnotation方法的典型用法代码示例。如果您正苦于以下问题:Java FieldUtils.getFieldsWithAnnotation方法的具体用法?Java FieldUtils.getFieldsWithAnnotation怎么用?Java FieldUtils.getFieldsWithAnnotation使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.lang3.reflect.FieldUtils
的用法示例。
在下文中一共展示了FieldUtils.getFieldsWithAnnotation方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: save
import org.apache.commons.lang3.reflect.FieldUtils; //导入方法依赖的package包/类
/**
* Saves (persists) the options in the OptionsObject that are marked with the
* PreferencesField annotation.
*/
public static void save() {
OptionsObject oo = OptionsObject.getInstance();
Field[] fields = FieldUtils.getFieldsWithAnnotation(OptionsObject.class, PreferencesField.class);
for (Field field : fields) {
field.setAccessible(true);
Object value;
try {
value = field.get(oo);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
if (value != null) {
if (!field.getType().isPrimitive()) {
log.debug("Non primitive field found {}", field.getName());
}
preferences.put(field.getName(), value.toString());
}
}
}
示例2: LookupDao
import org.apache.commons.lang3.reflect.FieldUtils; //导入方法依赖的package包/类
/**
* Creates a new sharded DAO. The number of managed shards and bucketing is controlled by the {@link ShardManager}.
*
* @param sessionFactories a session provider for each shard
*/
public LookupDao(List<SessionFactory> sessionFactories,
Class<T> entityClass,
ShardManager shardManager,
BucketIdExtractor<String> bucketIdExtractor) {
this.shardManager = shardManager;
this.bucketIdExtractor = bucketIdExtractor;
this.daos = sessionFactories.stream().map(LookupDaoPriv::new).collect(Collectors.toList());
this.entityClass = entityClass;
Field fields[] = FieldUtils.getFieldsWithAnnotation(entityClass, LookupKey.class);
Preconditions.checkArgument(fields.length != 0, "At least one field needs to be sharding key");
Preconditions.checkArgument(fields.length == 1, "Only one field can be sharding key");
keyField = fields[0];
if(!keyField.isAccessible()) {
try {
keyField.setAccessible(true);
} catch (SecurityException e) {
log.error("Error making key field accessible please use a public method and mark that as LookupKey", e);
throw new IllegalArgumentException("Invalid class, DAO cannot be created.", e);
}
}
Preconditions.checkArgument(ClassUtils.isAssignable(keyField.getType(), String.class), "Key field must be a string");
}
示例3: RelationalDao
import org.apache.commons.lang3.reflect.FieldUtils; //导入方法依赖的package包/类
/**
* Create a relational DAO.
* @param sessionFactories List of session factories. One for each shard.
* @param entityClass The class for which the dao will be used.
* @param shardManager The {@link ShardManager} used to manage the bucket to shard mapping.
*/
public RelationalDao(List<SessionFactory> sessionFactories, Class<T> entityClass,
ShardManager shardManager, BucketIdExtractor<String> bucketIdExtractor) {
this.shardManager = shardManager;
this.bucketIdExtractor = bucketIdExtractor;
this.daos = sessionFactories.stream().map(RelationalDaoPriv::new).collect(Collectors.toList());
this.entityClass = entityClass;
Field fields[] = FieldUtils.getFieldsWithAnnotation(entityClass, Id.class);
Preconditions.checkArgument(fields.length != 0, "A field needs to be designated as @Id");
Preconditions.checkArgument(fields.length == 1, "Only one field can be designated as @Id");
keyField = fields[0];
if(!keyField.isAccessible()) {
try {
keyField.setAccessible(true);
} catch (SecurityException e) {
log.error("Error making key field accessible please use a public method and mark that as @Id", e);
throw new IllegalArgumentException("Invalid class, DAO cannot be created.", e);
}
}
}
示例4: findDiff
import org.apache.commons.lang3.reflect.FieldUtils; //导入方法依赖的package包/类
public static <E> Map<String, Object[]> findDiff(E inst1, E inst2, Set<String> excludedFields)
throws Exception {
Preconditions.checkNotNull(inst1);
Preconditions.checkNotNull(inst2);
HashMap<String, Object[]> diff = new HashMap<>();
//Compare all fields with @JsonProperty
for (Field f : FieldUtils.getFieldsWithAnnotation(inst1.getClass(), JsonProperty.class)) {
String name = f.getAnnotation(JsonProperty.class).value();
if (excludedFields.contains(name)) {
continue;
}
f.setAccessible(true);
Object left = f.get(inst1);
Object right = f.get(inst2);
if (left == null && right == null) {
continue;
}
if (f.getType().isArray()) {
if (!Arrays.equals((Object[]) left, (Object[]) right)) {
logger.info("Found diff on property {}", name);
diff.put(name, new Object[]{left, right});
}
} else if (f.getType() == Map.class) {
Map<String, Object[]> details = new HashMap<>();
getDetailsDiff((Map) left, (Map) right, details, name);
for (Map.Entry<String, Object[]> entry : details.entrySet()) {
diff.put(entry.getKey(), entry.getValue());
}
} else {
if (left == null || !left.equals(right)) {
logger.info("Found diff on property {}", name);
diff.put(name, new Object[]{left, right});
}
}
}
return diff;
}
示例5: injectResource
import org.apache.commons.lang3.reflect.FieldUtils; //导入方法依赖的package包/类
private void injectResource(final Object provider) throws IllegalAccessException {
final Field[] resourceFieldArray = FieldUtils.getFieldsWithAnnotation(provider.getClass(), Resource.class);
if (ArrayUtils.isEmpty(resourceFieldArray)) {
return;
}
for (final Field resourceField : resourceFieldArray) {
final Class<?> fieldType = resourceField.getType();
// ConfigInfo注入
if (ConfigInfo.class.isAssignableFrom(fieldType)) {
final ConfigInfo configInfo = new DefaultConfigInfo(cfg);
FieldUtils.writeField(resourceField, provider, configInfo, true);
}
}
}
示例6: getFieldsWithAnnotationsPerformaceTest
import org.apache.commons.lang3.reflect.FieldUtils; //导入方法依赖的package包/类
@Test
public void getFieldsWithAnnotationsPerformaceTest() {
long time = System.currentTimeMillis();
for(int i = 0 ; i < 1000 ; i++) {
FieldUtils.getFieldsWithAnnotation(
ClassF.class, ResponseParam.class);
}
long offset = System.currentTimeMillis() - time;
LOGGER.info("ResponseParamParserTest#getFieldsWithAnnotationsPerformaceTest time = " + offset);
}
示例7: injectRequiredResource
import org.apache.commons.lang3.reflect.FieldUtils; //导入方法依赖的package包/类
private void injectRequiredResource(final CoreModule coreModule) throws IllegalAccessException {
final Module module = coreModule.getModule();
final Field[] resourceFieldArray = FieldUtils.getFieldsWithAnnotation(module.getClass(), Resource.class);
if (ArrayUtils.isEmpty(resourceFieldArray)) {
return;
}
for (final Field resourceField : resourceFieldArray) {
final Class<?> fieldType = resourceField.getType();
// LoadedClassDataSource对象注入
if (LoadedClassDataSource.class.isAssignableFrom(fieldType)) {
FieldUtils.writeField(resourceField, module, classDataSource, true);
}
// ModuleContactorManager对象注入
else if (ModuleEventWatcher.class.isAssignableFrom(fieldType)) {
final ModuleEventWatcher moduleEventWatcher = new DefaultModuleEventWatcher(
inst,
classDataSource,
coreModule,
cfg.isEnableUnsafe()
);
moduleLifeCycleEventBus.append((DefaultModuleEventWatcher) moduleEventWatcher);
FieldUtils.writeField(resourceField, module, moduleEventWatcher, true);
}
// ModuleController对象注入
else if (ModuleController.class.isAssignableFrom(fieldType)) {
final ModuleController moduleController = new DefaultModuleController(coreModule, this);
FieldUtils.writeField(resourceField, module, moduleController, true);
}
// ModuleManager对象注入
else if (ModuleManager.class.isAssignableFrom(fieldType)) {
final ModuleManager moduleManager = new DefaultModuleManager(this);
FieldUtils.writeField(resourceField, module, moduleManager, true);
}
// ConfigInfo注入
else if (ConfigInfo.class.isAssignableFrom(fieldType)) {
final ConfigInfo configInfo = new DefaultConfigInfo(cfg);
FieldUtils.writeField(resourceField, module, configInfo, true);
}
// EventMonitor注入
else if (EventMonitor.class.isAssignableFrom(fieldType)) {
FieldUtils.writeField(resourceField, module, new DefaultEventMonitor(), true);
}
// 其他情况需要输出日志警告
else {
logger.warn("inject required @Resource field[name={};] into module[id={};class={};] failed, type={}; was not support yet.",
resourceField.getName(), coreModule.getUniqueId(), module.getClass(), fieldType);
}
}
}
示例8: inject
import org.apache.commons.lang3.reflect.FieldUtils; //导入方法依赖的package包/类
public void inject() {
Field[] fields = FieldUtils.getFieldsWithAnnotation(target.getClass(), ConfigValue.class);
initFields(config, fields);
}
示例9: getFields
import org.apache.commons.lang3.reflect.FieldUtils; //导入方法依赖的package包/类
private static Field[] getFields(Object obj) {
return FieldUtils.getFieldsWithAnnotation(
obj.getClass(),
ResponseParam.class);
}