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:com.netflix.paas.config.base.ConfigurationProxyUtils.java

static <T> Map<String, Supplier<?>> getMethodSuppliers(Class<T> configClass,
        DynamicPropertyFactory propertyFactory, AbstractConfiguration configuration) {
    final Map<String, Supplier<?>> properties = Maps.newHashMap();

    for (Method method : configClass.getMethods()) {
        Configuration c = method.getAnnotation(Configuration.class);
        if (c == null)
            continue;
        String defaultValue = null;
        DefaultValue dv = method.getAnnotation(DefaultValue.class);
        if (dv != null)
            defaultValue = dv.value();/*from  w  w w  .j a  v a  2  s .  co  m*/

        String name = getPropertyName(method, c);

        if (method.getReturnType().isAssignableFrom(Supplier.class)) {
            Type returnType = method.getGenericReturnType();

            if (returnType instanceof ParameterizedType) {
                ParameterizedType type = (ParameterizedType) returnType;
                Class<?> actualType = (Class<?>) type.getActualTypeArguments()[0];

                properties.put(method.getName(),
                        method.getAnnotation(Dynamic.class) != null
                                ? Suppliers.ofInstance(
                                        getDynamicSupplier(actualType, name, defaultValue, propertyFactory))
                                : Suppliers.ofInstance(
                                        getStaticSupplier(actualType, name, defaultValue, configuration)));
            } else {
                throw new RuntimeException("We'll get to this later");
            }
        } else {
            properties.put(method.getName(),
                    method.getAnnotation(Dynamic.class) != null
                            ? getDynamicSupplier(method.getReturnType(), name, defaultValue, propertyFactory)
                            : getStaticSupplier(method.getReturnType(), name, defaultValue, configuration));
        }
    }

    return properties;
}

From source file:com.espertech.esper.metrics.jmx.CommonJMXUtil.java

private static ModelMBeanOperationInfo[] extractOperationInfo(Object object) {
    ArrayList<ModelMBeanOperationInfo> infos = new ArrayList<ModelMBeanOperationInfo>();
    for (Method m : object.getClass().getMethods()) {
        JmxOperation jmxOperation = m.getAnnotation(JmxOperation.class);
        JmxGetter jmxGetter = m.getAnnotation(JmxGetter.class);
        JmxSetter jmxSetter = m.getAnnotation(JmxSetter.class);
        if (jmxOperation != null || jmxGetter != null || jmxSetter != null) {
            String description = "";
            int visibility = 1;
            int impact = MBeanOperationInfo.UNKNOWN;
            if (jmxOperation != null) {
                description = jmxOperation.description();
                impact = jmxOperation.impact();
            } else if (jmxGetter != null) {
                description = jmxGetter.description();
                impact = MBeanOperationInfo.INFO;
                visibility = 4;/* w w w.j a  v  a 2s. co  m*/
            } else if (jmxSetter != null) {
                description = jmxSetter.description();
                impact = MBeanOperationInfo.ACTION;
                visibility = 4;
            }
            ModelMBeanOperationInfo info = new ModelMBeanOperationInfo(m.getName(), description,
                    extractParameterInfo(m), m.getReturnType().getName(), impact);
            info.getDescriptor().setField("visibility", Integer.toString(visibility));
            infos.add(info);
        }
    }

    return infos.toArray(new ModelMBeanOperationInfo[infos.size()]);
}

From source file:com.espertech.esper.metrics.jmx.CommonJMXUtil.java

private static ModelMBeanAttributeInfo[] extractAttributeInfo(Object object) {
    Map<String, Method> getters = new HashMap<String, Method>();
    Map<String, Method> setters = new HashMap<String, Method>();
    Map<String, String> descriptions = new HashMap<String, String>();
    for (Method m : object.getClass().getMethods()) {
        JmxGetter getter = m.getAnnotation(JmxGetter.class);
        if (getter != null) {
            getters.put(getter.name(), m);
            descriptions.put(getter.name(), getter.description());
        }// w w w. ja v  a  2s .c  o  m
        JmxSetter setter = m.getAnnotation(JmxSetter.class);
        if (setter != null) {
            setters.put(setter.name(), m);
            descriptions.put(setter.name(), setter.description());
        }
    }

    Set<String> attributes = new HashSet<String>(getters.keySet());
    attributes.addAll(setters.keySet());
    List<ModelMBeanAttributeInfo> infos = new ArrayList<ModelMBeanAttributeInfo>();
    for (String name : attributes) {
        try {
            Method getter = getters.get(name);
            Method setter = setters.get(name);
            ModelMBeanAttributeInfo info = new ModelMBeanAttributeInfo(name, descriptions.get(name), getter,
                    setter);
            Descriptor descriptor = info.getDescriptor();
            if (getter != null)
                descriptor.setField("getMethod", getter.getName());
            if (setter != null)
                descriptor.setField("setMethod", setter.getName());
            info.setDescriptor(descriptor);
            infos.add(info);
        } catch (IntrospectionException e) {
            throw new RuntimeException(e);
        }
    }

    return infos.toArray(new ModelMBeanAttributeInfo[infos.size()]);
}

