List of usage examples for java.lang.reflect Field getAnnotations
public Annotation[] getAnnotations()
From source file:com.hemou.component.saripaar.Validator.java
private List<Field> getViewFieldsWithAnnotations() { List<Field> fieldsWithAnnotations = new ArrayList<Field>(); List<Field> viewFields = getAllViewFields(); for (Field field : viewFields) { Annotation[] annotations = field.getAnnotations(); if (annotations == null || annotations.length == 0) { continue; }//from w w w. ja va 2 s . co m fieldsWithAnnotations.add(field); } return fieldsWithAnnotations; }
From source file:com.hemou.component.saripaar.Validator.java
private void _setEditTextTip() { List<Field> viewFields = _getEverCalculateAllViewFields(); for (Field field : viewFields) { Annotation[] annotations = field.getAnnotations(); if (annotations == null || annotations.length == 0) { continue; } else/* w w w . jav a 2 s .c o m*/ for (Annotation annotation : annotations) { if (annotation.annotationType().equals(TextRule.class)) { TextRule textRule = (TextRule) annotation; int tip; if ((tip = textRule.messageResId()) != 0) { final int mMax = textRule.maxLength(); final int mMin = textRule.minLength(); if (mMin > mMax) throw new IllegalArgumentException( "Illegal attribute on @TextRule annotation type:[minLength > maxLength]"); EditText editText = (EditText) getView(field); editText.addTextChangedListener(new _ListenerOnTextSize(textRule, tip)); } } } } }
From source file:com.hemou.component.saripaar.Validator.java
private List<AnnotationFieldPair> getSaripaarAnnotatedFields() { List<AnnotationFieldPair> annotationFieldPairs = new ArrayList<AnnotationFieldPair>(); List<Field> fieldsWithAnnotations = getViewFieldsWithAnnotations(); for (Field field : fieldsWithAnnotations) { Annotation[] annotations = field.getAnnotations(); for (Annotation annotation : annotations) { if (isSaripaarAnnotation(annotation)) { if (DEBUG) { Log.d(TAG, String.format("%s %s is annotated with @%s", field.getType().getSimpleName(), field.getName(), annotation.annotationType().getSimpleName())); }// ww w.jav a 2 s . c o m annotationFieldPairs.add(new AnnotationFieldPair(annotation, field)); } } } Collections.sort(annotationFieldPairs, new AnnotationFieldPairCompartor()); return annotationFieldPairs; }
From source file:net.ymate.platform.webmvc.ParameterMeta.java
public ParameterMeta(RequestMeta parent, Field paramField) { this.fieldName = paramField.getName(); this.paramType = paramField.getType(); this.isArray = this.paramType.isArray(); this.isUploadFile = this.paramType.equals(IUploadFileWrapper.class); ///* w w w .j a v a 2 s.c o m*/ this.parameterEscape = paramField.getAnnotation(ParameterEscape.class); if (this.parameterEscape == null) { this.parameterEscape = parent.getParameterEscape(); } if (this.parameterEscape != null && this.parameterEscape.skiped()) { this.parameterEscape = null; } // for (Annotation _anno : paramField.getAnnotations()) { this.isParamField = __doParseAnnotation(_anno); if (this.isParamField) { break; } } }
From source file:com.conversantmedia.mapreduce.tool.annotation.handler.MaraAnnotationUtil.java
protected void handleJobFieldAnnotations(Job job, Field jobField, JobInfo jobInfo) throws ToolException { // Create a list of annotations to handle. List<Annotation> annotations = new ArrayList<>(); // First add in any JobInfo nested annotations that are present. addNestedAnnotations(annotations, jobInfo.map(), jobInfo.reduce(), jobInfo.combine(), jobInfo.sorter(), jobInfo.grouping(), jobInfo.partitioner()); // Add in any additional annotations on the 'Job' field annotations.addAll(Arrays.asList(jobField.getAnnotations())); // Now run them all through appropriate handlers if one is registered for (Annotation annotation : annotations) { for (MaraAnnotationHandler handler : this.annotationHandlers) { if (handler.accept(annotation)) { handler.process(annotation, job, jobField); }/*from www . j a va 2s . co m*/ } } }
From source file:org.makersoft.activesql.builder.GenericStatementBuilder.java
public GenericStatementBuilder(Configuration configuration, Class<?> entityClass) { super(configuration); this.entityClass = entityClass; String resource = entityClass.getName().replace('.', '/') + ".java (best guess)"; assistant = new MapperBuilderAssistant(configuration, resource); entity = entityClass.getAnnotation(Entity.class); mapperType = entity.mapper();/* w ww . j av a 2 s . co m*/ if (!mapperType.isAssignableFrom(Void.class)) { namespace = mapperType.getName(); } else { namespace = entityClass.getName(); } assistant.setCurrentNamespace(namespace); databaseId = super.getConfiguration().getDatabaseId(); lang = super.getConfiguration().getDefaultScriptingLanuageInstance(); //~~~~~~~~~~~~~~~~~~~~~~~~~~~ Table table = entityClass.getAnnotation(Table.class); if (table == null) { tableName = CaseFormatUtils.camelToUnderScore(entityClass.getSimpleName()); } else { tableName = table.name(); } ///~~~~~~~~~~~~~~~~~~~~~~ idField = AnnotationUtils.findDeclaredFieldWithAnnoation(Id.class, entityClass); versionField = AnnotationUtils.findDeclaredFieldWithAnnoation(Version.class, entityClass); ReflectionUtils.doWithFields(entityClass, new FieldCallback() { @Override public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { columnFields.add(field); } }, new FieldFilter() { @Override public boolean matches(Field field) { if (Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers())) { return false; } for (Annotation annotation : field.getAnnotations()) { if (Transient.class.isAssignableFrom(annotation.getClass()) || Id.class.isAssignableFrom(annotation.getClass())) { return false; } } return true; } }); }
From source file:nl.knaw.dans.common.lang.search.bean.GenericSearchBeanConverter.java
@SuppressWarnings("unchecked") public IndexDocument toIndexDocument(Object searchBean) throws SearchBeanConverterException, SearchBeanException { Class sbClass = searchBean.getClass(); if (!sbClass.isAnnotationPresent(SearchBean.class)) throw new ObjectIsNotASearchBeanException(sbClass.toString()); SimpleIndexDocument indexDocument = new SimpleIndexDocument(SearchBeanUtil.getDefaultIndex(sbClass)); for (java.lang.reflect.Field classField : ClassUtil.getAllFields(sbClass).values()) { if (classField.isAnnotationPresent(SearchField.class)) { SearchField sbField = classField.getAnnotation(SearchField.class); String fieldName = sbField.name(); boolean isRequired = sbField.required(); Class<? extends SearchFieldConverter<?>> converter = sbField.converter(); String propName = classField.getName(); String getMethodName = "get" + StringUtils.capitalize(propName); addFieldToDocument(searchBean, indexDocument, getMethodName, fieldName, isRequired, converter); if (classField.isAnnotationPresent(CopyField.class)) { for (Annotation annot : classField.getAnnotations()) { if (annot instanceof CopyField) { CopyField sbCopyField = (CopyField) annot; fieldName = sbCopyField.name(); isRequired = sbCopyField.required(); getMethodName = "get" + StringUtils.capitalize(propName) + StringUtils.capitalize(sbCopyField.getterPostfix()); converter = sbCopyField.converter(); addFieldToDocument(searchBean, indexDocument, getMethodName, fieldName, isRequired, converter); }/*from w w w.j ava 2s. co m*/ } } } } if (indexDocument.getIndex() != null) { if (indexDocument.getFields().getByFieldName(indexDocument.getIndex().getPrimaryKey()) == null) throw new PrimaryKeyMissingException("Primary key not set to search bean object."); } return indexDocument; }
From source file:com.cnd.greencube.server.dao.jdbc.JdbcDAO.java
@SuppressWarnings("rawtypes") private List<Column2Property> getColumnsFromObj(Object obj, String[] columns) { Class clazz = obj.getClass(); List<Column2Property> validColumns = new ArrayList<Column2Property>(); // ???/* ww w . jav a 2 s . co m*/ for (Field field : clazz.getDeclaredFields()) { boolean skip = true; String columnName = null; Annotation[] annotations = field.getAnnotations(); for (Annotation a : annotations) { if (a instanceof Column) { columnName = ((Column) a).name(); if (columns != null && !_inarray_(columns, columnName)) skip = true; else { skip = false; } break; } } String s = field.getName(); PropertyDescriptor pd = null; if (!skip) { // ?gettersetter try { pd = new PropertyDescriptor(s, clazz); if (pd == null || pd.getWriteMethod() == null || pd.getReadMethod() == null) { skip = true; } } catch (Exception e) { skip = true; } } if (!skip) { Column2Property c = new Column2Property(); c.propertyName = pd.getName(); c.setterMethodName = pd.getWriteMethod().getName(); c.getterMethodName = pd.getReadMethod().getName(); c.columnName = columnName; validColumns.add(c); } } Column2Property c = new Column2Property(); c.propertyName = "id"; c.setterMethodName = "setId"; c.getterMethodName = "getId"; c.columnName = "C_ID"; validColumns.add(c); return validColumns; }
From source file:org.nuunframework.cli.NuunCliPlugin.java
private Option createOptionFromField(Field field) { Option option = null;/*from w w w . ja v a 2s . co m*/ // Cli Option Builder is completly static :-/ // so we synchronized it ... synchronized (OptionBuilder.class) { // reset the builder creating a dummy option OptionBuilder.withLongOpt("dummy"); OptionBuilder.create(); // NuunOption nuunOption = field.getAnnotation(NuunOption.class); if (nuunOption == null) { for (Annotation anno : field.getAnnotations()) { if (AssertUtils.hasAnnotationDeep(anno.annotationType(), NuunOption.class)) { nuunOption = AssertUtils.annotationProxyOf(NuunOption.class, anno); break; } } } // longopt if (!Strings.isNullOrEmpty(nuunOption.longOpt())) { OptionBuilder.withLongOpt(nuunOption.longOpt()); } // description if (!Strings.isNullOrEmpty(nuunOption.description())) { OptionBuilder.withDescription(nuunOption.description()); } // required OptionBuilder.isRequired((nuunOption.required())); // arg OptionBuilder.hasArg((nuunOption.arg())); // args if (nuunOption.args()) { if (nuunOption.numArgs() > 0) { OptionBuilder.hasArgs(nuunOption.numArgs()); } else { OptionBuilder.hasArgs(); } } // is optional if (nuunOption.optionalArg()) { OptionBuilder.hasOptionalArg(); } // nuun OptionBuilder.withValueSeparator(nuunOption.valueSeparator()); // opt if (!Strings.isNullOrEmpty(nuunOption.opt())) { option = OptionBuilder.create(nuunOption.opt()); } else { option = OptionBuilder.create(); } } return option; }
From source file:com.prepaird.objectgraphdb.ObjectGraphDb.java
private void registerEdgeRelations(Object o, Map<Method, AnnotationFieldValueWrapper> savedRelations) { Class cl = o.getClass();//from w w w . j a v a 2 s .c o m // Checking all the fields for annotations while (cl.getSuperclass() != null) { for (Field f : cl.getDeclaredFields()) { try { // Processing all the annotations on a single field for (Annotation a : f.getAnnotations()) { if (a.annotationType() == OEdgeRelation.class || a.annotationType() == OGRelation.class) { // Setting the field to be accessible from our class // is it is a private member of the class under processing // (which its most likely going to be) // The setAccessible method will not work if you have // Java SecurityManager configured and active. f.setAccessible(true); //this is assumed to be a pojo. call the getter method. //captialize the first letter and add "get" Method getterMethod = Utils.getGetterMethod(cl, f); if (getterMethod == null) { //both methods are null. no good. cannot get value Log.error("could not find getter method for " + f.getName() + " on object of class " + cl.getSimpleName() + ". Cannot save edgerelations"); continue; } String setterName = Utils.getSetterName(f); Class[] params = { getterMethod.getReturnType() }; Method setterMethod = Utils.getMethod(cl, setterName, params); if (setterMethod == null) { Log.error("Setter method " + setterName + " is null for class " + cl.getSimpleName() + ". Cannot save relation for field " + f.getName()); continue; } // Checking for a NullValueValidate annotation savedRelations.put(setterMethod, new AnnotationFieldValueWrapper(f, a, getterMethod.invoke(o))); //if(!Modifier.isTransient(f.getModifiers())){ //set the value to null so it isn't saved as a link. This shouldn't be necessary if the field is transient though Object[] nullArray = { null }; setterMethod.invoke(o, nullArray); } } } catch (RuntimeException ex) { Log.error(ex); throw ex; } catch (Exception ex) { Log.error(ex); } } cl = cl.getSuperclass(); } }