本文整理匯總了Java中org.apache.commons.lang3.reflect.FieldUtils.readField方法的典型用法代碼示例。如果您正苦於以下問題:Java FieldUtils.readField方法的具體用法?Java FieldUtils.readField怎麽用?Java FieldUtils.readField使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.lang3.reflect.FieldUtils
的用法示例。
在下文中一共展示了FieldUtils.readField方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: 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;
}
示例2: 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;
}
}
示例3: handle
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
@Override
public void handle(Connection connection, ChatMessagePacket packet)
{
Field connectionsField = FieldUtils.getField(Server.class, "connections", true);
Connection[] connections;
try
{
connections = (Connection[])FieldUtils.readField(connectionsField, server, true);
} catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
ChatMessageReplyPacket newPacket = new ChatMessageReplyPacket();
Character sender = gameData.getUserCharacterByConnectionId(connection.getID());
String nickname = sender.getNickname();
newPacket.setMessage(packet.getMessage());
newPacket.setNickname(nickname);
Character character;
for(Connection client : connections)
if((character = gameData.getUserCharacterByConnectionId(client.getID())) != null)
{
newPacket.setSourceCharacterId(character.getId());
server.sendToTCP(client.getID(), newPacket);
}
}
示例4: closeWebDriverPool
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
private void closeWebDriverPool() {
try {
Runtime runtime = (Runtime) FieldUtils.readField(this, "runtime", true);
Collection<? extends Backend> backends =
(Collection<? extends Backend>) FieldUtils.readField(runtime, "backends", true);
for (Backend backend : backends) {
if (backend instanceof JavaBackend) {
GuiceFactory factory =
(GuiceFactory) FieldUtils.readField(backend, "objectFactory", true);
WebDriverRegistry webDriverRegistry = factory.getInstance(WebDriverRegistry.class);
webDriverRegistry.shutdown();
}
}
} catch (IllegalAccessException e) {
LOG.error("unable to close web driver pool", e);
}
}
示例5: getResourcePath
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
protected String getResourcePath() {
String id = getObjectId();
String account_id = getAccountId();
String resourcePath = null;
try {
String resource = (String) FieldUtils.readField(resourceObject, "RESOURCE", true);
String resourceCollection = (String) FieldUtils.readField(resourceObject, "RESOURCE_COLLECTION", true);
if (null != resourceCollection) {
resourcePath = (id != null) ? resource.replace("{account_id}", account_id).replace("{id}", id)
: resourceCollection.replace("{account_id}", account_id);
}
} catch (Exception e) {
log.warn("unable to read 'RESOURCE' or 'RESOURCE_COLLECTION', errror : {}", e.toString());
// return null;
}
log.info("resource path = "+resourcePath);
return resourcePath;
}
示例6: getProperty
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
/**
* Gets the property.
*
* @param obj the obj
* @param name the name
* @param defaultValue the default value
* @param forced whether or not to force access to the field
* @return the property
*/
protected Object getProperty(Object obj, String name, Object defaultValue, boolean forced) {
try {
if (forced) {
return FieldUtils.readField(obj, name, forced);
} else {
return PropertyUtils.isReadable(obj, name) ? PropertyUtils.getProperty(obj, name)
: defaultValue;
}
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException
| NoSuchMethodException e) {
logger.error("", e);
}
logger.debug("Could not access property '{}' of object '{}'", name, obj);
return defaultValue;
}
示例7: addItemToCollection
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
private void addItemToCollection(Object object, String fieldName, Object value)
throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
Object collection = null;
try {
collection = FieldUtils.readField(object, fieldName);
if (!(collection instanceof Collection)) {
collection = null;
}
} catch (Exception e) {
// Do nothing -> just using this to check if we have to use the field or the addXxxx() method
}
if (collection != null) {
MethodUtils.invokeExactMethod(collection, "add", new Object[]{value}, new Class[]{Object.class});
} else {
MethodUtils.invokeExactMethod(object, "add" + StringUtils.capitalize(fieldName), value);
}
}
示例8: shouldInvoke
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
private boolean shouldInvoke(MessageContext messageContext) throws Exception {
List<EndpointMapping> endpointMappings = messageDispatcher.getEndpointMappings();
// There should be only one endpoint mapped in the module: check it
Assert.isTrue(endpointMappings != null && endpointMappings.size() == 1);
EndpointMapping endpointMapping = endpointMappings.iterator().next();
for (EndpointInterceptor endpointInterceptor : endpointMapping.getEndpoint(messageContext).getInterceptors()) {
// Check to see if the interceptor is directly an instance of a service specific interceptor.
// If so, we don't have to fall back to our default delegate
if (endpointInterceptor instanceof ServiceSpecificEndpointInterceptor) {
return false;
}
// Check to see if the interceptor is decorated by DelegatingSmartEndpointInterceptor
// Unfortunately there is no standard API way of detecting this, so we need some reflection.
// If so, check that the decorated instance is instance of a service specific interceptor.
// If so, we don't have to fall back to our default delegate
if (endpointInterceptor instanceof DelegatingSmartEndpointInterceptor) {
if (FieldUtils.readField(endpointInterceptor, "delegate", true) instanceof ServiceSpecificEndpointInterceptor) {
return false;
}
}
}
return true;
}
示例9: getBXMLFieldValues
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
public static Map<String, Object> getBXMLFieldValues(Bindable obj) throws IllegalAccessException {
Map<String, Object> result = new HashMap<>();
Class<?> type = obj.getClass();
Field[] allFields = FieldUtils.getAllFields(type);
if (ArrayUtils.isNotEmpty(allFields)) {
for (Field field : allFields) {
BXML bxmlAnnotation = field.getAnnotation(BXML.class);
if (bxmlAnnotation != null) {
String id = bxmlAnnotation.id();
Object fieldValue = FieldUtils.readField(field, obj, true);
if (StringUtils.isNotBlank(id)) {
result.put(id, fieldValue);
} else {
result.put(field.getName(), fieldValue);
}
}
}
}
return result;
}
示例10: getForgeListenerOwners
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
public static Map<Object, ModContainer> getForgeListenerOwners() {
try {
return ((Map<Object, ModContainer>) FieldUtils.readField(MinecraftForge.EVENT_BUS, "listenerOwners", true));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
示例11: nullKeys
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
/**
* Get the keys of the element that have missing values.
*/
@SneakyThrows
public static <E extends Element> List<String> nullKeys(E element) {
List<String> keys = new ArrayList<>();
for (Field field : fields(element)) {
Object propertyValue = FieldUtils.readField(field, element);
if (isMissing(propertyValue)) {
String propertyName = propertyKey(field);
keys.add(propertyName);
}
}
return keys;
}
示例12: makePackageProcessBuilder
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
@Override
protected ProcessInfoBuilder makePackageProcessBuilder(AaptPackageConfig config) throws AaptException {
ProcessInfoBuilder processInfoBuilder = super.makePackageProcessBuilder(config);
List<String> args = null;
try {
args = (List<String>) FieldUtils.readField(processInfoBuilder, "mArgs", true);
} catch (IllegalAccessException e) {
throw new GradleException("getargs exception", e);
}
//args.remove("--no-version-vectors");
//
////Does not generate manifest_keep.txt file
//int indexD = args.indexOf("-D");
//if (indexD > 0){
// args.remove(indexD);
// args.remove(indexD);
//}
//Join the outputs of the R.txt file
String sybolOutputDir = config.getSymbolOutputDir().getAbsolutePath();
if (!args.contains("--output-text-symbols") && null != sybolOutputDir) {
args.add("--output-text-symbols");
args.add(sybolOutputDir);
}
return processInfoBuilder;
}
示例13: readFieldOfType
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
public static <T> T readFieldOfType(Object o, Class<T> t) throws IllegalAccessException
{
for (Field f : FieldUtils.getAllFields(o.getClass()))
{
if (!Modifier.isStatic(f.getModifiers()) && f.getType().equals(t))
{
return (T) FieldUtils.readField(f, o, true);
}
}
throw new IllegalAccessException();
}
示例14: readParameterizedFieldOfType
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
public static <C,T> C readParameterizedFieldOfType(Object o, Class<C> collection, Class<T> genertic) throws IllegalAccessException
{
for (Field f : FieldUtils.getAllFields(o.getClass()))
{
if (!Modifier.isStatic(f.getModifiers()) && f.getType().equals(collection) && f.getGenericType().equals(genertic))
{
return (C) FieldUtils.readField(f, o, true);
}
}
throw new IllegalAccessException();
}
示例15: hasImage
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
private boolean hasImage(CallbackMessage<CallbackWallPost> message, List<EmbedBuilder> builders) {
EmbedBuilder prevBuilder = CollectionUtils.isNotEmpty(builders) ? builders.get(builders.size() - 1) : null;
try {
if (prevBuilder != null && FieldUtils.readField(prevBuilder, "image", true) != null) {
return true;
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
return false;
}