本文整理汇总了Java中org.apache.commons.lang3.reflect.FieldUtils类的典型用法代码示例。如果您正苦于以下问题:Java FieldUtils类的具体用法?Java FieldUtils怎么用?Java FieldUtils使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FieldUtils类属于org.apache.commons.lang3.reflect包,在下文中一共展示了FieldUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeAllFieldsOfType
import org.apache.commons.lang3.reflect.FieldUtils; //导入依赖的package包/类
public static <T> void writeAllFieldsOfType(Object o, T t, Class<T> c) throws IllegalAccessException
{
boolean wroteSomething = false;
for (Field f : FieldUtils.getAllFields(o.getClass()))
{
if (!Modifier.isStatic(f.getModifiers()) && f.getType().equals(c))
{
FieldUtils.writeField(f, o, t, true);
wroteSomething = true;
}
}
if (!wroteSomething)
throw new IllegalAccessException();
}
示例2: createProxy
import org.apache.commons.lang3.reflect.FieldUtils; //导入依赖的package包/类
public static Object createProxy(Object realObject) {
try {
MethodInterceptor interceptor = new HammerKiller();
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(realObject.getClass());
enhancer.setCallbackType(interceptor.getClass());
Class classForProxy = enhancer.createClass();
Enhancer.registerCallbacks(classForProxy, new Callback[]{interceptor});
Object createdProxy = classForProxy.newInstance();
for (Field realField : FieldUtils.getAllFieldsList(realObject.getClass())) {
if (Modifier.isStatic(realField.getModifiers()))
continue;
realField.setAccessible(true);
realField.set(createdProxy, realField.get(realObject));
}
CreeperKiller.LOG.info("Removed HammerCore main menu hook, ads will no longer be displayed.");
return createdProxy;
} catch (Exception e) {
CreeperKiller.LOG.error("Failed to create a proxy for HammerCore ads, they will not be removed.", e);
}
return realObject;
}
示例3: contributeMocks
import org.apache.commons.lang3.reflect.FieldUtils; //导入依赖的package包/类
@Override
public Map<Field, Object> contributeMocks() {
injectMocks(testInstance);
return stream(FieldUtils.getAllFields(testInstance.getClass()))
.filter(this::isMockField)
.collect(Collectors.toMap(
it -> it,
it -> {
try {
it.setAccessible(true);
return it.get(testInstance);
} catch (IllegalAccessException e) {
throw new TestEEfiException("Failed to retrieve mock from test instance", e);
}
}
));
}
示例4: validate
import org.apache.commons.lang3.reflect.FieldUtils; //导入依赖的package包/类
private void validate(Object object, Class<?> clazz) throws Exception {
for (Field field : FieldUtils.getAllFields(clazz)) {
int modifiers = field.getModifiers();
// Ignore static fields
if (Modifier.isStatic(modifiers)) {
continue;
}
assertTrue("Field is private", Modifier.isPrivate(modifiers));
assertTrue("Field is final", Modifier.isFinal(modifiers));
Method getter = clazz.getMethod(getterName(field.getName()));
assertNotNull("Getter exists", getter);
assertTrue("Getter is public", Modifier.isPublic(getter.getModifiers()));
}
// Check that hashCode, toString and equals are defined
assertNotNull(clazz.getDeclaredMethod("hashCode").invoke(object));
assertNotNull(clazz.getDeclaredMethod("toString").invoke(object));
assertTrue((Boolean) clazz.getDeclaredMethod("equals", Object.class).invoke(object, object));
}
示例5: getClassParameters
import org.apache.commons.lang3.reflect.FieldUtils; //导入依赖的package包/类
private Map<String, String> getClassParameters() {
final List<Field> fields = FieldUtils.getFieldsListWithAnnotation(getType(),
ru.yandex.qatools.allure.annotations.Parameter.class);
return fields.stream().collect(
Collectors.toMap(Allure1Utils::getParameterName, f -> Allure1Utils.getParameterValue(f, target))
);
}
示例6: getIdField
import org.apache.commons.lang3.reflect.FieldUtils; //导入依赖的package包/类
private Field getIdField(Class<?> domainClass) {
Field idField = null;
final List<Field> fields = FieldUtils.getFieldsListWithAnnotation(domainClass, Id.class);
if (fields.isEmpty()) {
idField = ReflectionUtils.findField(getJavaType(), "id");
} else if (fields.size() == 1) {
idField = fields.get(0);
} else {
throw new IllegalArgumentException("only one field with @Id annotation!");
}
if (idField != null && idField.getType() != String.class) {
throw new IllegalArgumentException("type of id field must be String");
}
return idField;
}
示例7: getPartitionKeyField
import org.apache.commons.lang3.reflect.FieldUtils; //导入依赖的package包/类
private Field getPartitionKeyField(Class<?> domainClass) {
Field partitionKeyField = null;
final List<Field> fields = FieldUtils.getFieldsListWithAnnotation(domainClass, PartitionKey.class);
if (fields.size() == 1) {
partitionKeyField = fields.get(0);
} else if (fields.size() > 1) {
throw new IllegalArgumentException("Azure Cosmos DB supports only one partition key, " +
"only one field with @PartitionKey annotation!");
}
if (partitionKeyField != null && partitionKeyField.getType() != String.class) {
throw new IllegalArgumentException("type of PartitionKey field must be String");
}
return partitionKeyField;
}
示例8: traceObjectPath
import org.apache.commons.lang3.reflect.FieldUtils; //导入依赖的package包/类
@Override
@Nullable
public final List<ObjectTreeAware> traceObjectPath(@NonNull final Object _objectToFind) {
for (final Field field : FieldUtils.getAllFieldsList(getClass())) {
try {
final List<ObjectTreeAware> result = findValue(FieldUtils.readField(this, field.getName(), true), _objectToFind);
if (result != null) {
return result;
}
} catch (IllegalAccessException _e) {
// safe to ignore
}
}
return null;
}
示例9: some
import org.apache.commons.lang3.reflect.FieldUtils; //导入依赖的package包/类
/**
* Given an {@link Element}, {@link #some} filter's its fields using the given predicate, creates
* a property key and value for each field, flattens them, and returns it as a {@link List}.
*
* @throws IllegalArgumentException, if it finds a property that doesn't have a value, and that
* property corresponds to a {@link org.apache.tinkerpop.gremlin.object.model.PrimaryKey}
* or {@link org.apache.tinkerpop.gremlin.object.model.OrderingKey}
* field.
*/
@SneakyThrows
public static <E extends Element> List<Object> some(E element, Predicate<Field> predicate) {
List<Object> properties = new ArrayList<>();
for (Field field : fields(element, predicate)) {
Object propertyValue = FieldUtils.readField(field, element);
if (isMissing(propertyValue)) {
if (isKey(field)) {
throw Element.Exceptions.requiredKeysMissing(element.getClass(), propertyKey(field));
}
continue;
}
String propertyName = propertyKey(field);
properties.add(propertyName);
if (isPrimitive(field)) {
if (field.getType().isEnum()) {
properties.add(((Enum) propertyValue).name());
} else {
properties.add(propertyValue);
}
} else {
properties.add(propertyValue);
}
}
return properties;
}
示例10: updateTransformTaskConfig
import org.apache.commons.lang3.reflect.FieldUtils; //导入依赖的package包/类
/**
* Update the parameters for the transformTask
*
* @param transformTask
* @param consumedInputStreams
* @param referencedInputStreams
* @param outputStream
*/
private void updateTransformTaskConfig(TransformTask transformTask,
@NonNull Collection<TransformStream> consumedInputStreams,
@NonNull Collection<TransformStream> referencedInputStreams,
@Nullable IntermediateStream outputStream) throws IllegalAccessException {
Field consumedInputStreamsField = FieldUtils.getDeclaredField(StreamBasedTask.class,
"consumedInputStreams",
true);
Field referencedInputStreamsField = FieldUtils.getDeclaredField(StreamBasedTask.class,
"referencedInputStreams",
true);
Field outputStreamField = FieldUtils.getDeclaredField(StreamBasedTask.class,
"outputStream",
true);
if (null == consumedInputStreamsField ||
null == referencedInputStreamsField ||
null == outputStreamField) {
throw new StopExecutionException(
"The TransformTask does not has field with name: consumedInputStreams or referencedInputStreams or outputStream! Plugin version does not support!");
}
consumedInputStreamsField.set(transformTask, consumedInputStreams);
referencedInputStreamsField.set(transformTask, referencedInputStreams);
outputStreamField.set(transformTask, outputStream);
}
示例11: getTransformParam
import org.apache.commons.lang3.reflect.FieldUtils; //导入依赖的package包/类
/**
* Gets the parameters of a transformTask
*
* @param transformTask
* @return
* @throws IllegalAccessException
*/
private TransformTaskParam getTransformParam(TransformTask transformTask) throws IllegalAccessException {
TransformTaskParam transformTaskParam = new TransformTaskParam();
Field consumedInputStreamsField = FieldUtils.getDeclaredField(StreamBasedTask.class,
"consumedInputStreams",
true);
Field referencedInputStreamsField = FieldUtils.getDeclaredField(StreamBasedTask.class,
"referencedInputStreams",
true);
Field outputStreamField = FieldUtils.getDeclaredField(StreamBasedTask.class,
"outputStream",
true);
if (null == consumedInputStreamsField ||
null == referencedInputStreamsField ||
null == outputStreamField) {
throw new StopExecutionException(
"The TransformTask does not has field with name: consumedInputStreams or referencedInputStreams or outputStream! Plugin version does not support!");
}
transformTaskParam.consumedInputStreams = (Collection<TransformStream>) consumedInputStreamsField
.get(transformTask);
transformTaskParam.referencedInputStreams = (Collection<TransformStream>) referencedInputStreamsField
.get(transformTask);
transformTaskParam.outputStream = (IntermediateStream) outputStreamField.get(transformTask);
return transformTaskParam;
}
示例12: checkCommands
import org.apache.commons.lang3.reflect.FieldUtils; //导入依赖的package包/类
private void checkCommands(final String os, final String... command) throws ReflectiveOperationException {
final URL[] urLs = ((URLClassLoader) Main.class.getClassLoader()).getURLs();
ThreadClassLoaderScope scope = null;
try {
System.setProperty("os.name", os);
final URLClassLoader urlClassLoader = new URLClassLoader(urLs, null);
scope = new ThreadClassLoaderScope(urlClassLoader);
final Object terra = urlClassLoader.loadClass("org.ligoj.app.plugin.prov.terraform.TerraformUtils").newInstance();
final Object mock = MethodUtils.invokeStaticMethod(urlClassLoader.loadClass("org.mockito.Mockito"), "mock",
urlClassLoader.loadClass("org.ligoj.bootstrap.resource.system.configuration.ConfigurationResource"));
FieldUtils.writeField(terra, "configuration", mock, true);
Assert.assertEquals(Arrays.asList(command),
((ProcessBuilder) MethodUtils.invokeMethod(terra, true, "newBuilder", new Object[] { new String[] { "terraform" } }))
.command());
} finally {
IOUtils.closeQuietly(scope);
}
}
示例13: isValid
import org.apache.commons.lang3.reflect.FieldUtils; //导入依赖的package包/类
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
try {
String typeKey = (String) FieldUtils.readField(value, typeKeyField, true);
if (value instanceof XmEntity) {
return xmEntitySpecService.getAllKeys().containsKey(typeKey);
} else {
XmEntity entity = (XmEntity) FieldUtils.readField(value, entityField, true);
if (entity == null) {
return true;
}
String entityTypeKey = entity.getTypeKey();
Map<String, Set<String>> keysByEntityType = xmEntitySpecService.getAllKeys().get(entityTypeKey);
return !(keysByEntityType == null || keysByEntityType.get(getClassName(value)) == null)
&& keysByEntityType.get(getClassName(value)).contains(typeKey);
}
} catch (IllegalAccessException e) {
log.debug("Could not get keys for validation", e);
return false;
}
}
示例14: 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());
}
}
}
示例15: writeAllStaticFieldsOfType
import org.apache.commons.lang3.reflect.FieldUtils; //导入依赖的package包/类
@SuppressWarnings({ "rawtypes" })
public static <T> void writeAllStaticFieldsOfType(Class d, T t, Class c) throws IllegalAccessException
{
boolean wroteSomething = false;
for (Field f : FieldUtils.getAllFields(d))
{
if (Modifier.isStatic(f.getModifiers()) && f.getType().equals(c))
{
FieldUtils.writeStaticField(f, t, true);
wroteSomething = true;
}
}
if (!wroteSomething)
throw new IllegalAccessException();
}