本文整理了Java中java.lang.reflect.Field.setInt()
方法的一些代码示例,展示了Field.setInt()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Field.setInt()
方法的具体详情如下:
包路径:java.lang.reflect.Field
类名称:Field
方法名:setInt
[英]Set the value of the field in the specified object to the intvalue. This reproduces the effect of object.fieldName = value
If this field is static, the object argument is ignored. Otherwise, if the object is null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.
If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.
If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.
[中]将指定对象中字段的值设置为intvalue。这再现了对象的效果。字段名=值
如果此字段是静态的,则忽略对象参数。否则,如果对象为null,则抛出NullPointerException。如果该对象不是该方法声明类的实例,则会引发IllegalArgumentException。
如果此字段对象正在强制访问控制(请参见AccessibleObject),并且无法从当前上下文访问此字段,则会引发IllegaAccessException。
如果无法通过加宽转换将值转换为字段类型,则会引发IllegalArgumentException。
代码示例来源:origin: android-hacker/VirtualXposed
public void set(Object obj, int intValue) {
try {
this.field.setInt(obj, intValue);
} catch (Exception e) {
//Ignore
}
}
}
代码示例来源:origin: android-hacker/VirtualXposed
public void set(int value) {
try {
this.field.setInt(null, value);
} catch (Exception e) {
//Ignore
}
}
}
代码示例来源:origin: redisson/redisson
public final void setIntValue(Object newObj, int i1) throws IllegalAccessException {
if (!isAndroid && memOffset >= 0) {
FSTUtil.unFlaggedUnsafe.putInt(newObj, memOffset, i1);
return;
}
field.setInt(newObj, i1);
}
代码示例来源:origin: spring-projects/spring-loaded
public void setj(int newj) throws Exception {
if (fj == null) {
fj = FieldWriting.class.getDeclaredField("j");
}
fj.setInt(this, newj);
}
}
代码示例来源:origin: spring-projects/spring-loaded
public void seti(int newi) throws Exception {
if (fi == null) {
fi = FieldWriting2.class.getDeclaredField("i");
}
fi.setInt(this, newi);
}
代码示例来源:origin: stackoverflow.com
import java.lang.reflect.*;
public class EverythingIsTrue {
static void setFinalStatic(Field field, Object newValue) throws Exception {
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, newValue);
}
public static void main(String args[]) throws Exception {
setFinalStatic(Boolean.class.getField("FALSE"), true);
System.out.format("Everything is %s", false); // "Everything is true"
}
}
代码示例来源:origin: redisson/redisson
private static void patchUriField(Long maskValue, String fieldName)
throws IOException {
try {
Field field = URI.class.getDeclaredField(fieldName);
Field modifiers = Field.class.getDeclaredField("modifiers");
modifiers.setAccessible(true);
modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.setAccessible(true);
field.setLong(null, maskValue);
} catch (NoSuchFieldException e) {
// skip for Android platform
} catch (Exception e) {
throw new IOException(e);
}
}
代码示例来源:origin: redisson/redisson
private static void patchUriField(Long maskValue, String fieldName)
throws IOException {
try {
Field field = URI.class.getDeclaredField(fieldName);
Field modifiers = Field.class.getDeclaredField("modifiers");
modifiers.setAccessible(true);
modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.setAccessible(true);
field.setLong(null, maskValue);
} catch (NoSuchFieldException e) {
// skip for Android platform
} catch (Exception e) {
throw new IOException(e);
}
}
代码示例来源:origin: stackoverflow.com
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
Field mDragger = mDrawerLayout.getClass().getDeclaredField(
"mLeftDragger");//mRightDragger for right obviously
mDragger.setAccessible(true);
ViewDragHelper draggerObj = (ViewDragHelper) mDragger
.get(mDrawerLayout);
Field mEdgeSize = draggerObj.getClass().getDeclaredField(
"mEdgeSize");
mEdgeSize.setAccessible(true);
int edge = mEdgeSize.getInt(draggerObj);
mEdgeSize.setInt(draggerObj, edge * 5); //optimal value as for me, you may set any constant in dp
代码示例来源:origin: stackoverflow.com
static void setFinalStatic(Field field, Object newValue) throws Exception {
field.setAccessible(true);
// 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);
}
代码示例来源:origin: robolectric/robolectric
private static void makeFieldVeryAccessible(Field field) {
field.setAccessible(true);
try {
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
try {
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
} catch (NoSuchFieldException e) {
// ignore missing fields
}
}
代码示例来源:origin: apache/incubator-dubbo
@Override
void deserialize(AbstractHessianInput in, Object obj)
throws IOException {
int value = 0;
try {
value = in.readInt();
_field.setInt(obj, value);
} catch (Exception e) {
logDeserializeError(_field, obj, value, e);
}
}
}
代码示例来源:origin: Meituan-Dianping/Robust
public static void setFieldValue(String name, Object instance, int value, Class cl) {
try {
getReflectField(name, instance, cl).setInt(instance, value);
} catch (Exception e) {
e.printStackTrace();
if (isThrowable) {
throw new RuntimeException("setFieldValue int error " + name + " target " + instance + " value " + value);
}
}
}
代码示例来源:origin: Meituan-Dianping/Robust
public static void setStaticFieldValue(String name, Class clazz, int value) {
try {
getReflectStaticField(name, clazz).setInt(null, value);
} catch (Exception e) {
e.printStackTrace();
if (isThrowable) {
throw new RuntimeException("setStaticFieldValue int error " + name + " Class " + clazz + " value " + value);
}
}
}
代码示例来源:origin: hankcs/HanLP
public static void setStaticFinalField(
Field field, Object value)
throws NoSuchFieldException, IllegalAccessException
{
// 获得 public 权限
field.setAccessible(true);
// 将modifiers域设为非final,这样就可以修改了
Field modifiersField =
Field.class.getDeclaredField(MODIFIERS_FIELD);
modifiersField.setAccessible(true);
int modifiers = modifiersField.getInt(field);
// 去掉 final 标志位
modifiers &= ~Modifier.FINAL;
modifiersField.setInt(field, modifiers);
FieldAccessor fa = reflection.newFieldAccessor(
field, false
);
fa.set(null, value);
}
}
代码示例来源:origin: ben-manes/caffeine
/** Force the random seed to a predictable value. */
public static void ensureRandomSeed(FrequencySketch<?> sketch) {
try {
Field field = FrequencySketch.class.getDeclaredField("randomSeed");
field.setAccessible(true);
field.setInt(sketch, RANDOM_SEED);
} catch (Exception e) {
Throwables.throwIfUnchecked(e);
throw new RuntimeException(e);
}
}
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
public DatePatternConverterTest(final Boolean threadLocalEnabled) throws Exception {
// Setting the system property does not work: the Constant field has already been initialized...
//System.setProperty("log4j2.enable.threadlocals", threadLocalEnabled.toString());
final Field field = Constants.class.getDeclaredField("ENABLE_THREADLOCALS");
field.setAccessible(true); // make non-private
final Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); // make non-final
field.setBoolean(null, threadLocalEnabled.booleanValue());
}
代码示例来源:origin: apache/ignite
/** Reflections {@link GridDiscoveryManager#mem} field which was made accessible and mutable */
@NotNull private Field createAccessibleMemField() throws NoSuchFieldException, IllegalAccessException {
final Field memField = GridDiscoveryManager.class.getDeclaredField("mem");
memField.setAccessible(true);
final Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(memField, memField.getModifiers() & ~Modifier.FINAL);
return memField;
}
代码示例来源:origin: apache/hbase
private int setNumTries(ConnectionImplementation hci, int newVal) throws Exception {
Field numTries = hci.getClass().getDeclaredField("numTries");
numTries.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(numTries, numTries.getModifiers() & ~Modifier.FINAL);
final int prevNumRetriesVal = (Integer)numTries.get(hci);
numTries.set(hci, newVal);
return prevNumRetriesVal;
}
代码示例来源:origin: apache/incubator-druid
public static void forceRandomSeed(CaffeineCache cache) throws Exception
{
final Map map = cache.getCache().asMap();
final Method getFrequencySketch = map.getClass().getDeclaredMethod("frequencySketch");
getFrequencySketch.setAccessible(true);
final Object frequencySketch = getFrequencySketch.invoke(map);
final Field seedField = frequencySketch.getClass().getDeclaredField("randomSeed");
seedField.setAccessible(true);
seedField.setInt(frequencySketch, RANDOM_SEED);
}
}
内容来源于网络,如有侵权,请联系作者删除!