Skip to content

Conversation

Stanislav-Zabramnyi
Copy link
Contributor

…cise

Copy link
Contributor

@tboychuk tboychuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Stanislav-Zabramnyi thanks for the impl. Now we need to talk. I'd like to know more about those interceptors :)

* if parameter is marked with {@link Trimmed} annotation.
* <p>
*
* <p>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Stanislav-Zabramnyi such changes should be a part of main

beansToBeProxied.put(beanName, bean);
beanInterceptors.put(beanName, new MethodInterceptor[]{provideTrimmingInterceptor()});
}
return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Stanislav-Zabramnyi you can go with super.postProcessBeforeInitialization(bean, beanName);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, this is just a default IDEA overriding

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually no, I cant. Idea throws a compilation error if I try to change it to super.postProcessBeforeInitialization(bean, beanName);`

//todo: Implement TrimmedAnnotationBeanPostProcessor according to javadoc
public class TrimmedAnnotationBeanPostProcessor implements BeanPostProcessor {

private final Map<String, Object> beansToBeProxied = new HashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Stanislav-Zabramnyi you don't need to store a bean, only it's class. So it should be
private final Map<String, Class<?>> classesToBeProxied = new HashMap<>();

private Object[] processParameters(Method method, Object[] args) {
Parameter[] parameters = method.getParameters();
for (int i = 0; i < parameters.length; i++) {
if (parameters[i].isAnnotationPresent(Trimmed.class)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Stanislav-Zabramnyi so are you sure that this Method is not a method of a proxy class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A method of a proxy class has type MethodProxy but not a Method.

var method = Arrays.stream(bean.getClass().getDeclaredMethods()).filter(m -> m.getName().equals("getCallbacks"))
.findFirst();
if (method.isPresent()) {
return (MethodInterceptor[]) method.get().invoke(bean);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Stanislav-Zabramnyi it looks scary! 😳

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree. But It`s reflection))

I will think how to do it in more elegant way.

}

private void setInterceptorsIfNeeded(Enhancer enhancer, Object bean, String beanName) {
MethodInterceptor[] methodInterceptors = beanInterceptors.get(beanName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Stanislav-Zabramnyi to me this second map beanInterceptors is redundant. The only thing it's storing is an interceptor created by provideTrimmingInterceptor. So we should get rid of that map, and just call provideTrimmingInterceptor when we need it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But what if we have another interceptors created by another BPPs? if we do just enhancer.setCallabck(interceptor), wouldn`t we loose 'old' interceptors ?

return args;
}

private Object trimMarkedString(Object arg) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Stanislav-Zabramnyi I would rather say

        if (arg != null && arg instanceof String string) {
            return string.trim();
        }
        return arg;

Copy link
Contributor Author

@Stanislav-Zabramnyi Stanislav-Zabramnyi Sep 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

condition (arg! = null) has already covered by (arg instanceof String), if arg is null , (arg instanceof String) will just return false

@Stanislav-Zabramnyi Stanislav-Zabramnyi force-pushed the GP-160-create-a-completed-solution-completed branch 2 times, most recently from f0e310d to dc61ae7 Compare September 30, 2022 14:04
@Stanislav-Zabramnyi Stanislav-Zabramnyi force-pushed the GP-160-create-a-completed-solution-completed branch from dc61ae7 to 115395f Compare September 30, 2022 14:06
Copy link
Contributor

@tboychuk tboychuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍

@Stanislav-Zabramnyi Stanislav-Zabramnyi merged commit 58d8fde into completed Oct 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants