List of usage examples for java.lang Class getDeclaredFields
@CallerSensitive public Field[] getDeclaredFields() throws SecurityException
From source file:com.fengduo.bee.commons.util.ObjectUtils.java
/** * string?trim?/*from w w w .jav a2 s . c o m*/ * * @param object * @throws Exception */ @SuppressWarnings({ "unchecked", "rawtypes" }) private static void trimStringField(Object object, Class<?> clazz) throws Exception { if (object instanceof Map<?, ?>) { Map<Object, Object> target = new HashMap<Object, Object>(); for (Entry<?, ?> entry : ((Map<?, ?>) object).entrySet()) { Object key = entry.getKey(); Object value = entry.getValue(); if (key instanceof String) { key = StringUtils.trim((String) key); } else { trim(key); } if (value instanceof String) { value = StringUtils.trim((String) value); value = StringUtils.replace((String) value, "\"", StringUtils.EMPTY); } else { trim(value); } target.put(key, value); } ((Map<?, ?>) object).clear(); ((Map) object).putAll((Map) target); return; } Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (field.getType() == String.class) { boolean isFoolback = false; if (field.isAccessible() == false) { isFoolback = true; field.setAccessible(true); } String value = (String) field.get(object); if (StringUtils.isNotEmpty(value)) { value = value.trim(); field.set(object, value); } if (isFoolback) { field.setAccessible(false); } } } }
From source file:cn.org.eshow.framwork.soap.AbSoapParams.java
/** * Instantiates a new ab soap params.//ww w .j a v a 2s . c om * * @param obj * the obj */ public AbSoapParams(Object obj) { try { init(); Class<?> clazz = obj.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { String fieldName = field.getName(); field.setAccessible(true); String fieldValue = (String) field.get(obj); params.put(fieldName, fieldValue); } } catch (Exception e) { e.printStackTrace(); } }
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 ww w . ja v a 2s.c om*/ */ @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:HashCodeAssist.java
/** * <p>/* w w w . j av a 2 s.c o m*/ * Appends the fields and values defined by the given object of the given * <code>Class</code>. * </p> * * @param object * the object to append details of * @param clazz * the class to append details of * @param builder * the builder to append to * @param useTransients * whether to use transient fields * @param excludeFields * Collection of String field names to exclude from use in * calculation of hash code */ private static void reflectionAppend(Object object, Class<?> clazz, HashCodeAssist builder, boolean useTransients, String... excludeFields) { if (isRegistered(object)) { return; } try { register(object); Field[] fields = clazz.getDeclaredFields(); List<String> excludedFieldList = excludeFields != null ? Arrays.asList(excludeFields) : Collections.EMPTY_LIST; AccessibleObject.setAccessible(fields, true); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; if (!excludedFieldList.contains(field.getName()) && (field.getName().indexOf('$') == -1) && (useTransients || !Modifier.isTransient(field.getModifiers())) && (!Modifier.isStatic(field.getModifiers()))) { try { Object fieldValue = field.get(object); builder.append(fieldValue); } catch (IllegalAccessException e) { // this can't happen. Would get a Security exception // instead // throw a runtime exception in case the impossible // happens. throw new InternalError("Unexpected IllegalAccessException"); } } } } finally { unregister(object); } }
From source file:org.openlmis.fulfillment.ExposedMessageSourceIntegrationTest.java
private Set<String> getConstantValues(Class clazz) throws IllegalAccessException { Set<String> set = new HashSet<>(); for (Field field : clazz.getDeclaredFields()) { int modifiers = field.getModifiers(); if (isPublic(modifiers) && isStatic(modifiers) && isFinal(modifiers)) { set.add(String.valueOf(field.get(null))); }//w w w .j a v a 2 s.c om } return set; }
From source file:com.redhat.rhn.frontend.nav.test.NavNodeTest.java
public void testLocalizedName() throws Exception { NavNode n1 = new NavNode(); String randName = TestUtils.randomString(); n1.setName(randName);/* w w w . j ava 2s.c o m*/ node.addNode(n1); NavNode n2 = node.getNodes().get(0); assertEquals("**" + randName + "**", n2.getName()); Class c = n2.getClass(); Field[] fields = c.getDeclaredFields(); String privateValue = null; for (int i = 0; i < fields.length; i++) { if (fields[i].getName().equals("name")) { fields[i].setAccessible(true); privateValue = (String) fields[i].get(n1); } } assertEquals(randName, privateValue); }
From source file:com.cassius.spring.assembly.test.common.process.AbstractAnoProcessor.java
/** * Process before test execution//from w w w.j a v a 2 s .c o m * * @param context Spring Context * @param instance Test instance * @throws Exception the exception */ @Override public void before(ApplicationContext context, Object instance) throws Exception { Class<?> clazz = instance.getClass(); while (clazz != null) { Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (needProcessBefore(instance, field)) { doProcessBefore(context, instance, field); } } clazz = clazz.getSuperclass(); } }
From source file:com.cassius.spring.assembly.test.common.process.AbstractAnoProcessor.java
/** * Process after test execution/* w w w . j a v a 2 s .c om*/ * * @param context Spring Context * @param instance Test instance * @throws Exception the exception */ @Override public void after(ApplicationContext context, Object instance) throws Exception { Class<?> clazz = instance.getClass(); while (clazz != null) { Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (needProcessAfter(instance, field)) { doProcessAfter(context, instance, field); } } clazz = clazz.getSuperclass(); } }
From source file:com.geodevv.testing.irmina.MockedOrSpiedInjectionPointScanner.java
public List<InjectionPointDefinition> scanForMock(Class<?> aClass) { ArrayList<InjectionPointDefinition> result = Lists.newArrayList(); for (Field field : aClass.getDeclaredFields()) { Mock annotation = field.getAnnotation(Mock.class); if (annotation != null) { List<Annotation> annotations = getQualifierOrNamed(field); result.add(new InjectionPointDefinition(annotations, field.getType())); }//from w ww.j ava 2 s . c o m } return result; }
From source file:com.geodevv.testing.irmina.MockedOrSpiedInjectionPointScanner.java
public List<InjectionPointDefinition> scanForSpies(Class<?> aClass) { ArrayList<InjectionPointDefinition> result = Lists.newArrayList(); for (Field field : aClass.getDeclaredFields()) { Spy annotation = field.getAnnotation(Spy.class); if (annotation != null) { List<Annotation> annotations = getQualifierOrNamed(field); result.add(new InjectionPointDefinition(annotations, field.getType())); }// w ww .j a v a 2s . c om } return result; }