List of usage examples for org.apache.commons.lang3.reflect FieldUtils writeField
public static void writeField(final Object target, final String fieldName, final Object value, final boolean forceAccess) throws IllegalAccessException
From source file:com.mkl.websuites.command.BaseCommand.java
/** * Used internally to inject source file information in the command error messages. * //from w w w . j av a2 s. c o m * @param exception */ protected void augmentErrorMessageWithCommandSourceFileInfo(Throwable exception) { try { String newMessage = exception.getMessage() + "\n" + getCommandSourceLine().printSourceInfo(); FieldUtils.writeField(exception, "detailMessage", newMessage, true); } catch (IllegalArgumentException | IllegalAccessException | SecurityException e1) { e1.printStackTrace(); } }
From source file:com.github.mjeanroy.junit.servers.annotations.handlers.HttpClientAnnotationHandlerTest.java
@Test public void it_should_set_client_instance() throws Exception { EmbeddedServer server = mock(EmbeddedServer.class); Foo foo = new Foo(); Field field = Foo.class.getDeclaredField("client"); HttpClientAnnotationHandler handler = newHttpClientAnnotationHandler(server); handler.before(foo, field);/*from ww w . j av a2s . c o m*/ assertThat(foo.client).isNotNull(); HttpClient spy = spy(foo.client); FieldUtils.writeField(foo, "client", spy, true); handler.after(foo, field); verify(spy).destroy(); assertThat(foo.client).isNull(); }
From source file:com.adeptj.modules.jaxrs.resteasy.internal.ResteasyProviderFactoryAdapter.java
ResteasyProviderFactoryAdapter(String[] blacklistedProviders) throws ServletException { this.blacklistedProviders = blacklistedProviders; this.providers = new CopyOnWriteArraySet<>(); // 27.03.2019 - Using reflection as the field is made private in 4.0.0.RC1, it was protected earlier. try {/*w ww. ja v a2 s .c o m*/ FieldUtils.writeField(this, FIELD_PROVIDER_INSTANCES, this.providers, true); } catch (IllegalAccessException iae) { throw new ServletException(iae); } }
From source file:com.astamuse.asta4d.util.annotation.AnnotatedPropertyInfo.java
public void assignValue(Object instance, Object value) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { if (this.field == null) { setter.invoke(instance, value);/*w w w . ja v a2s .c o m*/ } else { FieldUtils.writeField(field, instance, value, true); } }
From source file:com.adobe.acs.commons.mcp.util.AnnotatedFieldDeserializer.java
@SuppressWarnings("squid:S3776") private static void parseInput(Object target, ValueMap input, Field field) throws ReflectiveOperationException, ParseException { FormField inputAnnotation = field.getAnnotation(FormField.class); Object value;//from w ww. jav a 2 s.c o m if (input.get(field.getName()) == null) { if (inputAnnotation != null && inputAnnotation.required()) { if (field.getType() == Boolean.class || field.getType() == Boolean.TYPE) { value = false; } else { throw new NullPointerException("Required field missing: " + field.getName()); } } else { return; } } else { value = input.get(field.getName()); } if (hasMultipleValues(field.getType())) { parseInputList(target, serializeToStringArray(value), field); } else { Object val = value; if (value.getClass().isArray()) { val = ((Object[]) value)[0]; } if (val instanceof RequestParameter) { /** Special case handling uploaded files; Method call ~ copied from parseInputValue(..) **/ if (field.getType() == RequestParameter.class) { FieldUtils.writeField(field, target, val, true); } else { try { FieldUtils.writeField(field, target, ((RequestParameter) val).getInputStream(), true); } catch (IOException ex) { LOG.error("Unable to get InputStream for uploaded file [ {} ]", ((RequestParameter) val).getName(), ex); } } } else { parseInputValue(target, String.valueOf(val), field); } } }
From source file:core.plugin.mybatis.PageInterceptor.java
@Override public Object intercept(Invocation inv) throws Throwable { // prepare?Connection Connection connection = (Connection) inv.getArgs()[0]; String dbType = connection.getMetaData().getDatabaseProductName(); L.debug(dbType);//from w w w . ja va 2s. c o m Dialect dialect = null; if (StringUtils.equalsIgnoreCase("ORACLE", dbType)) { dialect = new OracleDialect(); } else if (StringUtils.equalsIgnoreCase("H2", dbType)) { dialect = new H2Dialect(); } else { throw new AppRuntimeException("A404: Not Support ['" + dbType + "'] Pagination Yet!"); } StatementHandler target = (StatementHandler) inv.getTarget(); BoundSql boundSql = target.getBoundSql(); String sql = boundSql.getSql(); if (StringUtils.isBlank(sql)) { return inv.proceed(); } // ?select?? if (sql.matches(SQL_SELECT_REGEX) && !Pattern.matches(SQL_COUNT_REGEX, sql)) { Object obj = FieldUtils.readField(target, "delegate", true); // ??? RowBounds RowBounds rowBounds = (RowBounds) FieldUtils.readField(obj, "rowBounds", true); // ??SQL if (rowBounds != null && rowBounds != RowBounds.DEFAULT) { FieldUtils.writeField(boundSql, "sql", dialect.getSqlWithPagination(sql, rowBounds), true); // ???(?) FieldUtils.writeField(rowBounds, "offset", RowBounds.NO_ROW_OFFSET, true); FieldUtils.writeField(rowBounds, "limit", RowBounds.NO_ROW_LIMIT, true); } } return inv.proceed(); }
From source file:com.adobe.acs.commons.mcp.util.AnnotatedFieldDeserializer.java
private static void parseInputList(Object target, String[] values, Field field) throws ReflectiveOperationException, ParseException { List convertedValues = new ArrayList(); Class type = getCollectionComponentType(field); for (String value : values) { Object val = convertValue(value, type); convertedValues.add(val); }//from www. j a v a2s. c o m if (field.getType().isArray()) { Object array = Array.newInstance(field.getType().getComponentType(), convertedValues.size()); for (int i = 0; i < convertedValues.size(); i++) { Array.set(array, i, convertedValues.get(i)); } FieldUtils.writeField(field, target, array, true); } else { Collection c = (Collection) getInstantiatableListType(field.getType()).newInstance(); c.addAll(convertedValues); FieldUtils.writeField(field, target, c, true); } }
From source file:com.adobe.acs.commons.mcp.util.AnnotatedFieldDeserializer.java
private static void parseInputValue(Object target, String value, Field field) throws ReflectiveOperationException, ParseException { FieldUtils.writeField(field, target, convertValue(value, field.getType()), true); }
From source file:com.carmanconsulting.cassidy.util.CassidyUtils.java
public static void setFieldValue(Object target, String fieldName, Object value) { try {/*from w ww.j a v a2 s . c o m*/ LOGGER.debug("Setting field {} to value {} on object {} (type={})...", fieldName, value, target, getClassName(target)); FieldUtils.writeField(target, fieldName, value, true); } catch (IllegalAccessException e) { throw new CassidyException(String.format("Unable to write field %s value on object of type %s.", fieldName, getClassName(target)), e); } }
From source file:com.opencsv.bean.AbstractBeanField.java
/** * Sets a field in a bean if there is no setter available. * Turns off all accessibility checking to accomplish the goal, and handles * errors as best it can./* www .ja v a2 s . c o m*/ * * @param <T> Type of the bean * @param bean The bean in which the field is located * @param obj The data to be assigned to this field of the destination bean * @throws CsvDataTypeMismatchException If the data to be assigned cannot * be assigned */ private <T> void writeWithoutSetter(T bean, Object obj) throws CsvDataTypeMismatchException { try { FieldUtils.writeField(field, bean, obj, true); } catch (IllegalAccessException e2) { // The Apache Commons Lang Javadoc claims this can be thrown // if the field is final, but it's not true if we override // accessibility. This is never thrown. } catch (IllegalArgumentException e2) { CsvDataTypeMismatchException csve = new CsvDataTypeMismatchException(obj, field.getType()); csve.initCause(e2); throw csve; } }