當前位置: 首頁>>代碼示例>>Java>>正文


Java FieldUtils.getFieldsWithAnnotation方法代碼示例

本文整理匯總了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());
		}
	}
}
 
開發者ID:KodeMunkie,項目名稱:imagetozxspec,代碼行數:24,代碼來源:PreferencesService.java

示例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");
}
 
開發者ID:santanusinha,項目名稱:dropwizard-db-sharding-bundle,代碼行數:29,代碼來源:LookupDao.java

示例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);
        }
    }
}
 
開發者ID:santanusinha,項目名稱:dropwizard-db-sharding-bundle,代碼行數:27,代碼來源:RelationalDao.java

示例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;
}
 
開發者ID:pinterest,項目名稱:soundwave,代碼行數:40,代碼來源:JsonCompareUtil.java

示例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);
        }
    }
}
 
開發者ID:alibaba,項目名稱:jvm-sandbox,代碼行數:15,代碼來源:DefaultProviderManager.java

示例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);
}
 
開發者ID:youngmonkeys,項目名稱:ezyfox-sfs2x,代碼行數:11,代碼來源:ResponseParamParserTest.java

示例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);
            }

        }

    }
 
開發者ID:alibaba,項目名稱:jvm-sandbox,代碼行數:61,代碼來源:DefaultCoreModuleManager.java

示例8: inject

import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
public void inject() {
    Field[] fields = FieldUtils.getFieldsWithAnnotation(target.getClass(), ConfigValue.class);

    initFields(config, fields);
}
 
開發者ID:balamaci,項目名稱:muninn,代碼行數:6,代碼來源:ConfigInjector.java

示例9: getFields

import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
private static Field[] getFields(Object obj) {
	return FieldUtils.getFieldsWithAnnotation(
			obj.getClass(), 
			ResponseParam.class);
}
 
開發者ID:youngmonkeys,項目名稱:ezyfox-sfs2x,代碼行數:6,代碼來源:TestFields.java


注:本文中的org.apache.commons.lang3.reflect.FieldUtils.getFieldsWithAnnotation方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。