List of usage examples for java.lang.reflect Field set
@CallerSensitive @ForceInline public void set(Object obj, Object value) throws IllegalArgumentException, IllegalAccessException
From source file:jfix.util.Reflections.java
/** * Initializes all declared static string fields in given class with name of * fields.//from w w w . ja v a2 s . c om */ public static void init(Class<?> clazz) { for (Field field : clazz.getDeclaredFields()) { field.setAccessible(true); try { field.set(null, field.getName()); } catch (Exception e) { } } }
From source file:io.cloudslang.content.database.services.databases.MSSqlDatabase.java
private static void setJavaLibraryPath(String sqlJdbcAuthFilePath) { String javaLibraryPath = System.getProperty(JAVA_LIBRARY_PATH); if (StringUtils.isEmpty(javaLibraryPath)) { javaLibraryPath = sqlJdbcAuthFilePath; } else {/* w w w . j a v a 2 s . co m*/ javaLibraryPath = javaLibraryPath.substring(0, javaLibraryPath.length() - 1) + sqlJdbcAuthFilePath + System.getProperty(PATH_SEPARATOR) + CURRENT_DIRECTORY_NOTATION; } System.setProperty(JAVA_LIBRARY_PATH, javaLibraryPath); try { Field sysPathsField = ClassLoader.class.getDeclaredField(SYS_PATHS); sysPathsField.setAccessible(true); sysPathsField.set(null, null); } catch (NoSuchFieldException | IllegalAccessException e) { throw new RuntimeException(INACCESSIBLE_OR_INEXISTENT_SYS_PATHS_FIELD_EXCEPTION); } }
From source file:com.xhsoft.framework.common.utils.ReflectUtil.java
/** * <p>Description:setFieldValue</p> * @param target/* w w w.java2s . c o m*/ * @param fname * @param ftype * @param fvalue * @return void */ @SuppressWarnings("unchecked") public static void setFieldValue(Object target, String fname, Class ftype, Object fvalue) { if (target == null || fname == null || "".equals(fname) || (fvalue != null && !ftype.isAssignableFrom(fvalue.getClass()))) { return; } Class clazz = target.getClass(); try { Method method = clazz .getDeclaredMethod("set" + Character.toUpperCase(fname.charAt(0)) + fname.substring(1), ftype); if (!Modifier.isPublic(method.getModifiers())) { method.setAccessible(true); } method.invoke(target, fvalue); } catch (Exception me) { try { Field field = clazz.getDeclaredField(fname); if (!Modifier.isPublic(field.getModifiers())) { field.setAccessible(true); } field.set(target, fvalue); } catch (Exception fe) { if (logger.isDebugEnabled()) { logger.debug(fe); } } } }
From source file:org.trpr.platform.batch.impl.spring.admin.repository.MapStepExecutionDao.java
private static void copy(final StepExecution sourceExecution, final StepExecution targetExecution) { // Cheaper than full serialization is a reflective field copy, which is // fine for volatile storage ReflectionUtils.doWithFields(StepExecution.class, new ReflectionUtils.FieldCallback() { @Override/*from www .j a v a 2 s . co m*/ public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { field.setAccessible(true); field.set(targetExecution, field.get(sourceExecution)); } }); }
From source file:org.jongo.model.IdSpecSet.java
public static <T> T id(T specInstance, Object id) { Field field = idField(specInstance.getClass()); try {// w w w. ja va2 s. com field.setAccessible(true); if (ObjectId.class.isAssignableFrom(field.getType())) { field.set(specInstance, (ObjectId) id); } else { field.set(specInstance, id.toString()); } return specInstance; } catch (Exception e) { throw new RuntimeException("could not set id field", e); } }
From source file:com.microsoft.applicationinsights.internal.channel.common.ApacheSenderFactoryTest.java
private static ApacheSender createApacheSender(boolean isNewVersion) throws NoSuchFieldException, IllegalAccessException { Field field = ClassDataUtils.class.getDeclaredField("verifier"); field.setAccessible(true);//from ww w.j a va 2 s . co m ClassDataVerifier mockVerifier = Mockito.mock(ClassDataVerifier.class); Mockito.doReturn(isNewVersion).when(mockVerifier).verifyClassExists(anyString()); field.set(ClassDataUtils.INSTANCE, mockVerifier); ApacheSender sender = new ApacheSenderFactory().create(); assertNotNull(sender); return sender; }
From source file:com.github.piasy.biv.example.App.java
static void fixLeakCanary696(Context context) { if (!isEmui()) { Log.w(TAG, "not emui"); return;// ww w .j a va2s .co m } try { Class clazz = Class.forName("android.gestureboost.GestureBoostManager"); Log.w(TAG, "clazz " + clazz); Field _sGestureBoostManager = clazz.getDeclaredField("sGestureBoostManager"); _sGestureBoostManager.setAccessible(true); Field _mContext = clazz.getDeclaredField("mContext"); _mContext.setAccessible(true); Object sGestureBoostManager = _sGestureBoostManager.get(null); if (sGestureBoostManager != null) { _mContext.set(sGestureBoostManager, context); } } catch (Exception ignored) { ignored.printStackTrace(); } }
From source file:jp.co.acroquest.endosnipe.perfdoctor.rule.RuleInstanceUtil.java
/** * ??/*from w w w . j a v a 2 s . c o m*/ * @param obj ? * @param fieldName ?? * @param value * @throws RuleCreateException ??????? */ protected static void setValue(final Object obj, final String fieldName, final String value) throws RuleCreateException { Class<? extends Object> clazz = obj.getClass(); Object[] args = new Object[] { clazz.getCanonicalName(), fieldName, value }; try { Field field = clazz.getField(fieldName); Object convertedValue = ConvertUtils.convert(value, field.getType()); field.set(obj, convertedValue); } catch (NoSuchFieldException ex) { throw new RuleCreateException(PerfConstants.PROPERTY_NOT_FOUND, args); } catch (SecurityException ex) { throw new RuleCreateException(PerfConstants.PROPERTY_ERROR, args); } catch (IllegalArgumentException ex) { throw new RuleCreateException(PerfConstants.PROPERTY_TYPE_ERROR, args); } catch (IllegalAccessException ex) { throw new RuleCreateException(PerfConstants.PROPERTY_ACCESS_ERROR, args); } }
From source file:Main.java
public static void fixInputMethodManagerLeak(Context context) { if (context == null) { return;/*from w w w.jav a 2s . c om*/ } try { InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); if (imm == null) { return; } Field f_mCurRootView = imm.getClass().getDeclaredField("mCurRootView"); Field f_mServedView = imm.getClass().getDeclaredField("mServedView"); Field f_mNextServedView = imm.getClass().getDeclaredField("mNextServedView"); f_mCurRootView.setAccessible(true); f_mCurRootView.set(imm, null); f_mServedView.setAccessible(true); f_mServedView.set(imm, null); f_mNextServedView.setAccessible(true); f_mNextServedView.set(imm, null); } catch (Throwable t) { t.printStackTrace(); } }
From source file:de.micromata.genome.gwiki.utils.ClassUtils.java
public static void populateBeanWithPuplicMembers(Object bean, Map<String, Object> reqMap) { Class<?> cls = bean.getClass(); for (Map.Entry<String, Object> me : reqMap.entrySet()) { try {/*from w w w . j av a2s .c om*/ String s = getSetter(me.getKey()); if (hasMethod(cls, s) == true) { BeanUtilsBean.getInstance().setProperty(bean, me.getKey(), me.getValue()); } else { Field f; try { f = cls.getField(me.getKey()); if (f != null) { f.set(bean, convert(me.getValue(), f.getType())); } } catch (NoSuchFieldException ex) { continue; } } } catch (Exception ex) { throw new RuntimeException("Failure to set propert: " + me.getKey() + " in class: " + cls.getName() + "; with value: " + me.getValue() + ";" + ex.getMessage(), ex); } } }