Example usage for java.lang Class isAnnotationPresent

List of usage examples for java.lang Class isAnnotationPresent

Introduction

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

Prototype

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

Source Link

Usage

From source file:com.googlecode.wicketelements.security.AnnotationSecurityCheck.java

private <T extends Annotation> boolean impliesAction(final Class<T> annotationParam,
        final Class<? extends Annotation> impliedParam) {
    if (impliedParam.isAssignableFrom(annotationParam)) {
        return true;
    }//from  w  ww  . j a  v a 2s. co m
    if (annotationParam.isAnnotationPresent(ImpliesSecurityAction.class)) {
        final ImpliesSecurityAction a = annotationParam.getAnnotation(ImpliesSecurityAction.class);
        if (a.impliedActions().length > 0) {
            return impliesAction(a.impliedActions()[0], impliedParam);
        }
    }
    return false;
}

From source file:org.openmrs.module.privilegehelper.PrivilegeLogger.java

/**
 * Inspects the stack trace to find a place where the privilege was checked
 * //from   w  w  w  .  ja  v  a 2  s . co  m
 * @return the class.method or <code>null</code> if it cannot be found
 */
private StackTraceInfo inspectStackTrace() {
    final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
    boolean requiredPrivilege = false;
    for (int i = 0; i < stackTrace.length; i++) {
        final StackTraceElement unrelatedElement = stackTrace[i];

        if (UserContext.class.getName().equals(unrelatedElement.getClassName())
                && "hasPrivilege".equals(unrelatedElement.getMethodName())) {

            for (int j = i + 1; j < stackTrace.length; j++) {
                final StackTraceElement element = stackTrace[j];

                if (element.getFileName() != null && element.getFileName().endsWith("_jsp")) {
                    String jsp = element.getFileName();
                    int indexOfView = jsp.indexOf("view");
                    if (indexOfView > 0) {
                        jsp = jsp.substring(indexOfView);
                    }
                    jsp = jsp.replace(".", "/");
                    jsp = jsp.replace("_", ".");

                    return new StackTraceInfo(jsp, requiredPrivilege);
                }

                if (!element.getClassName().startsWith("org.openmrs")) {
                    continue;
                }

                //Determine if it is a required privilege or a simple check
                if (RequireTag.class.getName().equals(element.getClassName())) {
                    requiredPrivilege = true;
                    continue;
                }
                if (PrivilegeTag.class.getName().equals(element.getClassName())) {
                    requiredPrivilege = false;
                    continue;
                }
                if (AuthorizationAdvice.class.getName().equals(element.getClassName())) {
                    requiredPrivilege = true;
                    continue;
                }
                if (Context.class.getName().equals(element.getClassName())) {
                    if ("requirePrivilege".equals(element.getMethodName())) {
                        requiredPrivilege = true;
                    }
                    continue;
                }

                try {
                    final Class<?> clazz = OpenmrsClassLoader.getInstance().loadClass(element.getClassName());

                    if (clazz.isAnnotationPresent(Controller.class)) {
                        String url = "";

                        final RequestMapping clazzRequestMapping = clazz.getAnnotation(RequestMapping.class);
                        if (clazzRequestMapping != null) {
                            url = clazzRequestMapping.value()[0];
                        }

                        final Method[] methods = clazz.getMethods();
                        for (Method method : methods) {
                            if (method.getName().equals(element.getMethodName())) {
                                final RequestMapping requestMapping = method
                                        .getAnnotation(RequestMapping.class);
                                if (requestMapping != null) {
                                    url += requestMapping.value()[0];
                                }
                                break;
                            }
                        }

                        if (url.isEmpty()) {
                            return new StackTraceInfo(element.toString(), requiredPrivilege);
                        } else {
                            return new StackTraceInfo(element.toString() + ", URL: " + url, requiredPrivilege);
                        }
                    }
                } catch (ClassNotFoundException e) {
                }

                return new StackTraceInfo(element.toString(), requiredPrivilege);
            }
        }
    }
    return null;
}

From source file:de.extra.client.core.annotation.PropertyPlaceholderPluginConfigurer.java

@Override
protected void processProperties(final ConfigurableListableBeanFactory beanFactory, final Properties properties)
        throws BeansException {
    super.processProperties(beanFactory, properties);

    for (final String beanName : beanFactory.getBeanDefinitionNames()) {
        final Class<?> clazz = beanFactory.getType(beanName);

        if (LOG.isDebugEnabled()) {
            LOG.debug("Configuring properties for bean=" + beanName + "[" + clazz + "]");
        }/* w  w w  .ja  v a  2  s.c  o  m*/

        if (clazz != null && clazz.isAnnotationPresent(PluginConfiguration.class)) {
            setPluginProperties(beanFactory, properties, beanName, clazz);
        }

    }
}

