Fixed 31480: Remove unnecessary casts in code
authorStefan Hühner <stefan.huehner@openbravo.com>
Sat, 14 Nov 2015 13:26:10 +0100
changeset 27904 3a7faced04fe
parent 27903 dcb3471fa95d
child 27905 b252497be371
Fixed 31480: Remove unnecessary casts in code

A few distinct patterns:
- Cast from XX directly to XX just not needed at all
- sometimes from .uniqueResult()
- somtimes from i.e. reading from typed Map
- Cast from Object to XX then passing to API using object anyway
i.e. result of .uniqueResult() then passing to Restrictions.eq
- some misc ones like DalUtil.getId to String when not required
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/filterexpression/AddPaymentDefaultValuesHandler.java
src-test/src/org/openbravo/test/role/inheritance/RoleInheritanceTestUtils.java
src/org/openbravo/costing/CostingMigrationProcess.java
src/org/openbravo/event/GLJournalLineEventHandler.java
src/org/openbravo/role/inheritance/InheritedAccessEnabledEventHandler.java
src/org/openbravo/role/inheritance/RecalculatePermissionsHandler.java
src/org/openbravo/role/inheritance/RoleInheritanceEventHandler.java
src/org/openbravo/role/inheritance/RoleInheritanceManager.java
src/org/openbravo/role/inheritance/access/AccessTypeInjector.java
src/org/openbravo/role/inheritance/access/FieldAccessInjector.java
src/org/openbravo/role/inheritance/access/TabAccessInjector.java
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/filterexpression/AddPaymentDefaultValuesHandler.java	Sat Nov 14 13:22:20 2015 +0100
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/filterexpression/AddPaymentDefaultValuesHandler.java	Sat Nov 14 13:26:10 2015 +0100
@@ -511,6 +511,6 @@
       qry.setNamedParameter("currency", OBDal.getInstance().get(Currency.class, currencyId));
     }
 
-    return (FinAccPaymentMethod) qry.uniqueResult();
+    return qry.uniqueResult();
   }
 }
--- a/src-test/src/org/openbravo/test/role/inheritance/RoleInheritanceTestUtils.java	Sat Nov 14 13:22:20 2015 +0100
+++ b/src-test/src/org/openbravo/test/role/inheritance/RoleInheritanceTestUtils.java	Sat Nov 14 13:26:10 2015 +0100
@@ -348,7 +348,7 @@
         RoleOrganization.class);
     orgAccessCriteria.add(Restrictions.eq(RoleOrganization.PROPERTY_ROLE, role));
     orgAccessCriteria.add(Restrictions.eq(RoleOrganization.PROPERTY_ORGANIZATION,
-        (Organization) orgCriteria.uniqueResult()));
+        orgCriteria.uniqueResult()));
     orgAccessCriteria.setMaxResults(1);
     RoleOrganization ro = (RoleOrganization) orgAccessCriteria.uniqueResult();
     ro.setOrgAdmin(isOrgAdmin);
@@ -404,7 +404,7 @@
         WindowAccess.class);
     windowAccessCriteria.add(Restrictions.eq(WindowAccess.PROPERTY_ROLE, role));
     windowAccessCriteria.add(Restrictions.eq(WindowAccess.PROPERTY_WINDOW,
-        (Window) windowCriteria.uniqueResult()));
+        windowCriteria.uniqueResult()));
     windowAccessCriteria.setMaxResults(1);
     WindowAccess wa = (WindowAccess) windowAccessCriteria.uniqueResult();
     wa.setEditableField(editableField);
@@ -493,9 +493,8 @@
     final OBCriteria<TabAccess> tabAccessCriteria = OBDal.getInstance().createCriteria(
         TabAccess.class);
     tabAccessCriteria.add(Restrictions.eq(TabAccess.PROPERTY_WINDOWACCESS,
-        (WindowAccess) waCriteria.uniqueResult()));
-    tabAccessCriteria
-        .add(Restrictions.eq(TabAccess.PROPERTY_TAB, (Tab) tabCriteria.uniqueResult()));
+        waCriteria.uniqueResult()));
+    tabAccessCriteria.add(Restrictions.eq(TabAccess.PROPERTY_TAB, tabCriteria.uniqueResult()));
     tabAccessCriteria.setMaxResults(1);
     TabAccess ta = (TabAccess) tabAccessCriteria.uniqueResult();
     ta.setEditableField(editableTab);