From source file:com.shigengyu.hyperion.server.RestServer.java

private static Collection<ControllerMethod> extractControllerMethods(
        Iterable<ResourceProvider> resourceProviders) {

    TreeMap<String, ControllerMethod> controllerMethods = Maps.newTreeMap(new Comparator<String>() {

        @Override//w w w  . j a va 2 s.c  o m
        public int compare(String first, String second) {
            return first.compareTo(second);
        }
    });

    for (ResourceProvider resourceProvider : resourceProviders) {
        String controllerPath = resourceProvider.getResourceClass().getAnnotation(Path.class).value();
        for (Method method : resourceProvider.getResourceClass().getMethods()) {
            if (!method.isAnnotationPresent(Path.class)) {
                continue;
            }

            String methodPath = method.getAnnotation(Path.class).value();

            String httpMethod = null;
            if (method.isAnnotationPresent(GET.class)) {
                httpMethod = HttpMethods.GET;
            } else if (method.isAnnotationPresent(POST.class)) {
                httpMethod = HttpMethods.POST;
            }

            ControllerMethod controllerMethod = new ControllerMethod(httpMethod, controllerPath, methodPath);
            controllerMethods.put(controllerMethod.getUrl(), controllerMethod);
        }
    }

    return controllerMethods.values();
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.ActiveXObject.java

/**
 * Gets the first method found of the class with the given name
 * and the correct annotation/*from w  ww .j  av  a 2 s .com*/
 * @param clazz the class to search on
 * @param name the name of the searched method
 * @param annotationClass the class of the annotation required
 * @return {@code null} if not found
 */
static Method getMethod(final Class<? extends SimpleScriptable> clazz, final String name,
        final Class<? extends Annotation> annotationClass) {
    if (name == null) {
        return null;
    }

    Method foundMethod = null;
    int foundByNameOnlyCount = 0;
    for (final Method method : clazz.getMethods()) {
        if (method.getName().equals(name)) {
            if (null != method.getAnnotation(annotationClass)) {
                return method;
            }
            foundByNameOnlyCount++;
            foundMethod = method;
        }
    }
    if (foundByNameOnlyCount > 1) {
        throw new IllegalArgumentException(
                "Found " + foundByNameOnlyCount + " methods for name '" + name + "' in class '" + clazz + "'.");
    }
    return foundMethod;
}

From source file:org.querybyexample.jpa.JpaUtil.java

private static boolean isManuallyAssigned(Method method) {
    if (method.getAnnotation(Id.class) != null) {
        return method.getAnnotation(GeneratedValue.class) == null;
    } else if (method.getAnnotation(EmbeddedId.class) != null) {
        return true;
    } else {//from ww w.  ja v a2s.co m
        return false;
    }
}

From source file:com.sun.socialsite.util.DebugBeanJsonConverter.java

private static String getPropertyName(Method method) {
    JsonProperty property = method.getAnnotation(JsonProperty.class);
    if (property == null) {
        String name = method.getName();
        if (name.startsWith("set")) {
            return name.substring(3, 4).toLowerCase() + name.substring(4);
        }//from   ww  w  .  j a va 2s .c om
        return null;
    } else {
        return property.value();
    }
}

From source file:net.sf.companymanager.qbe.JpaUtil.java

private static boolean isPrimaryKey(final Method method) {
    return isPublic(method.getModifiers())
            && (method.getAnnotation(Id.class) != null || method.getAnnotation(EmbeddedId.class) != null);
}

From source file:com.manydesigns.portofino.logic.SecurityLogic.java

public static RequiresPermissions getRequiresPermissionsAnnotation(Method handler, Class<?> theClass) {
    RequiresPermissions requiresPermissions = handler.getAnnotation(RequiresPermissions.class);
    if (requiresPermissions != null) {
        logger.debug("Action method requires specific permissions: {}", handler);
    } else {/*from  w w  w.  j  av  a  2 s .co m*/
        requiresPermissions = theClass.getAnnotation(RequiresPermissions.class);
        if (requiresPermissions != null) {
            logger.debug("Action class requires specific permissions: {}", theClass);
        }
    }
    return requiresPermissions;
}

From source file:org.querybyexample.jpa.JpaUtil.java

public static <T extends Identifiable<?>> String compositePkPropertyName(T entity) {
    for (Method m : entity.getClass().getMethods()) {
        if (m.getAnnotation(EmbeddedId.class) != null) {
            return BeanUtils.findPropertyForMethod(m).getName();
        }//from  w w  w  . j a v  a  2s . c  o  m
    }
    for (Field f : entity.getClass().getFields()) {
        if (f.getAnnotation(EmbeddedId.class) != null) {
            return f.getName();
        }
    }
    return null;
}