From source file:org.codehaus.groovy.grails.validation.DefaultConstraintEvaluator.java

/**
 * Evaluates the constraints closure to build the list of constraints
 *
 * @param theClass  The domain class to evaluate constraints for
 * @param properties The properties of the instance
 *
 * @return A Map of constraints//from   w  w w .j a va  2 s  .  co  m
 */
protected Map<String, ConstrainedProperty> evaluateConstraints(final Class<?> theClass,
        GrailsDomainClassProperty[] properties) {

    boolean javaEntity = theClass.isAnnotationPresent(Entity.class);
    LinkedList<?> classChain = getSuperClassChain(theClass);
    Class<?> clazz;

    ConstrainedPropertyBuilder delegate = new ConstrainedPropertyBuilder(theClass);

    // Evaluate all the constraints closures in the inheritance chain
    for (Object aClassChain : classChain) {
        clazz = (Class<?>) aClassChain;
        Closure<?> c = (Closure<?>) GrailsClassUtils.getStaticFieldValue(clazz, PROPERTY_NAME);
        if (c == null) {
            c = getConstraintsFromScript(theClass);
        }

        if (c != null) {
            c = (Closure<?>) c.clone();
            c.setResolveStrategy(Closure.DELEGATE_ONLY);
            c.setDelegate(delegate);
            c.call();
        } else {
            LOG.debug("User-defined constraints not found on class [" + clazz
                    + "], applying default constraints");
        }
    }

    Map<String, ConstrainedProperty> constrainedProperties = delegate.getConstrainedProperties();
    if (properties != null && !(constrainedProperties.isEmpty() && javaEntity)) {

        for (GrailsDomainClassProperty p : properties) {
            // assume no formula issues if Hibernate isn't available to avoid CNFE
            if (canPropertyBeConstrained(p)) {
                if (p.isDerived()) {
                    if (constrainedProperties.remove(p.getName()) != null) {
                        LOG.warn("Derived properties may not be constrained. Property [" + p.getName()
                                + "] of domain class " + theClass.getName()
                                + " will not be checked during validation.");
                    }
                } else {
                    final String propertyName = p.getName();
                    ConstrainedProperty cp = constrainedProperties.get(propertyName);
                    if (cp == null) {
                        cp = new ConstrainedProperty(p.getDomainClass().getClazz(), propertyName, p.getType());
                        cp.setOrder(constrainedProperties.size() + 1);
                        constrainedProperties.put(propertyName, cp);
                    }
                    // Make sure all fields are required by default, unless
                    // specified otherwise by the constraints
                    // If the field is a Java entity annotated with @Entity skip this
                    applyDefaultConstraints(propertyName, p, cp, defaultConstraints);
                }
            }
        }
    }

    if (properties == null || properties.length == 0) {
        final Set<Entry<String, ConstrainedProperty>> entrySet = constrainedProperties.entrySet();
        for (Entry<String, ConstrainedProperty> entry : entrySet) {
            final ConstrainedProperty constrainedProperty = entry.getValue();
            if (!constrainedProperty.hasAppliedConstraint(ConstrainedProperty.NULLABLE_CONSTRAINT)) {
                applyDefaultNullableConstraint(constrainedProperty);
            }
        }
    }

    applySharedConstraints(delegate, constrainedProperties);

    return constrainedProperties;
}

From source file:org.brushingbits.jnap.struts2.config.RestControllerConfigBuilder.java

/**
 * TODO/*  w  w  w .j av  a  2  s.c  om*/
 * 
 * @param controllerClass
 * @return
 */
protected String determineControllerNamespace(Class<?> controllerClass) {
    String controllerPkgName = controllerClass.getPackage().getName();
    String namespace = "";
    if (controllerClass.isAnnotationPresent(Namespace.class)) {
        namespace = controllerClass.getAnnotation(Namespace.class).value();
    } else if (controllerPkgName.length() > this.packageLocatorsBasePackage.length()) {
        namespace = controllerPkgName.substring(this.packageLocatorsBasePackage.length());
        namespace = namespace.replaceAll("\\.", "/");
    }
    return namespace;
}

From source file:com.sqewd.open.dal.core.persistence.DataManager.java

