/** * Whether the specified JavaBeans property exists on the given type or not. * * @param clazz the type of interest * @param property the JavaBeans property name * @param elementType the element type to check, must be either {@link ElementType#FIELD} or * {@link ElementType#METHOD}. * @return {@code true} if the specified property exists, {@code false} otherwise */ public static boolean propertyExists(Class<?> clazz, String property, ElementType elementType) { if ( ElementType.FIELD.equals( elementType ) ) { return getDeclaredField( clazz, property ) != null; } else { String capitalizedPropertyName = capitalize( property ); Method method = getMethod( clazz, PROPERTY_ACCESSOR_PREFIX_GET + capitalizedPropertyName ); if ( method != null && method.getReturnType() != void.class ) { return true; } method = getMethod( clazz, PROPERTY_ACCESSOR_PREFIX_IS + capitalizedPropertyName ); if ( method != null && method.getReturnType() == boolean.class ) { return true; } } return false; }
private static Tuple extractFromPojo(Object obj) throws Exception { Tuple tuple = new Tuple(); Map<String, Object> introspect = ReflectionHelper.introspect( obj ); introspect.forEach( tuple::put ); return tuple; } }
private static void setParams( String storedProcedureName, ProcedureQueryParameters queryParameters, Callable<?> callable ) { Map<String, Object> params = queryParameters.getNamedParameters(); for ( Map.Entry<String, Object> entry : params.entrySet() ) { try { ReflectionHelper.setField( callable, entry.getKey(), entry.getValue() ); } catch (Exception e) { throw log.cannotSetStoredProcedureParameter( storedProcedureName, entry.getKey(), entry.getValue(), e ); } } }
/** * Set value for given object field. * * @param object object to be updated * @param field field name * @param value field value * * @throws NoSuchMethodException if property writer is not available * @throws InvocationTargetException if property writer throws an exception * @throws IllegalAccessException if property writer is inaccessible */ public static void setField(Object object, String field, Object value) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Class<?> clazz = object.getClass(); Method m = clazz.getMethod( PROPERTY_ACCESSOR_PREFIX_SET + capitalize( field ), value.getClass() ); m.invoke( object, value ); }
public void configureProperty(String propertyName, ElementType elementType) { if ( elementType != ElementType.FIELD && elementType != ElementType.METHOD ) { throw log.getUnsupportedElementTypeException( elementType ); } if ( !ReflectionHelper.propertyExists( currentEntityType, propertyName, elementType ) ) { throw log.getPropertyDoesNotExistException( currentEntityType.getName(), propertyName, elementType ); } this.currentPropertyName = propertyName; }
private Map<PropertyKey, OptionsContainerBuilder> getPropertyOptions(final Class<?> entityClass) { final Map<PropertyKey, OptionsContainerBuilder> optionsByProperty = new HashMap<PropertyKey, OptionsContainerBuilder>(); for ( final Method method : entityClass.getMethods() ) { String propertyName = ReflectionHelper.getPropertyName( method ); if ( propertyName == null ) { continue; } final OptionsContainerBuilder optionsOfProperty = convertOptionAnnotations( method.getAnnotations() ); if ( optionsOfProperty != null ) { optionsByProperty.put( new PropertyKey( entityClass, propertyName ), optionsOfProperty ); } } for ( final Field field : entityClass.getDeclaredFields() ) { PropertyKey key = new PropertyKey( entityClass, field.getName() ); OptionsContainerBuilder optionsOfField = convertOptionAnnotations( field.getAnnotations() ); if ( optionsOfField != null ) { OptionsContainerBuilder optionsOfProperty = optionsByProperty.get( key ); if ( optionsOfProperty != null ) { optionsOfProperty.addAll( optionsOfField ); } else { optionsByProperty.put( key, optionsOfField ); } } } return optionsByProperty; }
private static void setParams( String storedProcedureName, ProcedureQueryParameters queryParameters, Callable<?> callable ) { Map<String, Object> params = queryParameters.getNamedParameters(); for ( Map.Entry<String, Object> entry : params.entrySet() ) { try { ReflectionHelper.setField( callable, entry.getKey(), entry.getValue() ); } catch (Exception e) { throw log.cannotSetStoredProcedureParameter( storedProcedureName, entry.getKey(), entry.getValue(), e ); } } }
public static int compareWithOrderChain(Object o1, Object o2, List<AssociationOrderBy> orders) { Map<String, Object> o1Map; Map<String, Object> o2Map; try { o1Map = ReflectionHelper.introspect( o1 ); o2Map = ReflectionHelper.introspect( o2 ); } catch (IntrospectionException | InvocationTargetException | IllegalAccessException e) { throw log.errorIntrospectingObject( e ); } for ( AssociationOrderBy order : orders ) { int compare = order.compare( o1Map, o2Map ); if ( compare != 0 ) { return compare; } // try next order in case of even } return 0; } }