Example usage for java.lang.reflect Method isAnnotationPresent

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

Introduction

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

Prototype

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

Source Link

Usage

From source file:org.b3log.latke.servlet.converter.Converters.java

/**
 * getRendererId from mark {@link org.b3log.latke.servlet.annotation.Render},using"-" as split:class_method_PARAMETER.
 *
 * @param processorClass  class//from ww w. j a va  2 s  . c  o m
 * @param processorMethod method
 * @param i               the index of the
 * @return string
 */
private static String getRendererId(final Class<?> processorClass, final Method processorMethod, final int i) {
    final StringBuilder sb = new StringBuilder();

    if (processorClass.isAnnotationPresent(Render.class)) {
        final String v = processorClass.getAnnotation(Render.class).value();

        if (StringUtils.isNotBlank(v)) {
            sb.append(v).append(v);
        }
    }

    if (processorMethod.isAnnotationPresent(Render.class)) {

        final String v = processorClass.getAnnotation(Render.class).value();

        if (StringUtils.isNotBlank(v)) {
            if (sb.length() > 0) {
                sb.append("-");
            }
            sb.append(v).append(v);
        }
    }

    for (java.lang.annotation.Annotation annotation : processorMethod.getParameterAnnotations()[i]) {
        if (annotation instanceof Render) {
            final String v = ((PathVariable) annotation).value();

            if (sb.length() > 0) {
                sb.append("-");
            }
            sb.append(v).append(v);
        }
    }

    return sb.toString();
}

From source file:com.github.gekoh.yagen.ddl.TableConfig.java

private static <T extends Annotation> Set<AccessibleObject> getAnnotatedFieldOrMethod(
        Set<AccessibleObject> fieldsOrMethods, Class<T> annotationClass, Class entityClass,
        boolean withInheritance) {
    for (Field field : entityClass.getDeclaredFields()) {
        if (field.isAnnotationPresent(annotationClass)) {
            fieldsOrMethods.add(field);/*w ww  .j av a 2s  .  com*/
        }
    }
    for (Method method : entityClass.getDeclaredMethods()) {
        if (method.isAnnotationPresent(annotationClass)) {
            fieldsOrMethods.add(method);
        }
    }
    if (entityClass.getSuperclass() != null && withInheritance) {
        return getAnnotatedFieldOrMethod(fieldsOrMethods, annotationClass, entityClass.getSuperclass(),
                withInheritance);
    }
    return fieldsOrMethods;
}

From source file:com.yahoo.elide.core.EntityDictionary.java

/**
 * Find an arbitrary method./*w  ww  .  j  a  va  2s  .  c o m*/
 *
 * @param entityClass the entity class
 * @param name the name
 * @param paramClass the param class
 * @return method method
 * @throws NoSuchMethodException the no such method exception
 */
public static Method findMethod(Class<?> entityClass, String name, Class<?>... paramClass)
        throws NoSuchMethodException {
    Method m = entityClass.getMethod(name, paramClass);
    int modifiers = m.getModifiers();
    if (Modifier.isAbstract(modifiers)
            || (m.isAnnotationPresent(Transient.class) && !m.isAnnotationPresent(ComputedAttribute.class))) {
        throw new NoSuchMethodException(name);
    }
    return m;
}

From source file:com.industrieit.ohr.OHRJavassister.java

private static boolean isMethodReifAnnotated(Method m) {
    if (m == null) {
        return false;
    }//  w ww. j a v a 2s.c  om
    return m.isAnnotationPresent(Reify.class);
}

From source file:com.mmnaseri.dragonfly.metadata.impl.AnnotationTableMetadataResolver.java

private static String determineValueGenerator(Method method) {
    return method.isAnnotationPresent(GeneratedValue.class)
            ? method.getAnnotation(GeneratedValue.class).generator()
            : null;//from w  w w . j a  v  a  2s .c o m
}

From source file:com.mmnaseri.dragonfly.metadata.impl.AnnotationTableMetadataResolver.java

private static RelationType getRelationType(Method method) {
    if (method.isAnnotationPresent(OneToMany.class)) {
        return RelationType.ONE_TO_MANY;
    } else if (method.isAnnotationPresent(ManyToOne.class)) {
        return RelationType.MANY_TO_ONE;
    } else if (method.isAnnotationPresent(ManyToMany.class)) {
        return RelationType.MANY_TO_MANY;
    }//from   ww w  . j a v a2  s . co m
    return RelationType.ONE_TO_ONE;
}

