Advertisement
ohumbel

Decisions according to generic types

Jan 15th, 2014
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.73 KB | None | 0 0
  1.   final IEntityView getEntityView(Object trigger) throws BusinessLogicException {
  2.     IEntityView result = _entityView;
  3.     if (trigger instanceof EntityManagerPlug2<?>) {
  4.       // check if currently set entity view matches the generic declaration
  5.  
  6.       // get generic interfaces from trigger object
  7.       for (Type possibleType : trigger.getClass().getGenericInterfaces()) {
  8.         if ((result = getEntityViewFromGenericType(possibleType, _entityView)) != null) {
  9.           if (_entityView == null) {
  10.             _entityView = result;
  11.           }
  12.           return result;
  13.         }
  14.       }
  15.       // get generic interfaces from trigger object super class
  16.       Class<? extends Object> clazz = trigger.getClass();
  17.       Type possibleType = clazz.getGenericSuperclass();
  18.  
  19.       while (possibleType != null && !(possibleType instanceof ParameterizedType)) {
  20.         clazz = clazz.getSuperclass();
  21.         if (clazz != null) {
  22.           possibleType = clazz.getGenericSuperclass();
  23.         } else {
  24.           possibleType = null;
  25.         }
  26.       }
  27.  
  28.       if (possibleType != null) {
  29.         if ((result = getEntityViewFromGenericType(possibleType, _entityView)) != null) {
  30.           if (_entityView == null) {
  31.             _entityView = result;
  32.           }
  33.           return result;
  34.         }
  35.       }
  36.       _logger.error("No implementation found for {}.", trigger);
  37.     } else if (result != null && !result.getClass().getSimpleName().endsWith(ALL_JOY_BUO_EXT_EMA)) {
  38.       // check if currently set entity view matches the old rule of being a "AllJoyBuoExtEma" if not set result to null?
  39.       result = null;
  40.     }
  41.     return result;
  42.   }
  43.  
  44.  
  45.  
  46.  
  47.   IEntityView getEntityViewFromGenericType(Type possibleType, IEntityView defaultView) throws BusinessLogicException {
  48.     try {
  49.       if (possibleType instanceof ParameterizedType) {
  50.         // loop thru all implemented interfaces
  51.         for (Type parameterizedType : ((ParameterizedType)possibleType).getActualTypeArguments()) {
  52.           // check all parameterized types
  53.           if (IEntityView.class.isAssignableFrom((Class<?>)parameterizedType)) {
  54.             // take first implemented interface using IEntityView
  55.             @SuppressWarnings("unchecked")
  56.             Class<IEntityView> typeClass = (Class<IEntityView>)parameterizedType;
  57.             if (typeClass.equals(IEntityView.class)) {
  58.               // if given interface is the IEntityView itself use old logic ...
  59.                 return constructor.newInstance(getSession());
  60.               } else {
  61.                 return defaultView;
  62.               }
  63.             } else if (typeClass.isInterface()) {
  64.               if (defaultView == null || !typeClass.isAssignableFrom(defaultView.getClass())) {
  65.                 // if parameter type is a interface use EntityViewFactory
  66.                 if (_logger.isDebugEnabled()) {
  67.                   _logger.debug("Creating new entity view implementation from interface {}.", typeClass);
  68.                 }
  69.                 return EntityViewFactory.create(typeClass, getSession());
  70.               } else {
  71.                 return defaultView;
  72.               }
  73.             } else if (defaultView == null || !typeClass.equals(defaultView.getClass())) {
  74.               // if parameter type is a classic use it's constructor
  75.               Constructor<IEntityView> constructor = typeClass.getConstructor(SessionContext.class);
  76.               if (_logger.isDebugEnabled()) {
  77.                 _logger.debug("Creating classic implementation {} thru constuctor.", typeClass);
  78.               }
  79.               return constructor.newInstance(getSession());
  80.             }
  81.           }
  82.         }
  83.       }
  84.       return defaultView;
  85.     } catch (Exception e) {
  86.       throw BusinessLogicException.wrap(e);
  87.     }
  88.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement