Exemplo n.º 1
0
 /**
  * Called when getters and setters are to be kept together in pairs. Searches the list of entries
  * for any getter/setter methods that matches this getter method. Normally there is only one
  * setter per getter, but one could have a "getXXX", "isXXX" and "setXXX" trio which need to be
  * related. In this case, the first getter encountered finds the other getter and setter and the
  * three will be emitted in that order.
  *
  * @param possibleMethods entry list, possibly containing the corresponding setter for this
  *     getter.
  * @param settings
  */
 public void determineSetter(
     final List<ClassContentsEntry> possibleMethods, RearrangerSettings settings) {
   if (!isGetter()) // wasted assertion, already checked before calling
   {
     return;
   }
   final PsiMethod thisMethod = (PsiMethod) myEnd;
   String thisProperty = MethodUtil.getPropertyName(thisMethod);
   if (isGetter() && !myKeptWithProperty) {
     if (settings.isKeepGettersSettersWithProperty()) {
       hookGetterToProperty(possibleMethods);
     }
   }
   for (ClassContentsEntry o : possibleMethods) {
     if (o instanceof RelatableEntry) {
       MethodEntry me = (MethodEntry) o;
       // don't use a setter twice (could be two methods which both look like getters; assign the
       // setter
       // to only one of them.)  Also, associate all getter/setter methods for the same property
       // with the
       // first one encountered.
       if ((me.isSetter() || me.isGetter())
           && me.myCorrespondingGetterSetters.size() == 0
           && me != this) {
         PsiMethod m = (PsiMethod) me.myEnd;
         String otherProperty = MethodUtil.getPropertyName(m);
         if (thisProperty.equals(otherProperty)) {
           LOG.debug(
               "method " + thisMethod.toString() + " is getter; its setter is " + m.toString());
           // place getters ahead of setters
           if (me.isGetter()) {
             myCorrespondingGetterSetters.add(0, me);
             // clear the getter flag and set the setter flag; this causes the method to be emitted
             // under the first getter encountered.
             me.setGetter(false);
             me.setSetter(true);
           } else {
             myCorrespondingGetterSetters.add(me);
           }
           me.myCorrespondingGetterSetters.add(this);
         }
       }
     }
   }
 }
Exemplo n.º 2
0
 /**
  * Called after top level methods (and other items) have been moved from entries to
  * ruleInstanceList. Task now is to scan ruleInstanceList looking for MethodEntry objects with
  * dependent (related, extracted) methods. Move these from the entries list to the correct place
  * in the ruleInstanceList list.
  *
  * @param entries Remaining (unarranged) items, including all dependent (extracted) methods.
  * @param ruleInstance Rule containing parents (callers) of potentially related methods
  */
 public static void rearrangeRelatedItems(
     List<ClassContentsEntry> entries, RuleInstance ruleInstance, RelatedMethodsSettings rms) {
   List<RangeEntry> parentEntries = new ArrayList<RangeEntry>(ruleInstance.getMatches());
   for (RangeEntry o : parentEntries) {
     if (o instanceof RelatableEntry) {
       MethodEntry me = (MethodEntry) o;
       if (me.isGetter()) {
         if (me.myKeptWithProperty) {
           if (me.getMatchedRule() != null && me.getMatchedRule().getMatches() != null) {
             // prevent the getter from appearing under a rule it matches; it will be placed under
             // the property
             me.getMatchedRule().getMatches().remove(me);
           }
         }
         for (MethodEntry theSetter : me.myCorrespondingGetterSetters) {
           final RuleInstance theRule = theSetter.getMatchedRule();
           LOG.debug(
               "rearrangeRelatedItems: for getter method "
                   + me
                   + ", corresponding setter is "
                   + theSetter);
           if (theRule != null && theRule.getMatches() != null) {
             LOG.debug(
                 "remove entry "
                             + theSetter.myEnd
                             + " from matched rule"
                             + theRule
                             + "; matches = "
                             + ((java.util.List) theRule.getMatches())
                         == null
                     ? "null"
                     : "");
             theRule.getMatches().remove(theSetter);
           }
         }
       }
       if (me.myCalledMethods.size() > 0) {
         List<MethodEntry> parents = new LinkedList<MethodEntry>();
         parents.add(me);
         moveRelatedItems(
             entries,
             parents,
             rms,
             ((PsiMethod) me.myEnd).getName(),
             ((PsiMethod) me.myEnd).getName() + "()",
             1);
         if (LOG.isDebugEnabled()) {
           // dump sorted children recursively
           me.dumpChild(0);
         }
         me.assignComments(rms);
       }
     }
   }
 }