List of usage examples for java.lang.reflect Field getAnnotation
public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
From source file:me.ronghai.sa.model.ModelMeta.java
public ModelMeta(Class<T> clazz) { this.clazz = clazz; this.columnFields = new HashMap<>(); this.field2Setter = new HashMap<>(); this.field2Getter = new HashMap<>(); Map<String, PropertyDescriptor> fieldName2PropertyDescriptor = ReflectUtils .findFieldName2PropertyDescriptor(clazz); List<Field> cf = ReflectUtils.getDeclaredFields((List<Field>) null, clazz, false); for (Field field : cf) { if (field.isAnnotationPresent(Column.class)) { Column annotation = (Column) field.getAnnotation(Column.class); String cname;//from w w w . j a v a2 s . c om if (annotation != null && org.apache.commons.lang.StringUtils.isNotEmpty(annotation.name())) { cname = annotation.name(); cname = cname.replaceAll("[\\[\\]]", "`"); } else { cname = field.getName(); } this.columnFields.put(cname, field); this.field2Getter.put(field.getName(), ReflectUtils.findGetter(this.clazz, field, fieldName2PropertyDescriptor, null)); this.field2Setter.put(field.getName(), ReflectUtils.findSetter(this.clazz, field, fieldName2PropertyDescriptor, null)); } } }
From source file:com.dngames.mobilewebcam.PhotoSettings.java
public static void GETSettings(Context context, String configtext, SharedPreferences prefs) { String[] settings = configtext.split("\n"); Editor edit = prefs.edit();/*from w w w .j ava2 s. c om*/ Map<String, Object> all = new HashMap<String, Object>(); for (Field f : PhotoSettings.class.getFields()) { { BooleanPref bp = f.getAnnotation(BooleanPref.class); if (bp != null) { all.put(bp.key(), bp.val()); if (bp.night()) all.put(bp.key() + PhotoSettings.NIGHTPOSTFIX, bp.val()); } } { StringPref sp = f.getAnnotation(StringPref.class); if (sp != null) { all.put(sp.key(), getDefaultString(context, sp)); if (sp.night()) all.put(sp.key() + PhotoSettings.NIGHTPOSTFIX, getDefaultString(context, sp)); } } { EditIntPref ip = f.getAnnotation(EditIntPref.class); if (ip != null) { all.put(ip.key(), ip.val() + ""); if (ip.night()) all.put(ip.key() + PhotoSettings.NIGHTPOSTFIX, ip.val() + ""); } } { IntPref ip = f.getAnnotation(IntPref.class); if (ip != null) { all.put(ip.key(), ip.val()); if (ip.night()) all.put(ip.key() + PhotoSettings.NIGHTPOSTFIX, ip.val()); } } { EditFloatPref fp = f.getAnnotation(EditFloatPref.class); if (fp != null) { all.put(fp.key(), fp.val() + ""); if (fp.night()) all.put(fp.key() + PhotoSettings.NIGHTPOSTFIX, fp.val() + ""); } } } for (String s : settings) { try { String[] setting = s.split(":", 2); String param = setting[0]; if (all.containsKey(param)) { String value = setting[1]; if (value.length() > 0) { Class<? extends Object> c = all.get(param).getClass(); Object val = parseObjectFromString(value, c); if (c == String.class) edit.putString(param, (String) val); else if (c == Boolean.class) edit.putBoolean(param, (Boolean) val); else if (c == Integer.class) edit.putInt(param, (Integer) val); else throw new UnsupportedOperationException(c.toString()); } else { MobileWebCam.LogE("Warning: config.txt entry '" + param + "' value is empty!"); } } } catch (Exception e) { if (e.getMessage() != null) MobileWebCam.LogE(e.getMessage()); e.printStackTrace(); } } edit.commit(); }
From source file:com.monits.jpack.codec.ObjectCodec.java
public ObjectCodec(Class<? extends E> struct) { this.struct = struct; fields = new ArrayList<FieldData>(); for (Field field : struct.getDeclaredFields()) { Encode annotation = field.getAnnotation(Encode.class); if (annotation != null) { FieldData data = new FieldData(); data.metadata = annotation;//from w ww.j av a 2 s .co m data.codec = (Codec<Object>) CodecFactory.get(field); if (data.codec == null) { continue; } data.field = field; data.field.setAccessible(true); fields.add(data); } } Collections.sort(fields, new Comparator<FieldData>() { @Override public int compare(FieldData a, FieldData b) { return a.metadata.value() - b.metadata.value(); } }); }
From source file:in.hatimi.nosh.support.CmdLineManager.java
private Option optionFromField(Field field) { CmdLineOption clo = field.getAnnotation(CmdLineOption.class); if (clo == null) { return null; }//from ww w .j a va 2s.c o m Option option = new Option(clo.name(), clo.description()); //Option option = new Option(clo.name(), clo.longName(), clo.argCount() > 0, clo.description()); if (StringUtils.isNotBlank(clo.longName())) { option.setLongOpt(clo.longName()); } //option.set` option.setArgs(clo.argCount()); option.setRequired(clo.required()); option.setOptionalArg(clo.optionalArg()); option.setValueSeparator(clo.valueSeparator()); return option; }
From source file:be.bittich.dynaorm.maping.BasicColumnMapping.java
/** * do the mapping//w w w. j a va2s. co m * * @throws java.lang.IllegalAccessException */ @Override public <T> KeyValue<List<String>, List<String>> getColumnsValuesMap(T t, TableColumn tableColumn) throws IllegalAccessException { Map<String, Field> mapFields = this.mapToSQLColumns(t, tableColumn); List<String> columnNames = new LinkedList(); List<String> values = new LinkedList(); for (String columnName : mapFields.keySet()) { try { Field field = mapFields.get(columnName); field.setAccessible(true); PrimaryKey annotationPK = field.getAnnotation(PrimaryKey.class); //we don't add generated values to the request if (annotationPK == null || !annotationPK.autoGenerated()) { MetaColumn metaColumn = field.getAnnotation(MetaColumn.class); if (metaColumn != null) { if (metaColumn.notNull() && field.get(t) == null) { throw new NullPointerException( String.format("Column %s should not be null!", columnName)); } } Object fieldVal = field.get(t); String valString = doFilterBeforeApplyToString(fieldVal); values.add(valString); columnNames.add(columnName); } } catch (IllegalArgumentException ex) { Logger.getLogger(BasicColumnMapping.class.getName()).log(Level.SEVERE, null, ex); } } return new DefaultKeyValue<List<String>, List<String>>(columnNames, values); }
From source file:de.ks.flatadocdb.metamodel.Parser.java
private Map<Field, PropertyPersister> resolvePropertyPersisters(Set<Field> allFields) { HashMap<Field, PropertyPersister> retval = new HashMap<>(); Set<Field> fields = allFields.stream().filter(f -> f.isAnnotationPresent(Property.class)) .collect(Collectors.toSet()); for (Field field : fields) { Property annotation = field.getAnnotation(Property.class); Class<? extends PropertyPersister> persisterClass = annotation.value(); PropertyPersister instance = getInstance(persisterClass); retval.put(field, instance);//from ww w.j av a 2 s .c om } return retval; }
From source file:com.arvato.thoroughly.service.tmall.impl.APIServiceImpl.java
private String startSearch(final Class<?> classType, String prefix) { final Field[] objectFields = classType.getDeclaredFields(); final StringBuffer sb = new StringBuffer(); String fieldsSeparatedByCommas = null; prefix = prefix == null ? "" : prefix + "."; for (final Field item : objectFields) { if ("serialVersionUID".equals(item.getName())) { continue; }/*from w ww.j a va 2 s . co m*/ final ApiListField apiListField = item.getAnnotation(ApiListField.class); if (apiListField != null) { continue; } final ApiField annotation = item.getAnnotation(ApiField.class); sb.append(prefix + annotation.value() + ","); } if (StringUtils.isNotEmpty(sb)) { fieldsSeparatedByCommas = sb.toString().substring(0, sb.toString().length() - 1); fields.put(classType.getTypeName(), fieldsSeparatedByCommas); } return fieldsSeparatedByCommas; }
From source file:com.dngames.mobilewebcam.PhotoSettings.java
public static String DumpSettings(Context context, SharedPreferences prefs) { StringBuilder s = new StringBuilder(); Map<String, Object[]> all = new HashMap<String, Object[]>(); for (Field f : PhotoSettings.class.getFields()) { {// w ww . j av a 2s.c o m BooleanPref bp = f.getAnnotation(BooleanPref.class); if (bp != null) { String key = bp.key(); boolean val = prefs.getBoolean(key, bp.val()); all.put(key, new Object[] { val, bp.help() }); if (bp.night()) { key = key + PhotoSettings.NIGHTPOSTFIX; val = prefs.getBoolean(key, bp.val()); all.put(key, new Object[] { val, bp.help() }); } } } { StringPref sp = f.getAnnotation(StringPref.class); if (sp != null) { String key = sp.key(); String val = prefs.getString(key, getDefaultString(context, sp)); all.put(key, new Object[] { val, sp.help() }); if (sp.night()) { key = key + PhotoSettings.NIGHTPOSTFIX; val = prefs.getString(key, getDefaultString(context, sp)); all.put(key, new Object[] { val, sp.help() }); } } } { EditIntPref ip = f.getAnnotation(EditIntPref.class); if (ip != null) { String key = ip.key(); String val = prefs.getString(key, "" + ip.val()); all.put(ip.key(), new Object[] { val, ip.help() }); if (ip.night()) { key = key + PhotoSettings.NIGHTPOSTFIX; val = prefs.getString(key, "" + ip.val()); all.put(key, new Object[] { val, ip.help() }); } } } { IntPref ip = f.getAnnotation(IntPref.class); if (ip != null) { String key = ip.key(); int val = prefs.getInt(key, ip.val()); all.put(ip.key(), new Object[] { val, ip.help() }); if (ip.night()) { key = key + PhotoSettings.NIGHTPOSTFIX; val = prefs.getInt(key, ip.val()); all.put(key, new Object[] { val, ip.help() }); } } } { EditFloatPref fp = f.getAnnotation(EditFloatPref.class); if (fp != null) { String key = fp.key(); String val = prefs.getString(key, "" + fp.val()); all.put(fp.key(), new Object[] { val, fp.help() }); if (fp.night()) { key = key + PhotoSettings.NIGHTPOSTFIX; val = prefs.getString(key, "" + fp.val()); all.put(key, new Object[] { val, fp.help() }); } } } } for (Map.Entry<String, ?> p : all.entrySet()) { Object[] vals = (Object[]) p.getValue(); if (((String) vals[1]).length() > 0) s.append("// " + vals[1] + "\n"); s.append(p.getKey() + ":" + vals[0] + "\n"); } return s.toString(); }
From source file:ru.portal.services.TableServiceImpl.java
@Override public List<String> getTableOrViewMetaData(String tableOrViewName) { List<String> result = new ArrayList<>(); Set<EntityType<?>> set = em.getEntityManagerFactory().getMetamodel().getEntities(); for (EntityType<?> entityType : set) { if (entityType.getBindableJavaType().getAnnotation(PortalTable.class) != null) { if (entityType.getBindableJavaType().getName().equals(tableOrViewName)) { Field[] fields = entityType.getBindableJavaType().getDeclaredFields(); for (Field field : fields) { if (field.getAnnotation(Column.class) != null) { result.add(field.getAnnotation(Column.class).name()); }//ww w .j a v a 2 s. com if (field.getAnnotation(Id.class) != null) { result.add("id"); } } } } } return result; }
From source file:com.urhola.vehicletracker.request.mattersoft.MatterSoftRequest.java
@Override public List<NameValuePair> getParams() { List<NameValuePair> keyValuePairs = new ArrayList<>(); Field[] fields = this.getClass().getDeclaredFields(); for (Field field : fields) { try {/*from w w w .j ava2 s . c om*/ field.setAccessible(true); String key = field.getAnnotation(Title.class).value(); String value = null; if (field.get(this) instanceof List) { for (Object obj : (List<?>) field.get(this)) { String objValue = (String) obj; if (value == null) value = objValue; else value += "_" + objValue; } } else { value = field.get(this).toString(); } keyValuePairs.add(new BasicNameValuePair(key, value)); } catch (IllegalArgumentException | IllegalAccessException | NullPointerException e) { e.printStackTrace(); } } return keyValuePairs; }