Example usage for java.lang.reflect Field isAnnotationPresent

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

Introduction

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

Prototype

@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) 

Source Link

Usage

From source file:code.elix_x.excore.utils.nbt.mbt.encoders.NBTClassEncoder.java

@Override
public NBTTagCompound toNBT(MBT mbt, Object t) {
    NBTTagCompound nbt = new NBTTagCompound();
    Class clazz = t.getClass();/*from  w  ww  .  j av a 2s .  co m*/
    if (encodeClass)
        nbt.setString(CLASS, clazz.getName());
    while (clazz != null && clazz != Object.class) {
        if (!clazz.isAnnotationPresent(MBTIgnore.class)) {
            for (Field field : clazz.getDeclaredFields()) {
                if (!field.isAnnotationPresent(MBTIgnore.class)) {
                    field.setAccessible(true);
                    if ((encodeFinal || !Modifier.isFinal(field.getModifiers()))
                            && (encodeStatic || !Modifier.isStatic(field.getModifiers()))) {
                        try {
                            nbt.setTag(field.getName(), mbt.toNBT(field.get(t)));
                        } catch (IllegalArgumentException e) {
                            Throwables.propagate(e);
                        } catch (IllegalAccessException e) {
                            Throwables.propagate(e);
                        }
                    }
                }
            }
        }
        clazz = encodeSuper ? clazz.getSuperclass() : Object.class;
    }
    return nbt;
}

From source file:me.henrytao.observableorm.orm.ObservableModel.java

public T deserialize(Map<String, Object> map) throws IllegalAccessException {
    Field[] fields = getDeclaredFields();
    for (Field f : fields) {
        if (!f.isAnnotationPresent(Column.class)) {
            continue;
        }// w  ww. j  a  v a 2  s . c  o  m
        Column column = f.getAnnotation(Column.class);
        if (!column.deserialize()) {
            continue;
        }
        f.setAccessible(true);
        String name = column.name();
        Object value = map.get(name);
        Class type = f.getType();
        Deserializer deserializer = deserializerMap.get(type);
        if (deserializer != null) {
            f.set(this, deserializer.deserialize(value));
        } else if (boolean.class.isAssignableFrom(type)) {
            f.setBoolean(this, value == null ? false : (Boolean) value);
        } else if (double.class.isAssignableFrom(type)) {
            f.setDouble(this, value == null ? 0.0 : (Double) value);
        } else if (float.class.isAssignableFrom(type)) {
            f.setFloat(this, value == null ? 0f : (Float) value);
        } else if (int.class.isAssignableFrom(type)) {
            f.setInt(this, value == null ? 0 : (int) value);
        } else if (short.class.isAssignableFrom(type)) {
            f.setShort(this, value == null ? 0 : (short) value);
        } else if (byte.class.isAssignableFrom(type)) {
            f.setByte(this, value == null ? 0 : (byte) value);
        } else if (String.class.isAssignableFrom(type)) {
            f.set(this, value);
        } else if (JSONObject.class.isAssignableFrom(type)) {
            // todo: nested object should be another model
        }
    }
    return (T) this;
}

From source file:net.ceos.project.poi.annotated.annotation.XlsElementTest.java

/**
 * Test initialization of the comment attribute with specific value.
 *///from w w  w.j  av a2s . c  o  m
@Test
public void checkCommentAttribute() {
    Class<XMenFactory.Cyclops> oC = XMenFactory.Cyclops.class;

    List<Field> fL = Arrays.asList(oC.getDeclaredFields());
    for (Field f : fL) {
        // Process @XlsElement
        if (f.isAnnotationPresent(XlsElement.class)) {

            XlsElement xlsElement = (XlsElement) f.getAnnotation(XlsElement.class);

            if (StringUtils.isNotBlank(xlsElement.comment())) {
                assertEquals(xlsElement.comment(), "This is a comment");
            }
        }
    }
}

From source file:net.ceos.project.poi.annotated.annotation.XlsElementTest.java

