List of usage examples for java.lang.reflect Field get
@CallerSensitive @ForceInline public Object get(Object obj) throws IllegalArgumentException, IllegalAccessException
From source file:com.ocs.dynamo.test.MockUtil.java
/** * Registers all fields that are annotated with "@Mock" as beans in the Spring context * /*w ww . j av a2 s .c om*/ * @param factory * @param subject * @param clazz */ public static void registerMocks(ConfigurableListableBeanFactory factory, Object subject, Class<?> clazz) { try { Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); if (field.getAnnotation(Mock.class) != null) { factory.registerSingleton(field.getName(), field.get(subject)); } } if (clazz.getSuperclass() != null) { registerMocks(factory, subject, clazz.getSuperclass()); } } catch (Exception e) { throw new OCSRuntimeException(e.getMessage(), e); } }
From source file:org.eclipse.virgo.ide.runtime.core.ServerUtils.java
/** * Returns the {@link JavaVersion} of the given {@link IJavaProject} *///from w ww .j a v a2 s . c o m public static JavaVersion getJavaVersion(IProject project) { IJavaProject javaProject = JdtUtils.getJavaProject(project); if (javaProject != null) { // first check the manifest for that // Bundle-RequiredExecutionEnvironment BundleManifest bundleManifest = BundleManifestCorePlugin.getBundleManifestManager() .getBundleManifest(javaProject); Dictionary<String, String> manifest = bundleManifest.toDictionary(); if (manifest != null && manifest.get(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT) != null) { String javaVersion = manifest.get(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT); return JAVA_VERSION_MAPPING.get(javaVersion); } // second check the project for a matching jvm try { IClasspathContainer container = JavaCore.getClasspathContainer(JRE_CONTAINER_PATH, javaProject); if (container != null && container instanceof JREContainer) { // reflection hack to get the internal jvm install Field field = JREContainer.class.getDeclaredField("fVMInstall"); field.setAccessible(true); IVMInstall vm = (IVMInstall) field.get((JREContainer) container); IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager(); // check for strict match for (IExecutionEnvironment executionEnvironment : manager.getExecutionEnvironments()) { if (executionEnvironment.isStrictlyCompatible(vm)) { return JAVA_VERSION_MAPPING.get(executionEnvironment.getId()); } } // check for default for (IExecutionEnvironment executionEnvironment : manager.getExecutionEnvironments()) { if (executionEnvironment.getDefaultVM() != null && executionEnvironment.getDefaultVM().equals(vm)) { return JAVA_VERSION_MAPPING.get(executionEnvironment.getId()); } } // check for compatibility for (IExecutionEnvironment executionEnvironment : manager.getExecutionEnvironments()) { if (Arrays.asList(executionEnvironment.getCompatibleVMs()).contains(vm)) { return JAVA_VERSION_MAPPING.get(executionEnvironment.getId()); } } } } catch (Exception e) { SpringCore.log(e); } } return null; }
From source file:me.bird.util.Reflections.java
/** * ?, private/protected, ??getter./*from w w w . j av a 2 s. co m*/ */ public static Object getFieldValue(final Object obj, final String fieldName) { Field field = FieldUtils.getDeclaredField(obj.getClass(), fieldName, true); if (field == null) { throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]"); } Object result = null; try { result = field.get(obj); } catch (IllegalAccessException e) { logger.error("??", e); } return result; }
From source file:gov.abrs.etms.common.util.ReflectionUtils.java
/** * ?, private/protected, ??getter.//from w ww . j a v a 2 s.c om */ public static Object getFieldValue(final Object obj, final String fieldName) { Field field = getAccessibleField(obj, fieldName); if (field == null) { throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]"); } Object result = null; try { result = field.get(obj); } catch (IllegalAccessException e) { logger.error("??{}" + e.getMessage()); } return result; }
From source file:io.tempra.AppServer.java
/** * Adds the specified path to the java library path * * @param pathToAdd//from ww w . j a v a2s .c o m * the path to add * @throws Exception */ public static void addLibraryPath(String pathToAdd) throws Exception { final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths"); usrPathsField.setAccessible(true); // get array of paths final String[] paths = (String[]) usrPathsField.get(null); // check if the path to add is already present for (String path : paths) { if (path.equals(pathToAdd)) { return; } } // add the new path final String[] newPaths = Arrays.copyOf(paths, paths.length + 1); newPaths[newPaths.length - 1] = pathToAdd; usrPathsField.set(null, newPaths); }
From source file:com.yize.broadcast.core.util.ReflectionUtils.java
/** * ?, private/protected, ??getter./*from w ww .jav a2s .c om*/ */ public static Object getFieldValue(final Object obj, final String fieldName) { Field field = getAccessibleField(obj, fieldName); if (field == null) { throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]"); } Object result = null; try { result = field.get(obj); } catch (IllegalAccessException e) { LOGGER.error("??{}", e.getMessage()); } return result; }
From source file:com.austin.base.commons.util.ReflectUtil.java
/** * @param owner//from ww w .j a v a 2 s. c om * 17 * @param fieldName * ?? * @return 1717 */ public static Object getProperty(Object owner, String fieldName) { if (owner == null) { return ""; } Class ownerClass = owner.getClass(); Object property = null; try { Field field = ownerClass.getField(fieldName); property = field.get(owner); } catch (Exception ex) { log.error(ex); //throw ex; } return property; }
From source file:com.yimidida.shards.utils.ParameterUtil.java
public static Serializable extractPrimaryKey(Object object) { if (object != null) { Class<?> clazz = object.getClass(); Field[] first = clazz.getDeclaredFields(); Field[] second = clazz.getSuperclass().getDeclaredFields(); Field[] fields = Arrays.copyOf(first, first.length + second.length); System.arraycopy(second, 0, fields, first.length, second.length); for (Field field : fields) { field.setAccessible(true);/*from ww w.j a va 2s . c om*/ PrimaryKey primaryKey = field.getAnnotation(PrimaryKey.class); if (primaryKey != null) { try { //0 Object result = field.get(object); if (result != null && "0".equals(result.toString())) { return null; } return (Serializable) result; } catch (Exception e) { e.printStackTrace(); } } } } return null; }
From source file:com.u2apple.tool.util.AndroidDeviceUtils.java
public static String getPropertyByKey(AndroidDeviceRanking androidDevice, String key) { if (androidDevice == null || key == null) { return null; }/*from w ww .j a va 2 s . c om*/ for (Field field : androidDevice.getClass().getSuperclass().getDeclaredFields()) { field.setAccessible(true); if (field.isAnnotationPresent(Key.class)) { Key keyAnno = field.getAnnotation(Key.class); if (keyAnno.value().equals(key)) { try { return (String) field.get(androidDevice); } catch (IllegalArgumentException | IllegalAccessException ex) { Logger.getLogger(AndroidDeviceUtils.class.getName()).log(Level.SEVERE, null, ex); } } } } return null; }
From source file:ee.ria.xroad.common.message.SoapUtils.java
static Object getFieldValue(Field field, Object object) { field.setAccessible(true);/*from w ww. j a va 2 s. co m*/ try { return field.get(object); } catch (Exception e) { throw translateException(e); } }