List of usage examples for java.lang.reflect Field setAccessible
@Override @CallerSensitive public void setAccessible(boolean flag)
From source file:com.iflytek.edu.cloud.frame.web.filter.CheckOpenServiceFilterTest.java
@BeforeClass public static void init() { InitializingService initializingService = new InitializingService(); try {//from w ww. jav a 2 s . c o m initializingService.afterPropertiesSet(); } catch (Exception e1) { e1.printStackTrace(); } ErrorRequestMessageConverter messageConverter = new ErrorRequestMessageConverter(); messageConverter.setJsonMessageConverter(new MappingJackson2HttpMessageConverter()); messageConverter.setXmlMessageConverter(new Jaxb2RootElementHttpMessageConverterExt()); filter = new CheckOpenServiceFilter(); filter.setMessageConverter(messageConverter); try { Field field = filter.getClass().getDeclaredField("methodVersionMap"); field.setAccessible(true); Multimap<String, String> methodVersionMap = (Multimap<String, String>) field.get(filter); methodVersionMap.put("user.get", "1.0.0"); } catch (Exception e) { e.printStackTrace(); } }
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/*from ww w . j a va 2 s.co m*/ * @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:Main.java
/** * returns canAccessSystemClipboard field from InputEvent * * @param ie InputEvent to get the field from *//* w w w. j ava 2 s . c o m*/ private static synchronized boolean inputEvent_canAccessSystemClipboard(InputEvent ie) { if (inputEvent_CanAccessSystemClipboard_Field == null) { inputEvent_CanAccessSystemClipboard_Field = AccessController .doPrivileged(new java.security.PrivilegedAction<Field>() { public Field run() { try { Field field = InputEvent.class.getDeclaredField("canAccessSystemClipboard"); field.setAccessible(true); return field; } catch (SecurityException e) { } catch (NoSuchFieldException e) { } return null; } }); } if (inputEvent_CanAccessSystemClipboard_Field == null) { return false; } boolean ret = false; try { ret = inputEvent_CanAccessSystemClipboard_Field.getBoolean(ie); } catch (IllegalAccessException e) { } return ret; }
From source file:com.qualogy.qafe.business.integration.adapter.MappingAdapter.java
private static Object adaptToJavaObject(String mappingId, String attributeName, Object result, Object valueToAdapt, ClassLoader classLoader) { try {//from w ww .j ava2 s . c o m if (valueToAdapt != null) { Class resultClazz = classLoader.loadClass(result.getClass().getName()); Field field = resultClazz.getDeclaredField(attributeName); field.setAccessible(true); String fieldName = field.getType().getName(); String valueName = valueToAdapt.getClass().getName(); logger.info(field.getType().getName()); if (!fieldName.equals(valueName)) { if (PredefinedClassTypeConverter.isPredefined(field.getType())) valueToAdapt = PredefinedAdapterFactory.create(field.getType()).convert(valueToAdapt); else throw new IllegalArgumentException("Object passed [" + valueToAdapt + "] cannot be converted to type wanted[" + field.getType() + "] for field[" + field.getName() + "] in adapter[" + mappingId + "]"); } else { if (!PredefinedClassTypeConverter.isPredefined(field.getType())) { Class paramClazz = classLoader.loadClass(fieldName); valueToAdapt = paramClazz.cast(valueToAdapt); } } field.set(result, valueToAdapt); } } catch (IllegalArgumentException e) { throw new UnableToAdaptException( "arg [" + valueToAdapt + "] is illegal for field with name [" + attributeName + "]", e); } catch (SecurityException e) { throw new UnableToAdaptException(e); } catch (IllegalAccessException e) { throw new UnableToAdaptException( "field [" + attributeName + "] does not accessible on class [" + result.getClass() + "]", e); } catch (NoSuchFieldException e) { logger.log(Level.WARNING, "field [" + attributeName + "] does not exist on class [" + result.getClass() + "]", e); } catch (ClassNotFoundException e) { throw new UnableToAdaptException( "field [" + attributeName + "] class not found [" + result.getClass() + "]", e); } return result; }
From source file:com.sunway.cbm.util.web.ReflectionUtils.java
/** * ?, ?DeclaredField, ?./* ww w. j av a 2s .c o m*/ * * ?Object?, null. */ 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 // Field??,? } } return null; }
From source file:de.axelfaust.alfresco.nashorn.repo.web.scripts.RepositoryScriptLocation.java
protected static String tryAndGetField(final ScriptContent content, final String fieldName) { String result;/*from ww w . j a v a2 s. c o m*/ try { final Field field = content.getClass().getDeclaredField(fieldName); if (field.getType().isAssignableFrom(String.class)) { field.setAccessible(true); result = (String) field.get(content); } else { result = null; } } catch (final IllegalAccessException | NoSuchFieldException e) { result = null; } return result; }
From source file:com.netflix.spinnaker.orca.config.RedisConfiguration.java
@Deprecated // rz - Kept for backwards compat with old connection configs public static JedisPool createPool(GenericObjectPoolConfig redisPoolConfig, String connection, int timeout, Registry registry, String poolName) { URI redisConnection = URI.create(connection); String host = redisConnection.getHost(); int port = redisConnection.getPort() == -1 ? Protocol.DEFAULT_PORT : redisConnection.getPort(); String redisConnectionPath = isNotEmpty(redisConnection.getPath()) ? redisConnection.getPath() : "/" + DEFAULT_DATABASE; int database = Integer.parseInt(redisConnectionPath.split("/", 2)[1]); String password = redisConnection.getUserInfo() != null ? redisConnection.getUserInfo().split(":", 2)[1] : null;//from ww w.j a va2s .com JedisPool jedisPool = new JedisPool( redisPoolConfig != null ? redisPoolConfig : new GenericObjectPoolConfig(), host, port, timeout, password, database, null); final Field poolAccess; try { poolAccess = Pool.class.getDeclaredField("internalPool"); poolAccess.setAccessible(true); GenericObjectPool<Jedis> pool = (GenericObjectPool<Jedis>) poolAccess.get(jedisPool); registry.gauge(registry.createId("redis.connectionPool.maxIdle", "poolName", poolName), pool, (GenericObjectPool<Jedis> p) -> Integer.valueOf(p.getMaxIdle()).doubleValue()); registry.gauge(registry.createId("redis.connectionPool.minIdle", "poolName", poolName), pool, (GenericObjectPool<Jedis> p) -> Integer.valueOf(p.getMinIdle()).doubleValue()); registry.gauge(registry.createId("redis.connectionPool.numActive", "poolName", poolName), pool, (GenericObjectPool<Jedis> p) -> Integer.valueOf(p.getNumActive()).doubleValue()); registry.gauge(registry.createId("redis.connectionPool.numIdle", "poolName", poolName), pool, (GenericObjectPool<Jedis> p) -> Integer.valueOf(p.getMaxIdle()).doubleValue()); registry.gauge(registry.createId("redis.connectionPool.numWaiters", "poolName", poolName), pool, (GenericObjectPool<Jedis> p) -> Integer.valueOf(p.getMaxIdle()).doubleValue()); return jedisPool; } catch (NoSuchFieldException | IllegalAccessException e) { throw new BeanCreationException("Error creating Redis pool", e); } }
From source file:app.commons.ReflectionUtils.java
/** * We cannot use <code>clazz.getField(String fieldName)</code> because it only returns public fields. *//*from ww w. j a v a 2 s .co m*/ public static Field getField(Class clazz, String fieldName) { Class currentClass = clazz; while (currentClass != null) { try { Field field = currentClass.getDeclaredField(fieldName); field.setAccessible(true); return field; } catch (NoSuchFieldException e) { currentClass = currentClass.getSuperclass(); } } throw new RuntimeException(new NoSuchFieldException(fieldName)); }
From source file:com.jim.im.offline.repo.MessageConverter.java
private static ImMessage convert(DBObject dbObject) { ImMessage imMessage = new ImMessage(); imMessage.setCreateTime(0L);//from w w w . j a v a 2 s . com Map map = dbObject.toMap(); for (String key : FIELD_KEYS) { Field field = FIELD_MAP.get(key); org.springframework.data.mongodb.core.mapping.Field annotation = field.getAnnotation(ALIAS_NAME); if (annotation != null) key = annotation.value(); field.setAccessible(true); Object value = getValue(field, map.get(key)); if (value == null) continue; ReflectionUtils.setField(field, imMessage, value); } return imMessage; }
From source file:com.mewmew.fairy.v1.cli.AnnotatedCLI.java
private static Unsafe stealUnsafe() { try {/*w w w . j a va2 s . co m*/ Class objectStreamClass = Class.forName("java.io.ObjectStreamClass$FieldReflector"); Field unsafeField = objectStreamClass.getDeclaredField("unsafe"); unsafeField.setAccessible(true); return (Unsafe) unsafeField.get(null); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } }