List of usage examples for java.lang.reflect Field isAccessible
@Deprecated(since = "9") public boolean isAccessible()
From source file:info.guardianproject.netcipher.web.WebkitProxy.java
/** * Set Proxy for Android 3.2 and below./* ww w . j av a2s .co m*/ */ @SuppressWarnings("all") private static boolean setProxyUpToHC(WebView webview, String host, int port) { Log.d(TAG, "Setting proxy with <= 3.2 API."); HttpHost proxyServer = new HttpHost(host, port); // Getting network Class networkClass = null; Object network = null; try { networkClass = Class.forName("android.webkit.Network"); if (networkClass == null) { Log.e(TAG, "failed to get class for android.webkit.Network"); return false; } Method getInstanceMethod = networkClass.getMethod("getInstance", Context.class); if (getInstanceMethod == null) { Log.e(TAG, "failed to get getInstance method"); } network = getInstanceMethod.invoke(networkClass, new Object[] { webview.getContext() }); } catch (Exception ex) { Log.e(TAG, "error getting network: " + ex); return false; } if (network == null) { Log.e(TAG, "error getting network: network is null"); return false; } Object requestQueue = null; try { Field requestQueueField = networkClass.getDeclaredField("mRequestQueue"); requestQueue = getFieldValueSafely(requestQueueField, network); } catch (Exception ex) { Log.e(TAG, "error getting field value"); return false; } if (requestQueue == null) { Log.e(TAG, "Request queue is null"); return false; } Field proxyHostField = null; try { Class requestQueueClass = Class.forName("android.net.http.RequestQueue"); proxyHostField = requestQueueClass.getDeclaredField("mProxyHost"); } catch (Exception ex) { Log.e(TAG, "error getting proxy host field"); return false; } boolean temp = proxyHostField.isAccessible(); try { proxyHostField.setAccessible(true); proxyHostField.set(requestQueue, proxyServer); } catch (Exception ex) { Log.e(TAG, "error setting proxy host"); } finally { proxyHostField.setAccessible(temp); } Log.d(TAG, "Setting proxy with <= 3.2 API successful!"); return true; }
From source file:org.jasig.cas.util.annotation.AbstractAnnotationBeanPostProcessor.java
public final Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { final List<Field> fields = new ArrayList<Field>(); final Class<?> clazz = bean.getClass(); final Class<?>[] classes = clazz.getClasses(); addDeclaredFields(clazz, fields);/*www . j a v a 2s .c om*/ for (int i = 0; i < classes.length; i++) { addDeclaredFields(classes[i], fields); } try { for (final Field field : fields) { final boolean originalValue = field.isAccessible(); field.setAccessible(true); final Annotation annotation = field.getAnnotation(getSupportedAnnotation()); if (annotation != null) { processField(field, annotation, bean, beanName); } field.setAccessible(originalValue); } } catch (final IllegalAccessException e) { log.warn("Could not access field: " + e.getMessage(), e); } return bean; }
From source file:net.pkhsolutions.ceres.ddd.jpa.AbstractJpaEmbeddableValueObject.java
/** * This implementation uses reflection to calculate the hash code based on * all declared fields in this class and all super classes except * <code>Object</code>. Subclasses that do not want to include all fields in * the calculation, or that do no want to use reflection, may override. <p> * {@inheritDoc }//from www . j a va 2s . com */ @Override public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); try { Class<?> clazz = getClass(); while (clazz != Object.class) { for (Field f : clazz.getDeclaredFields()) { final boolean oldAccessible = f.isAccessible(); f.setAccessible(true); try { builder.append(f.get(this)); } finally { f.setAccessible(oldAccessible); } } clazz = clazz.getSuperclass(); } } catch (Exception e) { throw new RuntimeException("Could not calculate hashcode", e); } return builder.toHashCode(); }
From source file:br.com.cybereagle.androidtestlibrary.shadow.ShadowAsyncTaskLoader.java
private void updateLastLoadCompleteTimeField() { try {/*w w w .ja v a2 s .c o m*/ Field mLastLoadCompleteTimeField = AsyncTaskLoader.class.getDeclaredField("mLastLoadCompleteTimeField"); if (!mLastLoadCompleteTimeField.isAccessible()) { mLastLoadCompleteTimeField.setAccessible(true); } mLastLoadCompleteTimeField.set(asyncTaskLoader, SystemClock.uptimeMillis()); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }
From source file:org.rapidpm.microservice.optionals.cli.CmdLineParser.java
private void initWithArgs() { try {/* w ww . jav a 2 s .c o m*/ final Field cliArguments = Main.class.getDeclaredField("cliArguments"); final boolean accessible = cliArguments.isAccessible(); cliArguments.setAccessible(true); final Optional<String[]> cliOptional = (Optional<String[]>) cliArguments.get(null); if (cliOptional != null) { cliOptional.ifPresent(args -> this.args = args); } cliArguments.setAccessible(accessible); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } }
From source file:org.vulpe.commons.util.VulpeReflectUtil.java
/** * Returns field value from object.// ww w .j av a 2 s.co m * * @param <T> * @param object * @param fieldName * @return */ public static <T> T getFieldValue(final Object object, final String fieldName) { try { final String name = VulpeStringUtil.upperCaseFirst(fieldName); final Method method = getMethod(object.getClass(), "get".concat(name)); if (method != null) { synchronized (method) { boolean setted = false; if (!method.isAccessible()) { method.setAccessible(true); setted = true; } try { return (T) method.invoke(object); } catch (Exception e) { LOG.error(e.getMessage()); } finally { if (setted) { method.setAccessible(false); } } } } final Field field = getField(object.getClass(), fieldName); if (field != null) { synchronized (field) { boolean setted = false; if (!field.isAccessible()) { field.setAccessible(true); setted = true; } try { return (T) field.get(object); } finally { if (setted) { field.setAccessible(false); } } } } return null; } catch (Exception e) { throw new VulpeSystemException(e); } }
From source file:org.atemsource.atem.impl.jpa.SessionFactoryFactoryBean.java
public void afterPropertiesSet() throws Exception { Object o = Proxy.getInvocationHandler(entityManagerFactory); Field field = null; try {/* w w w. j a v a 2 s. c o m*/ field = o.getClass().getDeclaredField("targetEntityManagerFactory"); if (!field.isAccessible()) { field.setAccessible(true); } sessionFactory = ((EntityManagerFactoryImpl) field.get(o)).getSessionFactory(); } catch (NoSuchFieldException e) { field = o.getClass().getDeclaredField("entityManagerFactoryBean"); if (!field.isAccessible()) { field.setAccessible(true); } AbstractEntityManagerFactoryBean factory = (AbstractEntityManagerFactoryBean) field.get(o); sessionFactory = ((EntityManagerFactoryImpl) factory.nativeEntityManagerFactory).getSessionFactory(); } }
From source file:org.vulpe.commons.util.VulpeReflectUtil.java
/** * Sets field value in object.// www . j a v a 2 s. c om * * @param object * @param fieldName * @param value */ public static void setFieldValue(final Object object, final String fieldName, final Object value) { try { final String name = VulpeStringUtil.upperCaseFirst(fieldName); Method method = null; Class<?> classField = (value == null ? getFieldClass(object.getClass(), fieldName) : value.getClass()); while (classField != null && !classField.equals(Object.class)) { method = getMethod(object.getClass(), "set".concat(name), classField); if (method != null) { break; } classField = classField.getSuperclass(); } if (method != null) { synchronized (method) { boolean setted = false; if (!method.isAccessible()) { method.setAccessible(true); setted = true; } try { method.invoke(object, value); } catch (Exception e) { LOG.error(e.getMessage()); } finally { if (setted) { method.setAccessible(false); } } } } else { final Field field = getField(object.getClass(), fieldName); if (field != null) { synchronized (field) { boolean setted = false; if (!field.isAccessible()) { field.setAccessible(true); setted = true; } try { if (field.getType().isAssignableFrom(value.getClass())) { field.set(object, value); } else { Object newValue = value; if (Long.class.isAssignableFrom(field.getType())) { newValue = new Long(value.toString()); } field.set(object, newValue); } } finally { if (setted) { field.setAccessible(false); } } } } } } catch (Exception e) { throw new VulpeSystemException(e); } }
From source file:org.apache.sling.discovery.base.connectors.ping.TopologyRequestValidatorTest.java
private void setPrivate(Object o, String field, Object value) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field f = o.getClass().getDeclaredField(field); if (!f.isAccessible()) { f.setAccessible(true);//from w ww . j a va 2s .c o m } f.set(o, value); }
From source file:net.ageto.gyrex.logback.extensions.csv.CsvPatternLayout.java
@SuppressWarnings("unchecked") @Override//w w w. j av a2s. c om public void start() { final CSVFormatBuilder formatBuilder = CSVFormat.newBuilder(); if (separator != null) { if (separator.length() != 1) { addError("Invalid separator:'" + separator + "'"); return; } formatBuilder.withDelimiter(separator.charAt(0)); } if (newline != null) { if (newline.length() == 0) { addError("Invalid newline:'" + newline + "'"); return; } formatBuilder.withRecordSeparator(newline); } if (encapsulator != null) { if (encapsulator.length() != 1) { addError("Invalid encapsulator:'" + encapsulator + "'"); return; } formatBuilder.withQuoteChar(encapsulator.charAt(0)); formatBuilder.withQuotePolicy(Quote.MINIMAL); } if (escape != null) { if (escape.length() != 1) { addError("Invalid escape:'" + escape + "'"); return; } formatBuilder.withEscape(escape.charAt(0)); if (encapsulator == null) { formatBuilder.withQuotePolicy(Quote.NONE); } } super.start(); try { final Field field = getClass().getDeclaredField("head"); if (!field.isAccessible()) { field.setAccessible(true); } head = (Converter<ILoggingEvent>) field.get(this); } catch (final Exception e) { addError("Unable to initialize internal variable", e); stop(); return; } }