Example usage for java.lang.reflect Method getAnnotation

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

Introduction

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

Prototype

public <T extends Annotation> T getAnnotation(Class<T> annotationClass) 

Source Link

Usage

From source file:co.paralleluniverse.springframework.web.method.support.FiberInvocableHandlerMethod.java

/**
 * @return `true` if the method is not annotated with `@Suspendable` and it doesn't throw `SuspendExecution` nor it is instrumented in any other way.
 *//*from w  w  w. j a v  a2s.  co  m*/
private boolean isSpringTraditionalThreadBlockingControllerMethod() {
    final Method m = getMethod();
    return m.getAnnotation(Suspendable.class) == null
            && !Arrays.asList(m.getExceptionTypes()).contains(SuspendExecution.class)
            && !SuspendableHelper.isInstrumented(m);
}

From source file:net.nelz.simplesm.aop.InvalidateSingleCacheAdvice.java

@Around("invalidateSingle()")
public Object cacheInvalidateSingle(final ProceedingJoinPoint pjp) throws Throwable {
    // This is injected caching.  If anything goes wrong in the caching, LOG the crap outta it,
    // but do not let it surface up past the AOP injection itself.
    String cacheKey = null;/*  w  w  w  . j a  va 2  s. c  o  m*/
    final AnnotationData annotationData;
    try {
        final Method methodToCache = getMethodToCache(pjp);
        final InvalidateSingleCache annotation = methodToCache.getAnnotation(InvalidateSingleCache.class);
        annotationData = AnnotationDataBuilder.buildAnnotationData(annotation, InvalidateSingleCache.class,
                methodToCache);
        if (annotationData.getKeyIndex() > -1) {
            final Object keyObject = getIndexObject(annotationData.getKeyIndex(), pjp, methodToCache);
            final Method keyMethod = getKeyMethod(keyObject);
            final String objectId = generateObjectId(keyMethod, keyObject);
            cacheKey = buildCacheKey(objectId, annotationData);
        }
    } catch (Throwable ex) {
        LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error.", ex);
        return pjp.proceed();
    }

    final Object result = pjp.proceed();

    // This is injected caching.  If anything goes wrong in the caching, LOG the crap outta it,
    // but do not let it surface up past the AOP injection itself.
    try {
        // If we have a -1 key index, then build the cacheKey now.
        if (annotationData.getKeyIndex() == -1) {
            final Method keyMethod = getKeyMethod(result);
            final String objectId = generateObjectId(keyMethod, result);
            cacheKey = buildCacheKey(objectId, annotationData);
        }
        if (cacheKey == null || cacheKey.trim().length() == 0) {
            throw new InvalidParameterException("Unable to find a cache key");
        }
        cache.delete(cacheKey);
    } catch (Throwable ex) {
        LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error.", ex);
    }
    return result;
}

From source file:de.mogwai.common.web.fieldinformationresolver.jpa.JPAAnnotationFieldInformationResolver.java

public Boolean getRequiredInformation(Application aApplication, FacesContext aContext, Object aBase,
        String aProperty) {// ww  w. ja  va2s .  c o m
    try {
        PropertyDescriptor theDescriptor = PropertyUtils.getPropertyDescriptor(aBase, aProperty);
        if (theDescriptor == null) {
            return null;
        }

        Method theReadMethod = theDescriptor.getReadMethod();
        if (theReadMethod != null) {
            Column theColumn = theReadMethod.getAnnotation(Column.class);
            if (theColumn != null) {
                return !theColumn.nullable();
            }
        }

        return null;

    } catch (Exception e) {
        LOGGER.error("Error", e);
        return null;
    }
}

From source file:com.baidu.rigel.test.dbunit.DBUnitExecutionListener.java

@Override
public void afterTestMethod(TestContext testContext) throws Exception {
    Object testInstance = testContext.getTestInstance();
    Method testMethod = testContext.getTestMethod();
    if (testInstance instanceof AbstractDBUnitSpringContextTests) {
        DBUnitFile annotation = testMethod.getAnnotation(DBUnitFile.class);
        if (annotation != null) {
            ((AbstractDBUnitSpringContextTests) testInstance).afterForDBUnit();
        }// ww w .  java  2  s . c o m
    }
}

