Package org.castor.cpa.jpa.processors
Class ReflectionsHelper
- java.lang.Object
-
- org.castor.cpa.jpa.processors.ReflectionsHelper
-
public final class ReflectionsHelper extends java.lang.Object
Tool class that offers methods to retrieve information fromAnnotatedElement
s (Methods or Fields).- Version:
- 12.02.2009
- Author:
- Peter Schmidt
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.Class<?>
getCollectionType(java.lang.reflect.AnnotatedElement target, boolean jpaTypesOnly)
Get the (raw) type of collection from the annotations target and optional check for JPA 1.0 restrictions (only Collection, Map, List and Set are allowed).static java.lang.String
getFieldnameFromGetter(java.lang.reflect.Method getter)
Little helper to get a field (bean property) name from a getter method.static java.lang.reflect.Method
getSetterMethodFromGetter(java.lang.reflect.Method getter)
Return a setterMethod
for a given getterMethod
.static java.lang.Class<?>
getTargetEntityFromGenerics(java.lang.reflect.AnnotatedElement target)
Get the target entity information from the target (necessary if it was not specified by the annotation itself).static boolean
isGetter(java.lang.reflect.Method method)
Convenience method to check whether aMethod
is a getter method, i.e.
-
-
-
Method Detail
-
getCollectionType
public static java.lang.Class<?> getCollectionType(java.lang.reflect.AnnotatedElement target, boolean jpaTypesOnly) throws AnnotationTargetException
Get the (raw) type of collection from the annotations target and optional check for JPA 1.0 restrictions (only Collection, Map, List and Set are allowed).- Parameters:
target
- The ManyToMany annotations target.jpaTypesOnly
- If set to true only Collection types of JPA 1.0 are allowed. Others will lead to a thrown Exception.- Returns:
- The raw collection type.
- Throws:
AnnotationTargetException
- if the raw collection type can not be inferred from the type definition or if the type is not supported by JPA 1.0 and jpaTypesOnly was set to true.
-
getTargetEntityFromGenerics
public static java.lang.Class<?> getTargetEntityFromGenerics(java.lang.reflect.AnnotatedElement target) throws AnnotationTargetException
Get the target entity information from the target (necessary if it was not specified by the annotation itself).- Parameters:
target
- The *ToMany annotations target. This method will fail if target is not of TypeField
orMethod
!- Returns:
- the relations target entity inferred by the targets generic definition or null if no generic definition was found at all.
- Throws:
AnnotationTargetException
- if the generic definition is not sufficient
-
getFieldnameFromGetter
public static java.lang.String getFieldnameFromGetter(java.lang.reflect.Method getter)
Little helper to get a field (bean property) name from a getter method.
Example:getFoo() => "foo"
Example:isBar() => "bar"
- Parameters:
getter
- theMethod
to analyse.- Returns:
- the field name determined by the methods name or NULL if the method is not a getter (start with "get" or "is").
-
getSetterMethodFromGetter
public static java.lang.reflect.Method getSetterMethodFromGetter(java.lang.reflect.Method getter) throws java.lang.SecurityException, java.lang.NoSuchMethodException
Return a setterMethod
for a given getterMethod
.- Parameters:
getter
- The getterMethod
- Returns:
- The setter Method for the given getter Method.
- Throws:
java.lang.NoSuchMethodException
- If the setter method does not existjava.lang.SecurityException
- If the setter method is not accessible
-
isGetter
public static boolean isGetter(java.lang.reflect.Method method)
Convenience method to check whether aMethod
is a getter method, i.e. starts with "get" or "is".- Parameters:
method
- theMethod
to check.- Returns:
- true if the methods name starts with "get" or "is" (Java Beans convention).
-
-