aspect PermuteParameters { public void MethodDecl.permuteParameters(int[] perm, boolean createDelegate) { int n = getNumParameter(); if(!isPerm(perm, n)) throw new RefactoringException("not a permutation"); if(isIdentityPerm(perm)) return; programRoot().lockMethodNames(name()); programRoot().lockOverridingDependencies(name()); for(MethodDecl md : relatives()) { Collection uses = md.uses(); if(md.isNative() || !md.fromSource()) throw new RefactoringException("cannot permute parameters"); if(md.isVariableArity() && perm[n-1] != n-1) throw new RefactoringException("variable arity parameter has to come last"); if(createDelegate) { MethodDecl delegate = (MethodDecl)md.fullCopyAndDetach(); delegate.clearOverridingDependencies(); if(!md.hostType().isInterfaceDecl()) { delegate.getModifiers().removeModifiers("abstract", "native"); List args = new List(); for(ParameterDeclaration pd : md.getParameters()) args.add(new VarAccess(pd.name())); args.permute(perm); delegate.setBlock(new Block(new List().add(new ReturnStmt(md.createLockedAccess(args))))); } md.hostType().getBodyDeclList().insertChild(delegate, md.getChildIndex()); } md.getParameterList().permute(perm); md.flushCaches(); for(MethodAccess ma : uses) { ma.getArgList().lockDataFlow(); ma.getArgList().permute(perm); } } } public void MethodDecl.doPermuteParameters(int[] perm, boolean createDelegate) { permuteParameters(perm, createDelegate); programRoot().eliminate(RETURN_VOID, LOCKED_DATAFLOW, LOCKED_NAMES, LOCKED_OVERRIDING); } // check whether perm is a permutation of the integers {0, ..., max-1} public static boolean ASTNode.isPerm(int[] perm, int max) { if(perm.length != max) return false; boolean[] gotit = new boolean[max]; for(int i=0;i= max) return false; if(gotit[perm[i]]) return false; gotit[perm[i]] = true; } return true; } public static boolean ASTNode.isIdentityPerm(int[] perm) { for(int i=0;i