From source file:de.mogwai.common.web.fieldinformationresolver.jpa.JPAAnnotationFieldInformationResolver.java

public Integer getMaxLengthInformationProvided(Application aApplication, FacesContext aContext, Object aBase,
        String aProperty) {/* w ww  . j  a v  a 2 s.  com*/

    try {
        PropertyDescriptor theDescriptor = PropertyUtils.getPropertyDescriptor(aBase, aProperty);
        if (theDescriptor == null) {
            return null;
        }

        Method theReadMethod = theDescriptor.getReadMethod();
        if (theReadMethod != null) {
            Column theColumn = theReadMethod.getAnnotation(Column.class);
            if (theColumn != null) {
                return theColumn.length();
            }
        }

        return null;

    } catch (Exception e) {
        LOGGER.error("Error", e);
        return null;
    }
}

From source file:com.heliumv.annotation.HvJudgeAspect.java

@Before("anyPublicOperation() && inHeliumv() && @annotation(com.heliumv.annotation.HvJudge)")
public void processAttribute(JoinPoint pjp) throws Throwable {
    //      Class clazz = pjp.getSignature().getDeclaringType() ;
    //      List<HvJudge> attributesList = new ArrayList<HvJudge>() ;
    //   /*from   www .  j ava2  s  .  c o  m*/
    //      System.out.println("In declaring clazz " + clazz.getName() + " inspecting annotations...") ;

    MethodSignature methodSig = getMethodSignatureFrom(pjp);
    Method method = getMethodFrom(pjp);
    HvJudge theModul = (HvJudge) method.getAnnotation(HvJudge.class);
    if (theModul == null)
        return;

    //      System.out.println("Having the HvJudge Annotation with name |'" + theModul.recht() + "'|" +
    //            StringUtils.join(theModul.rechtOder(), "|") + "<") ;
    log.debug("Having the HvJudge Annotation with name |'" + theModul.recht() + "'|"
            + StringUtils.join(theModul.rechtOder(), "|") + "<");

    if (theModul.recht().length() > 0) {
        if (!judgeCall.hatRecht(theModul.recht())) {
            log.info("judge disallowed: '" + theModul.recht() + "' for '" + methodSig.getMethod().getName()
                    + "'");
            throw new EJBExceptionLP(EJBExceptionLP.FEHLER_UNZUREICHENDE_RECHTE,
                    methodSig.getMethod().getName() + ":" + theModul.recht());
        }

        //         System.out.println("judge allowed: '" + theModul.recht() + "'") ;
        log.debug("judge allowed: '" + theModul.recht() + "'");
        return;
    }

    if (theModul.rechtOder().length > 0) {
        for (String recht : theModul.rechtOder()) {
            if (judgeCall.hatRecht(recht)) {
                log.debug("judge allowed: '" + recht + "'");
                //               System.out.println("judge allowed: '" + recht + "'") ;
                return;
            }
        }

        log.info("judge disallowed: '" + Arrays.toString(theModul.rechtOder()) + "' for '"
                + methodSig.getMethod().getName() + "'");
        throw new EJBExceptionLP(EJBExceptionLP.FEHLER_UNZUREICHENDE_RECHTE,
                methodSig.getMethod().getName() + ":" + Arrays.toString(theModul.rechtOder()));
    }
}

From source file:com.visural.domo.spring.TransactionInterceptor.java

