/** * 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); } } } } }
public void determineGetterSetterAndExtractedMethodStatus(RearrangerSettings settings) { LOG.debug("building method call & getter-setter graph for " + getName()); /** * check all rules to find first that this method matches. If that rule specifies that matching * methods are excluded from extracted method treatment, then skip this step. If this method is * a getter (according to that rule's definition) then determine its setter (according to that * rule's definition) at this point also. */ setNoExtractedMethods(false); /** * The following code determines whether the method is a getter/setter based on default * definition. This is necessary in case there are no rules, but "Keep Getters/Setters Together" * is set. If the method matches an individual rule with its own getter/setter definition, the * values of getter and setter will be changed to match that rule's definition. */ setGetter(MethodUtil.isGetter((PsiMethod) myEnd, settings.getDefaultGSDefinition())); setSetter(MethodUtil.isSetter((PsiMethod) myEnd, settings.getDefaultGSDefinition())); for (Rule rule : settings.getItemOrderAttributeList()) { if (rule instanceof IRestrictMethodExtraction) { if (rule.isMatch(this)) { if (((IRestrictMethodExtraction) rule).isNoExtractedMethods()) { LOG.debug("excluding " + myEnd.toString() + " from extracted method consideration"); setNoExtractedMethods(true); } setGetter(false); if (rule instanceof IHasGetterSetterDefinition) { if (MethodUtil.isGetter( (PsiMethod) getEnd(), ((IHasGetterSetterDefinition) rule).getGetterSetterDefinition())) { if (settings.isKeepGettersSettersTogether()) { setGetter(true); } } setSetter( MethodUtil.isSetter( (PsiMethod) getEnd(), ((IHasGetterSetterDefinition) rule).getGetterSetterDefinition())); } break; } } } }
/** * Getters/Setters are supposed to be kept with their associated property. Search the list of * entries to find the property and attach the setter. * * @param entries list of all items (methods, fields) in the class. */ private void hookGetterToProperty(List<ClassContentsEntry> entries) { ListIterator<ClassContentsEntry> li = entries.listIterator(); String property = MethodUtil.getPropertyName((PsiMethod) myEnd); while (li.hasNext()) { Object o = li.next(); if (o instanceof FieldEntry) { FieldEntry fe = (FieldEntry) o; StringBuffer sb = new StringBuffer(fe.getName()); sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); if (fe.getGetterMethod() == null && property.equals(sb.toString())) { fe.setGetterMethod(this); myKeptWithProperty = true; break; } } } }