From source file:com.mmnaseri.dragonfly.metadata.impl.AnnotationTableMetadataResolver.java

private static boolean determineLaziness(Method method) {
    return method.isAnnotationPresent(OneToOne.class)
            && method.getAnnotation(OneToOne.class).fetch().equals(FetchType.LAZY)
            || method.isAnnotationPresent(OneToMany.class)
                    && method.getAnnotation(OneToMany.class).fetch().equals(FetchType.LAZY)
            || method.isAnnotationPresent(ManyToOne.class)
                    && method.getAnnotation(ManyToOne.class).fetch().equals(FetchType.LAZY)
            || method.isAnnotationPresent(ManyToMany.class)
                    && method.getAnnotation(ManyToMany.class).fetch().equals(FetchType.LAZY);
}

From source file:org.agiso.core.i18n.util.I18nUtils.java

private static String findGetterFieldCode(Class<?> c, String field, boolean reflectionCheck)
        throws IntrospectionException {
    for (PropertyDescriptor pd : Introspector.getBeanInfo(c).getPropertyDescriptors()) {
        if (pd.getName().equals(field)) {
            final Method g = pd.getReadMethod();
            if (g != null) {
                // Jeli jest adnotacja I18n na metodzie odczytujcej pole, to pobieranie
                // pobieranie jej klucza (okrelonego przez 'value') lub klucza domylnego:
                if (g.isAnnotationPresent(I18n.class)) {
                    if (g.getAnnotation(I18n.class).value().length() > 0) {
                        return g.getAnnotation(I18n.class).value();
                    } else {
                        return g.getDeclaringClass().getName() + CODE_SEPARATOR + field;
                    }/*from w w  w .j a  v  a  2s  .co  m*/
                } else if (reflectionCheck) {
                    // Pole nie jest opisane adnotacj I18n. Jeli do wyszukania maj by
                    // wykorzystane mechanizmy refleksji, to sprawdzamy interfejsy i nadklas:
                    for (Class<?> i : c.getInterfaces()) {
                        String i18nCode = findGetterFieldCode(i, field, false);
                        if (i18nCode != null) {
                            return i18nCode;
                        }
                    }
                    Class<?> s = c.getSuperclass();
                    if (s != null) {
                        return findGetterFieldCode(s, field, true);
                    }
                }
            }
        }
    }
    if (reflectionCheck) {
        for (Class<?> i : c.getInterfaces()) {
            String i18nCode = findGetterFieldCode(i, field, false);
            if (i18nCode != null) {
                return i18nCode;
            }
        }
    }

    return null;
}

From source file:com.mmnaseri.dragonfly.metadata.impl.AnnotationTableMetadataResolver.java

private static ColumnMetadata determineForeignReference(Method method) {
    final String name;
    final Class<?> entityType;
    if (method.isAnnotationPresent(OneToOne.class)) {
        final OneToOne annotation = method.getAnnotation(OneToOne.class);
        name = "";
        entityType = annotation.targetEntity().equals(void.class) ? method.getReturnType()
                : annotation.targetEntity();
    } else if (method.isAnnotationPresent(ManyToOne.class)) {
        final ManyToOne annotation = method.getAnnotation(ManyToOne.class);
        name = "";
        entityType = annotation.targetEntity().equals(void.class) ? method.getReturnType()
                : annotation.targetEntity();
    } else {/*from w  w  w . j  ava2 s .  co  m*/
        throw new UnsupportedOperationException();
    }
    //noinspection unchecked
    return new UnresolvedColumnMetadata(name, new UnresolvedTableMetadata<Object>((Class<Object>) entityType));
}

From source file:org.guzz.builder.JPA2AnnotationsBuilder.java

