net.craftforge.essential.controller.utils
Class AnnotationUtils

java.lang.Object
  extended by net.craftforge.essential.controller.utils.AnnotationUtils

public class AnnotationUtils
extends Object

Utilities for annotation actions.

Since:
17.02.2011
Author:
Christian Bick

Constructor Summary
AnnotationUtils()
           
 
Method Summary
static List<Field> getAnnotatedFieldsFromClass(Class<?> clazz)
          Gets a list of all fields with an @Param, @Body, @Header or @Property annotation, looking them up on field level and on method level of getters and setters.
static String getBestMatchingMediaType(Object supplier, Class<? extends Annotation> annotationType, String mediaTypes)
          Gets the media type of a consumer or producer being annotated with the @Produces or @Consumes annotation which value matches best the specified media types.
static Method getBestMatchingMethod(Object supplier, Class<? extends Annotation> annotationType, String mediaTypes)
          Gets the method of a consumer or producer being annotated with the @Produces or @Consumes annotation which value matches best the specified media types.
static List<Field> getBodyFieldsFromClass(Class<?> clazz)
          Gets a list of all fields with an @Body annotation, looking them up on field level and on method level of getters and setters.
static Class<?> getConsumerFromMethodOrClass(Method method)
          Gets the @Consumer annotation value of a method, looking it up on method level first, then class level.
static Method getConsumerMethod(Object consumer, String mediaTypes)
          Gets the method of a consumer being annotated with the @Consumes annotation which value matches best the media types.
static String[] getDefaultValuesFromProperty(Field field)
          Gets the @DefaultValue annotation value(s) of a field, looking it up on field level and on method level of getters and setters.
static List<Field> getHeaderFieldsFromClass(Class<?> clazz)
          Gets a list of all fields with an @Header annotation, looking them up on field level and on method level of getters and setters.
static String getHeaderFromProperty(Field field)
          Gets the @Header annotations value of a property, looking it up on field level and on method level of getters and setters.
static String getHttpMethod(Method method)
          Gets the REST annotation (@Get, @Post, etc.) of a method, looking it up on method level.
static List<Field> getParamFieldsFromClass(Class<?> clazz)
          Gets a list of all fields with an @Param annotation, looking them up on field level and on method level of getters and setters.
static String getParamFromProperty(Field field)
          Gets the @Param annotations value of a property, looking it up on field level and on method level of getters and setters.
static String getPathFromClass(Class<?> clazz)
          Gets the @Path annotation value of a class, looking it up on type level.
static String getPathFromMethod(Method method)
          Gets the @Path annotation value of a method, looking it up on method level.
static Class<?> getProducerFromMethodOrClass(Method method)
          Gets the @Producer annotation value of a method, looking it up on method level first, then class level.
static Method getProducerMethod(Object producer, String mediaTypes)
          Gets the method of a producer being annotated with the @Produces annotation which value matches best the specified media types.
static List<Field> getPropertyFieldsFromClass(Class<?> clazz)
          Gets a list of all fields with an @Property annotation, looking them up on field level and on method level of getters and setters.
static String getPropertyFromProperty(Field field)
          Gets the @Property annotation value of a property, looking it up on field level and on method level of getters and setters.
static List<Method> getResourceMethodsFromClass(Class<?> clazz)
          Gets a list of all methods with a REST annotation (@Get, @Post, etc.)
static List<String> getSupportedConsumerMediaTypes(Class<?> clazz)
          Gets the supported consumer media types from a consumer class.
static List<String> getSupportedProducerMediaTypes(Class<?> clazz)
          Gets the supported producer media types from a producer class.
static Class[][] getValidConsumerParameterTypes()
          Gets all valid consumer parameter type combinations.
static Class[][] getValidProducerParameterTypes()
          Gets all valid producer parameter type combinations.
static boolean isBodyOnProperty(Field field)
          Determines whether the @Body annotation is present on a property, looking it up on field level and on method level of getters and setters.
static boolean isPublic(Method method)
          Checks if the @Public annotation is present, looking it up on method level first, then class level.
static boolean isStreamingSupported(Consumer consumer, String mediaType)
          Checks if for the given media type streaming is supported by the specified consumer.
static boolean isStreamingSupported(Producer producer, String mediaType)
          Checks if for the given media type streaming is supported by the specified producer.
static boolean isValidConsumerMethod(Method method)
          Checks if a consumer method has a valid signature.
static boolean isValidMethod(Method method, Class[][] validParameterTypes)
          Checks if a method has a method signature by comparing its parameter types against a set of valid parameter type combinations.
static boolean isValidProducerMethod(Method method)
          Checks if a producer method has a valid signature.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AnnotationUtils

