-
-
Notifications
You must be signed in to change notification settings - Fork 94
GP - 160: create a completed solution for Enable-string-trimming exer… #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GP - 160: create a completed solution for Enable-string-trimming exer… #31
Conversation
There was a problem hiding this 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 :)
...ork/3-3-0-enable-string-trimming/src/main/java/com/bobocode/StringTrimmingConfiguration.java
Show resolved
Hide resolved
* if parameter is marked with {@link Trimmed} annotation. | ||
* <p> | ||
* | ||
* <p> |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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);
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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<>(); |
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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! 😳
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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;
There was a problem hiding this comment.
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
f0e310d
to
dc61ae7
Compare
dc61ae7
to
115395f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks 👍
…cise