@@ -563,8 +562,7 @@
     Tab tab = (Tab) tabCriteria.uniqueResult();
 
     final OBCriteria<TabAccess> taCriteria = OBDal.getInstance().createCriteria(TabAccess.class);
-    taCriteria.add(Restrictions.eq(TabAccess.PROPERTY_WINDOWACCESS,
-        (WindowAccess) waCriteria.uniqueResult()));
+    taCriteria.add(Restrictions.eq(TabAccess.PROPERTY_WINDOWACCESS, waCriteria.uniqueResult()));
     taCriteria.add(Restrictions.eq(TabAccess.PROPERTY_TAB, tab));
     taCriteria.setMaxResults(1);
 
@@ -615,7 +613,7 @@
     final OBCriteria<TabAccess> tabAccessCriteria = OBDal.getInstance().createCriteria(
         TabAccess.class);
     tabAccessCriteria.add(Restrictions.eq(TabAccess.PROPERTY_WINDOWACCESS,
-        (WindowAccess) waCriteria.uniqueResult()));
+        waCriteria.uniqueResult()));
     tabAccessCriteria.add(Restrictions.eq(TabAccess.PROPERTY_TAB, tab));
     tabAccessCriteria.setMaxResults(1);
     TabAccess ta = (TabAccess) tabAccessCriteria.uniqueResult();
@@ -629,7 +627,7 @@
         FieldAccess.class);
     fieldAccessCriteria.add(Restrictions.eq(FieldAccess.PROPERTY_TABACCESS, ta));
     fieldAccessCriteria.add(Restrictions.eq(FieldAccess.PROPERTY_FIELD,
-        (Field) fieldCriteria.uniqueResult()));
+        fieldCriteria.uniqueResult()));
     fieldAccessCriteria.setMaxResults(1);
 
     FieldAccess fa = (FieldAccess) fieldAccessCriteria.uniqueResult();
@@ -719,7 +717,7 @@
         org.openbravo.model.ad.access.ProcessAccess.PROPERTY_ROLE, role));
     processAccessCriteria.add(Restrictions.eq(
         org.openbravo.model.ad.access.ProcessAccess.PROPERTY_PROCESS,
-        (Process) processCriteria.uniqueResult()));
+        processCriteria.uniqueResult()));
     processAccessCriteria.setMaxResults(1);
     org.openbravo.model.ad.access.ProcessAccess pa = (org.openbravo.model.ad.access.ProcessAccess) processAccessCriteria
         .uniqueResult();
@@ -773,7 +771,7 @@
         FormAccess.class);
     formAccessCriteria.add(Restrictions.eq(FormAccess.PROPERTY_ROLE, role));
     formAccessCriteria.add(Restrictions.eq(FormAccess.PROPERTY_SPECIALFORM,
-        (Form) formCriteria.uniqueResult()));
+        formCriteria.uniqueResult()));
     formAccessCriteria.setMaxResults(1);
     FormAccess fa = (FormAccess) formAccessCriteria.uniqueResult();
     fa.setActive(isActive);
@@ -826,7 +824,7 @@
         WidgetClassAccess.class);
     widgetAccessCriteria.add(Restrictions.eq(WidgetClassAccess.PROPERTY_ROLE, role));
     widgetAccessCriteria.add(Restrictions.eq(WidgetClassAccess.PROPERTY_WIDGETCLASS,
-        (WidgetClass) widgetCriteria.uniqueResult()));
+        widgetCriteria.uniqueResult()));
     widgetAccessCriteria.setMaxResults(1);
     WidgetClassAccess wa = (WidgetClassAccess) widgetAccessCriteria.uniqueResult();
     wa.setActive(isActive);
@@ -881,7 +879,7 @@
         ViewRoleAccess.class);
     viewAccessCriteria.add(Restrictions.eq(ViewRoleAccess.PROPERTY_ROLE, role));
     viewAccessCriteria.add(Restrictions.eq(ViewRoleAccess.PROPERTY_VIEWIMPLEMENTATION,
-        (OBUIAPPViewImplementation) viewCriteria.uniqueResult()));
+        viewCriteria.uniqueResult()));
     viewAccessCriteria.setMaxResults(1);
     ViewRoleAccess va = (ViewRoleAccess) viewAccessCriteria.uniqueResult();
     va.setActive(isActive);