/**
 * Test initialization of the decorator attribute with specific value.
 *//*from  ww  w .  j a v a  2 s.c o  m*/
@Test
public void checkDecoratorAttribute() {
    Class<XMenFactory.Cyclops> oC = XMenFactory.Cyclops.class;

    List<Field> fL = Arrays.asList(oC.getDeclaredFields());
    for (Field f : fL) {
        // Process @XlsElement
        if (f.isAnnotationPresent(XlsElement.class)) {

            XlsElement xlsElement = (XlsElement) f.getAnnotation(XlsElement.class);

            if (StringUtils.isNotBlank(xlsElement.decorator())) {
                assertEquals(xlsElement.decorator(), "extendedIntDecorator");
            }
        }
    }
}

From source file:net.ceos.project.poi.annotated.annotation.XlsElementTest.java

/**
 * Test initialization of the formatMask attribute with specific value.
 *//* w ww.  ja  v  a 2  s.c  o  m*/
@Test
public void checkFormatMaskAttribute() {
    Class<XMenFactory.Cyclops> oC = XMenFactory.Cyclops.class;

    List<Field> fL = Arrays.asList(oC.getDeclaredFields());
    for (Field f : fL) {
        // Process @XlsElement
        if (f.isAnnotationPresent(XlsElement.class)) {

            XlsElement xlsElement = (XlsElement) f.getAnnotation(XlsElement.class);

            if (StringUtils.isNotBlank(xlsElement.formatMask())) {
                assertEquals(xlsElement.formatMask(), "0.000");
            }
        }
    }
}

From source file:net.ceos.project.poi.annotated.annotation.XlsElementTest.java

/**
 * Test initialization of the transformMask attribute with specific value.
 *//*from  w  w w.j  ava2s.  c o  m*/
@Test
public void checkTransformMaskAttribute() {
    Class<XMenFactory.Cyclops> oC = XMenFactory.Cyclops.class;

    List<Field> fL = Arrays.asList(oC.getDeclaredFields());
    for (Field f : fL) {
        // Process @XlsElement
        if (f.isAnnotationPresent(XlsElement.class)) {

            XlsElement xlsElement = (XlsElement) f.getAnnotation(XlsElement.class);

            if (StringUtils.isNotBlank(xlsElement.transformMask())) {
                assertEquals(xlsElement.transformMask(), "0.0");
            }
        }
    }
}

From source file:net.ceos.project.poi.annotated.annotation.XlsElementTest.java

/**
 * Test initialization of the isFormula & formula attribute with specific
 * value.//www  .j  ava2  s  .c o m
 */
@Test
public void checkFormulaAttribute() {
    Class<XMenFactory.Cyclops> oC = XMenFactory.Cyclops.class;

    List<Field> fL = Arrays.asList(oC.getDeclaredFields());
    for (Field f : fL) {
        // Process @XlsElement
        if (f.isAnnotationPresent(XlsElement.class)) {

            XlsElement xlsElement = (XlsElement) f.getAnnotation(XlsElement.class);

            if (xlsElement.isFormula()) {
                assertEquals(xlsElement.formula(), "SUM(E3,F3)");
            }
        }
    }
}

From source file:org.kaleidofoundry.spring.context.BeanContextPostProcessor.java

