Example usage for java.lang.reflect Field getDeclaredAnnotations

List of usage examples for java.lang.reflect Field getDeclaredAnnotations

Introduction

In this page you can find the example usage for java.lang.reflect Field getDeclaredAnnotations.

Prototype

public Annotation[] getDeclaredAnnotations() 

Source Link

Usage

From source file:ro.allevo.fintpws.test.TestUtils.java

/**
 * Method compareJSONObjects.// w  w  w  .j ava  2s.  c  o  m
 * 
 * @param entity
 *            Object
 * @param insertedEntity
 *            JSONObject
 * @param readedEntity
 *            JSONObject
 * @param String
 *            fields excluded from comparison
 * @return boolean
 */
public static boolean compareJSONObjects(Object entity, JSONObject insertedEntity, JSONObject readedEntity,
        String fields) {

    String fieldName = "";
    boolean isTransient = false;
    Field field = null;
    boolean haveAnnotation = false;
    final Method[] allMethods = entity.getClass().getDeclaredMethods();
    boolean isEqual = true;

    for (Method classMethod : allMethods) {
        if (classMethod.getName().startsWith("set")) {
            isTransient = false;
            haveAnnotation = false;
            fieldName = classMethod.getName().substring(3).toLowerCase();

            try {
                field = entity.getClass().getDeclaredField(fieldName);
                // Ex. Annotation : @javax.persistence.Column(name=,
                // unique=false, nullable=true, insertable=true,
                // updatable=true, columnDefinition=, table=, length=30,
                // precision=0, scale=0)
                Annotation[] annotations = field.getDeclaredAnnotations();
                for (Annotation ant : annotations) {
                    isTransient = ant.toString().contains("Transient");
                    haveAnnotation = true;
                }
                if (!fields.contains(fieldName) && (!isTransient || !haveAnnotation)
                        && (!field.getType().toString().contains("ro.allevo.fintpws.model"))
                        && (!field.getType().toString().contains("java.util.List"))) {

                    if (field.getType().toString().contains("BigDecimal")) {
                        if (insertedEntity.optDouble(fieldName) != readedEntity.optDouble(fieldName)) {
                            logger.debug(fieldName);

                            isEqual = false;
                        }
                    } else {
                        if (field.getType().toString().contains("Timestamp")) {
                            Timestamp first = new Timestamp(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
                                    .parse(insertedEntity.getString(fieldName)).getTime());
                            Timestamp second = new Timestamp(
                                    new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
                                            .parse(readedEntity.optString(fieldName)).getTime());
                            if (first.compareTo(second) != 0) {
                                logger.debug(fieldName);
                                isEqual = false;
                            }
                        } else {
                            if (!insertedEntity.optString(fieldName)
                                    .equals(readedEntity.optString(fieldName))) {
                                logger.debug(fieldName);
                                isEqual = false;
                            }
                        }
                    }

                }
            } catch (SecurityException e) {
                logger.error("reflectin security exception");
                isEqual = false;
            } catch (NoSuchFieldException e) {
                logger.error("reflection field exception");
                if (!fields.contains(fieldName))
                    isEqual = false;
            } catch (JSONException e) {
                logger.error("json exception");
                isEqual = false;
            } catch (ParseException e) {
                logger.error("parse exception");
                isEqual = false;
            }
        }
    }
    return isEqual;
}

From source file:samples.builder.AbstractBuilderAnnotation.java

protected AbstractBuilderAnnotation() {
    if (!analyzed) {
        final Class<?> clazz = this.getClass();
        name = clazz.getSimpleName();//ww  w .j  av a  2  s.  co m

        for (Field field : clazz.getDeclaredFields()) {
            for (Annotation annotation : field.getDeclaredAnnotations()) {
                if (EqualsProperty.class.equals(annotation.annotationType())) {
                    EQUALS_FUNCTIONS.add(getFunction(clazz, field));
                } else if (ToStringProperty.class.equals(annotation.annotationType())) {
                    TOSTRING_FUNCTIONS.put(field.getName(), getFunction(clazz, field));
                }
            }
        }

        analyzed = true;
    }
}

From source file:samples.builder.BuilderAnnotation.java

default void prepareBuilders() {
    final Class<?> clazz = this.getClass();
    if (!Store.EQUALS_FUNCTIONS.containsKey(clazz)) {

        final List<Function<BuilderAnnotation<?>, Object>> list = new ArrayList<>();
        Store.EQUALS_FUNCTIONS.put(clazz, list);
        final Map<CharSequence, Function<BuilderAnnotation<?>, Object>> map = new HashMap<>();
        Store.TOSTRING_FUNCTIONS.put(clazz, map);

        for (Field field : this.getClass().getDeclaredFields()) {
            for (Annotation annotation : field.getDeclaredAnnotations()) {
                if (EqualsProperty.class.equals(annotation.annotationType())) {
                    list.add(Store.getFunction(clazz, field));
                } else if (ToStringProperty.class.equals(annotation.annotationType())) {
                    map.put(field.getName(), Store.getFunction(clazz, field));
                }//w  w w . j  a v a  2  s . co  m
            }
        }
    }
}

From source file:com.google.ie.web.controller.ObjectionableController.java

