View Javadoc

    1   /*
    2    * joey-gen and its relative products are published under the terms
    3    * of the Apache Software License.
    4    *
    5    * Created on 2005/01/25 15:00:50
    6    */
    7   package org.asyrinx.joey.gen.command.java;
    8
    9   import java.util.HashMap;
    10  import java.util.Iterator;
    11  import java.util.Map;
    12
    13  import org.asyrinx.brownie.core.lang.StringUtils;
    14  import org.asyrinx.joey.gen.model.Element;
    15  import org.asyrinx.joey.gen.model.java.Entity;
    16  import org.asyrinx.joey.gen.model.java.Property;
    17  import org.asyrinx.joey.gen.model.java.Reference;
    18
    19  /***
    20   * @author takeshi
    21   */
    22  public class CheckNameOfProperty extends JavaCommand {
    23
    24      /*
    25       * (non-Javadoc)
    26       *
    27       * @see org.asyrinx.joey.gen.command.java.JavaCommand#visit(org.asyrinx.joey.gen.model.java.Entity)
    28       */
    29      public void visit(Entity entity) {
    30          final Map checkedNames = new HashMap();
    31          for (Iterator i = entity.getPropertiesAll().iterator(); i.hasNext();) {
    32              final Element property = (Element) i.next();
    33              final String propertyName = property.getName();
    34              checkName(checkedNames, property, propertyName);
    35          }
    36          for (Iterator i = entity.getReferencesAll().iterator(); i.hasNext();) {
    37              final Reference reference = (Reference) i.next();
    38              final String propertyName = reference.getPropertyNameInLocal();
    39              checkName(checkedNames, reference, propertyName);
    40          }
    41          for (Iterator i = entity.getReferredAll().iterator(); i.hasNext();) {
    42              final Reference reference = (Reference) i.next();
    43              final String propertyName = reference.getPropertyNameInReferred();
    44              checkName(checkedNames, reference, propertyName);
    45          }
    46      }
    47
    48      /***
    49       * @param checkedNames
    50       * @param element
    51       * @param propertyName
    52       */
    53      private void checkName(Map checkedNames, Element element, String propertyName) {
    54          if (StringUtils.isEmpty(propertyName)) {
    55              addError(element.getFullName() + "(" + element.getOriginal() + ")"
    56                      + " has no property name");
    57          }
    58          final Element duplicated = (Element) checkedNames.get(propertyName);
    59          if (duplicated != null) {
    60              if (isDuplicated(element, duplicated))
    61                  addError("propertyName=" + propertyName + "  " + element + "("
    62                          + element.getOriginal() + ")" + " has duplicated property name with "
    63                          + duplicated + "(" + duplicated.getOriginal() + ")");
    64          }
    65          checkedNames.put(propertyName, element);
    66      }
    67
    68      private boolean isDuplicated(Element element1, Element element2) {
    69          if (element1 == element2)
    70              return false;
    71          if ((element1 instanceof Property) && (element2 instanceof Property)) {
    72              if (isExtended((Property) element1, (Property) element2))
    73                  return false;
    74              if (isExtended((Property) element2, (Property) element1))
    75                  return false;
    76          }
    77          return true;
    78      }
    79
    80      /***
    81       * @param property1
    82       * @param property2
    83       * @return
    84       */
    85      private boolean isExtended(Property property1, Property property2) {
    86          if (!property1.isExtended())
    87              return false;
    88          if (property1.getParent().getSuperClass() == property2.getParent())
    89              return true;
    90          return false;
    91      }
    92
    93  }