Thursday 11 June 2015

What is the Real Time Use Of Reflection in Java

Java: What scenarios call for the use of reflection?  

Perfect Explanation from stackoverflow:-
http://stackoverflow.com/questions/9222621/java-what-scenarios-call-for-the-use-of-reflection?rq=1

http://stackoverflow.com/questions/25906480/how-does-reflection-in-java-affects-the-applications-performance-and-what-are-t
----------------------------------------------------------------------------------------------------------------

Real time Example Based on application..

here we are sending let consider 10 parameter to the action layer. now we need to get all 10 parmeter and set to any VO. the best solution is here is reflection see below.

Field[] fields=configureFieldsVO.getClass().getDeclaredFields();
        String param = null;
        String[] value = null;
       
        if (fields != null &&CommonConstants.SHOW_DIV.equalsIgnoreCase(applyVTO.getBrandingInfoVO().getPrefellFlag())) {
            notification.notify(new NotifyVO(NotificationConstants.INFO,"### Prefill Flag is enabled",this,Thread.currentThread().getStackTrace()[1].getMethodName() +
                                     NotificationConstants.LINE +Thread.currentThread().getStackTrace()[1].getLineNumber()));

            for (int i = 0; i < fields.length; i++) {
                param = fields[i].getName();
                if (param != null) {
                    try {
                        if (requestMap.get(param) != null && prefillData.contains(param)) {
                            value = (String[])requestMap.get(param);
                            setPrefillValue(applyVTO, param, value[0]);
                        }
                    }catch (Exception e) {
                        notification.notify(new NotifyVO(NotificationConstants.ERROR, NotificationConstants.EXCEPTION_ASSIGNING_VALUES,this,
                                                Thread.currentThread().getStackTrace()[1].getMethodName()+NotificationConstants.LINE+Thread.currentThread().getStackTrace()[1].getLineNumber()));
                     
                    }
                }
            }
            notification.notify(new NotifyVO(NotificationConstants.INFO, NotificationConstants.CONSUMER_DATA_RECEIVED,this,
                                    Thread.currentThread().getStackTrace()[1].getMethodName()+NotificationConstants.LINE+Thread.currentThread().getStackTrace()[1].getLineNumber()));          
         
        }
---------------------------------
   private void setPrefillValue(ApplyVTO applyVTO, String param,
                                 String reqVal) throws ApplicationException {
        Field field = null;
        try {
            field =
                    applyVTO.getApplicantVO().getClass().getDeclaredField(param);
            if (field != null) {
                field.setAccessible(true);
                if (reqVal != null) {
                    field.set(applyVTO.getApplicantVO(), reqVal.trim());
                }
            }
        } catch (NoSuchFieldException e) {
            notification.notify(new NotifyVO(NotificationConstants.DEBUG, NotificationConstants.FIELD_NOT_AVAILABLE,this,
                                    Thread.currentThread().getStackTrace()[1].getMethodName()+NotificationConstants.LINE+Thread.currentThread().getStackTrace()[1].getLineNumber()));
        } catch (IllegalAccessException e) {
            notification.notify(new NotifyVO(NotificationConstants.DEBUG, NotificationConstants.FIELD_HAS_NO_PUBLIC_ACCESS,this,
                                    Thread.currentThread().getStackTrace()[1].getMethodName()+NotificationConstants.LINE+Thread.currentThread().getStackTrace()[1].getLineNumber()));
        }
    }

No comments:

Post a Comment