本文整理汇总了Java中org.springframework.util.ReflectionUtils.findField方法的典型用法代码示例。如果您正苦于以下问题:Java ReflectionUtils.findField方法的具体用法?Java ReflectionUtils.findField怎么用?Java ReflectionUtils.findField使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.util.ReflectionUtils
的用法示例。
在下文中一共展示了ReflectionUtils.findField方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: JBossModulesAdapter
import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
public JBossModulesAdapter(ClassLoader loader) {
this.classLoader = loader;
try {
Field transformer = ReflectionUtils.findField(loader.getClass(), "transformer");
transformer.setAccessible(true);
this.delegatingTransformer = transformer.get(loader);
if (!this.delegatingTransformer.getClass().getName().equals(DELEGATING_TRANSFORMER_CLASS_NAME)) {
throw new IllegalStateException("Transformer not of the expected type DelegatingClassFileTransformer: " +
this.delegatingTransformer.getClass().getName());
}
this.addTransformer = ReflectionUtils.findMethod(this.delegatingTransformer.getClass(),
"addTransformer", ClassFileTransformer.class);
this.addTransformer.setAccessible(true);
}
catch (Exception ex) {
throw new IllegalStateException("Could not initialize JBoss 7 LoadTimeWeaver", ex);
}
}
示例2: parseToBean
import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
/**
* 字段-值 键值对转为pojo
*
* @param mapping field-value mapper
* @param clazz 待转换的pojo类-class对象
* @throws Exception InstantiationException, IllegalAccessException
*/
public static <T> T parseToBean(Map<String, Object> mapping, Class<T> clazz) throws Exception {
T t = clazz.newInstance();
for (Entry<String, Object> entry : mapping.entrySet()) {
// 字段名 下划线转为驼峰风格
String fieldName = StringUtil.underlineToCamel(entry.getKey());
Field field = ReflectionUtils.findField(clazz, fieldName);
if (field != null) {
FieldReflectUtil.setFieldValue(t, field, entry.getValue());
}
}
return t;
}
示例3: toString
import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
private String toString(DataSource dataSource) {
if (dataSource == null) {
return "<none>";
} else {
try {
Field urlField = ReflectionUtils.findField(dataSource.getClass(), "url");
ReflectionUtils.makeAccessible(urlField);
return stripCredentials((String) urlField.get(dataSource));
} catch (Exception fe) {
try {
Method urlMethod = ReflectionUtils.findMethod(dataSource.getClass(), "getUrl");
ReflectionUtils.makeAccessible(urlMethod);
return stripCredentials((String) urlMethod.invoke(dataSource, (Object[])null));
} catch (Exception me){
return "<unknown> " + dataSource.getClass();
}
}
}
}
示例4: assertDataSourceOfType
import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
private <T extends DataSource> FlexyPoolDataSource<T> assertDataSourceOfType(DataSource dataSource, Class<T> realDataSourceClass) {
assertThat(dataSource).isInstanceOf(DecoratedDataSource.class);
DataSource decoratedDataSource = ((DecoratedDataSource) dataSource).getDecoratedDataSource();
assertThat(decoratedDataSource).isInstanceOf(FlexyPoolDataSource.class);
Field field = ReflectionUtils.findField(FlexyPoolDataSource.class, "targetDataSource");
ReflectionUtils.makeAccessible(field);
Object targetDataSource = ReflectionUtils.getField(field, decoratedDataSource);
assertThat(targetDataSource).isInstanceOf(realDataSourceClass);
return (FlexyPoolDataSource<T>) decoratedDataSource;
}
开发者ID:gavlyukovskiy,项目名称:spring-boot-data-source-decorator,代码行数:12,代码来源:FlexyPoolConfigurationTests.java
示例5: getValue
import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
/**
* 获取实例属性值
*
* @param object 对象实例
* @param fieldName 属性名
* @param <T> 属性类型
* @return 属性值
*/
static <T> T getValue(Object object, String fieldName) {
Field field = ReflectionUtils.findField(object.getClass(), fieldName);
Objects.requireNonNull(field, String.format("Field %s not found in %s", fieldName, object.getClass()));
field.setAccessible(true);
try {
return (T) field.get(object);
} catch (IllegalAccessException e) {
throw new IllegalStateException(
"Unexpected reflection exception - " + e.getClass().getName() + ": " + e.getMessage());
}
}
示例6: isTransactionalApplicationEventListener
import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
private static boolean isTransactionalApplicationEventListener(ApplicationListener<?> listener) {
Class<?> targetClass = AopUtils.getTargetClass(listener);
if (!ApplicationListenerMethodAdapter.class.isAssignableFrom(targetClass)) {
return false;
}
Field field = ReflectionUtils.findField(ApplicationListenerMethodAdapter.class, "method");
ReflectionUtils.makeAccessible(field);
Method method = (Method) ReflectionUtils.getField(field, listener);
return AnnotatedElementUtils.hasAnnotation(method, TransactionalEventListener.class);
}
开发者ID:olivergierke,项目名称:spring-domain-events,代码行数:15,代码来源:PersistentApplicationEventMulticaster.java
示例7: getField
import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
private static Field getField(String fieldName) {
try {
return ReflectionUtils.findField(WithConfigurationTestCase.class, fieldName);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
示例8: CoapTransportResource
import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
public CoapTransportResource(MsgProducer msgProducer, AttributesService attributesService, DeviceAuthService authService, String name, long timeout) {
super(name);
this.msgProducer = msgProducer;
this.attributesService = attributesService;
this.authService = authService;
this.timeout = timeout;
// This is important to turn off existing observable logic in
// CoapResource. We will have our own observe monitoring due to 1:1
// observe relationship.
this.setObservable(false);
observerField = ReflectionUtils.findField(Exchange.class, "observer");
observerField.setAccessible(true);
}
示例9: should_return_the_attribute_name
import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
@Test public void
should_return_the_attribute_name(){
MongoPersistentEntity<FieldWithoutAnnotation> entity = new BasicMongoPersistentEntity<FieldWithoutAnnotation>(ClassTypeInformation.from(FieldWithoutAnnotation.class));
java.lang.reflect.Field field = ReflectionUtils.findField(FieldWithoutAnnotation.class, "name");
BasicMongoPersistentProperty persistentProperty = new BasicMongoPersistentProperty(field, null, entity, new SimpleTypeHolder());
String fieldName = persistentProperty.getFieldName();
assertEquals("Class attribute reading failed", field.getName(), fieldName);
}
示例10: setLastTimeRun
import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
public void setLastTimeRun(LocalDateTime lastTimeRun) {
Field lastTimeRunField = ReflectionUtils.findField(AbstractNotifier.class, "lastTimeRun");
ReflectionUtils.makeAccessible(lastTimeRunField);
ReflectionUtils.setField(lastTimeRunField, this, lastTimeRun);
}
示例11: getRowIndex
import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
private Integer getRowIndex(Result<?> result) {
Field rowIndexField = ReflectionUtils.findField(result.getClass(), "rowIndex");
rowIndexField.setAccessible(true);
return (Integer) ReflectionUtils.getField(rowIndexField, result);
}
示例12: setField
import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
public static <T> void setField(final T targetObject, final String fieldName, final Object targetValue) {
final Field field = ReflectionUtils.findField(targetObject.getClass(), fieldName);
field.setAccessible(true);
ReflectionUtils.setField(field, targetObject, targetValue);
}
示例13: setField
import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
void setField(String name, Object value) {
Field field = ReflectionUtils.findField(EurekaJacksonCodec.class, name);
ReflectionUtils.makeAccessible(field);
ReflectionUtils.setField(field, this, value);
}
示例14: setField
import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
/**
* 设置对应参数的值
*
* @param target
* @param methodName
* @param args
* @return
* @throws Exception
*/
public static void setField(Object target, String fieldName, Object args) throws Exception {
// 查找对应的方法
Field field = ReflectionUtils.findField(target.getClass(), fieldName);
ReflectionUtils.makeAccessible(field);
ReflectionUtils.setField(field, target, args);
}
示例15: setValue
import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
/**
* 给实例属性赋值
*
* @param object 对象实例
* @param fieldName 属性名
* @param value 属性值
*/
static void setValue(Object object, String fieldName, Object value) {
Field field = ReflectionUtils.findField(object.getClass(), fieldName);
Objects.requireNonNull(field, String.format("Field %s not found in %s", fieldName, object.getClass()));
setValue(object, field, value);
}