protected static void parseClassForAttributes(GuzzContextImpl gf, POJOBasedObjectMapping map, Business business,
        DBGroup dbGroup, SimpleTable st, Class domainClass) {
    //???/*www. jav a2s  .  c o  m*/
    Class parentCls = domainClass.getSuperclass();
    if (parentCls != null && parentCls.isAnnotationPresent(MappedSuperclass.class)) {
        parseClassForAttributes(gf, map, business, dbGroup, st, parentCls);
    }

    javax.persistence.Access access = (javax.persistence.Access) domainClass
            .getAnnotation(javax.persistence.Access.class);
    AccessType accessType = null;

    if (access == null) {
        //@Id@Idfieldproperty
        boolean hasColumnAOnField = false;
        boolean hasColumnAOnProperty = false;

        //detect from @Id, field first.
        Field[] fs = domainClass.getDeclaredFields();

        for (Field f : fs) {
            if (f.isAnnotationPresent(Transient.class))
                continue;
            if (f.isAnnotationPresent(javax.persistence.Id.class)) {
                accessType = AccessType.FIELD;
                break;
            } else if (f.isAnnotationPresent(javax.persistence.Column.class)) {
                hasColumnAOnField = true;
            } else if (f.isAnnotationPresent(org.guzz.annotations.Column.class)) {
                hasColumnAOnField = true;
            }
        }

        if (accessType == null) {
            Method[] ms = domainClass.getDeclaredMethods();
            for (Method m : ms) {
                if (m.isAnnotationPresent(Transient.class))
                    continue;
                if (m.isAnnotationPresent(javax.persistence.Id.class)) {
                    accessType = AccessType.PROPERTY;
                    break;
                } else if (m.isAnnotationPresent(javax.persistence.Column.class)) {
                    hasColumnAOnProperty = true;
                } else if (m.isAnnotationPresent(org.guzz.annotations.Column.class)) {
                    hasColumnAOnProperty = true;
                }
            }
        }

        //@Id@Column@Columnfield?
        if (accessType == null) {
            if (hasColumnAOnField) {
                accessType = AccessType.FIELD;
            } else if (hasColumnAOnProperty) {
                accessType = AccessType.PROPERTY;
            } else {
                accessType = AccessType.FIELD;
            }
        }
    } else {
        accessType = access.value();
    }

    //orm by field
    if (accessType == AccessType.FIELD) {
        Field[] fs = domainClass.getDeclaredFields();

        for (Field f : fs) {
            if (f.isAnnotationPresent(Transient.class))
                continue;
            if (Modifier.isTransient(f.getModifiers()))
                continue;
            if (Modifier.isStatic(f.getModifiers()))
                continue;

            if (f.isAnnotationPresent(javax.persistence.Id.class)) {
                addIdMapping(gf, map, st, dbGroup, f.getName(), domainClass, f);
            } else {
                addPropertyMapping(gf, map, st, f.getName(), f, f.getType());
            }
        }
    } else {
        Method[] ms = domainClass.getDeclaredMethods();
        for (Method m : ms) {
            if (m.isAnnotationPresent(Transient.class))
                continue;
            if (Modifier.isTransient(m.getModifiers()))
                continue;
            if (Modifier.isStatic(m.getModifiers()))
                continue;
            if (Modifier.isPrivate(m.getModifiers()))
                continue;

            String methodName = m.getName();
            String fieldName = null;

            if (m.getParameterTypes().length != 0) {
                continue;
            } else if (Void.TYPE.equals(m.getReturnType())) {
                continue;
            }

            if (methodName.startsWith("get")) {
                fieldName = methodName.substring(3);
            } else if (methodName.startsWith("is")) {//is boolean?
                Class retType = m.getReturnType();

                if (boolean.class.isAssignableFrom(retType)) {
                    fieldName = methodName.substring(2);
                } else if (Boolean.class.isAssignableFrom(retType)) {
                    fieldName = methodName.substring(2);
                }
            }

            //not a javabean read method
            if (fieldName == null) {
                continue;
            }

            fieldName = java.beans.Introspector.decapitalize(fieldName);

            if (m.isAnnotationPresent(javax.persistence.Id.class)) {
                addIdMapping(gf, map, st, dbGroup, fieldName, domainClass, m);
            } else {
                addPropertyMapping(gf, map, st, fieldName, m, m.getReturnType());
            }
        }
    }

    //?attribute override
    AttributeOverride gao = (AttributeOverride) domainClass.getAnnotation(AttributeOverride.class);
    AttributeOverrides gaos = (AttributeOverrides) domainClass.getAnnotation(AttributeOverrides.class);
    AttributeOverride[] aos = gao == null ? new AttributeOverride[0] : new AttributeOverride[] { gao };
    if (gaos != null) {
        ArrayUtil.addToArray(aos, gaos.value());
    }

    for (AttributeOverride ao : aos) {
        String name = ao.name();
        Column col = ao.column();

        TableColumn tc = st.getColumnByPropName(name);
        Assert.assertNotNull(tc,
                "@AttributeOverride cann't override a attribute that doesn't exist. The attribute is:" + name);

        //update is remove and add
        st.removeColumn(tc);

        //change the column name in the database.
        tc.setColName(col.name());

        st.addColumn(tc);
    }
}