List of usage examples for java.lang.reflect Field setInt
@CallerSensitive @ForceInline public void setInt(Object obj, int i) throws IllegalArgumentException, IllegalAccessException
From source file:com.adobe.cq.wcm.core.components.internal.servlets.AdaptiveImageServletTest.java
private void setFinalStatic(Field field, Object newValue) throws Exception { field.setAccessible(true);/* w w w . ja v a2 s.co m*/ // remove final modifier from field Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); field.set(null, newValue); }
From source file:org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDataChangeListenerTest.java
void setFinalStatic(Class cls, String fieldName, Object newValue) throws Exception { Field fields[] = FieldUtils.getAllFields(cls); for (Field field : fields) { if (fieldName.equals(field.getName())) { field.setAccessible(true);// www. j a v a2s . co m Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); field.set(null, newValue); break; } } }
From source file:org.kaaproject.kaa.server.appenders.oraclenosql.appender.OracleNoSqlLogAppenderTest.java
@Test public void doAppendClosedTest() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { Logger testLogger = Mockito.mock(Logger.class); Field field = logAppender.getClass().getDeclaredField("LOG"); field.setAccessible(true);// w ww . j a v a 2 s .c om Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); field.set(null, testLogger); logAppender.close(); TestLogDeliveryCallback callback = new TestLogDeliveryCallback(); LogSchemaDto schemaDto = new LogSchemaDto(); LogSchema schema = new LogSchema(schemaDto, BasicEndpointProfile.SCHEMA$.toString()); EndpointProfileDataDto profileDto = new EndpointProfileDataDto("1", ENDPOINT_KEY, 1, "test", 0, null); BaseLogEventPack logEventPack = new BaseLogEventPack(profileDto, DATE_CREATED, schema.getVersion(), null); logEventPack.setLogSchema(schema); logAppender.doAppend(logEventPack, callback); Assert.assertTrue(callback.internallError); }
From source file:de.micromata.genome.util.bean.PrivateBeanUtils.java
/** * Write final static field.//from w ww . j a v a2 s .co m * * @param field the field * @param value the value */ public static void writeFinalStaticField(Field field, Object value) { AccessibleScope ascope = new AccessibleScope(field); try { Field modifiersField = Field.class.getDeclaredField("modifiers"); AccessibleScope modscope = new AccessibleScope(field); try { int oldmods = field.getModifiers(); modifiersField.setInt(field, oldmods & ~Modifier.FINAL); writeField(null, field, value); modifiersField.setInt(field, oldmods); } finally { modscope.restore(); } } catch (RuntimeException ex) { // NOSONAR "Illegal Catch" framework throw ex; // NOSONAR "Illegal Catch" framework } catch (Exception ex) { // NOSONAR "Illegal Catch" framework throw new RuntimeException( "Cannot write field: " + field.getDeclaringClass().getName() + "." + field.getName());// NOSONAR "Illegal Catch" framework } finally { ascope.restore(); } }
From source file:com.robestone.hudson.compactcolumns.CompactColumnsTest.java
public void testStableColor() throws Exception { assertEquals(Color.BLUE, BuildInfo.getStableColor()); assertFalse(ColorPalette.BLUE.equals(BuildInfo.getStableColor())); Field colorValue = Color.class.getDeclaredField("value"); colorValue.setAccessible(true);/*from www.j a v a2 s. c o m*/ colorValue.setInt(ColorPalette.BLUE, new Color(172, 218, 0).getRGB()); assertEquals(ColorPalette.BLUE, BuildInfo.getStableColor()); assertFalse(Color.BLUE.equals(BuildInfo.getStableColor())); }
From source file:org.sonar.api.checks.AnnotationCheckFactory.java
private void configureField(Object check, Field field, String value) { try {//from www. j av a 2 s. co m field.setAccessible(true); if (field.getType().equals(String.class)) { field.set(check, value); } else if ("int".equals(field.getType().getSimpleName())) { field.setInt(check, Integer.parseInt(value)); } else if ("short".equals(field.getType().getSimpleName())) { field.setShort(check, Short.parseShort(value)); } else if ("long".equals(field.getType().getSimpleName())) { field.setLong(check, Long.parseLong(value)); } else if ("double".equals(field.getType().getSimpleName())) { field.setDouble(check, Double.parseDouble(value)); } else if ("boolean".equals(field.getType().getSimpleName())) { field.setBoolean(check, Boolean.parseBoolean(value)); } else if ("byte".equals(field.getType().getSimpleName())) { field.setByte(check, Byte.parseByte(value)); } else if (field.getType().equals(Integer.class)) { field.set(check, Integer.parseInt(value)); } else if (field.getType().equals(Long.class)) { field.set(check, Long.parseLong(value)); } else if (field.getType().equals(Double.class)) { field.set(check, Double.parseDouble(value)); } else if (field.getType().equals(Boolean.class)) { field.set(check, Boolean.parseBoolean(value)); } else { throw new SonarException( "The type of the field " + field + " is not supported: " + field.getType()); } } catch (IllegalAccessException e) { throw new SonarException( "Can not set the value of the field " + field + " in the class: " + check.getClass().getName(), e); } }
From source file:org.jgentleframework.utils.ReflectUtils.java
/** * Sets new value to static final field. * <p>//from w ww . java 2 s .c o m * <b>Note</b>: this method only run on Sun JDK * * @param field * the field * @param value * the new value * @throws NoSuchFieldException * the no such field exception * @throws IllegalAccessException * the illegal access exception */ public static void setStaticFinalField(Field field, Object value) throws NoSuchFieldException, IllegalAccessException { field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField(MODIFIERS_FIELD); modifiersField.setAccessible(true); int modifiers = modifiersField.getInt(field); modifiers &= ~Modifier.FINAL; modifiersField.setInt(field, modifiers); FieldAccessor fa = reflection.newFieldAccessor(field, false); fa.set(null, value); }
From source file:com.microsoft.tfs.client.common.ui.framework.layout.GridDataBuilder.java
public GridDataBuilder vIndent(final int verticalIndent) { try {//from ww w.ja v a 2s . c o m final Class gdClass = gridData.getClass(); final Field viField = gdClass.getField("verticalIndent"); //$NON-NLS-1$ viField.setInt(gridData, verticalIndent); } catch (final Exception e) { // PRE SWT 3.1, ignore } return this; }
From source file:com.baidu.drapi.autosdk.core.CommonService.java
/** * Get current version./*from w w w . j a va 2 s . c o m*/ * @return */ // protected Version getCurrentVersion() { // return currentVersion; // } private void setField(Field field, String value) throws Exception { field.setAccessible(true); Class<?> cls = field.getType(); if (cls.equals(int.class)) { field.setInt(this, Integer.parseInt(value)); } else if (cls.equals(long.class)) { field.setLong(this, Long.parseLong(value)); } else if (cls.equals(boolean.class)) { field.setBoolean(this, Boolean.parseBoolean(value)); } else if (cls.equals(Integer.class)) { field.set(this, Integer.parseInt(value)); } else if (cls.equals(Long.class)) { field.set(this, Long.parseLong(value)); } else if (cls.equals(Boolean.class)) { field.set(this, Boolean.parseBoolean(value)); } else { field.set(this, value); } }
From source file:org.sonar.api.checks.checkers.AnnotationCheckerFactory.java
private void configureField(Object checker, Field field, Map.Entry<String, String> parameter) throws IllegalAccessException { field.setAccessible(true);//from w w w.j av a2 s. c o m if (field.getType().equals(String.class)) { field.set(checker, parameter.getValue()); } else if (field.getType().getSimpleName().equals("int")) { field.setInt(checker, Integer.parseInt(parameter.getValue())); } else if (field.getType().getSimpleName().equals("short")) { field.setShort(checker, Short.parseShort(parameter.getValue())); } else if (field.getType().getSimpleName().equals("long")) { field.setLong(checker, Long.parseLong(parameter.getValue())); } else if (field.getType().getSimpleName().equals("double")) { field.setDouble(checker, Double.parseDouble(parameter.getValue())); } else if (field.getType().getSimpleName().equals("boolean")) { field.setBoolean(checker, Boolean.parseBoolean(parameter.getValue())); } else if (field.getType().getSimpleName().equals("byte")) { field.setByte(checker, Byte.parseByte(parameter.getValue())); } else if (field.getType().equals(Integer.class)) { field.set(checker, new Integer(Integer.parseInt(parameter.getValue()))); } else if (field.getType().equals(Long.class)) { field.set(checker, new Long(Long.parseLong(parameter.getValue()))); } else if (field.getType().equals(Double.class)) { field.set(checker, new Double(Double.parseDouble(parameter.getValue()))); } else if (field.getType().equals(Boolean.class)) { field.set(checker, Boolean.valueOf(Boolean.parseBoolean(parameter.getValue()))); } else { throw new UnvalidCheckerException( "The type of the field " + field + " is not supported: " + field.getType()); } }