本文整理匯總了Java中java.lang.reflect.Field.set方法的典型用法代碼示例。如果您正苦於以下問題:Java Field.set方法的具體用法?Java Field.set怎麽用?Java Field.set使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.reflect.Field
的用法示例。
在下文中一共展示了Field.set方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: CustomZombie
import java.lang.reflect.Field; //導入方法依賴的package包/類
public CustomZombie(Player player, PetBlock petBlock) {
super(((CraftWorld) player.getWorld()).getHandle());
this.b(true);
try {
final Field bField = PathfinderGoalSelector.class.getDeclaredField("b");
bField.setAccessible(true);
final Field cField = PathfinderGoalSelector.class.getDeclaredField("c");
cField.setAccessible(true);
bField.set(this.goalSelector, new UnsafeList<PathfinderGoalSelector>());
bField.set(this.targetSelector, new UnsafeList<PathfinderGoalSelector>());
cField.set(this.goalSelector, new UnsafeList<PathfinderGoalSelector>());
cField.set(this.targetSelector, new UnsafeList<PathfinderGoalSelector>());
this.getAttributeInstance(GenericAttributes.d).setValue(0.30000001192092896D * ConfigPet.getInstance().getModifier_petwalking());
this.goalSelector.a(0, new PathfinderGoalFloat(this));
this.goalSelector.a(1, new OwnerPathfinder(this,petBlock));
} catch (final Exception exc) {
PetBlocksPlugin.logger().log(Level.WARNING, "EntityNMS exception.", exc);
}
this.petBlock = petBlock;
this.S = (float) ConfigPet.getInstance().getModifier_petclimbing();
}
示例2: hackNatives
import java.lang.reflect.Field; //導入方法依賴的package包/類
private static void hackNatives()
{
String paths = System.getProperty("java.library.path");
String nativesDir = "F:/GradleStore/caches/minecraft/net/minecraft/natives/1.10.2";
if (Strings.isNullOrEmpty(paths))
paths = nativesDir;
else
paths += File.pathSeparator + nativesDir;
System.setProperty("java.library.path", paths);
// hack the classloader now.
try
{
final Field sysPathsField = ClassLoader.class.getDeclaredField("sys_paths");
sysPathsField.setAccessible(true);
sysPathsField.set(null, null);
}
catch(Throwable t) {};
}
示例3: addLibraryDir
import java.lang.reflect.Field; //導入方法依賴的package包/類
private static void addLibraryDir(String libraryPath) throws Exception {
Field userPathsField = ClassLoader.class.getDeclaredField("usr_paths");
userPathsField.setAccessible(true);
String[] paths = (String[]) userPathsField.get(null);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < paths.length; i++) {
if (libraryPath.equals(paths[i])) {
continue;
}
sb.append(paths[i]).append(';');
}
sb.append(libraryPath);
System.setProperty("java.library.path", sb.toString());
final Field sysPathsField = ClassLoader.class.getDeclaredField("sys_paths");
sysPathsField.setAccessible(true);
sysPathsField.set(null, null);
}
示例4: setField
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* 設置給定的對象中給定名稱的字段的值
*
* @param object 給定的對象
* @param fieldName 要設置的字段的名稱
* @param newValue 要設置的字段的值
* @param isFindDeclaredField 是否查找Declared字段
* @param isUpwardFind 如果在當前類中找不到的話,是否取其父類中查找
* @return 設置是否成功。false:字段不存在或新的值與字段的類型不一樣,導致轉型失敗
*/
public static boolean setField(Object object, String fieldName, Object newValue, boolean isFindDeclaredField, boolean isUpwardFind) {
boolean result = false;
Field field = getField(object.getClass(), fieldName, isFindDeclaredField, isUpwardFind);
if (field != null) {
try {
field.setAccessible(true);
field.set(object, newValue);
result = true;
} catch (IllegalAccessException e) {
e.printStackTrace();
result = false;
}
}
return result;
}
示例5: clearPreloadTypedArrayIssue
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* Why must I do these?
* Resource has mTypedArrayPool field, which just like Message Poll to reduce gc
* MiuiResource change TypedArray to MiuiTypedArray, but it get string block from offset instead of assetManager
*/
private static void clearPreloadTypedArrayIssue(Resources resources) {
// Perform this trick not only in Miui system since we can't predict if any other
// manufacturer would do the same modification to Android.
// if (!isMiuiSystem) {
// return;
// }
Log.w(TAG, "try to clear typedArray cache!");
// Clear typedArray cache.
try {
Field typedArrayPoolField = ShareReflectUtil.findField(Resources.class, "mTypedArrayPool");
final Object origTypedArrayPool = typedArrayPoolField.get(resources);
Field poolField = ShareReflectUtil.findField(origTypedArrayPool, "mPool");
final Constructor<?> typedArrayConstructor = origTypedArrayPool.getClass().getConstructor(int.class);
typedArrayConstructor.setAccessible(true);
final int poolSize = ((Object[]) poolField.get(origTypedArrayPool)).length;
final Object newTypedArrayPool = typedArrayConstructor.newInstance(poolSize);
typedArrayPoolField.set(resources, newTypedArrayPool);
} catch (Throwable ignored) {
Log.e(TAG, "clearPreloadTypedArrayIssue failed, ignore error: " + ignored);
}
}
示例6: setup
import java.lang.reflect.Field; //導入方法依賴的package包/類
@Before
public void setup() throws IllegalAccessException, NoSuchFieldException {
Assume.assumeTrue(System.getProperty("skip.long") == null);
TestUtils.disableSslCertChecking();
amazonS3Client = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(
LocalstackTestRunner.getEndpointS3(),
LocalstackTestRunner.getDefaultRegion()))
.withChunkedEncodingDisabled(true)
.withPathStyleAccessEnabled(true).build();
amazonS3Client.createBucket(bucketName);
S3Config config = new S3Config();
Field field = StorageServiceImpl.class.getDeclaredField("s3TransferManager");
field.setAccessible(true);
field.set(underTest, config.s3TransferManager(amazonS3Client));
field = StorageServiceImpl.class.getDeclaredField("environment");
field.setAccessible(true);
field.set(underTest, environment);
}
示例7: scanIdAnnotations
import java.lang.reflect.Field; //導入方法依賴的package包/類
private static void scanIdAnnotations(Object target, Field f, View view) {
ViewId viewIds = f.getAnnotation(ViewId.class);
if (viewIds != null) {
View targetView = findViewByIds(view, viewIds.value());
if (targetView == null && viewIds.optional()) {
return;
}
else if (!viewIds.optional() && targetView == null) {
throw new RuntimeException("Mandatory view id not found");
}
if (!f.isAccessible()) {
f.setAccessible(true);
}
try {
f.set(target, targetView);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
}
示例8: fixInputMethodManagerLeak
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* 解決InputMethodManager內存泄露現象
*/
private static void fixInputMethodManagerLeak(Context destContext) {
if (destContext == null) {
return;
}
InputMethodManager imm = (InputMethodManager) destContext
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm == null) {
return;
}
String[] arr = new String[]{"mCurRootView", "mServedView", "mNextServedView"};
Field f;
Object obj_get;
for (String param : arr) {
try {
f = imm.getClass().getDeclaredField(param);
if (!f.isAccessible()) {
f.setAccessible(true);
} // author: sodino mail:[email protected]
obj_get = f.get(imm);
if (obj_get != null && obj_get instanceof View) {
View v_get = (View) obj_get;
if (v_get
.getContext() == destContext) { // 被InputMethodManager持有引用的context是想要目標銷毀的
f.set(imm, null); // 置空,破壞掉path to gc節點
} else {
// 不是想要目標銷毀的,即為又進了另一層界麵了,不要處理,避免影響原邏輯,也就不用繼續for循環了
/*if (QLog.isColorLevel()) {
QLog.d(ReflecterHelper.class.getSimpleName(), QLog.CLR, "fixInputMethodManagerLeak break, context is not suitable, get_context=" + v_get.getContext()+" dest_context=" + destContext);
}*/
break;
}
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}
示例9: getUrl_should_return_Url_of_Type_String_StockFileLicenseProp
import java.lang.reflect.Field; //導入方法依賴的package包/類
@Test(groups = { "Getters" })
void getUrl_should_return_Url_of_Type_String_StockFileLicenseProp()
throws NoSuchFieldException, IllegalAccessException {
Field f = prop.getClass().getDeclaredField("mUrl");
f.setAccessible(true);
f.set(prop, "www.example.com");
Assert.assertTrue(prop.getUrl().equals("www.example.com"));
}
示例10: setPrivateField
import java.lang.reflect.Field; //導入方法依賴的package包/類
public final static void setPrivateField(final Object obj, final String field_name, final Object value) {
Field field = getFieldIncludingSuper(obj.getClass(), field_name);
if (field != null) {
field.setAccessible(true);
try {
field.set(obj, value);
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
示例11: getFilterEditorial_should_return_media_Filter_Editorial_of_Type_Boolean
import java.lang.reflect.Field; //導入方法依賴的package包/類
@Test(groups = { "Getters" })
void getFilterEditorial_should_return_media_Filter_Editorial_of_Type_Boolean()
throws NoSuchFieldException,
IllegalAccessException {
Assert.assertTrue(param.getFilterEditorial() == null);
Field f = param.getClass().getDeclaredField("mFilterEditorial");
f.setAccessible(true);
f.set(param, true);
Assert.assertEquals(true, param.getFilterEditorial().booleanValue());
}
示例12: getCreatorName_should_return_media_CreatorName_ofType_String_StockLicenseHistoryFile
import java.lang.reflect.Field; //導入方法依賴的package包/類
@Test(groups = { "Getters" })
void getCreatorName_should_return_media_CreatorName_ofType_String_StockLicenseHistoryFile()
throws NoSuchFieldException, IllegalAccessException {
Field f = stocklicensehistoryfile.getClass().getDeclaredField("mCreatorName");
f.setAccessible(true);
f.set(stocklicensehistoryfile, "SomeText");
Assert.assertTrue(stocklicensehistoryfile.getCreatorName().equals("SomeText"));
}
示例13: setColor
import java.lang.reflect.Field; //導入方法依賴的package包/類
private void setColor(int color) {
Field textColorField;
try {
textColorField = TextView.class.getDeclaredField("mCurTextColor");
textColorField.setAccessible(true);
textColorField.set(this, color);
textColorField.setAccessible(false);
} catch (Exception e) {
e.printStackTrace();
}
mPaint.setColor(color);
}
示例14: setValue
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* Update {@link #opcode} value.
*
* @param value
*/
private void setValue(int value) {
try {
Field op = AbstractInsnNode.class.getDeclaredField("opcode");
op.setAccessible(true);
op.set(opcode, value);
list.repaint();
} catch (Exception e) {
e.printStackTrace();
}
}
示例15: getCategoryId_should_return_IntegerValue_of_Field_CategoryId
import java.lang.reflect.Field; //導入方法依賴的package包/類
@Test(groups = { "Getters" })
void getCategoryId_should_return_IntegerValue_of_Field_CategoryId()
throws NoSuchFieldException, IllegalAccessException {
Field f = request.getClass().getDeclaredField("mCategoryId");
f.setAccessible(true);
f.set(request, 1231);
Assert.assertEquals(1231, request.getCategoryId().intValue());
}