public void determineSettersAndMethodCalls( RearrangerSettings settings, List<ClassContentsEntry> contents) { if (isGetter()) { if (settings.isKeepGettersSettersTogether()) { determineSetter( contents, settings); // link getters/setters via correspondingGetterSetter entries } } if (!isNoExtractedMethods() && settings.getExtractedMethodsSettings().isMoveExtractedMethods()) { determineMethodCalls(contents, settings); } }
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; } } } }
/** * 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 JLabel getPopupEntryText(RearrangerSettings settings) { StringBuffer name = new StringBuffer(80); PsiMethod m = (PsiMethod) myEnd; if (m.getReturnTypeElement() != null && !settings.isShowTypeAfterMethod()) { name.append(m.getReturnTypeElement().getText()); name.append(' '); } name.append(m.getName()); PsiParameterList plist = m.getParameterList(); if (settings.isShowParameterNames() || settings.isShowParameterTypes()) { if (plist != null) { name.append("("); for (int i = 0; i < plist.getParameters().length; i++) { if (i > 0) { name.append(", "); } if (settings.isShowParameterTypes()) { String modifiers = plist.getParameters()[i].getModifierList().getText(); if (modifiers.length() > 0) { name.append(modifiers); name.append(' '); } name.append(plist.getParameters()[i].getTypeElement().getText()); if (settings.isShowParameterNames()) { name.append(' '); } } if (settings.isShowParameterNames()) { name.append(plist.getParameters()[i].getName()); } } name.append(")"); } } if (m.getReturnTypeElement() != null && settings.isShowTypeAfterMethod()) { name.append(": "); name.append(m.getReturnTypeElement().getText()); } return new JLabel(name.toString()); }
/** * If setting indicates, move all overloaded methods (of the same name) adjacent with the first * encountered. Sort in the configured order (original order, or by number of parameters). */ public static void handleOverloadedMethods( List<ClassContentsEntry> contents, RearrangerSettings settings) { if (!settings.isKeepOverloadedMethodsTogether()) return; /** * first, detect overloaded methods and move them to the first method's list of overloaded * methods. Make two passes, first for extracted methods, then for non-extracted methods. This * will organize any overloaded methods for extracted methods with them (as opposed to whichever * was seen first.) */ cullOverloadedMethods(contents, true); LOG.debug("entered handleOverloadedMethods(): move overloaded methods together"); cullOverloadedMethods(contents, false); List<ClassContentsEntry> copy = new ArrayList<ClassContentsEntry>(contents); for (ClassContentsEntry rangeEntry : copy) { if (rangeEntry instanceof RelatableEntry) { MethodEntry current = (MethodEntry) rangeEntry; if (current.myOverloadedMethods.size() > 0) { List<MethodEntry> newList = new ArrayList<MethodEntry>(current.myOverloadedMethods.size() + 1); newList.add(current); /** * we are looking at the head of a list of overloaded methods. We need to sort the list if * necessary and, if the head of the list has changed, replace it in the contents array. */ switch (settings.getOverloadedOrder()) { case RearrangerSettings.OVERLOADED_ORDER_RETAIN_ORIGINAL: // list is already in original order, except perhaps that the top-most extracted // method // comes first (if there is one). newList.addAll(current.myOverloadedMethods); break; case RearrangerSettings.OVERLOADED_ORDER_ASCENDING_PARAMETERS: case RearrangerSettings.OVERLOADED_ORDER_DESCENDING_PARAMETERS: for (MethodEntry entry : current.myOverloadedMethods) { boolean inserted = false; for (int index = 0; index < newList.size(); index++) { MethodEntry me = newList.get(index); if (settings.getOverloadedOrder() == RearrangerSettings.OVERLOADED_ORDER_ASCENDING_PARAMETERS ? me.getnParameters() > entry.getnParameters() : me.getnParameters() < entry.getnParameters()) { newList.add(index, entry); inserted = true; break; } } if (!inserted) { newList.add(entry); } } break; } current.myOverloadedMethods.clear(); /** * if the head of the arraylist is not the same as "current", then the sort operation * moved another method to the head of the list. Replace that in the contents array. Then * assign a new ordered list. */ int index = contents.indexOf(current); if (newList.get(0) != current) { contents.set(index, ((MethodEntry) newList.get(0))); } /** insert the remaining overloaded methods after the current entry. */ newList.remove(0); for (int j = 0; j < newList.size(); j++) { contents.add(index + j + 1, ((MethodEntry) newList.get(j))); } } } } }