Example usage for java.lang.reflect Field getAnnotations

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

Introduction

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

Prototype

public Annotation[] getAnnotations() 

Source Link

Usage

From source file:com.hiperium.dao.common.generic.GenericDAO.java

/**
 * Gets the columns that form the primary key of an entity.
 * //  www.j  a va 2  s  .  c o  m
 * @param object
 *            Entity that contains the primary fields.
 * @return the list of primary key fields.
 */
@SuppressWarnings("unchecked")
private <E> List<String> getIdFields(Object object) {
    Class<E> classe = (Class<E>) object.getClass();
    Field[] fields = classe.getDeclaredFields();
    List<String> primaryKeys = new ArrayList<String>();
    for (Field field : fields) {
        Annotation[] annotationArray = field.getAnnotations();
        for (Annotation annotation : annotationArray) {
            if ("@javax.persistence.Id()".equals(annotation.toString())) {
                primaryKeys.add(field.getName());
                break;
            } else if ("@javax.persistence.EmbeddedId()".equals(annotation.toString())) {
                String fieldName = field.getName();
                String methodName = "get" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
                try {
                    Method method = classe.getMethod(methodName, new Class[] {});
                    Object result = method.invoke(object, new Object[] {});
                    Class<E> classe1 = (Class<E>) result.getClass();
                    Field[] fields1 = classe1.getDeclaredFields();
                    for (Field fieldPK : fields1) {
                        if (!"serialVersionUID".equals(fieldPK.getName())) {
                            primaryKeys.add(fieldName + "." + fieldPK.getName());
                        }
                    }
                } catch (RuntimeException e) {
                    continue;
                } catch (NoSuchMethodException e) {
                    continue;
                } catch (IllegalAccessException e) {
                    continue;
                } catch (InvocationTargetException e) {
                    continue;
                }
            }
        }
    }
    return primaryKeys;
}

From source file:org.castor.jaxb.reflection.FieldAnnotationProcessingServiceTest.java

@Test
public final void testWithXmlElementRefs() {
    Class<WithXmlElementRefs> clazz = WithXmlElementRefs.class;
    Assert.assertFalse(clazz.isEnum());//from ww w .ja  v  a2 s . c o m
    Assert.assertNotNull(fieldAnnotationProcessingService);
    Field[] fields = clazz.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        JaxbFieldNature fi = new JaxbFieldNature(new FieldInfo(field.getName()));
        fieldAnnotationProcessingService.processAnnotations(fi, field.getAnnotations());
        if ("anythingInAList".equals(field.getName())) {
            Assert.assertTrue(fi.hasXmlElementRefs());
        }
    }
}

From source file:org.castor.jaxb.reflection.FieldAnnotationProcessingServiceTest.java

@Test
public void testWithXmlAnyAttribute() {
    Class<WithXmlAnyAttribute> clazz = WithXmlAnyAttribute.class;
    Assert.assertFalse(clazz.isEnum());//ww  w .jav  a  2 s .  c o  m
    Assert.assertNotNull(fieldAnnotationProcessingService);
    Field[] fields = clazz.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        JaxbFieldNature fi = new JaxbFieldNature(new FieldInfo(field.getName()));
        fieldAnnotationProcessingService.processAnnotations(fi, field.getAnnotations());
        if ("anythingInAList".equals(field.getName())) {
            Assert.assertTrue(fi.hasXmlAnyAttribute());
        }
    }
}

From source file:org.castor.jaxb.reflection.FieldAnnotationProcessingServiceTest.java

@Test
public void testWithXmlTransient() {
    Class<WithXmlTransient> clazz = WithXmlTransient.class;
    Assert.assertFalse(clazz.isEnum());//  ww w.j  a va 2s  .c  o m
    Assert.assertNotNull(fieldAnnotationProcessingService);
    Field[] fields = clazz.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        JaxbFieldNature fi = new JaxbFieldNature(new FieldInfo(field.getName()));
        fieldAnnotationProcessingService.processAnnotations(fi, field.getAnnotations());
        if ("anythingInAList".equals(field.getName())) {
            Assert.assertTrue(fi.hasXmlTransient());
        }
    }
}

From source file:org.nuunframework.cli.NuunCliPlugin.java

