List of usage examples for java.lang.reflect Field getType
public Class<?> getType()
From source file:com.stratio.deep.es.utils.UtilES.java
/** * converts from JSONObject to an entity class with deep's anotations * * @param classEntity the entity name.// w w w.j av a 2 s. c om * @param jsonObject the instance of the JSONObject to convert. * @param <T> return type. * @return the provided JSONObject converted to an instance of T. * @throws IllegalAccessException * @throws InstantiationException * @throws java.lang.reflect.InvocationTargetException */ public static <T> T getObjectFromJson(Class<T> classEntity, LinkedMapWritable jsonObject) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException { T t = classEntity.newInstance(); Field[] fields = AnnotationUtils.filterDeepFields(classEntity); Object insert; for (Field field : fields) { Method method = Utils.findSetter(field.getName(), classEntity, field.getType()); Class<?> classField = field.getType(); String key = AnnotationUtils.deepFieldName(field); Text text = new org.apache.hadoop.io.Text(key); Writable currentJson = jsonObject.get(text); if (currentJson != null) { if (Iterable.class.isAssignableFrom(classField)) { Type type = field.getGenericType(); insert = subDocumentListCase(type, (ArrayWritable) currentJson); method.invoke(t, (insert)); } else if (IDeepType.class.isAssignableFrom(classField)) { insert = getObjectFromJson(classField, (LinkedMapWritable) currentJson); method.invoke(t, (insert)); } else { insert = currentJson; try { method.invoke(t, getObjectFromWritable((Writable) insert)); } catch (Exception e) { LOG.error("impossible to convert field " + t + " :" + field + " error: " + e.getMessage()); method.invoke(t, Utils.castNumberType(getObjectFromWritable((Writable) insert), t.getClass())); } } } } return t; }
From source file:com.eviware.x.form.support.ADialogBuilder.java
public static XFormDialog buildWizard(Class<? extends Object> tabbedFormClass) { AForm formAnnotation = tabbedFormClass.getAnnotation(AForm.class); if (formAnnotation == null) { throw new RuntimeException("formClass is not annotated correctly.."); }/*from w ww .ja va 2s . co m*/ MessageSupport messages = MessageSupport.getMessages(tabbedFormClass); XFormDialogBuilder builder = XFormFactory.createDialogBuilder(formAnnotation.name()); for (Field field : tabbedFormClass.getFields()) { APage pageAnnotation = field.getAnnotation(APage.class); if (pageAnnotation != null) { buildForm(builder, pageAnnotation.name(), field.getType(), messages); } } XFormDialog dialog = builder.buildWizard(formAnnotation.description(), UISupport.createImageIcon(formAnnotation.icon()), formAnnotation.helpUrl()); return dialog; }
From source file:org.web4thejob.util.L10nUtil.java
public static List<L10nString> getLocalizableResources(final Class<?> localizable) { final List<L10nString> strings = new ArrayList<L10nString>(); final Set<Class> classes = new HashSet<Class>(); classes.add(localizable);//w w w. j a v a 2 s . co m classes.addAll(ClassUtils.getAllInterfacesForClassAsSet(localizable)); for (Class<?> clazz : classes) { ReflectionUtils.doWithFields(clazz, new ReflectionUtils.FieldCallback() { @Override public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { strings.add((L10nString) field.get(null)); } }, new ReflectionUtils.FieldFilter() { @Override public boolean matches(Field field) { return ReflectionUtils.isPublicStaticFinal(field) && L10nString.class.equals(field.getType()); } }); } //get localizable resources from extension modules for (Module module : ContextUtil.getModules()) { if (module instanceof LocalizableModule) { strings.addAll(((LocalizableModule) module).getLocalizableStrings(classes)); } } // add commands,settings and global strings here... if (DesktopLayoutPanel.class.isAssignableFrom(localizable)) { for (CommandEnum commandEnum : CommandEnum.values()) { L10nString l10nString = new L10nString(CommandEnum.class, commandEnum.name(), L10nUtil.getMessage(commandEnum.getClass(), commandEnum.name(), commandEnum.name())); strings.add(l10nString); } for (SettingEnum settingEnum : SettingEnum.values()) { L10nString l10nString = new L10nString(SettingEnum.class, settingEnum.name(), L10nUtil.getMessage(settingEnum.getClass(), settingEnum.name(), settingEnum.name())); strings.add(l10nString); } for (Condition condition : Condition.getConditions()) { L10nString l10nString = new L10nString(Condition.class, condition.getKey(), condition.getKey()); strings.add(l10nString); } } return strings; }
From source file:ch.ralscha.extdirectspring.generator.association.AbstractAssociation.java
public static AbstractAssociation createAssociation(ModelAssociation associationAnnotation, ModelBean model, Field field) { ModelAssociationType type = associationAnnotation.value(); Class<?> associationClass = associationAnnotation.model(); if (associationClass == Object.class) { associationClass = field.getType(); }// ww w . j a v a 2 s. c o m AbstractAssociation association; if (type == ModelAssociationType.HAS_MANY) { association = new HasManyAssociation(associationClass); } else if (type == ModelAssociationType.BELONGS_TO) { association = new BelongsToAssociation(associationClass); } else { association = new HasOneAssociation(associationClass); } association.setAssociationKey(field.getName()); if (StringUtils.hasText(associationAnnotation.foreignKey())) { association.setForeignKey(associationAnnotation.foreignKey()); } else if (type == ModelAssociationType.HAS_MANY) { association.setForeignKey(StringUtils.uncapitalize(field.getDeclaringClass().getSimpleName()) + "_id"); } else if (type == ModelAssociationType.BELONGS_TO || type == ModelAssociationType.HAS_ONE) { association.setForeignKey(StringUtils.uncapitalize(associationClass.getSimpleName()) + "_id"); } if (StringUtils.hasText(associationAnnotation.primaryKey())) { association.setPrimaryKey(associationAnnotation.primaryKey()); } else if (type == ModelAssociationType.HAS_MANY && StringUtils.hasText(model.getIdProperty()) && !model.getIdProperty().equals("id")) { association.setPrimaryKey(model.getIdProperty()); } else if (type == ModelAssociationType.BELONGS_TO || type == ModelAssociationType.HAS_ONE) { Model associationModelAnnotation = associationClass.getAnnotation(Model.class); if (associationModelAnnotation != null && StringUtils.hasText(associationModelAnnotation.idProperty()) && !associationModelAnnotation.idProperty().equals("id")) { association.setPrimaryKey(associationModelAnnotation.idProperty()); } } if (type == ModelAssociationType.HAS_MANY) { HasManyAssociation hasManyAssociation = (HasManyAssociation) association; if (StringUtils.hasText(associationAnnotation.setterName())) { LogFactory.getLog(ModelGenerator.class) .warn(getWarningText(field, association.getType(), "setterName")); } if (StringUtils.hasText(associationAnnotation.getterName())) { LogFactory.getLog(ModelGenerator.class) .warn(getWarningText(field, association.getType(), "getterName")); } if (associationAnnotation.autoLoad()) { hasManyAssociation.setAutoLoad(true); } if (StringUtils.hasText(associationAnnotation.name())) { hasManyAssociation.setName(associationAnnotation.name()); } else { hasManyAssociation.setName(field.getName()); } } else if (type == ModelAssociationType.BELONGS_TO) { BelongsToAssociation belongsToAssociation = (BelongsToAssociation) association; if (StringUtils.hasText(associationAnnotation.setterName())) { belongsToAssociation.setSetterName(associationAnnotation.setterName()); } else { belongsToAssociation.setSetterName("set" + StringUtils.capitalize(field.getName())); } if (StringUtils.hasText(associationAnnotation.getterName())) { belongsToAssociation.setGetterName(associationAnnotation.getterName()); } else { belongsToAssociation.setGetterName("get" + StringUtils.capitalize(field.getName())); } if (associationAnnotation.autoLoad()) { LogFactory.getLog(ModelGenerator.class) .warn(getWarningText(field, association.getType(), "autoLoad")); } if (StringUtils.hasText(associationAnnotation.name())) { LogFactory.getLog(ModelGenerator.class).warn(getWarningText(field, association.getType(), "name")); } } else { HasOneAssociation hasOneAssociation = (HasOneAssociation) association; if (StringUtils.hasText(associationAnnotation.setterName())) { hasOneAssociation.setSetterName(associationAnnotation.setterName()); } else { hasOneAssociation.setSetterName("set" + StringUtils.capitalize(field.getName())); } if (StringUtils.hasText(associationAnnotation.getterName())) { hasOneAssociation.setGetterName(associationAnnotation.getterName()); } else { hasOneAssociation.setGetterName("get" + StringUtils.capitalize(field.getName())); } if (associationAnnotation.autoLoad()) { LogFactory.getLog(ModelGenerator.class) .warn(getWarningText(field, association.getType(), "autoLoad")); } if (StringUtils.hasText(associationAnnotation.name())) { LogFactory.getLog(ModelGenerator.class).warn(getWarningText(field, association.getType(), "name")); } } return association; }
From source file:org.uimafit.factory.ConfigurationParameterFactory.java
private static boolean isMultiValued(Field field) { Class<?> parameterClass = field.getType(); if (parameterClass.isArray()) { return true; } else if (Collection.class.isAssignableFrom(parameterClass)) { return true; }//from w w w . java 2 s.c o m return false; }
From source file:org.appverse.web.framework.backend.api.converters.helpers.ConverterUtils.java
/** * Copy source bean collection properties of type bean to target bean as * detached bean collections//from ww w . ja va 2s . c om * * @param sourceBean * Source Bean * @param targetBean * Target Bean * @param childrenBeanConverters * List of converters needed to convert source bean childs of * type bean collection to target bean childs of type bean * collection * @param sourceFields * Fields of source Bean * @throws Exception */ private static void convertBeanCollectionFields(AbstractBean sourceBean, AbstractBean targetBean, IBeanConverter[] childrenBeanConverters, Field[] sourceFields) throws Exception { // Arrays to hold bean collection field names, target collection types, // source collection parameter types and target collection parameter // types String[] beanCollectionFieldNames = new String[0]; Type[] targetFieldTypes = new Type[0]; Type[] sourceFieldParameterTypes = new Type[0]; Type[] targetFieldParameterTypes = new Type[0]; // For each source field... for (Field field : sourceFields) { // If source field is a collection... if (Collection.class.isAssignableFrom(field.getType())) { // Get Field (Remember is a collection) parameters Type[] sourceFieldParameters = ((ParameterizedType) field.getGenericType()) .getActualTypeArguments(); // If field is a collection of something extending // AbstractBean... if (sourceFieldParameters.length == 1 && AbstractBean.class.isAssignableFrom((Class<?>) sourceFieldParameters[0])) { // If target field is a collection if (Collection.class .isAssignableFrom(targetBean.getClass().getDeclaredField(field.getName()).getType())) { // Get target field parameter types Type[] targetFieldParameters = ((ParameterizedType) targetBean.getClass() .getDeclaredField(field.getName()).getGenericType()).getActualTypeArguments(); // If target field is a collection of something // extending // AbstractBean... if (targetFieldParameters.length == 1 && AbstractBean.class.isAssignableFrom((Class<?>) targetFieldParameters[0])) { // Fill Arrays with name, source field type, target // type and target // parameter type beanCollectionFieldNames = (String[]) ArrayUtils.add(beanCollectionFieldNames, field.getName()); targetFieldTypes = (Type[]) ArrayUtils.add(targetFieldTypes, targetBean.getClass().getDeclaredField(field.getName()).getType()); sourceFieldParameterTypes = (Type[]) ArrayUtils.add(sourceFieldParameterTypes, sourceFieldParameters[0]); targetFieldParameterTypes = (Type[]) ArrayUtils.add(targetFieldParameterTypes, targetFieldParameters[0]); } else { throw new Exception("Target collection parameter type mismatch"); } } else { throw new Exception("Target parameter type mismatch"); } } } } // For each field with collection of bean type... for (int i = 0; i < beanCollectionFieldNames.length; i++) { String beanCollectionFieldName = beanCollectionFieldNames[i]; Type targetFieldType = targetFieldTypes[i]; Type sourceFieldParameterType = sourceFieldParameterTypes[i]; Type targetFieldParameterType = targetFieldParameterTypes[i]; // Select the appropiate converter... IBeanConverter converter = getConverter(childrenBeanConverters, sourceFieldParameterType, targetFieldParameterType); // Create a target detached (LazyArrayList) collection using the // source collection, the target parameter type y the converter... @SuppressWarnings("unchecked") IndirectArrayList<AbstractBean, AbstractBean> lazyCollection = new IndirectArrayList<AbstractBean, AbstractBean>( (List<AbstractBean>) getGetterMethod(beanCollectionFieldName, sourceBean).invoke(sourceBean), (Class<AbstractBean>) targetFieldParameterType, converter); // Set the detached collection on target bean getSetterMethod(beanCollectionFieldName, targetFieldType, targetBean).invoke(targetBean, lazyCollection); } }
From source file:com.taobao.android.builder.tools.guide.AtlasConfigHelper.java
public static void setProperty(Object object, String fieldName, String value) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchFieldException { String[] fieldNames = fieldName.split("\\."); Object last = object;/*w w w .j av a 2s.co m*/ for (int i = 0; i < fieldNames.length - 1; i++) { String field = fieldNames[i]; if (last instanceof NamedDomainObjectContainer) { last = ((NamedDomainObjectContainer) last).maybeCreate(field); } else { Field declaredField = last.getClass().getField(field); declaredField.setAccessible(true); if (null == declaredField.get(last)) { Object newInstance = declaredField.getType().getConstructors().getClass().newInstance(); declaredField.set(last, newInstance); } last = declaredField.get(last); } } BeanUtils.setProperty(last, fieldNames[fieldNames.length - 1], value); }
From source file:com.browseengine.bobo.serialize.JSONSerializer.java
private static void dumpObject(JSONObject jsonObj, Field f, JSONSerializable srcObj) throws JSONSerializationException, JSONException { Object value;/*from w w w . java 2 s.c o m*/ try { value = f.get(srcObj); } catch (Exception e) { throw new JSONSerializationException(e.getMessage(), e); } Class type = f.getType(); String name = f.getName(); if (type.isPrimitive()) { jsonObj.put(name, String.valueOf(value)); } else if (type == String.class) { if (value != null) { jsonObj.put(name, String.valueOf(value)); } } else if (JSONSerializable.class.isInstance(value)) { jsonObj.put(name, serializeJSONObject((JSONSerializable) value)); } }
From source file:org.uimafit.factory.ConfigurationParameterFactory.java
private static String getConfigurationParameterType(Field field) { Class<?> parameterClass = field.getType(); String parameterClassName;//from w w w .j a va 2s.c o m if (parameterClass.isArray()) { parameterClassName = parameterClass.getComponentType().getName(); } else if (Collection.class.isAssignableFrom(parameterClass)) { ParameterizedType collectionType = (ParameterizedType) field.getGenericType(); parameterClassName = ((Class<?>) (collectionType.getActualTypeArguments()[0])).getName(); } else { parameterClassName = parameterClass.getName(); } String parameterType = javaUimaTypeMap.get(parameterClassName); if (parameterType == null) { return ConfigurationParameter.TYPE_STRING; } return parameterType; }
From source file:com.conversantmedia.mapreduce.tool.DistributedResourceManager.java
public static void setFieldValue(Field field, Object bean, Object value) throws IllegalAccessException { // Set it on the field field.setAccessible(true);/*from w w w.ja v a 2 s . co m*/ // Perform type conversion if possible.. SimpleTypeConverter converter = new SimpleTypeConverter(); try { value = converter.convertIfNecessary(value, field.getType()); } catch (ConversionNotSupportedException e) { // If conversion isn't supported, ignore and try and set anyway. } field.set(bean, value); }