Я пытаюсь создать пользовательскую аннотацию для форматирования номера телефона.
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ContactNumberFormate {
}
Реализация BeanPostProcessor:
@Component
public class PhoneFormatter implements BeanPostProcessor {
private Object beanIs;
private PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
@Override
public Object postProcessBeforeInitialization(Object bean, String s) throws BeansException {
if (bean.getClass().isAnnotationPresent(ContactNumberFormate.class)) {
beanIs = bean.getClass();
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String s) throws BeansException {
String phone = "";
String locale = "";
try {
phone = (String) beanIs.getClass().getDeclaredField("phone").get(bean);
locale = (String) beanIs.getClass().getDeclaredField("locale").get(bean);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
//Other code//
return formattedPhoneNumber;
}
}
Но для "beanIs = bean.getClass ()" Я ловлю NullPointedException
Полный журнал:
java.lang.NullPointerException
at ru.spb.dreamwhite.util.phoneUtil.PhoneFormatter.postProcessAfterInitialization(PhoneFormatter.java:33)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:431)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1807)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595)