@@ -941,7 +939,7 @@
         org.openbravo.client.application.ProcessAccess.PROPERTY_ROLE, role));
     processAccessCriteria.add(Restrictions.eq(
         org.openbravo.client.application.ProcessAccess.PROPERTY_OBUIAPPPROCESS,
-        (org.openbravo.client.application.Process) processCriteria.uniqueResult()));
+        processCriteria.uniqueResult()));
     processAccessCriteria.setMaxResults(1);
     org.openbravo.client.application.ProcessAccess pa = (org.openbravo.client.application.ProcessAccess) processAccessCriteria
         .uniqueResult();
@@ -997,7 +995,7 @@
         TableAccess.class);
     tableAccessCriteria.add(Restrictions.eq(TableAccess.PROPERTY_ROLE, role));
     tableAccessCriteria.add(Restrictions.eq(TableAccess.PROPERTY_TABLE,
-        (Table) tableCriteria.uniqueResult()));
+        tableCriteria.uniqueResult()));
     tableAccessCriteria.setMaxResults(1);
     TableAccess ta = (TableAccess) tableAccessCriteria.uniqueResult();
     ta.setReadOnly(isReadOnly);
@@ -1051,7 +1049,7 @@
         AlertRecipient.class);
     alertRecipientCriteria.add(Restrictions.eq(AlertRecipient.PROPERTY_ROLE, role));
     alertRecipientCriteria.add(Restrictions.eq(AlertRecipient.PROPERTY_ALERTRULE,
-        (AlertRule) alertCriteria.uniqueResult()));
+        alertCriteria.uniqueResult()));
     alertRecipientCriteria.setMaxResults(1);
     AlertRecipient ar = (AlertRecipient) alertRecipientCriteria.uniqueResult();
     ar.setSendEMail(isSendEmail);
@@ -1218,8 +1216,7 @@
     obCriteria.setMaxResults(1);
 
     final OBCriteria<TabAccess> tabCriteria = OBDal.getInstance().createCriteria(TabAccess.class);
-    tabCriteria.add(Restrictions.eq(TabAccess.PROPERTY_WINDOWACCESS,
-        (WindowAccess) obCriteria.uniqueResult()));
+    tabCriteria.add(Restrictions.eq(TabAccess.PROPERTY_WINDOWACCESS, obCriteria.uniqueResult()));
     tabCriteria.addOrderBy(TabAccess.PROPERTY_TAB + "." + Tab.PROPERTY_NAME, true);
     List<TabAccess> list = tabCriteria.list();
     String[] result = new String[list.size() * 2];
--- a/src/org/openbravo/costing/CostingMigrationProcess.java	Sat Nov 14 13:22:20 2015 +0100
+++ b/src/org/openbravo/costing/CostingMigrationProcess.java	Sat Nov 14 13:26:10 2015 +0100
@@ -271,7 +271,7 @@
         phql.setNamedParameter("date", new Date());
         phql.setNamedParameter("org", organization);
         phql.setMaxResult(1);
-        PeriodControl period = (PeriodControl) phql.uniqueResult();
+        PeriodControl period = phql.uniqueResult();
         if (period == null) {
           throw new OBException(String.format(OBMessageUtils.messageBD("PeriodClosedForMMI"),
               organization.getName()));
@@ -319,7 +319,7 @@
     sdphql.setFilterOnReadableClients(false);
     sdphql.setFilterOnReadableOrganization(false);
     sdphql.setMaxResult(1);
-    StorageDetail storageDetailProduct = (StorageDetail) sdphql.uniqueResult();
+    StorageDetail storageDetailProduct = sdphql.uniqueResult();
     if (storageDetailProduct != null) {
       throw new OBException("@ProductStockInDifferentUOM@");
     }
--- a/src/org/openbravo/event/GLJournalLineEventHandler.java	Sat Nov 14 13:22:20 2015 +0100
+++ b/src/org/openbravo/event/GLJournalLineEventHandler.java	Sat Nov 14 13:26:10 2015 +0100
@@ -63,14 +63,14 @@
           .getPreviousState(credit)) != 0
           || ((BigDecimal) event.getCurrentState(debit)).compareTo((BigDecimal) event
               .getPreviousState(debit)) != 0
