List of usage examples for java.lang Class getDeclaredFields
@CallerSensitive public Field[] getDeclaredFields() throws SecurityException
From source file:org.ext4spring.parameter.DefaultParameterBeanService.java
private List<Field> getSupportedFields(Class<?> clazz) { List<Field> supportedFields = new ArrayList<Field>(); for (Field field : clazz.getDeclaredFields()) { int modifiers = field.getModifiers(); if (!Modifier.isStatic(modifiers)) { //!Modifier.isFinal(modifiers) && supportedFields.add(field);/*from www . ja va 2 s . c o m*/ } } return supportedFields; }
From source file:edu.ku.brc.af.ui.forms.DataGetterForObj.java
/** * @param obj//from www. j a v a2 s.c om * @return */ public String makeToString(final Object obj) { showErrors = false; StringBuilder sb = new StringBuilder(); Class<?> clazz = obj.getClass(); for (Field field : clazz.getDeclaredFields()) { Object val = getFieldValue(obj, field.getName()); sb.append(field.getName()); sb.append("="); sb.append(val); sb.append("\n"); } return sb.toString(); }
From source file:edu.usu.sdl.openstorefront.validation.ValidationUtil.java
private static List<RuleResult> validateFields(final ValidationModel validateModel, Class dataClass, String parentFieldName, String parentType) { List<RuleResult> ruleResults = new ArrayList<>(); if (validateModel.getDataObject() == null && validateModel.isAcceptNull() == false) { RuleResult validationResult = new RuleResult(); validationResult.setMessage("The whole data object is null."); validationResult.setValidationRule("Don't allow null object"); validationResult.setEntityClassName(parentType); validationResult.setFieldName(parentFieldName); ruleResults.add(validationResult); } else {/* w w w.j a v a 2 s .c om*/ if (validateModel.getDataObject() != null) { if (dataClass.getSuperclass() != null) { ruleResults.addAll(validateFields(validateModel, dataClass.getSuperclass(), null, null)); } for (Field field : dataClass.getDeclaredFields()) { Class fieldClass = field.getType(); boolean process = true; if (validateModel.isConsumeFieldsOnly()) { ConsumeField consumeField = (ConsumeField) field.getAnnotation(ConsumeField.class); if (consumeField == null) { process = false; } } if (process) { if (ReflectionUtil.isComplexClass(fieldClass)) { //composition class if (Logger.class.getName().equals(fieldClass.getName()) == false && fieldClass.isEnum() == false) { try { Method method = validateModel.getDataObject().getClass().getMethod( "get" + StringUtils.capitalize(field.getName()), (Class<?>[]) null); Object returnObj = method.invoke(validateModel.getDataObject(), (Object[]) null); boolean check = true; if (returnObj == null) { NotNull notNull = (NotNull) fieldClass.getAnnotation(NotNull.class); if (notNull == null) { check = false; } } if (check) { ruleResults.addAll( validateFields(ValidationModel.copy(validateModel, returnObj), fieldClass, field.getName(), validateModel.getDataObject().getClass().getSimpleName())); } } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { throw new OpenStorefrontRuntimeException(ex); } } } else if (fieldClass.getSimpleName().equalsIgnoreCase(List.class.getSimpleName()) || fieldClass.getSimpleName().equalsIgnoreCase(Map.class.getSimpleName()) || fieldClass.getSimpleName().equalsIgnoreCase(Collection.class.getSimpleName()) || fieldClass.getSimpleName().equalsIgnoreCase(Set.class.getSimpleName())) { //multi if (fieldClass.getSimpleName().equalsIgnoreCase(Map.class.getSimpleName())) { try { Method method = validateModel.getDataObject().getClass().getMethod( "get" + StringUtils.capitalize(field.getName()), (Class<?>[]) null); Object returnObj = method.invoke(validateModel.getDataObject(), (Object[]) null); Map mapObj = (Map) returnObj; for (Object entryObj : mapObj.entrySet()) { ruleResults.addAll( validateFields(ValidationModel.copy(validateModel, entryObj), entryObj.getClass(), field.getName(), validateModel.getDataObject().getClass().getSimpleName())); } } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { throw new OpenStorefrontRuntimeException(ex); } } else { try { Method method = validateModel.getDataObject().getClass().getMethod( "get" + StringUtils.capitalize(field.getName()), (Class<?>[]) null); Object returnObj = method.invoke(validateModel.getDataObject(), (Object[]) null); if (returnObj != null) { for (Object itemObj : (Collection) returnObj) { if (itemObj != null) { ruleResults.addAll(validateFields( ValidationModel.copy(validateModel, itemObj), itemObj.getClass(), field.getName(), validateModel.getDataObject().getClass().getSimpleName())); } else { log.log(Level.WARNING, "There is a NULL item in a collection. Check data passed in to validation."); } } } else { NotNull notNull = field.getAnnotation(NotNull.class); if (notNull != null) { RuleResult ruleResult = new RuleResult(); ruleResult.setMessage("Collection is required"); ruleResult.setEntityClassName( validateModel.getDataObject().getClass().getSimpleName()); ruleResult.setFieldName(field.getName()); ruleResult.setValidationRule("Requires value"); ruleResults.add(ruleResult); } } } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { throw new OpenStorefrontRuntimeException(ex); } } } else { //simple case for (BaseRule rule : rules) { //Stanize if requested if (validateModel.getSantize()) { Sanitize santize = field.getAnnotation(Sanitize.class); if (santize != null) { try { Sanitizer santizer = santize.value().newInstance(); Method method = dataClass.getMethod( "get" + StringUtils.capitalize(field.getName()), (Class<?>[]) null); Object returnObj = method.invoke(validateModel.getDataObject(), (Object[]) null); Object newValue = santizer.santize(returnObj); method = dataClass.getMethod( "set" + StringUtils.capitalize(field.getName()), String.class); method.invoke(validateModel.getDataObject(), newValue); } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException | InvocationTargetException ex) { throw new OpenStorefrontRuntimeException(ex); } } } RuleResult validationResult = rule.processField(field, validateModel.getDataObject()); if (validationResult != null) { ruleResults.add(validationResult); } } } } } } } return ruleResults; }
From source file:com.liferay.portal.spring.extender.internal.bean.ServiceReferenceAnnotationBeanPostProcessor.java
protected void autoInject(Object targetBean, Class<?> beanClass) { if ((beanClass == null) || beanClass.isInterface()) { return;/* w w w .j a v a2 s.com*/ } Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { ServiceReference serviceReference = field.getAnnotation(ServiceReference.class); if (serviceReference == null) { continue; } org.osgi.framework.ServiceReference<?> osgiServiceReference = null; try { String filterString = serviceReference.filterString(); if (filterString.isEmpty()) { Class<?> typeClass = serviceReference.type(); osgiServiceReference = _bundleContext.getServiceReference(typeClass.getName()); } else { Class<?> typeClass = serviceReference.type(); org.osgi.framework.ServiceReference<?>[] serviceReferences = _bundleContext .getServiceReferences(typeClass.getName(), filterString); if (serviceReferences != null) { osgiServiceReference = serviceReferences[0]; } } ReflectionUtils.makeAccessible(field); field.set(targetBean, _bundleContext.getService(osgiServiceReference)); } catch (Throwable t) { throw new BeanCreationException(beanClass.getName(), "Unable to inject bean reference fields", t); } _serviceReferences.add(osgiServiceReference); } autoInject(targetBean, beanClass.getSuperclass()); }
From source file:com.ngandroid.lib.NgAndroid.java
public <T> T buildScope(Class<T> clss) { T instance;/*from w ww.j a va 2 s . com*/ try { instance = clss.newInstance(); Field[] fields = clss.getDeclaredFields(); for (Field f : fields) { f.setAccessible(true); Class type = f.getType(); if (type.isInterface() && !f.isAnnotationPresent(Ignore.class)) { f.set(instance, buildModel(type)); } } } catch (InstantiationException | IllegalAccessException e) { // TODO throw new RuntimeException("Error instantiating scope.", e); } return instance; }
From source file:com.sunsprinter.diffunit.core.injection.Injector.java
protected void inject(final Class<?> currentClass, final Class<?> testClass, final Object test) throws DiffUnitInjectionException { if (currentClass != Object.class) { for (final Field field : currentClass.getDeclaredFields()) { final DiffUnitInject annotation = field.getAnnotation(DiffUnitInject.class); if (annotation != null) { final Object key = StringUtils.isEmpty(annotation.objectId()) ? field.getType() : annotation.objectId(); final boolean fieldAccessible = field.isAccessible(); try { field.setAccessible(true); field.set(test, getInjectionMap().get(key)); } catch (final Exception e) { throw new DiffUnitInjectionException(String.format( "DiffUnit unable to inject field '%s' of class '%s' on test of class '%s'. " + "Component key is '%s'. Target field type is '%s'.", field.getName(), currentClass.getName(), testClass.getName(), key, field.getType().getName()), e); } finally { field.setAccessible(fieldAccessible); }/* w w w.j av a 2 s .co m*/ } } inject(currentClass.getSuperclass(), testClass, test); } }
From source file:com.nabla.wapp.server.csv.CsvReader.java
private void buildColumnList(final Class recordClass) { if (recordClass != null) { for (Field field : recordClass.getDeclaredFields()) { final ICsvField definition = field.getAnnotation(ICsvField.class); if (definition != null) { final ICsvSetter writer = cache.get(field.getType()); Assert.notNull(writer,/*ww w . jav a2s . c o m*/ "no CSV setter defined for type '" + field.getType().getSimpleName() + "'"); if (!field.isAccessible()) field.setAccessible(true); // in order to lift restriction on 'private' fields final ICsvColumn column = new CsvColumn(field, writer); expectedColumns.put(column.getName().toLowerCase(), column); columns.add(column); } } buildColumnList(recordClass.getSuperclass()); } }
From source file:com.gisgraphy.webapp.taglib.ConstantsTei.java
/** * Return information about the scripting variables to be created. * //ww w . j a v a 2s .c om * @param data * the input data * @return VariableInfo array of variable information */ @Override public VariableInfo[] getVariableInfo(TagData data) { // loop through and expose all attributes List<VariableInfo> vars = new ArrayList<VariableInfo>(); try { String clazz = data.getAttributeString("className"); if (clazz == null) { clazz = Constants.class.getName(); } Class<?> c = Class.forName(clazz); // if no var specified, get all if (data.getAttributeString("var") == null) { Field[] fields = c.getDeclaredFields(); AccessibleObject.setAccessible(fields, true); for (Field field : fields) { String type = field.getType().getName(); vars.add(new VariableInfo(field.getName(), ((field.getType().isArray()) ? type.substring(2, type.length() - 1) + "[]" : type), true, VariableInfo.AT_END)); } } else { String var = data.getAttributeString("var"); String type = c.getField(var).getType().getName(); vars.add(new VariableInfo(c.getField(var).getName(), ((c.getField(var).getType().isArray()) ? type.substring(2, type.length() - 1) + "[]" : type), true, VariableInfo.AT_END)); } } catch (Exception cnf) { log.error(cnf.getMessage()); } return vars.toArray(new VariableInfo[] {}); }
From source file:com.oltpbenchmark.benchmarks.auctionmark.util.LoaderItemInfo.java
@Override public String toString() { Class<?> hints_class = this.getClass(); ListOrderedMap<String, Object> m = new ListOrderedMap<String, Object>(); for (Field f : hints_class.getDeclaredFields()) { String key = f.getName().toUpperCase(); Object val = null; try {/*from w w w . ja v a 2s . c o m*/ val = f.get(this); } catch (IllegalAccessException ex) { val = ex.getMessage(); } m.put(key, val); } // FOR return (StringUtil.formatMaps(m)); }
From source file:egov.data.ibatis.repository.support.SqlMapRepositoryFactory.java
private Field findSqlMapExecutorDelegate(SqlMapClient sqlMapClient) { Class<?> searchType = sqlMapClient.getClass(); while (!Object.class.equals(searchType) && searchType != null) { Field[] fields = searchType.getDeclaredFields(); for (Field field : fields) { if (SqlMapExecutorDelegate.class.isAssignableFrom(field.getType())) return field; }/*from w ww. j a v a2 s. c om*/ searchType = searchType.getSuperclass(); } return null; }