public AnnotationUtils()
Method Detail

getPathFromClass

public static String getPathFromClass(Class<?> clazz)
Gets the @Path annotation value of a class, looking it up on type level. Uses the complete class hierarchy, including interfaces.

Parameters:
clazz - The class
Returns:
The @Path annotation value, null if not found

getPathFromMethod

public static String getPathFromMethod(Method method)
Gets the @Path annotation value of a method, looking it up on method level. Uses the complete class hierarchy, including interfaces.

Parameters:
method - The method
Returns:
The @Path annotation value, null if not found

getParamFromProperty

public static String getParamFromProperty(Field field)
Gets the @Param annotations value of a property, looking it up on field level and on method level of getters and setters. Uses the complete class hierarchy, including interfaces.

Parameters:
field - The field
Returns:
The @Param annotation value, null if not found

getHeaderFromProperty

public static String getHeaderFromProperty(Field field)
Gets the @Header annotations value of a property, looking it up on field level and on method level of getters and setters. Uses the complete class hierarchy, including interfaces.

Parameters:
field - The field
Returns:
The @Header annotation value, null if not found

getPropertyFromProperty

public static String getPropertyFromProperty(Field field)
Gets the @Property annotation value of a property, looking it up on field level and on method level of getters and setters. Uses the complete class hierarchy, including interfaces.

Parameters:
field - The field
Returns:
The @Property annotation value, null if not found

isBodyOnProperty

public static boolean isBodyOnProperty(Field field)
Determines whether the @Body annotation is present on a property, looking it up on field level and on method level of getters and setters. Uses the complete class hierarchy, including interfaces.

Parameters:
field - The field
Returns:
The @Header annotation value, null if not found

getDefaultValuesFromProperty

public static String[] getDefaultValuesFromProperty(Field field)
Gets the @DefaultValue annotation value(s) of a field, looking it up on field level and on method level of getters and setters. Uses the complete class hierarchy, including interfaces.

Parameters:
field - The field
Returns:
The @DefaultValue annotation value, null if not found

getProducerMethod

public static Method getProducerMethod(Object producer,
                                       String mediaTypes)
                                throws ControllerException
Gets the method of a producer being annotated with the @Produces annotation which value matches best the specified media types.

Parameters:
producer - The producer method
mediaTypes - The media types
Returns:
The method matching best the annotation requirements
Throws:
ControllerException - Failed to find a producer method supplying one of the accepted media types

getConsumerMethod

public static Method getConsumerMethod(Object consumer,
                                       String mediaTypes)
                                throws ControllerException
Gets the method of a consumer being annotated with the @Consumes annotation which value matches best the media types.

Parameters:
consumer - The consumer
mediaTypes - The media types
Returns:
The method matching best the annotation requirements
Throws:
ControllerException - Failed to find a consumer method supplying one of the accepted media types

getBestMatchingMethod

public static Method getBestMatchingMethod(Object supplier,
                                           Class<? extends Annotation> annotationType,
                                           String mediaTypes)
                                    throws ControllerException
Gets the method of a consumer or producer being annotated with the @Produces or @Consumes annotation which value matches best the specified media types. The annotation type is used to distinct between a consumer and a producer.

Parameters:
supplier - The supplier
annotationType - The annotation type.
mediaTypes - The media types
Returns:
The method matching best the annotation requirements
Throws:
ControllerException - Failed to find a producer method supplying one of the accepted media types

getBestMatchingMediaType

public static String getBestMatchingMediaType(Object supplier,
                                              Class<? extends Annotation> annotationType,
                                              String mediaTypes)
                                       throws ControllerException
Gets the media type of a consumer or producer being annotated with the @Produces or @Consumes annotation which value matches best the specified media types. The annotation type is used to distinct between a consumer and a producer.

Parameters:
supplier - The supplier
annotationType - The annotation type.
mediaTypes - The media types
Returns:
The media type matching best the annotation requirements
Throws:
ControllerException - Failed to find a producer method supplying one of the accepted media types

getProducerFromMethodOrClass

public static Class<?> getProducerFromMethodOrClass(Method method)
Gets the @Producer annotation value of a method, looking it up on method level first, then class level. Uses the complete class hierarchy, including interfaces.

Parameters:
method - The method
Returns:
The @Producer annotation value or null if not found

getConsumerFromMethodOrClass

public static Class<?> getConsumerFromMethodOrClass(Method method)
Gets the @Consumer annotation value of a method, looking it up on method level first, then class level. Uses the complete class hierarchy, including interfaces.

Parameters:
method - The method
Returns:
The @Consumer annotation value or null if not found

isPublic

