List of usage examples for java.lang Class getField
@CallerSensitive public Field getField(String name) throws NoSuchFieldException, SecurityException
From source file:org.eclipse.wb.internal.core.model.property.editor.style.StylePropertyEditor.java
private static Field getField(EditorState state, Class<?> baseClass, String name) { try {/* w ww. j av a 2s. co m*/ return baseClass.getField(name); } catch (NoSuchFieldException e) { state.addWarning(new EditorWarning( "StylePropertyEditor: can not find field " + baseClass.getName() + "." + name, e)); return null; } }
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 {// ww w. ja va2s. c o m 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); } } }
From source file:nl.nn.adapterframework.util.ClassUtils.java
public static Object getFieldValue(Object o, Class c, String name) throws IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException { return c.getField(name).get(o); }
From source file:com.adito.agent.client.ProxyUtil.java
/** * Attempt to proxy settings from Internet Explorer. * /*from ww w.j av a2s . c om*/ * @return internet explorer proxy settings * @throws IOException if IE settings could not be obtained for some reason */ public static BrowserProxySettings lookupIEProxySettings() throws IOException { try { Vector addresses = new Vector(); Vector proxies = new Vector(); String proxyServerValue = null; String proxyOveride = null; /* Only use jRegistry if on Windows, running 1.3 or up Java * and NOT Windows Vista with JDK6.0 (because of jvm crash) */ if (Utils.isSupportedJRE("+1.3") && Utils.isSupportedPlatform( "Windows") /*&& !(Utils.isSupportedOSVersion("+6.0") && Utils.isSupportedJRE("+1.6"))*/) { /* * We can use jRegistryKey API to lookup IE settings in the * registry */ // RegistryKey key = new RegistryKey(RootKey.HKEY_CURRENT_USER, // "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"); //$NON-NLS-1$ String proxyEnable = WinRegistry.getRegistryValue("user", "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyEnable", "0"); if (proxyEnable != null) { //$NON-NLS-1$ /* * We have ProxyEnable so check to see if we are using a * proxy */ if (proxyEnable.equals("1")) { //$NON-NLS-1$ //$NON-NLS-2$ proxyServerValue = WinRegistry.getRegistryValue("user", "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyServer", null); if (proxyServerValue != null) { //$NON-NLS-1$ /** * We have some proxy settings. The values will be * in the format "server.proxy.net:8888" or */ proxyOveride = WinRegistry.getRegistryValue("user", "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyOverride", null); } } else { } } } else { if (System.getProperty("java.vendor").startsWith("Microsoft")) { //$NON-NLS-1$ //$NON-NLS-2$ try { Class clazz = Class.forName("com.ms.lang.RegKey"); //$NON-NLS-1$ int userRoot = clazz.getField("USER_ROOT").getInt(null); //$NON-NLS-1$ int keyOpenAll = clazz.getField("KEYOPEN_ALL").getInt(null); //$NON-NLS-1$ // #ifdef DEBUG log.info(Messages.getString("ProxyUtil.lookingForRoot")); //$NON-NLS-1$ // #endif Object rootKey = clazz.getMethod("getRootKey", new Class[] { int.class }).invoke(null, //$NON-NLS-1$ new Object[] { new Integer(userRoot) }); // #ifdef DEBUG log.info(Messages.getString("ProxyUtil.getIERegistryKey")); //$NON-NLS-1$ // #endif Object key = clazz.getConstructor(new Class[] { clazz, String.class, int.class }) .newInstance(new Object[] { rootKey, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", //$NON-NLS-1$ new Integer(keyOpenAll) }); // #ifdef DEBUG log.info(Messages.getString("ProxyUtil.checkingIfProxyEnabled")); //$NON-NLS-1$ // #endif if (((Integer) (clazz.getMethod("getIntValue", new Class[] { String.class }).invoke(key, //$NON-NLS-1$ new Object[] { "ProxyEnable" }))).intValue() == 1) { //$NON-NLS-1$ // #ifdef DEBUG log.info(Messages.getString("ProxyUtil.gettingProxyServerList")); //$NON-NLS-1$ // #endif proxyServerValue = (String) (clazz.getMethod("getStringValue", //$NON-NLS-1$ new Class[] { String.class, String.class }) .invoke(key, new Object[] { "ProxyServer", "" })); //$NON-NLS-1$ //$NON-NLS-2$ // #ifdef DEBUG log.info(Messages.getString("ProxyUtil.gettingProxyOverides")); //$NON-NLS-1$ // #endif proxyOveride = (String) (clazz .getMethod("getStringValue", new Class[] { String.class, String.class }) //$NON-NLS-1$ .invoke(key, new Object[] { "ProxyOverride", "" })); //$NON-NLS-1$ //$NON-NLS-2$ } } catch (Throwable t) { t.printStackTrace(); } } else { // #ifdef DEBUG log.info(MessageFormat.format(Messages.getString("ProxyUtil.unsupportedJavaRuntime"), //$NON-NLS-1$ new Object[] { System.getProperty("java.version"), //$NON-NLS-1$ System.getProperty("java.vendor") })); //$NON-NLS-1$ // #endif } } ProxyInfo p; if (proxyServerValue != null && proxyServerValue.indexOf(';') > -1) { /** * Format is multiple * "ftp=ftp.com:4444;gopher=gopher.com:3333;http=198.162.1.119:8888;https=https.com:2222;socks=socks.com:1111" */ StringTokenizer tokens = new StringTokenizer(proxyServerValue, ";"); //$NON-NLS-1$ while (tokens.hasMoreTokens()) { p = createProxyInfo(tokens.nextToken(), "IE Proxy Settings"); //$NON-NLS-1$ proxies.addElement(p); } } else if (proxyServerValue != null) { /** * Format is single "http=server.proxy.net:8888" or * "server.proxy.net:8888" */ p = createProxyInfo(proxyServerValue, "IE Proxy Settings"); //$NON-NLS-1$ proxies.addElement(p); } BrowserProxySettings bps = new BrowserProxySettings(); bps.setBrowser("Internet Explorer"); //$NON-NLS-1$ bps.setProxies(new ProxyInfo[proxies.size()]); proxies.copyInto(bps.getProxies()); if (proxyOveride != null) { StringTokenizer tokens = new StringTokenizer(proxyOveride, ";"); //$NON-NLS-1$ while (tokens.hasMoreTokens()) { addresses.addElement(tokens.nextToken()); } } bps.setBypassAddr(new String[addresses.size()]); addresses.copyInto(bps.getBypassAddr()); return bps; } catch (Throwable t) { t.printStackTrace(); throw new IOException(MessageFormat.format(Messages.getString("ProxyUtil.failedToLookupIEProxies"), //$NON-NLS-1$ new Object[] { t.getMessage() })); } }
From source file:com.cardvlaue.sys.util.ScreenUtil.java
private static int getStatusBarHeight(AppCompatActivity activity) { Class<?> c; Object obj;//w w w. ja v a 2 s. c o m Field field; int x; int statusBarHeight = 0; try { c = Class.forName("com.android.internal.R$dimen"); obj = c.newInstance(); field = c.getField("status_bar_height"); x = Integer.parseInt(field.get(obj).toString()); statusBarHeight = activity.getResources().getDimensionPixelSize(x); } catch (Exception e1) { e1.printStackTrace(); } return statusBarHeight; }
From source file:org.opoo.util.ClassUtils.java
public static Object getConstantValue(String name) { Class clazz; try {//from ww w. j a v a2 s .c o m clazz = forName(StringUtils.qualifier(name)); } catch (ClassNotFoundException cnfe) { return null; } try { return clazz.getField(StringUtils.unqualify(name)).get(null); } catch (Exception e) { return null; } }
From source file:org.gridgain.grid.util.GridConfigurationHelper.java
/** * Overrides configuration.// ww w .ja va 2 s . c om * * @param cfg Configuration. * @param props Properties. * @param gridName Grid name. * @param log Logger. * @throws GridException If overrides failed. */ public static void overrideConfiguration(GridConfiguration cfg, Map<Object, Object> props, String gridName, GridLogger log) throws GridException { assert cfg != null; assert props != null; assert log != null; Map<String, String> nameMap = new HashMap<String, String>(); // Properties for all grids. String prefix = PROPERTY_PREFIX + "*."; for (Map.Entry<Object, Object> entry : props.entrySet()) if (entry.getKey() instanceof String) { String key = (String) entry.getKey(); if (key.startsWith(prefix)) nameMap.put(key.substring(prefix.length()), key); } // Properties for grids with specified name. prefix = PROPERTY_PREFIX + (gridName != null ? gridName : "") + '.'; for (Map.Entry<Object, Object> entry : props.entrySet()) if (entry.getKey() instanceof String) { String key = (String) entry.getKey(); if (key.startsWith(prefix)) nameMap.put(key.substring(prefix.length()), key); } for (Map.Entry<String, String> entry : nameMap.entrySet()) { String key = entry.getKey(); String fullKey = entry.getValue(); Object objVal = props.get(fullKey); if (!(objVal instanceof String)) { U.warn(log, "Failed to override configuration property (value must be a String) [key=" + fullKey + ", value=" + objVal + ']'); break; } String val = (String) objVal; Object target; String targetPropName; if (key.startsWith(CONFIG_PREFIX)) { targetPropName = key.substring(CONFIG_PREFIX.length()); target = cfg; } else if (key.startsWith(SPI_PREFIX)) { String prop = key.substring(SPI_PREFIX.length()); String[] s = prop.split("\\."); if (s == null || s.length != 2) { U.warn(log, "Wrong SPI parameter name format (will ignore): " + fullKey); continue; } target = findSpi(cfg, s[0]); if (target == null) { U.warn(log, "Failed to find target spi for property (will ignore): " + fullKey); continue; } targetPropName = s[1]; } else if (key.startsWith(CACHE_PREFIX)) { String prop = key.substring(CACHE_PREFIX.length()); String[] s = prop.split("\\."); if (s == null || s.length != 2) { U.warn(log, "Wrong Cache parameter name format (will ignore): " + fullKey); continue; } target = findCache(cfg, s[0]); if (target == null) { U.warn(log, "Failed to find target cache configuration for property (will ignore): " + fullKey); continue; } targetPropName = s[1]; } else if (key.startsWith(LOG_PREFIX)) { boolean isLog4jUsed = GridConfigurationHelper.class.getClassLoader() .getResource("org/apache/log4j/Appender.class") != null; if (isLog4jUsed) { try { Class logCls = Class.forName("org.apache.log4j.Logger"); Object log4j = logCls.getMethod("getLogger", String.class).invoke(logCls, val); Class levelCls = Class.forName("org.apache.log4j.Level"); log4j.getClass().getMethod("setLevel", levelCls).invoke(log4j, levelCls.getField("DEBUG").get(levelCls)); } catch (Exception ignore) { U.warn(log, "Failed to change log level for property (will ignore): " + fullKey); } } continue; } else { U.warn(log, "Wrong Configuration parameter name format (will ignore): " + fullKey); continue; } assert target != null; if (F.isEmpty(targetPropName)) { U.warn(log, "Wrong configuration parameter name format (will ignore): " + fullKey); continue; } Method mtd = findSetterMethod(target.getClass(), targetPropName); if (mtd == null) { U.warn(log, "Failed to find target setter method for property (will ignore): " + fullKey); continue; } Object param = convert(val, mtd.getParameterTypes()[0], fullKey); try { mtd.invoke(target, param); } catch (IllegalArgumentException e) { throw new GridException("Failed to invoke setter to override configuration property [name=" + fullKey + ", value=" + val + ']', e); } catch (IllegalAccessException e) { throw new GridException( "Failed to invoke getter or setter to override configuration property [name=" + fullKey + ", value=" + val + ']', e); } catch (InvocationTargetException e) { throw new GridException( "Failed to invoke getter or setter to override configuration property [name=" + fullKey + ", value=" + val + ']', e); } if (log.isInfoEnabled()) log.info("Override configuration property [name=" + fullKey + ", value=" + val + ']'); } }
From source file:org.springframework.richclient.util.ClassUtils.java
/** * Gets the field value for the specified qualified field name. *///from w w w .j av a2s.co m public static Object getFieldValue(String qualifiedFieldName) { Class clazz; try { clazz = classForName(ClassUtils.qualifier(qualifiedFieldName)); } catch (ClassNotFoundException cnfe) { return null; } try { return clazz.getField(ClassUtils.unqualify(qualifiedFieldName)).get(null); } catch (Exception e) { return null; } }
From source file:org.hibernate.internal.util.ReflectHelper.java
/** * Resolve a constant to its actual value. * * @param name The name//from w w w. jav a2 s. c o m * @return The value */ public static Object getConstantValue(String name) { Class clazz; try { clazz = classForName(StringHelper.qualifier(name)); } catch (Throwable t) { return null; } try { return clazz.getField(StringHelper.unqualify(name)).get(null); } catch (Throwable t) { return null; } }
From source file:org.eclipse.xtend.typesystem.emf.EcoreUtil2.java
/** * Finds an EMF EPackage instance by its class name. The EPackage class must * be loadable by the ResourceLoader/*from w ww . j a va 2s .co m*/ * * @param ePackage * The class name of the EPackage interface * @return The EPackage instance. Returns <code>null</code> if any exception * occurs after the class was successfully loaded. * @throws ConifgurationException * If the class specified by <tt>ePackage</tt> cannot be loaded. */ public static EPackage getEPackageByClassName(final String ePackage) throws ConfigurationException { Class clazz; try { // try to load the EPackage class with the ResourceLoader clazz = ResourceLoaderFactory.createResourceLoader().loadClass(ePackage); if (clazz == null) throw new ConfigurationException("Couldn't find class " + ePackage); // each EPackage class has an 'eINSTANCE' field which holds the // implementation instance final Field f = clazz.getField("eINSTANCE"); // retrieve the EPackage instance from the eINSTANCE field final EPackage result = (EPackage) f.get(null); // register the EPackage instance in the EPackage Registry EPackage.Registry.INSTANCE.put(result.getNsURI(), result); return result; } catch (final SecurityException e) { log.error(e); return null; } catch (final NoSuchFieldException e) { log.error(e); return null; } catch (final IllegalArgumentException e) { log.error(e); return null; } catch (final IllegalAccessException e) { log.error(e); return null; } finally { logPackages(); } }