List of usage examples for java.lang.reflect Field setAccessible
@Override @CallerSensitive public void setAccessible(boolean flag)
From source file:com.npower.dm.util.BeanHelper.java
/** * private/protected/*from w w w . j a v a 2 s. com*/ */ static public void setDeclaredProperty(Object object, Field field, Object newValue) throws IllegalAccessException { boolean accessible = field.isAccessible(); field.setAccessible(true); field.set(object, newValue); field.setAccessible(accessible); }
From source file:Main.java
/** * Sets the given field's value of the specified object instance. * /*from ww w. ja v a 2 s .c o m*/ * @throws IllegalArgumentException if the value cannot be set. */ public static void setValue(final Field field, final Object instance, final Object value) { try { field.setAccessible(true); field.set(instance, convert(value, field.getType())); } catch (final IllegalAccessException e) { throw new IllegalArgumentException("No access to field: " + field.getName(), e); } }
From source file:com.taobao.adfs.database.tdhsocket.client.protocol.TDHSProtocolBinary.java
private static void encodeRequest(Request o, ByteArrayOutputStream out, String charestName) throws IllegalAccessException, IOException, TDHSEncodeException { for (Field f : o.getClass().getDeclaredFields()) { if (!Modifier.isPublic(f.getModifiers())) { f.setAccessible(true); }//from w w w . jav a2s. com Object v = f.get(o); if (v instanceof Request) { encodeRequest((Request) v, out, charestName); } else if (f.getName().startsWith("_")) { writeObjectToStream(v, out, f.getName(), charestName); } } }
From source file:jfix.util.Reflections.java
/** * Initializes all declared static string fields in given class with name of * fields.//from w w w . java 2 s. co m */ 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:ml.dmlc.xgboost4j.java.NativeLibLoader.java
/** * Add libPath to java.library.path, then native library in libPath would be load properly * * @param libPath library path//w w w . j a va2s .co m * @throws IOException exception */ private static void addNativeDir(String libPath) throws IOException { try { Field field = ClassLoader.class.getDeclaredField("usr_paths"); field.setAccessible(true); String[] paths = (String[]) field.get(null); for (String path : paths) { if (libPath.equals(path)) { return; } } String[] tmp = new String[paths.length + 1]; System.arraycopy(paths, 0, tmp, 0, paths.length); tmp[paths.length] = libPath; field.set(null, tmp); } catch (IllegalAccessException e) { logger.error(e.getMessage()); throw new IOException("Failed to get permissions to set library path"); } catch (NoSuchFieldException e) { logger.error(e.getMessage()); throw new IOException("Failed to get field handle to set library path"); } }
From source file:com.xhsoft.framework.common.utils.ReflectUtil.java
/** * <p>Description:setFieldValue</p> * @param target/*from w w w . j av a2 s . c om*/ * @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:com.harshad.linconnectclient.NotificationUtilities.java
@SuppressLint("DefaultLocale") public static ArrayList<String> getNotificationText(Notification notification) { RemoteViews views = notification.contentView; Class<?> secretClass = views.getClass(); try {//from w w w. j a va2 s . co m ArrayList<String> notificationData = new ArrayList<String>(); Field outerFields[] = secretClass.getDeclaredFields(); for (int i = 0; i < outerFields.length; i++) { if (!outerFields[i].getName().equals("mActions")) continue; outerFields[i].setAccessible(true); @SuppressWarnings("unchecked") ArrayList<Object> actions = (ArrayList<Object>) outerFields[i].get(views); for (Object action : actions) { Field innerFields[] = action.getClass().getDeclaredFields(); Object value = null; for (Field field : innerFields) { field.setAccessible(true); // Value field could possibly contain text if (field.getName().equals("value")) { value = field.get(action); } } // Check if value is a String if (value != null && value.getClass().getName().toUpperCase().contains("STRING")) { notificationData.add(value.toString()); } } return notificationData; } } catch (Exception e) { } return null; }
From source file:com.dianping.lion.util.BeanUtils.java
@SuppressWarnings("unchecked") public static <T> T getDeclaredFieldValue(Object object, String propertyName) throws NoSuchFieldException { Assert.notNull(object);//ww w . ja v a 2s . c o m Assert.hasText(propertyName); Field field = getDeclaredField(object.getClass(), propertyName); boolean accessible = field.isAccessible(); Object result = null; synchronized (field) { field.setAccessible(true); try { result = field.get(object); } catch (IllegalAccessException e) { throw new NoSuchFieldException("No such field: " + object.getClass() + '.' + propertyName); } finally { field.setAccessible(accessible); } } return (T) result; }
From source file:com.bjwg.back.util.ReflectionUtils.java
/** * //from w ww.j av a 2s.c om */ public static Field getAccessibleField(final Object obj, final String fieldName) { AssertUtils.notNull(obj, "object?"); AssertUtils.hasText(fieldName, "fieldName"); for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass .getSuperclass()) { try { Field field = superClass.getDeclaredField(fieldName); field.setAccessible(true); return field; } catch (NoSuchFieldException e) {//NOSONAR } } return null; }
From source file:org.comicwiki.Repository.java
private static Object mergeObjects(Object source, Object target) { Field[] fields = source.getClass().getFields(); for (Field field : fields) { field.setAccessible(true); try {//from w w w. j a v a 2 s . c o m Object sourceValue = field.get(source); if (sourceValue == null) { continue; } Object targetValue = field.get(target); if (targetValue == null) { field.set(target, sourceValue); } else if (targetValue instanceof Thing) {// CreativeWork mergeObjects(sourceValue, targetValue); } else if (targetValue instanceof IRI[]) { IRI[] tV = (IRI[]) targetValue; IRI[] sV = (IRI[]) sourceValue; field.set(target, Add.both(tV, sV, IRI.class)); } else if (targetValue instanceof String[]) { String[] tV = (String[]) targetValue; String[] sV = (String[]) sourceValue; field.set(target, Add.both(sV, tV, String.class)); } else if (targetValue instanceof Number[]) { Number[] tV = (Number[]) targetValue; Number[] sV = (Number[]) sourceValue; field.set(target, Add.both(tV, sV, Number.class)); } else if (targetValue instanceof URL[]) { URL[] tV = (URL[]) targetValue; URL[] sV = (URL[]) sourceValue; field.set(target, Add.both(tV, sV, URL.class)); } } catch (Exception e) { e.printStackTrace(); } } return target; }