mardi 30 avril 2019

Getting Created Instance of A Class With Reflection

I am trying to use singleton objects and trying to set field during application load. But when i try to set field, i am getting class cast exception. Otherwise i am trying to cast but this time i'am using newInstance and i am loosing old instance.

My goal is that creating beans like spring without a constructor. And adding proxy to beans during application load.

--Not: I don't wanna use constructor. Just creating beans like spring.

I tried different solutions

Trying to class

field.set(classObject, object); --> throw Lang.class can not cast my.package.classObject

Trying to create new intsance

field.set(classObject.cast(classObject.newInstance), object); --> creating new intsance and i am loosing singleton programming approach.

And example code

//here i am searching all classes and adding bean and their called classes to list.
static void loadApp() {
    try {
        List<Class> classList = PackageScanner.getClasses(ApplicationProperties.getProperty("app.package"));
        classList.forEach(item -> {
            Annotation[] t = item.getAnnotationsByType(ServiceBean.class);
            if (ArrayUtils.isNotEmpty(t)) {
                ServiceBean bean = (ServiceBean) t[0];
                if (StringUtils.isEmpty(bean.value())) {
                    BeanUtil.addBean(item.getSimpleName(), item);
                } else {
                    BeanUtil.addBean(bean.value(), item);
                }
            }
            for (Field field : item.getDeclaredFields()) {
                Annotation[] a = field.getAnnotationsByType(Bean.class);
                if (ArrayUtils.isNotEmpty(a)) {
                    try {
                        BeanUtil.addField(field.getName(), new AnnotatedFieldModel(item, field));
                    } catch (Exception e) {
                        LOGGER.error("Creatiton error ", e);
                    }
                }
            }
            item.getFields();
        });
    } catch (Exception e) {
        LOGGER.error("Package Reading problem ", e);
    }
}

//I am getting classes from list. I don't have object bcs i didnt create any object yet.
    static void addPrxoy() {
    try {
        BeanUtil.getFields().keySet().forEach(item -> {
            AnnotatedFieldModel annotatedFieldModel = BeanUtil.getField(item);
            Class clazz = BeanUtil.getBean(item);
            Object object = Proxy.newProxyInstance(clazz.getClassLoader(),
                    new Class[]{annotatedFieldModel.getField().getType()},
                    new DynamicInvocationHandler());
            annotatedFieldModel.getField().setAccessible(true);
            try {
                annotatedFieldModel.getField().set(annotatedFieldModel.getClazz(), object);
            } catch (Exception e) {
                LOGGER.error("Bean creation error", e);
            }
        });
    } catch (Exception e) {
        LOGGER.error("Package Reading problem ", e);
    }
}





Aucun commentaire:

Enregistrer un commentaire