List of usage examples for java.lang Class getDeclaredField
@CallerSensitive public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException
From source file:com.gigaspaces.internal.utils.ClassLoaderCleaner.java
private static void clearReferencesThreadLocals(ClassLoader classLoader) { Thread[] threads = getThreads(); try {//from w w w .ja va2 s. c o m // Make the fields in the Thread class that store ThreadLocals // accessible Field threadLocalsField = Thread.class.getDeclaredField("threadLocals"); threadLocalsField.setAccessible(true); Field inheritableThreadLocalsField = Thread.class.getDeclaredField("inheritableThreadLocals"); inheritableThreadLocalsField.setAccessible(true); // Make the underlying array of ThreadLoad.ThreadLocalMap.Entry objects // accessible Class<?> tlmClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap"); Field tableField = tlmClass.getDeclaredField("table"); tableField.setAccessible(true); for (int i = 0; i < threads.length; i++) { Object threadLocalMap; if (threads[i] != null) { // Clear the first map threadLocalMap = threadLocalsField.get(threads[i]); clearThreadLocalMap(threadLocalMap, tableField, classLoader); // Clear the second map threadLocalMap = inheritableThreadLocalsField.get(threads[i]); clearThreadLocalMap(threadLocalMap, tableField, classLoader); } } } catch (Exception e) { logger.log(Level.WARNING, "Failed to clear ThreadLocal references", e); } }
From source file:org.jfree.graphics2d.svg.SVGHints.java
private static RenderingHints.Key fetchKey(String className, String fieldName) { Class<?> hintsClass; try {//from ww w. ja va 2s . c om hintsClass = Class.forName(className); Field f = hintsClass.getDeclaredField(fieldName); return (RenderingHints.Key) f.get(null); } catch (ClassNotFoundException e) { return null; } catch (NoSuchFieldException ex) { return null; } catch (SecurityException ex) { return null; } catch (IllegalArgumentException ex) { return null; } catch (IllegalAccessException ex) { return null; } }
From source file:com.github.dozermapper.core.classmap.ClassMapBuilder.java
private static boolean requireMapping(Mapping mapping, Class<?> clazz, String fieldName, String pairName) { try {//w ww . jav a 2 s . com return !mapping.optional() || (mapping.optional() && clazz.getDeclaredField(pairName.isEmpty() ? fieldName : pairName) != null); } catch (NoSuchFieldException e) { return false; } }
From source file:com.bosscs.spark.commons.utils.Utils.java
/** * @param object/*from w w w. j a v a 2 s. co m*/ * @param fieldName * @param fieldValue * @return if the operation has been correct. */ public static boolean setFieldWithReflection(Object object, String fieldName, Object fieldValue) { Class<?> clazz = object.getClass(); while (clazz != null) { try { Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); field.set(object, fieldValue); return true; } catch (NoSuchFieldException e) { clazz = clazz.getSuperclass(); } catch (Exception e) { throw new IllegalStateException(e); } } return false; }
From source file:de.ailis.xadrian.utils.SwingUtils.java
/** * Sets the application name. There is no API for this (See * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6528430) so this * method uses reflection to do this. This may fail if the Java * implementation is changed but any exception here will be ignored. * //from w ww. j a va 2 s .c o m * The application name is currently only used for X11 desktops and only * important for some window managers like Gnome Shell. * * @param appName * The application name to set. */ public static void setAppName(final String appName) { final Toolkit toolkit = Toolkit.getDefaultToolkit(); final Class<?> cls = toolkit.getClass(); try { // When X11 toolkit is used then set the awtAppClassName field if (cls.getName().equals("sun.awt.X11.XToolkit")) { final Field field = cls.getDeclaredField("awtAppClassName"); field.setAccessible(true); field.set(toolkit, appName); } } catch (final Exception e) { LOG.warn("Unable to set application name: " + e, e); } }
From source file:com.gigaspaces.internal.utils.ClassLoaderCleaner.java
/** * This depends on the internals of the Sun JVM so it does everything by reflection. *//* w ww. ja v a 2 s. co m*/ private static void clearReferencesRmiTargets(ClassLoader classLoader) { try { // Need access to the ccl field of sun.rmi.transport.Target Class<?> objectTargetClass = Class.forName("sun.rmi.transport.Target"); Field cclField = objectTargetClass.getDeclaredField("ccl"); cclField.setAccessible(true); // Clear the objTable map Class<?> objectTableClass = Class.forName("sun.rmi.transport.ObjectTable"); Field objTableField = objectTableClass.getDeclaredField("objTable"); objTableField.setAccessible(true); Object objTable = objTableField.get(null); if (objTable == null) { return; } // Iterate over the values in the table if (objTable instanceof Map<?, ?>) { Iterator<?> iter = ((Map<?, ?>) objTable).values().iterator(); while (iter.hasNext()) { Object obj = iter.next(); Object cclObject = cclField.get(obj); if (classLoader == cclObject) { iter.remove(); } } } // Clear the implTable map Field implTableField = objectTableClass.getDeclaredField("implTable"); implTableField.setAccessible(true); Object implTable = implTableField.get(null); if (implTable == null) { return; } // Iterate over the values in the table if (implTable instanceof Map<?, ?>) { Iterator<?> iter = ((Map<?, ?>) implTable).values().iterator(); while (iter.hasNext()) { Object obj = iter.next(); Object cclObject = cclField.get(obj); if (classLoader == cclObject) { iter.remove(); } } } } catch (Exception e) { logger.log(Level.WARNING, "Failed to clear context class loader referenced from sun.rmi.transport.Target ", e); } }
From source file:com.google.code.siren4j.util.ReflectionUtils.java
public static Field findField(Class<?> clazz, String fieldName) throws NoSuchFieldException { Field field = null;//from w w w . jav a2s. co m try { field = clazz.getDeclaredField(fieldName); } catch (NoSuchFieldException e) { } if (field == null) { Class<?> superClazz = clazz.getSuperclass(); if (superClazz != null) { field = findField(superClazz, fieldName); } if (field == null) { throw new NoSuchFieldException("Field: " + fieldName); } else { return field; } } else { return field; } }
From source file:net.kamhon.ieagle.util.VoUtil.java
/** * To trim object String field/*from w w w. ja va2s.c om*/ * * @param all * default is false. true means toTrim all String field, false toTrim the fields have * net.kamhon.ieagle.vo.core.annotation.ToTrim. * * @param objects */ public static void toTrimProperties(boolean all, Object... objects) { for (Object object : objects) { if (object == null) { continue; } // getter for String field only Map<String, Method> getterMap = new HashMap<String, Method>(); // setter for String field only Map<String, Method> setterMap = new HashMap<String, Method>(); Class<?> clazz = object.getClass(); Method[] methods = clazz.getMethods(); for (Method method : methods) { if (method.getName().startsWith("get") && method.getParameterTypes().length == 0 && method.getReturnType().equals(String.class)) { // if method name is getXxx String fieldName = method.getName().substring(3); fieldName = fieldName.substring(0, 1).toLowerCase() + fieldName.substring(1); getterMap.put(fieldName, method); } else if (method.getName().startsWith("set") && method.getParameterTypes().length == 1 && method.getParameterTypes()[0] == String.class && method.getReturnType().equals(void.class)) { // log.debug("setter = " + method.getName()); // if method name is setXxx String fieldName = method.getName().substring(3); fieldName = fieldName.substring(0, 1).toLowerCase() + fieldName.substring(1); setterMap.put(fieldName, method); } } // if the key exists in both getter & setter for (String key : getterMap.keySet()) { if (setterMap.containsKey(key)) { try { Method getterMethod = getterMap.get(key); Method setterMethod = setterMap.get(key); // if not all, check on Field if (!all) { Field field = null; Class<?> tmpClazz = clazz; // looping up to superclass to get decleared field do { try { field = tmpClazz.getDeclaredField(key); } catch (Exception ex) { // do nothing } if (field != null) { break; } tmpClazz = tmpClazz.getSuperclass(); } while (tmpClazz != null); ToTrim toTrim = field.getAnnotation(ToTrim.class); if (toTrim == null || toTrim.trim() == false) { continue; } } String value = (String) getterMethod.invoke(object, new Object[] {}); if (StringUtils.isNotBlank(value)) setterMethod.invoke(object, value.trim()); } catch (Exception ex) { // log.error("Getter Setter for " + key + " has error ", ex); } } } } }
From source file:eu.vital.vitalcep.restApp.cepRESTApi.CEPICO.java
public static int getPid(Process process) { try {//from w ww. j a va2s. c o m Class<?> cProcessImpl = process.getClass(); java.lang.reflect.Field fPid = cProcessImpl.getDeclaredField("pid"); if (!fPid.isAccessible()) { fPid.setAccessible(true); } return fPid.getInt(process); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { return -1; } }
From source file:org.uimafit.factory.ConfigurationParameterFactory.java
/** * This method provides a convenient way to generate a configuration parameter name for a member * variable that is annotated with {@link org.uimafit.descriptor.ConfigurationParameter} and no * name is provided in the annotation./*from w w w .j a v a2 s. c o m*/ */ public static String createConfigurationParameterName(Class<?> clazz, String fieldName) throws IllegalArgumentException { try { return ConfigurationParameterFactory.getConfigurationParameterName(clazz.getDeclaredField(fieldName)); } catch (NoSuchFieldException e) { throw new IllegalArgumentException(e); } }