List of usage examples for java.lang Class getDeclaredField
@CallerSensitive public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException
From source file:com.agimatec.validation.util.PropertyAccess.java
public Object get(Object bean) { try {// w ww. j ava 2s. c o m if (rememberField != null) { // cache field of previous access return rememberField.get(bean); } try { // try public method return getProperty(bean, propertyName); } catch (NoSuchMethodException ex) { Object value; try { // try public field Field aField = bean.getClass().getField(propertyName); value = aField.get(bean); rememberField = aField; return value; } catch (NoSuchFieldException ex2) { // search for private/protected field up the hierarchy Class theClass = bean.getClass(); while (theClass != null) { try { Field aField = theClass.getDeclaredField(propertyName); if (!aField.isAccessible()) { aField.setAccessible(true); } value = aField.get(bean); rememberField = aField; return value; } catch (NoSuchFieldException ex3) { // do nothing } theClass = theClass.getSuperclass(); } throw new IllegalArgumentException("cannot access field " + propertyName); } } } catch (IllegalArgumentException e) { throw e; } catch (Exception e) { throw new IllegalArgumentException("cannot access " + propertyName, e); } }
From source file:io.neba.spring.web.NebaRequestContextFilterTest.java
private boolean valueOfField(Class type, String fieldName) throws NoSuchFieldException, IllegalAccessException { Field declaredField = type.getDeclaredField(fieldName); declaredField.setAccessible(true);/*from w w w . java 2 s .c o m*/ return (boolean) declaredField.get(this.testee); }
From source file:br.edu.ifes.bd2hibernate.cgt.Menu.java
private Object scanSpecific(String fieldName, Scanner sc, Class c) throws NoSuchFieldException { Field field = c.getDeclaredField(fieldName); boolean valid = false; Object value = null;//from w ww . j av a 2 s . c o m while (!valid) { System.out.println(this.messages.get(fieldName.toUpperCase())); String type = field.getType().getSimpleName(); switch (type.toUpperCase()) { case "STRING": value = sc.nextLine(); valid = true; break; case "DATE": String date = sc.nextLine(); valid = this.dateValidator.validate(date); if (valid) value = convertStringToDate(date); break; case "INT": value = sc.nextInt(); valid = true; break; case "FLOAT": value = sc.nextFloat(); valid = true; break; } } return value; }
From source file:br.ajmarques.cordova.plugin.localnotification.Options.java
/** * Gibt den Zahlwert des Icons an.//from w ww .j a v a2 s . c o m * * @param {String} className * @param {String} iconName */ private int getIconValue(String className, String iconName) { int icon = 0; try { Class<?> klass = Class.forName(className + ".R$drawable"); icon = (Integer) klass.getDeclaredField(iconName).get(Integer.class); } catch (Exception e) { } return icon; }
From source file:com.evanzeimet.queryinfo.jpa.result.AbstractTupleToPojoQueryInfoResultConverter.java
protected MethodHandle findFieldPutHandle(Class<?> hostClass, String memberName, Class<?> elementJavaType) { MethodHandle result = null;//from w w w . j a va 2 s .c o m try { Field field = hostClass.getDeclaredField(memberName); field.setAccessible(true); result = MethodHandles.lookup().unreflectSetter(field); } catch (NoSuchFieldException | SecurityException | IllegalAccessException e) { String message = String.format("Could not find field put handle for field [%s]", memberName); logger.debug(message); } if (result == null) { Class<?> hostSuperclass = hostClass.getSuperclass(); if (hostSuperclass != null) { result = findFieldPutHandle(hostSuperclass, memberName, elementJavaType); } } return result; }
From source file:io.cloudslang.lang.compiler.caching.CachedPrecompileServiceImplTest.java
@Test public void testInit() throws Exception { CachedPrecompileServiceImpl cachedPrecompileService = new CachedPrecompileServiceImpl(); cachedPrecompileService.init();// w w w . j a v a 2 s . com Class<? extends CachedPrecompileServiceImpl> cachePrecompileClass = cachedPrecompileService.getClass(); Field cachePrecompileClassField = cachePrecompileClass.getDeclaredField(CACHE); cachePrecompileClassField.setAccessible(true); @SuppressWarnings("unchecked") Cache<String, CacheValue> internalCache = (Cache<String, CacheValue>) cachePrecompileClassField .get(cachedPrecompileService); Assert.assertNotNull(internalCache); }
From source file:edu.brown.statistics.AbstractStatistics.java
protected String debug(Database catalog_db, Enum<?> elements[]) { Map<String, Object> m = new ListOrderedMap<String, Object>(); try {// w w w . j a v a 2 s .c om Class<?> statsClass = this.getClass(); for (Enum<?> element : elements) { Field field = statsClass.getDeclaredField(element.toString().toLowerCase()); Object value = field.get(this); if (field.getClass().isAssignableFrom(SortedMap.class)) { SortedMap<?, ?> orig_value = (SortedMap<?, ?>) value; Map<String, Object> inner_m = new ListOrderedMap<String, Object>(); for (Object inner_key : orig_value.keySet()) { Object inner_val = orig_value.get(inner_key); if (inner_val instanceof AbstractStatistics<?>) { inner_val = ((AbstractStatistics<?>) inner_val).debug(catalog_db); } inner_m.put(inner_key.toString(), inner_val); } // FOR value = StringUtil.formatMaps(inner_m); } m.put(element.toString(), value); } // FOR } catch (Exception ex) { ex.printStackTrace(); System.exit(1); } return (String.format("%s\n%s", this.getCatalogItem(catalog_db), StringUtil.prefix(StringUtil.formatMaps(m), DEBUG_SPACER))); }
From source file:jp.co.golorp.emarf.model.Models.java
/** * ??????????/*from w w w . ja v a 2s. c o m*/ * * @param <T> * * @param bean * ??bean */ private static <T> void cp2org(final T bean) { Class<?> clazz = bean.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (field.getName().startsWith(BeanGenerator.ORG_PREFIX)) { try { String orgName = field.getName(); String propertyName = orgName.replaceFirst(BeanGenerator.ORG_PREFIX, ""); Field property = clazz.getDeclaredField(propertyName); field.setAccessible(true); property.setAccessible(true); Object value = property.get(bean); field.set(bean, value); } catch (Exception e) { throw new SystemError(e); } } } }
From source file:org.jsonschema2pojo.integration.config.AnnotationStyleIT.java
@Test public void annotationStyleJackson2ProducesJsonPropertyDescription() throws Exception { Class<?> generatedType = schemaRule.generateAndCompile("/schema/description/description.json", "com.example", config("annotationStyle", "jackson2")).loadClass("com.example.Description"); Field field = generatedType.getDeclaredField("description"); assertThat(field.getAnnotation(JsonPropertyDescription.class).value(), is("A description for this property")); }