/**
 * Handles the request for checking objectionable content.
 * This request is initiated by TaskQueue for checking objectionable content
 * of ideas and it's started worker TaskQueue for checking objectionable
 * content of different idea attribute./*w w  w. j av a 2  s  .  co  m*/
 * 
 * @param key the key of the {@link EntityIndex} entity to be indexed
 * @return the name of the resource to which the request should be forwarded
 */
@RequestMapping("/check/{key}")
public String checkObjectionable(@PathVariable String key) {
    LOG.debug("Checking Objectionable content for idea having key: " + key);
    if (key != null && key.trim().length() > WebConstants.ZERO) {
        Field[] fields = Idea.class.getDeclaredFields();
        for (Field field : fields) {
            Annotation[] annotations = field.getDeclaredAnnotations();
            for (Annotation annotation : annotations) {
                if (annotation.annotationType() == SearchableProperty.class) {
                    ObjectionableManager.startCheckObjectionableWorker(key, field.getName());
                    break;
                }
            }
        }

    }
    return "queue/queue";
}

From source file:org.apache.directory.fortress.core.AdminMgrConsole.java

private static String getTestDataLabel3() {
    System.out.println("getTestDataLabel3");
    String fieldName = null;/*from w  ww.  j ava2  s  .  c om*/
    try {
        //Field field =... //obtain field object
        Field field = PolicyTestData.class.getField("POLICIES_TP1");
        //POLICIES_TP1
        Annotation[] annotations = field.getDeclaredAnnotations();

        for (Annotation annotation : annotations) {
            if (annotation instanceof MyAnnotation) {
                MyAnnotation myAnnotation = (MyAnnotation) annotation;
                System.out.println("name: " + myAnnotation.name());
                System.out.println("value: " + myAnnotation.value());
                fieldName = myAnnotation.name() + " " + myAnnotation.value();
            }
        }

    } catch (NoSuchFieldException e) {
        System.out.println("annotation excep=" + e);
    }

    return fieldName;
}

From source file:com.rosenvold.spring.SpringContextAnalyzer.java

private boolean isNonSpringManaged(Field field) {
    return containsAnnotation(field.getDeclaredAnnotations(), NonSpringManaged.class);
}

From source file:com.rosenvold.spring.SpringContextAnalyzer.java

private boolean isAutowired(Field field) {
    return containsAnnotation(field.getDeclaredAnnotations(), Autowired.class);
}

From source file:com.rosenvold.spring.SpringContextAnalyzer.java

private boolean isResource(Field field) {
    return containsAnnotation(field.getDeclaredAnnotations(), Resource.class);
}

From source file:org.omnaest.utils.beans.BeanUtils.java

/**
 * Returns a {@link Map} with all property names of the given Java Bean and a {@link Set} of all available annotations for the
 * properties, including the field, getter and setter methods.
 * //from w  w  w .  j a  v a 2  s .co m
 * @param beanClass
 * @return
 */
public static <B> Map<String, Set<Annotation>> propertyNameToBeanPropertyAnnotationSetMap(Class<B> beanClass) {
    //
    Map<String, Set<Annotation>> retmap = new HashMap<String, Set<Annotation>>();

    //
    Map<String, BeanPropertyAccessor<B>> propertyNameToBeanPropertyAccessorMap = BeanUtils
            .propertyNameToBeanPropertyAccessorMap(beanClass);
    for (String propertyName : propertyNameToBeanPropertyAccessorMap.keySet()) {
        //
        BeanPropertyAccessor<B> beanPropertyAccessor = propertyNameToBeanPropertyAccessorMap.get(propertyName);

        //
        Set<Annotation> annotationSet = new HashSet<Annotation>();
        {
            //
            Field field = beanPropertyAccessor.getField();
            if (field != null) {
                Annotation[] annotations = field.getDeclaredAnnotations();
                if (annotations != null) {
                    annotationSet.addAll(Arrays.asList(annotations));
                }
            }
        }
        {
            //
            Method methodGetter = beanPropertyAccessor.getMethodGetter();
            if (methodGetter != null) {
                Annotation[] annotations = methodGetter.getDeclaredAnnotations();
                if (annotations != null) {
                    annotationSet.addAll(Arrays.asList(annotations));
                }
            }
        }
        {
            //
            Method methodSetter = beanPropertyAccessor.getMethodSetter();
            if (methodSetter != null) {
                Annotation[] annotations = methodSetter.getDeclaredAnnotations();
                if (annotations != null) {
                    annotationSet.addAll(Arrays.asList(annotations));
                }
            }
        }

        //
        retmap.put(propertyName, annotationSet);

    }

    //
    return retmap;
}

From source file:com.impetus.kundera.validation.rules.RelationAttributeRule.java

@Override
public boolean validate(Field f) throws RuleValidationException {

    boolean checkvalidation = true;
    for (Annotation annotate : f.getDeclaredAnnotations()) {
        RelationType eruleType = getRuleType(annotate.annotationType().getSimpleName());

        if (eruleType != null) {

            switch (eruleType) {
            case MANY_TO_MANY:
                checkvalidation = validateManyToMany(f, annotate);
                break;
            case MANY_TO_ONE:
                checkvalidation = validateManyToOne(f, annotate);
                break;
            case ONE_TO_MANY:
                checkvalidation = validateOneToMany(f, annotate);
                break;
            case ONE_TO_ONE:
                checkvalidation = validateOneToOne(f, annotate);
                break;

            }//from   w w w .  j ava  2s .co  m

        }
    }

    return checkvalidation;
}