public static boolean isPublic(Method method)
Checks if the @Public annotation is present, looking it up on method level first, then class level. Uses the complete class hierarchy, including interfaces.

Parameters:
method - The method
Returns:
Whether the @Public annotation is present or not

getParamFieldsFromClass

public static List<Field> getParamFieldsFromClass(Class<?> clazz)
Gets a list of all fields with an @Param annotation, looking them up on field level and on method level of getters and setters. Uses the complete class hierarchy, including interfaces.

Parameters:
clazz - The class
Returns:
The list of fields

getHeaderFieldsFromClass

public static List<Field> getHeaderFieldsFromClass(Class<?> clazz)
Gets a list of all fields with an @Header annotation, looking them up on field level and on method level of getters and setters. Uses the complete class hierarchy, including interfaces.

Parameters:
clazz - The class
Returns:
The list of fields

getBodyFieldsFromClass

public static List<Field> getBodyFieldsFromClass(Class<?> clazz)
Gets a list of all fields with an @Body annotation, looking them up on field level and on method level of getters and setters. Uses the complete class hierarchy, including interfaces.

Parameters:
clazz - The class
Returns:
The list of fields

getPropertyFieldsFromClass

public static List<Field> getPropertyFieldsFromClass(Class<?> clazz)
Gets a list of all fields with an @Property annotation, looking them up on field level and on method level of getters and setters. Uses the complete class hierarchy, including interfaces.

Parameters:
clazz - The class
Returns:
The list of fields

getAnnotatedFieldsFromClass

public static List<Field> getAnnotatedFieldsFromClass(Class<?> clazz)
Gets a list of all fields with an @Param, @Body, @Header or @Property annotation, looking them up on field level and on method level of getters and setters. Uses the complete class hierarchy, including interfaces.

Parameters:
clazz - The class
Returns:
The list of fields

getResourceMethodsFromClass

public static List<Method> getResourceMethodsFromClass(Class<?> clazz)
Gets a list of all methods with a REST annotation (@Get, @Post, etc.) in the class, looking it up on method level. Uses the complete class hierarchy, including interfaces.

Parameters:
clazz - The class
Returns:
The list of resource methods

getSupportedProducerMediaTypes

public static List<String> getSupportedProducerMediaTypes(Class<?> clazz)
Gets the supported producer media types from a producer class.

Parameters:
clazz - The producer class
Returns:
The supported media types

getSupportedConsumerMediaTypes

public static List<String> getSupportedConsumerMediaTypes(Class<?> clazz)
Gets the supported consumer media types from a consumer class.

Parameters:
clazz - The consumer class
Returns:
The supported media types

getValidConsumerParameterTypes

public static Class[][] getValidConsumerParameterTypes()
Gets all valid consumer parameter type combinations.

Returns:
The Array of valid consumer parameter type combinations

getValidProducerParameterTypes

public static Class[][] getValidProducerParameterTypes()
Gets all valid producer parameter type combinations.

Returns:
The Array of valid producer parameter type combinations

isValidProducerMethod

public static boolean isValidProducerMethod(Method method)
Checks if a producer method has a valid signature.

Parameters:
method - The producer method
Returns:
Whether the producer method is valid or nor

isValidConsumerMethod

public static boolean isValidConsumerMethod(Method method)
Checks if a consumer method has a valid signature.

Parameters:
method - The consumer method
Returns:
Whether the consumer method is valid or not

isValidMethod

public static boolean isValidMethod(Method method,
                                    Class[][] validParameterTypes)
Checks if a method has a method signature by comparing its parameter types against a set of valid parameter type combinations.

Parameters:
method - The method
validParameterTypes - The valid parameter type combinations
Returns:
Whether the method has a valid signature or not

isStreamingSupported

public static boolean isStreamingSupported(Consumer consumer,
                                           String mediaType)
                                    throws ControllerException
Checks if for the given media type streaming is supported by the specified consumer.

Parameters:
consumer - The consumer
mediaType - The media type
Returns:
Whether streaming is supported or not
Throws:
ControllerException - if the specified consumer does not supply the given media type

isStreamingSupported

public static boolean isStreamingSupported(Producer producer,
                                           String mediaType)
                                    throws ControllerException
Checks if for the given media type streaming is supported by the specified producer.

Parameters:
producer - The producer
mediaType - The media type
Returns:
Whether streaming is supported or not
Throws:
ControllerException - if the specified producer does not supply the given media type

getHttpMethod

public static String getHttpMethod(Method method)
Gets the REST annotation (@Get, @Post, etc.) of a method, looking it up on method level. Uses the complete class hierarchy, including interfaces.

Parameters:
method - The method
Returns:
The REST annotation or null if none found


Copyright © 2011. All Rights Reserved.