-          || !((Boolean) event.getCurrentState(openItems)).equals((Boolean) event
-              .getPreviousState(openItems))) {
-        logger.info("Current credit: " + (BigDecimal) event.getCurrentState(credit)
-            + ". Previous Credit: " + (BigDecimal) event.getPreviousState(credit));
-        logger.info("Current debit: " + (BigDecimal) event.getCurrentState(debit)
-            + ". Previous Debit: " + (BigDecimal) event.getPreviousState(debit));
-        logger.info("Current Open items: " + (Boolean) event.getCurrentState(openItems)
-            + ". Previous Open items: " + (Boolean) event.getPreviousState(openItems));
+          || !((Boolean) event.getCurrentState(openItems))
+              .equals(event.getPreviousState(openItems))) {
+        logger.info("Current credit: " + event.getCurrentState(credit) + ". Previous Credit: "
+            + event.getPreviousState(credit));
+        logger.info("Current debit: " + event.getCurrentState(debit) + ". Previous Debit: "
+            + event.getPreviousState(debit));
+        logger.info("Current Open items: " + event.getCurrentState(openItems)
+            + ". Previous Open items: " + event.getPreviousState(openItems));
         throw new OBException("@ModifyGLJournalLine@");
       }
     }
--- a/src/org/openbravo/role/inheritance/InheritedAccessEnabledEventHandler.java	Sat Nov 14 13:22:20 2015 +0100
+++ b/src/org/openbravo/role/inheritance/InheritedAccessEnabledEventHandler.java	Sat Nov 14 13:26:10 2015 +0100
@@ -135,6 +135,6 @@
     if (role == null) {
       return true;
     }
-    return OBDal.getInstance().exists(Role.ENTITY_NAME, (String) DalUtil.getId(role));
+    return OBDal.getInstance().exists(Role.ENTITY_NAME, DalUtil.getId(role));
   }
 }