@Around("execution(@com.visural.domo.spring.Transactional * *(..))")
public Object transactional(ProceedingJoinPoint mi) throws Throwable {
    MethodSignature signature = (MethodSignature) mi.getSignature();
    Method method = signature.getMethod();
    String connectionSource = method.getAnnotation(Transactional.class).connectionSource();
    boolean alreadyTxForSource = scope.isInScope(connectionSource);
    scope.enter(connectionSource);// w w w.j  ava  2  s. c om
    try {
        if (scope.getSeed(connectionSource, ConnectionSource.class) == null) {
            GeneratorProvider gp = transactionConfig.getConnectionProvider()
                    .getGeneratorProvider(connectionSource);
            scope.seed(GeneratorProvider.class, gp);
            ConnectionSource con = transactionConfig.getConnectionProvider().get(connectionSource);
            scope.seed(ConnectionSource.class, con);
        }
        if (alreadyTxForSource) {
            Savepoint savepoint = null;
            if (transactionConfig.isSavepointAndRollbackNested()) {
                try {
                    savepoint = tx.savepoint();
                } catch (SQLFeatureNotSupportedException ns) {
                    // TODO: log
                    // not supported feature
                }
            }
            try {
                return mi.proceed();
            } catch (Throwable t) {
                if (savepoint != null) {
                    tx.rollbackToSavepoint(savepoint);
                }
                throw t;
            }
        } else {
            try {
                Object o = mi.proceed();
                tx.commit();
                return o;
            } catch (Throwable t) {
                try {
                    tx.rollback();
                } catch (SQLException se) {
                    // prefer to bubble the original error, but log
                    Logger.getLogger(TransactionInterceptor.class.getName()).log(Level.SEVERE,
                            "Failed rolling back transaction after prior error.", se);
                }
                throw t;
            }
        }
    } finally {
        // close connection if we are the outermost tx for this source
        if (!alreadyTxForSource) {
            IOUtil.silentClose(TransactionInterceptor.class, tx.getConnection());
        }
        scope.exit(connectionSource);
    }
}

From source file:com.ms.commons.test.annotation.PrepareAnnotationTool.java

public PrepareAnnotationTool(Method method) {
    this.method = method;
    this.prepare = method.getAnnotation(Prepare.class);
    this.noPrepare = method.getAnnotation(NoPrepare.class);
    this.prepareInfo = method.getDeclaringClass().getAnnotation(PrepareInfo.class);
}

From source file:com.baidu.rigel.test.dbunit.DBUnitExecutionListener.java

@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
    Object testInstance = testContext.getTestInstance();
    Method testMethod = testContext.getTestMethod();
    if (testInstance instanceof AbstractDBUnitSpringContextTests) {
        DBUnitFile annotation = testMethod.getAnnotation(DBUnitFile.class);
        if (annotation != null) {
            String value = annotation.value();
            ((AbstractDBUnitSpringContextTests) testInstance).setDbunitFile(value);
            ((AbstractDBUnitSpringContextTests) testInstance).beforeForDBUnit();
        }/*from  ww w  .ja  va  2  s  .  c  o m*/
    }
}

From source file:com.cloudera.nav.plugin.model.ValidationUtil.java

public void validateRequiredMProperties(Object mPropertyObj) {
    for (Method method : mPropertyObj.getClass().getMethods()) {
        if (!method.isBridge() && method.isAnnotationPresent(MProperty.class)
                && method.getAnnotation(MProperty.class).required()) {
            Class<?> returnType = method.getReturnType();
            Object value;//from w w w  . ja v  a 2  s  . c o  m
            try {
                value = method.invoke(mPropertyObj);
            } catch (IllegalAccessException e) {
                throw new IllegalArgumentException("Could not access MProperty method " + method.getName(), e);
            } catch (InvocationTargetException e) {
                throw new IllegalArgumentException("Could not get MProperty value for " + method.getName(), e);
            }
            if (Collection.class.isAssignableFrom(returnType)) {
                Preconditions.checkArgument(!CollectionUtils.isEmpty((Collection) value),
                        method.getName() + " returned empty collection");
            } else if (String.class.isAssignableFrom(returnType)) {
                Preconditions.checkArgument(!StringUtils.isEmpty((String) value),
                        method.getName() + " returned null or empty string");
            } else {
                Preconditions.checkArgument(value != null, method.getName() + " returned null");
            }
        }
    }
}