private void scanEntities(final List<Class<?>> classes) throws Exception {
    for (Class<?> type : classes) {
        if (type.isAnnotationPresent(Entity.class)) {
            log.debug("Found entity : [" + type.getCanonicalName() + "]["
                    + type.getClassLoader().getClass().getCanonicalName() + "]");
            ReflectionUtils.get().load(type);
        }//from  w  ww.  j a  v a2 s  .  c  o  m
    }
}

From source file:org.broadinstitute.gatk.tools.walkers.help.WalkerDocumentationHandler.java

/**
 * Utility function that determines the reference window size for an instance of class c.
 *
 * @param myClass the class to query for the settings
 * @param refWindow an empty HashMap in which to collect the info
 * @return a HashMap of the window start and stop, otherwise an empty HashMap
 *//*w  ww  . j av  a  2 s  .c  o  m*/
private HashMap<String, Object> getRefWindow(Class myClass, HashMap<String, Object> refWindow) {
    //
    // Retrieve annotation
    if (myClass.isAnnotationPresent(Reference.class)) {
        final Annotation thisAnnotation = myClass.getAnnotation(Reference.class);
        if (thisAnnotation instanceof Reference) {
            final Reference refAnnotation = (Reference) thisAnnotation;
            refWindow.put("start", refAnnotation.window().start());
            refWindow.put("stop", refAnnotation.window().stop());
        }
    }
    return refWindow;
}

From source file:org.slc.sli.dashboard.manager.component.impl.CustomizationAssemblyFactoryImpl.java

private void populateEntityReferenceToManagerMethodMap() {
    Map<String, InvokableSet> entityReferenceToManagerMethodMap = new HashMap<String, InvokableSet>();

    boolean foundInterface = false;
    for (Object manager : applicationContext.getBeansWithAnnotation(EntityMappingManager.class).values()) {
        logger.info(manager.getClass().getCanonicalName());
        // managers can be advised (proxied) so original annotation are not seen on the method but
        // still available on the interface
        foundInterface = false;//from w w  w  .  j  a  v a2  s  .  c  o  m
        for (Class<?> type : manager.getClass().getInterfaces()) {
            if (type.isAnnotationPresent(EntityMappingManager.class)) {
                foundInterface = true;
                findEntityReferencesForType(entityReferenceToManagerMethodMap, type, manager);
            }
        }
        if (!foundInterface) {
            findEntityReferencesForType(entityReferenceToManagerMethodMap, manager.getClass(), manager);
        }
    }
    this.entityReferenceToManagerMethodMap = Collections.unmodifiableMap(entityReferenceToManagerMethodMap);
}

From source file:com.garyclayburg.attributes.AttributeService.java

Map<String, Class> findAnnotatedGroovyClasses(Class<? extends Annotation> desiredAnnotation) {
    Map<String, Class> foundClasses = new HashMap<>();

    Class[] loadedClasses = loadAllGroovyClasses(desiredAnnotation);

    for (Class loadedClass : loadedClasses) {
        log.debug("gse loaded class: " + loadedClass.getName());
        if (loadedClass.isAnnotationPresent(desiredAnnotation)) {
            foundClasses.put(loadedClass.getName(), loadedClass);
            log.info("gse detected {} annotation in groovy class: {}", desiredAnnotation, loadedClass);
        }//from   w  ww .  ja v a  2s  .co  m
    }
    return foundClasses;
}

From source file:org.broadinstitute.gatk.tools.walkers.help.WalkerDocumentationHandler.java

/**
 * Utility function that determines the ActiveRegion settings for an instance of class c.
 *
 * @param myClass the class to query for the settings
 * @param activeRegion an empty HashMap in which to collect the info
 * @return a HashMap of the ActiveRegion parameters, otherwise an empty HashMap
 *///from w w  w  . j a v a 2s .  co m
private HashMap<String, Object> getActiveRegion(Class myClass, HashMap<String, Object> activeRegion) {
    //
    // Retrieve annotation
    if (myClass.isAnnotationPresent(ActiveRegionTraversalParameters.class)) {
        final Annotation thisAnnotation = myClass.getAnnotation(ActiveRegionTraversalParameters.class);
        if (thisAnnotation instanceof ActiveRegionTraversalParameters) {
            final ActiveRegionTraversalParameters arAnnotation = (ActiveRegionTraversalParameters) thisAnnotation;
            activeRegion.put("ext", arAnnotation.extension());
            activeRegion.put("max", arAnnotation.maxRegion());
            activeRegion.put("min", arAnnotation.minRegion());
        }
    }
    return activeRegion;
}