--- a/src/org/openbravo/role/inheritance/RecalculatePermissionsHandler.java	Sat Nov 14 13:22:20 2015 +0100
+++ b/src/org/openbravo/role/inheritance/RecalculatePermissionsHandler.java	Sat Nov 14 13:26:10 2015 +0100
@@ -131,7 +131,7 @@
     String text = "";
     try {
       for (String className : map.keySet()) {
-        CalculationResult counters = (CalculationResult) map.get(className);
+        CalculationResult counters = map.get(className);
         if (counters.getUpdated() > 0 || counters.getCreated() > 0) {
           Class<?> myClass = Class.forName(className);
           Entity entity = ModelProvider.getInstance().getEntity(myClass);
--- a/src/org/openbravo/role/inheritance/RoleInheritanceEventHandler.java	Sat Nov 14 13:22:20 2015 +0100
+++ b/src/org/openbravo/role/inheritance/RoleInheritanceEventHandler.java	Sat Nov 14 13:26:10 2015 +0100
@@ -108,7 +108,7 @@
     }
     final RoleInheritance inheritance = (RoleInheritance) event.getTargetInstance();
     boolean notDeletingParent = OBDal.getInstance().exists(Role.ENTITY_NAME,
-        (String) DalUtil.getId(inheritance.getRole()));
+        DalUtil.getId(inheritance.getRole()));
     if (notDeletingParent) {
       manager.applyRemoveInheritance(inheritance);
     }
@@ -123,7 +123,7 @@
   private boolean existCycles(Role role, String roleIdToFind) {
     boolean result = false;
     for (RoleInheritance ri : role.getADRoleInheritanceInheritFromList()) {
-      if (ri.isActive() && roleIdToFind.equals((String) DalUtil.getId(ri.getRole()))) {
+      if (ri.isActive() && roleIdToFind.equals(DalUtil.getId(ri.getRole()))) {
         return true;
       }
       if (!result) {
@@ -170,7 +170,7 @@
   private boolean findAncestor(Role role, Set<String> roleIdsToFind) {
     boolean result = false;
     for (RoleInheritance ri : role.getADRoleInheritanceList()) {
-      if (ri.isActive() && roleIdsToFind.contains((String) DalUtil.getId(ri.getInheritFrom()))) {
+      if (ri.isActive() && roleIdsToFind.contains(DalUtil.getId(ri.getInheritFrom()))) {
         return true;
       }
       if (!result) {
--- a/src/org/openbravo/role/inheritance/RoleInheritanceManager.java	Sat Nov 14 13:22:20 2015 +0100
+++ b/src/org/openbravo/role/inheritance/RoleInheritanceManager.java	Sat Nov 14 13:26:10 2015 +0100
@@ -236,7 +236,7 @@
       if (ri.isActive()) {
         Map<String, CalculationResult> result = recalculateAllAccessesForRole(ri.getRole());
         for (String accessClassName : result.keySet()) {
-          CalculationResult counters = (CalculationResult) result.get(accessClassName);
+          CalculationResult counters = result.get(accessClassName);
           if (counters.getUpdated() > 0 || counters.getCreated() > 0) {
             updatedRoles.add(ri.getRole());
           }
@@ -689,8 +689,7 @@
     final OBCriteria<RoleInheritance> obCriteria = OBDal.getInstance().createCriteria(
         RoleInheritance.class);
     obCriteria.add(Restrictions.eq(RoleInheritance.PROPERTY_ROLE, inheritance.getRole()));
-    obCriteria
-        .add(Restrictions.ne(RoleInheritance.PROPERTY_ID, (String) DalUtil.getId(inheritance)));
+    obCriteria.add(Restrictions.ne(RoleInheritance.PROPERTY_ID, DalUtil.getId(inheritance)));
     obCriteria.addOrderBy(RoleInheritance.PROPERTY_SEQUENCENUMBER, true);
     boolean added = false;
     for (RoleInheritance rh : obCriteria.list()) {
--- a/src/org/openbravo/role/inheritance/access/AccessTypeInjector.java	Sat Nov 14 13:22:20 2015 +0100
+++ b/src/org/openbravo/role/inheritance/access/AccessTypeInjector.java	Sat Nov 14 13:26:10 2015 +0100
@@ -209,7 +209,7 @@
       whereClause.append(" where p.").append(roleProperty).append(" = :roleId");
       addEntityWhereClause(whereClause);
       final OBQuery<T> query = OBDal.getInstance().createQuery(clazz, whereClause.toString());
-      query.setNamedParameter("roleId", (String) DalUtil.getId(role));
+      query.setNamedParameter("roleId", DalUtil.getId(role));
       doEntityParameterReplacement(query);
       query.setFilterOnActive(false);
       return (List<? extends InheritedAccessEnabled>) query.list();
--- a/src/org/openbravo/role/inheritance/access/FieldAccessInjector.java	Sat Nov 14 13:22:20 2015 +0100
+++ b/src/org/openbravo/role/inheritance/access/FieldAccessInjector.java	Sat Nov 14 13:26:10 2015 +0100
@@ -96,7 +96,7 @@
   public void removeReferenceInParentList(InheritedAccessEnabled access) {
     FieldAccess fa = (FieldAccess) access;
     boolean accessExists = OBDal.getInstance().exists(TabAccess.ENTITY_NAME,
-        (String) DalUtil.getId(fa.getTabAccess()));
+        DalUtil.getId(fa.getTabAccess()));
     if (accessExists) {
       fa.getTabAccess().getADFieldAccessList().remove(fa);
     }
--- a/src/org/openbravo/role/inheritance/access/TabAccessInjector.java	Sat Nov 14 13:22:20 2015 +0100
+++ b/src/org/openbravo/role/inheritance/access/TabAccessInjector.java	Sat Nov 14 13:26:10 2015 +0100
@@ -114,7 +114,7 @@
   public void removeReferenceInParentList(InheritedAccessEnabled access) {
     TabAccess ta = (TabAccess) access;
     boolean accessExists = OBDal.getInstance().exists(WindowAccess.ENTITY_NAME,
-        (String) DalUtil.getId(ta.getWindowAccess()));
+        DalUtil.getId(ta.getWindowAccess()));
     if (accessExists) {
       ta.getWindowAccess().getADTabAccessList().remove(ta);
     }