List of usage examples for java.lang.reflect Field get
@CallerSensitive @ForceInline public Object get(Object obj) throws IllegalArgumentException, IllegalAccessException
From source file:com.github.pjungermann.config.types.DefaultConfigFactorySelectorTest.java
@SuppressWarnings("unchecked") static Collection<ConfigFactory> getConfigFactories(DefaultConfigFactorySelector selector) { try {/*w w w . j a v a 2 s .com*/ Field listField = DefaultConfigFactorySelector.class.getDeclaredField("configFactories"); listField.setAccessible(true); return (Collection<ConfigFactory>) listField.get(selector); } catch (NoSuchFieldException e) { fail("field not found: " + e.getMessage()); } catch (IllegalAccessException e) { fail("field was not accessible: " + e.getMessage()); } return null; }
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/* w w w .j ava2 s. c o m*/ public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { field.setAccessible(true); field.set(targetExecution, field.get(sourceExecution)); } }); }
From source file:com.khepry.utilities.GenericUtilities.java
public static Map<Integer, String> getAllJdbcTypeNames() { Map<Integer, String> result = new HashMap<Integer, String>(); try {//ww w. j a va2 s.c o m for (Field field : Types.class.getFields()) { result.put((Integer) field.get(null), field.getName()); } } catch (IllegalArgumentException | IllegalAccessException ex) { Logger.getLogger(GenericUtilities.class.getName()).log(Level.SEVERE, null, ex); } return result; }
From source file:api_proto3.TestElf.java
public static void setSlf4jLogLevel(Class<?> clazz, Level logLevel) { try {/*from ww w . j a v a 2s .co m*/ //Log4jLogger log4Jlogger = (Log4jLogger) LoggerFactory.getLogger(clazz); Log4JLogger log4Jlogger = (Log4JLogger) LoggerFactory.getLogger(clazz); //Field field = clazz.getClassLoader().loadClass("org.apache.logging.slf4j.Log4jLogger").getDeclaredField("logger"); Field field = clazz.getClassLoader().loadClass("org.apache.commons.logging.impl.Log4JLogger") .getDeclaredField("logger"); field.setAccessible(true); Logger logger = (Logger) field.get(log4Jlogger); logger.setLevel(logLevel); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.google.api.ads.dfp.lib.utils.v201208.PqlUtils.java
/** * Gets the underlying value of the {@link Value} object. * * @param value the {@code Value} object to get the value from * @return the underlying value//w w w . ja va 2s . c om * @throws IllegalArgumentException if {@code value == null} * @throws IllegalAccessException if the value field cannot be accessed */ public static Object getValue(Value value) throws IllegalAccessException { if (value == null) { throw new IllegalArgumentException("Value object cannot be null."); } Field[] fields = value.getClass().getDeclaredFields(); for (Field field : fields) { if (field.getName().equals("value")) { field.setAccessible(true); return field.get(value); } } throw new IllegalArgumentException("No value field found in value object."); }
From source file:com.l2jfree.network.mmocore.packethandlers.PacketDefinition.java
@SuppressWarnings("unchecked") public static <S> S[] findStates(Class<?> clazz, S... defaultStates) throws Exception { // easier than checking all the modifiers, visibility, type, etc :) final Field field; try {/*from w w w. j ava 2s . co m*/ field = clazz.getField("STATES"); } catch (NoSuchFieldException e) { return defaultStates; } return (S[]) field.get(null); }
From source file:com.relicum.ipsum.Reflection.ReflectionUtil.java
/** * Returns the client {@link com.relicum.ipsum.Exception.Versioning.Version} a given {@link Player} is using. * * @param player Player to get version for * @return Their client version//w w w. ja v a 2s . c om */ public static final Versioning.Version getClientVersion(Player player) { Validate.notNull(player, "player cannot be null!"); try { Object handle = getHandle(player); Field playerConnectionField = getField(handle.getClass(), "playerConnection"); Object playerConnection = playerConnectionField.get(handle); Field networkManagerField = getField(playerConnection.getClass(), "networkManager"); Object networkManager = networkManagerField.get(playerConnection); Method getVersion = getMethod(networkManager.getClass(), "getVersion", new Class<?>[0]); int version = (int) getVersion.invoke(networkManager); switch (version) { case 4: case 5: return Versioning.Version.MC_17; case 47: return Versioning.Version.MC_18; default: return Versioning.Version.UNKNOWN; } } catch (Throwable ex) { } return Versioning.Version.UNKNOWN; }
From source file:spring.osgi.utils.OsgiBundleUtils.java
/** * Returns the underlying BundleContext for the given Bundle. This uses * reflection and highly dependent of the OSGi implementation. Should not be * used if OSGi 4.1 is being used./*from w w w. j av a2s .c om*/ * * @param bundle OSGi bundle * @return the bundle context for this bundle */ public static BundleContext getBundleContext(final Bundle bundle) { if (bundle == null) return null; // try Equinox getContext Method meth = ReflectionUtils.findMethod(bundle.getClass(), "getContext", new Class[0]); // fallback to getBundleContext (OSGi 4.1) if (meth == null) meth = ReflectionUtils.findMethod(bundle.getClass(), "getBundleContext", new Class[0]); final Method m = meth; if (meth != null) { ReflectionUtils.makeAccessible(meth); return (BundleContext) ReflectionUtils.invokeMethod(m, bundle); } // fallback to field inspection (KF and Prosyst) final BundleContext[] ctx = new BundleContext[1]; ReflectionUtils.doWithFields(bundle.getClass(), new FieldCallback() { public void doWith(final Field field) throws IllegalArgumentException, IllegalAccessException { ReflectionUtils.makeAccessible(field); ctx[0] = (BundleContext) field.get(bundle); } }, new FieldFilter() { public boolean matches(Field field) { return BundleContext.class.isAssignableFrom(field.getType()); } }); return ctx[0]; }
From source file:com.microsoft.rest.Validator.java
/** * Validates a user provided required parameter to be not null. * A {@link ServiceException} is thrown if a property fails the validation. * * @param parameter the parameter to validate * @throws ServiceException failures wrapped in {@link ServiceException} *//* ww w . j a v a 2 s .c om*/ public static void validate(Object parameter) throws ServiceException { // Validation of top level payload is done outside if (parameter == null) { return; } Class parameterType = parameter.getClass(); if (ClassUtils.isPrimitiveOrWrapper(parameterType) || parameterType.isEnum() || ClassUtils.isAssignable(parameterType, LocalDate.class) || ClassUtils.isAssignable(parameterType, DateTime.class) || ClassUtils.isAssignable(parameterType, String.class) || ClassUtils.isAssignable(parameterType, DateTimeRfc1123.class) || ClassUtils.isAssignable(parameterType, Period.class)) { return; } Field[] fields = FieldUtils.getAllFields(parameterType); for (Field field : fields) { field.setAccessible(true); JsonProperty annotation = field.getAnnotation(JsonProperty.class); Object property; try { property = field.get(parameter); } catch (IllegalAccessException e) { throw new ServiceException(e); } if (property == null) { if (annotation != null && annotation.required()) { throw new ServiceException( new IllegalArgumentException(field.getName() + " is required and cannot be null.")); } } else { try { Class propertyType = property.getClass(); if (ClassUtils.isAssignable(propertyType, List.class)) { List<?> items = (List<?>) property; for (Object item : items) { Validator.validate(item); } } else if (ClassUtils.isAssignable(propertyType, Map.class)) { Map<?, ?> entries = (Map<?, ?>) property; for (Map.Entry<?, ?> entry : entries.entrySet()) { Validator.validate(entry.getKey()); Validator.validate(entry.getValue()); } } else if (parameter.getClass().getDeclaringClass() != propertyType) { Validator.validate(property); } } catch (ServiceException ex) { IllegalArgumentException cause = (IllegalArgumentException) (ex.getCause()); if (cause != null) { // Build property chain throw new ServiceException( new IllegalArgumentException(field.getName() + "." + cause.getMessage())); } else { throw ex; } } } } }
From source file:api_proto3.TestElf.java
public static void setSlf4jTargetStream(Class<?> clazz, PrintStream stream) { try {//ww w.j av a 2 s . c om //Log4jLogger log4Jlogger = (Log4jLogger) LoggerFactory.getLogger(clazz); Log4JLogger log4Jlogger = (Log4JLogger) LoggerFactory.getLogger(clazz); //Field field = clazz.getClassLoader().loadClass("org.apache.logging.slf4j.Log4jLogger").getDeclaredField("logger"); Field field = clazz.getClassLoader().loadClass("org.apache.commons.logging.impl.Log4JLogger") .getDeclaredField("logger"); field.setAccessible(true); Logger logger = (Logger) field.get(log4Jlogger); if (logger.getAppenders().containsKey("string")) { Appender appender = logger.getAppenders().get("string"); logger.removeAppender(appender); } logger.addAppender(new StringAppender("string", stream)); } catch (Exception e) { throw new RuntimeException(e); } }