@Override
public Object postProcessBeforeInitialization(final Object beanInstance, final String beanName)
        throws BeansException {

    Set<Field> fields = ReflectionHelper.getAllDeclaredFields(beanInstance.getClass());

    for (Field field : fields) {
        // @Autowired is injected using spring bean
        if (field.isAnnotationPresent(Context.class) && !field.isAnnotationPresent(Autowired.class)
                && !field.isAnnotationPresent(Inject.class)) {

            final Context context = field.getAnnotation(Context.class);
            // do field is a runtime context class
            if (field.getType().isAssignableFrom(RuntimeContext.class)) {
                ReflectionUtils.makeAccessible(field);
                ReflectionUtils.setField(field, beanInstance,
                        RuntimeContext.createFrom(context, field.getName(), field.getDeclaringClass()));
            }/* w w w  . j  a v a2  s  . c  o  m*/
            // does the plugin interface have a provider specify
            else if (field.getType().isAnnotationPresent(Provider.class)) {

                try {
                    ReflectionUtils.makeAccessible(field);
                    Object fieldValue = field.get(beanInstance);

                    if (fieldValue == null) {
                        // create provider using annotation meta-information
                        final Provider provideInfo = field.getType().getAnnotation(Provider.class);
                        final Constructor<? extends ProviderService<?>> providerConstructor = provideInfo
                                .value().getConstructor(Class.class);
                        final ProviderService<?> fieldProviderInstance = providerConstructor
                                .newInstance(field.getType());

                        // invoke provides method with Context annotation meta-informations
                        final Method providesMethod = ReflectionUtils.findMethod(provideInfo.value(),
                                ProviderService.PROVIDES_METHOD, Context.class, String.class, Class.class);
                        // get the provider result
                        fieldValue = ReflectionUtils.invokeMethod(providesMethod, fieldProviderInstance,
                                context, field.getName(), field.getType());
                        // set the field that was not yet injected
                        ReflectionUtils.setField(field, beanInstance, fieldValue);
                    }

                } catch (IllegalArgumentException e) {
                    throw new BeanCreationException("Unexpected error during injection", e);
                } catch (IllegalAccessException e) {
                    throw new BeanCreationException("Unexpected error during injection", e);
                } catch (SecurityException e) {
                    throw new BeanCreationException("Unexpected error during injection", e);
                } catch (NoSuchMethodException e) {
                    throw new BeanCreationException("Unexpected error during injection", e);
                } catch (InstantiationException e) {
                    throw new BeanCreationException("Unexpected error during injection", e);
                } catch (InvocationTargetException ite) {
                    throw new BeanCreationException("", ite.getCause() != null ? ite.getCause()
                            : (ite.getTargetException() != null ? ite.getTargetException() : ite));
                } finally {

                }
            }
        }
    }

    return beanInstance;
}

From source file:es.caib.sgtsic.ejb3.AbstractFacade.java

public T wideFind(Object id) {

    boolean isBorrable = true;
    T item = this.find(id);

    if (item == null) {
        return item;
    }/*from w w  w  .jav  a2  s  .  c o  m*/

    for (Field f : entityClass.getDeclaredFields()) {

        boolean hasToManyAnnotations = (f.isAnnotationPresent(OneToMany.class))
                || (f.isAnnotationPresent(ManyToMany.class));

        if (hasToManyAnnotations) {

            try {
                f.setAccessible(true);
                isBorrable = isBorrable && ((List) f.get(item)).isEmpty();
                f.setAccessible(false);
            } catch (IllegalArgumentException | IllegalAccessException ex) {
                Logger.getLogger(AbstractFacade.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }

    return item;
}

From source file:com.threewks.thundr.search.gae.meta.SearchMetadata.java

private void findFieldAccessors(Class<T> type) {
    for (Field field : ReflectUtil.getSupportedFields(type)) {
        if (field.isAnnotationPresent(SearchId.class)) {
            String name = determineName(field);
            String encodedName = encodeFieldName(name);
            idAccessor = new FieldAccessor<T, K>(type, field, name, encodedName, IndexType.Identifier);
            encodedFieldNames.put(encodedName, name);
            accessors.put(name, idAccessor);
        }//from w  w w . j a va2s.c om
        if (field.isAnnotationPresent(SearchIndex.class)) {
            String name = determineName(field);
            String encodedNamed = encodeFieldName(name);
            IndexType indexType = field.getAnnotation(SearchIndex.class).as();
            if (indexType == IndexType.Automatic) {
                indexType = indexTypeLookup.inferIndexType(field.getGenericType());
            }
            accessors.put(name, new FieldAccessor<T, Object>(type, field, name, encodedNamed, indexType));
            encodedFieldNames.put(encodedNamed, name);
        }
    }
}