private Set<Field> annotatedFields(Class<?> class_, Class<? extends Annotation> annoClass) {
    Set<Field> fields = new HashSet<Field>();
    for (Field field : class_.getDeclaredFields()) {
        if (field.isAnnotationPresent(annoClass)) {
            fields.add(field);//from  w  w w.  j  ava 2s.c  om
        } else {
            for (Annotation annotation : field.getAnnotations()) {
                if (hasAnnotationDeep(annotation.annotationType(), annoClass)) {
                    //                        return true;
                    fields.add(field);
                }
            }
        }
    }

    return fields;
}

From source file:org.castor.jaxb.reflection.FieldAnnotationProcessingServiceTest.java

@Test
public final void testWithXmlValue() {
    Class<WithXmlValue> clazz = WithXmlValue.class;
    Assert.assertFalse(clazz.isEnum());//from  ww  w  .  jav a 2s.  c  o m
    Assert.assertNotNull(fieldAnnotationProcessingService);
    Field[] fields = clazz.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        JaxbFieldNature fi = new JaxbFieldNature(new FieldInfo(field.getName()));
        fieldAnnotationProcessingService.processAnnotations(fi, field.getAnnotations());
        if ("anythingInAList".equals(field.getName())) {
            Assert.assertTrue(fi.hasXmlValue());

        }
    }
}

From source file:org.castor.jaxb.reflection.FieldAnnotationProcessingServiceTest.java

@Test
public final void testWithXmlID() {
    Class<WithXmlID> clazz = WithXmlID.class;
    Assert.assertFalse(clazz.isEnum());/*from   w w w .j  a  va 2 s .co m*/
    Assert.assertNotNull(fieldAnnotationProcessingService);
    Field[] fields = clazz.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        JaxbFieldNature fi = new JaxbFieldNature(new FieldInfo(field.getName()));
        fieldAnnotationProcessingService.processAnnotations(fi, field.getAnnotations());
        if ("anythingInAList".equals(field.getName())) {
            Assert.assertTrue(fi.hasXmlID());

        }
    }
}

From source file:org.castor.jaxb.reflection.FieldAnnotationProcessingServiceTest.java

@Test
public final void testWithXmlIDREF() {
    Class<WithXmlIDREF> clazz = WithXmlIDREF.class;
    Assert.assertFalse(clazz.isEnum());/*from   w  w w.j a  v a 2 s .  c  o m*/
    Assert.assertNotNull(fieldAnnotationProcessingService);
    Field[] fields = clazz.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        JaxbFieldNature fi = new JaxbFieldNature(new FieldInfo(field.getName()));
        fieldAnnotationProcessingService.processAnnotations(fi, field.getAnnotations());
        if ("anythingInAList".equals(field.getName())) {
            Assert.assertTrue(fi.hasXmlIDREF());

        }
    }
}

From source file:org.castor.jaxb.reflection.FieldAnnotationProcessingServiceTest.java

@Test
public final void testWithXmlList() {
    Class<WithXmlList> clazz = WithXmlList.class;
    Assert.assertFalse(clazz.isEnum());/*from   w  w  w.  ja  va 2  s .  com*/
    Assert.assertNotNull(fieldAnnotationProcessingService);
    Field[] fields = clazz.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        JaxbFieldNature fi = new JaxbFieldNature(new FieldInfo(field.getName()));
        fieldAnnotationProcessingService.processAnnotations(fi, field.getAnnotations());
        if ("anythingInAList".equals(field.getName())) {
            Assert.assertTrue(fi.hasXmlList());

        }
    }
}

From source file:org.castor.jaxb.reflection.FieldAnnotationProcessingServiceTest.java

@Test
public final void testWithXmlMixed() {
    Class<WithXmlMixed> clazz = WithXmlMixed.class;
    Assert.assertFalse(clazz.isEnum());//w  w w. ja v  a 2 s . c  om
    Assert.assertNotNull(fieldAnnotationProcessingService);
    Field[] fields = clazz.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        JaxbFieldNature fi = new JaxbFieldNature(new FieldInfo(field.getName()));
        fieldAnnotationProcessingService.processAnnotations(fi, field.getAnnotations());
        if ("anythingInAList".equals(field.getName())) {
            Assert.assertTrue(fi.hasXmlMixed());

        }
    }
}