private void setIdOnProcessDefinition(final SProcessDefinition sProcessDefinition, long id) throws SReflectException { ClassReflector.invokeSetter(sProcessDefinition, "setId", Long.class, id); }
/** * call a setter by reflection * support pointed notation like pojo.child.name * @param object * object on with to call the setter * @param fieldName * * @param parameterValue * @throws SReflectException */ public static void setField(Object object, String fieldName, Object parameterValue) throws SReflectException { String[] getters = fieldName.split("\\."); int i; for (i = 0; i < getters.length -1; i++) { object = invokeMethodByName(object, getGetterName(getters[i])); } invokeMethodByName(object, getSetterName(getters[i]), parameterValue); }
public static Object invokeMethod(final Object entity, final String methodName, final Class<?>[] parameterType, final Object[] parameterValue) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { final Method method = getMethod(entity.getClass(), methodName, parameterType); return method.invoke(entity, parameterValue); }
@SuppressWarnings("unchecked") @Override public Serializable getJsonChildEntity(final String entityClassName, final Long identifier, final String childFieldName, final String businessDataURIPattern) throws SBusinessDataNotFoundException, SBusinessDataRepositoryException { final Class<? extends Entity> entityClass = loadClass(entityClassName); final Object entity = businessDataRepository.findById(entityClass, identifier); Object childEntity; java.lang.reflect.Type getterReturnType; try { final String getterName = ClassReflector.getGetterName(childFieldName); childEntity = ClassReflector.invokeGetter(entity, getterName); getterReturnType = ClassReflector.getGetterReturnType(entityClass, getterName); } catch (final SReflectException e) { throw new SBusinessDataRepositoryException(e); } if (childEntity == null) { return JsonBusinessDataSerializer.EMPTY_OBJECT; } if (childEntity instanceof Entity) { final Entity unwrap = businessDataRepository.unwrap((Entity) childEntity); return jsonBusinessDataSerializer.serializeEntity(unwrap, businessDataURIPattern); } else if (childEntity instanceof List) { final Class<?> type = (Class<?>) ((ParameterizedType) getterReturnType).getActualTypeArguments()[0]; if (Entity.class.isAssignableFrom(type)) { return jsonBusinessDataSerializer.serializeEntities((List<Entity>) childEntity, businessDataURIPattern); } } return null; }
protected void setTenant(final PersistentObject entity) throws SPersistenceException { if (entity == null) { return; } // if this entity has no id, set it Long tenantId = null; try { tenantId = ClassReflector.invokeGetter(entity, "getTenantId"); } catch (final Exception e) { // this is a new object to save } if (tenantId == null || tenantId == -1 || tenantId == 0) { setTenantByClassReflector(entity, tenantId); } }
@Override public void updateDataInstance(final SDataInstance dataInstance, final EntityUpdateDescriptor descriptor) throws SDataInstanceException { try { final String key = getKey(dataInstance); for (final Map.Entry<String, Object> field : descriptor.getFields().entrySet()) { try { final String setterName = "set" + WordUtils.capitalize(field.getKey()); ClassReflector.invokeMethodByName(dataInstance, setterName, field.getValue()); } catch (final Exception e) { throw new SUpdateDataInstanceException("Problem while updating entity: " + dataInstance + " with id: " + dataInstance.getId() + " in TransientDataInstanceDataSource.", e); } } cacheService.store(TRANSIENT_DATA_CACHE_NAME, key, dataInstance); } catch (final SCacheException e) { throw new SDataInstanceException("Impossible to update transient data", e); } }
public static <T> T getObject(final Class<T> clazz, final String className) throws SReflectException { try { return getClass(clazz, className).newInstance(); } catch (final Exception e) { throw new SReflectException(e); } }
@Override public void onUpdate(ClassLoader newClassLoader) { ClassReflector.clearCache(); }
private static void putIfAbsent(final Class<?> clazz, final String methodName, final String key) { if (!methods.containsKey(key)) { synchronized (MUTEX) { // ensure that key was not put before between check and lock if (!methods.containsKey(key)) { final Method method = getFirstMethodWithName(clazz, methodName); methods.put(key, method); } } } }
private Type getRelationType(final Entity businessObject, final String methodName) throws SBusinessDataRepositoryException { final String fieldName = ClassReflector.getFieldName(methodName); Annotation[] annotations; try { annotations = businessObject.getClass().getDeclaredField(fieldName).getAnnotations(); } catch (final NoSuchFieldException e) { return null; } catch (final SecurityException e) { throw new SBusinessDataRepositoryException(e); } for (final Annotation annotation : annotations) { final Set<Class<? extends Annotation>> annotationKeySet = getAnnotationKeySet(); if (annotationKeySet.contains(annotation.annotationType())) { try { final Method cascade = annotation.getClass().getMethod("cascade"); final CascadeType[] cascadeTypes = (CascadeType[]) cascade.invoke(annotation); if (CascadeType.MERGE.equals(cascadeTypes[0])) { return Type.AGGREGATION; } if (CascadeType.ALL.equals(cascadeTypes[0])) { return Type.COMPOSITION; } } catch (final Exception e) { throw new SBusinessDataRepositoryException(e); } } } return null; }
@SuppressWarnings("unchecked") @Override public Serializable getJsonChildEntity(final String entityClassName, final Long identifier, final String childFieldName, final String businessDataURIPattern) throws SBusinessDataNotFoundException, SBusinessDataRepositoryException { final Class<? extends Entity> entityClass = loadClass(entityClassName); final Object entity = businessDataRepository.findById(entityClass, identifier); Object childEntity; java.lang.reflect.Type getterReturnType; try { final String getterName = ClassReflector.getGetterName(childFieldName); childEntity = ClassReflector.invokeGetter(entity, getterName); getterReturnType = ClassReflector.getGetterReturnType(entityClass, getterName); } catch (final SReflectException e) { throw new SBusinessDataRepositoryException(e); } if (childEntity == null) { return JsonBusinessDataSerializer.EMPTY_OBJECT; } if (childEntity instanceof Entity) { final Entity unwrap = businessDataRepository.unwrap((Entity) childEntity); return jsonBusinessDataSerializer.serializeEntity(unwrap, businessDataURIPattern); } else if (childEntity instanceof List) { final Class<?> type = (Class<?>) ((ParameterizedType) getterReturnType).getActualTypeArguments()[0]; if (Entity.class.isAssignableFrom(type)) { return jsonBusinessDataSerializer.serializeEntities((List<Entity>) childEntity, businessDataURIPattern); } } return null; }
protected void setTenant(final PersistentObject entity) throws SPersistenceException { if (entity == null) { return; } // if this entity has no id, set it Long tenantId = null; try { tenantId = ClassReflector.invokeGetter(entity, "getTenantId"); } catch (final Exception e) { // this is a new object to save } if (tenantId == null || tenantId == -1 || tenantId == 0) { setTenantByClassReflector(entity, tenantId); } }
@Override public void updateDataInstance(final SDataInstance dataInstance, final EntityUpdateDescriptor descriptor) throws SDataInstanceException { try { final String key = getKey(dataInstance); for (final Map.Entry<String, Object> field : descriptor.getFields().entrySet()) { try { final String setterName = "set" + WordUtils.capitalize(field.getKey()); ClassReflector.invokeMethodByName(dataInstance, setterName, field.getValue()); } catch (final Exception e) { throw new SUpdateDataInstanceException("Problem while updating entity: " + dataInstance + " with id: " + dataInstance.getId() + " in TransientDataInstanceDataSource.", e); } } cacheService.store(TRANSIENT_DATA_CACHE_NAME, key, dataInstance); } catch (final SCacheException e) { throw new SDataInstanceException("Impossible to update transient data", e); } }
public static <T> T getObject(final Class<T> clazz, final String className) throws SReflectException { try { return getClass(clazz, className).newInstance(); } catch (final Exception e) { throw new SReflectException(e); } }
@Override public void onUpdate(ClassLoader newClassLoader) { ClassReflector.clearCache(); }
private static void putIfAbsent(final Class<?> clazz, final String methodName, final String key) { if (!methods.containsKey(key)) { synchronized (MUTEX) { // ensure that key was not put before between check and lock if (!methods.containsKey(key)) { final Method method = getFirstMethodWithName(clazz, methodName); methods.put(key, method); } } } }
private Type getRelationType(final Entity businessObject, final String methodName) throws SBusinessDataRepositoryException { final String fieldName = ClassReflector.getFieldName(methodName); Annotation[] annotations; try { annotations = businessObject.getClass().getDeclaredField(fieldName).getAnnotations(); } catch (final NoSuchFieldException e) { return null; } catch (final SecurityException e) { throw new SBusinessDataRepositoryException(e); } for (final Annotation annotation : annotations) { final Set<Class<? extends Annotation>> annotationKeySet = getAnnotationKeySet(); if (annotationKeySet.contains(annotation.annotationType())) { try { final Method cascade = annotation.getClass().getMethod("cascade"); final CascadeType[] cascadeTypes = (CascadeType[]) cascade.invoke(annotation); if (CascadeType.MERGE.equals(cascadeTypes[0])) { return Type.AGGREGATION; } if (CascadeType.ALL.equals(cascadeTypes[0])) { return Type.COMPOSITION; } } catch (final Exception e) { throw new SBusinessDataRepositoryException(e); } } } return null; }
private void setIdOnProcessDefinition(final SProcessDefinition sProcessDefinition, long id) throws SReflectException { ClassReflector.invokeSetter(sProcessDefinition, "setId", Long.class, id); }
public static Object invokeMethod(final Object entity, final String methodName, final Class<?> parameterType, final Object parameterValue) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { final Method method = getMethod(entity.getClass(), methodName, parameterType); return method.invoke(entity, parameterValue); }
/** * call a setter by reflection * support pointed notation like pojo.child.name * @param object * object on with to call the setter * @param fieldName * * @param parameterValue * @throws SReflectException */ public static void setField(Object object, String fieldName, Object parameterValue) throws SReflectException { String[] getters = fieldName.split("\\."); int i; for (i = 0; i < getters.length -1; i++) { object = invokeMethodByName(object, getGetterName(getters[i])); } invokeMethodByName(object, getSetterName(getters[i]), parameterValue); }