From e4581eabb5477f9134e353f377cca7ff658793da Mon Sep 17 00:00:00 2001 From: Emanuel Graf Date: Mon, 5 Jan 2009 12:48:23 +0000 Subject: [PATCH] FIXED - bug 239060: const parameters in extracted functions https://bugs.eclipse.org/bugs/show_bug.cgi?id=239060 --- .../ui/refactoring/NodeContainer.java | 53 +++++++++++++++---- .../extractfunction/ChooserComposite.java | 32 ++++++++++- .../ExtractFunctionRefactoring.java | 21 +++++++- .../refactoring/extractfunction/Messages.java | 3 +- .../extractfunction/messages.properties | 3 +- 5 files changed, 98 insertions(+), 14 deletions(-) diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/NodeContainer.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/NodeContainer.java index 01114a7028d..085dfc18f74 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/NodeContainer.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/NodeContainer.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Copyright (c) 2008, 2009 Institute for Software, HSR Hochschule fuer Technik * Rapperswil, University of applied sciences and others * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -49,11 +49,15 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTReferenceOperator; import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriter; public class NodeContainer { + + public final NameInformation NULL_NAME_INFORMATION = new NameInformation( + new CPPASTName()); private final ArrayList vec; private final ArrayList names; public class NameInformation { + private IASTName name; private IASTName declaration; private final ArrayList references; @@ -61,6 +65,8 @@ public class NodeContainer { private int lastCachedReferencesHash; private boolean isReference; private boolean isReturnValue; + private boolean isConst; + private boolean isWriteAccess; private boolean userSetIsReference; private boolean userSetIsReturnValue; @@ -103,7 +109,7 @@ public class NodeContainer { public ArrayList getReferencesAfterSelection() { if (referencesAfterCached == null - || lastCachedReferencesHash == references.hashCode()) { + || lastCachedReferencesHash != references.hashCode()) { lastCachedReferencesHash = references.hashCode(); referencesAfterCached = new ArrayList(); @@ -139,14 +145,16 @@ public class NodeContainer { IASTDeclarator declarator; if (sourceDeclarator instanceof IASTArrayDeclarator) { - IASTArrayDeclarator arrDeclarator = (IASTArrayDeclarator)sourceDeclarator; + IASTArrayDeclarator arrDeclarator = (IASTArrayDeclarator) sourceDeclarator; declarator = new CPPASTArrayDeclarator(); - IASTArrayModifier[] arrayModifiers = arrDeclarator.getArrayModifiers(); + IASTArrayModifier[] arrayModifiers = arrDeclarator + .getArrayModifiers(); for (IASTArrayModifier arrayModifier : arrayModifiers) { - ((IASTArrayDeclarator)declarator).addArrayModifier(arrayModifier); + ((IASTArrayDeclarator) declarator) + .addArrayModifier(arrayModifier); } - - }else { + + } else { declarator = new CPPASTDeclarator(); } declarator.setName(new CPPASTName(getDeclaration().toCharArray())); @@ -167,7 +175,7 @@ public class NodeContainer { } public boolean hasReferenceOperartor(IASTDeclarator declarator) { - for(IASTPointerOperator pOp :declarator.getPointerOperators()) { + for (IASTPointerOperator pOp : declarator.getPointerOperators()) { if (pOp instanceof ICPPASTReferenceOperator) { return true; } @@ -206,7 +214,8 @@ public class NodeContainer { @Override public String toString() { - return Messages.NodeContainer_Name + name + ' ' + isDeclarationInScope(); + return Messages.NodeContainer_Name + name + ' ' + + isDeclarationInScope(); } public boolean isReference() { @@ -248,6 +257,23 @@ public class NodeContainer { public void setUserSetName(String userSetName) { this.userSetName = userSetName; } + + public boolean isConst() { + return isConst; + } + + public void setConst(boolean isConst) { + this.isConst = isConst; + } + + public boolean isWriteAccess() { + return isWriteAccess; + } + + public void setWriteAccess(boolean isWriteAceess) { + this.isWriteAccess = isWriteAceess; + } + } public NodeContainer() { @@ -371,6 +397,14 @@ public class NodeContainer { declarations.add(nameInf.getDeclaration()); usedAfter.add(nameInf); + } else { + for (NameInformation nameInformation : usedAfter) { + if (nameInf.isWriteAccess() + && nameInf.getDeclaration() == nameInformation + .getDeclaration()) { + nameInformation.setWriteAccess(true); + } + } } } @@ -493,4 +527,5 @@ public class NodeContainer { public ArrayList getNames() { return names; } + } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ChooserComposite.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ChooserComposite.java index b681b5a88c6..b2dacd08a90 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ChooserComposite.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ChooserComposite.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Copyright (c) 2008, 2009 Institute for Software, HSR Hochschule fuer Technik * Rapperswil, University of applied sciences and others * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -30,6 +30,7 @@ import org.eclipse.cdt.core.dom.ast.IASTDeclarator; import org.eclipse.cdt.internal.ui.refactoring.NodeContainer.NameInformation; + public class ChooserComposite extends Composite { private static final String COLUMN_RETURN = Messages.ChooserComposite_Return; @@ -71,6 +72,7 @@ public class ChooserComposite extends Composite { addColumnToTable(table, COLUMN_TYPE); addColumnToTable(table, COLUMN_NAME); addColumnToTable(table, COLUMN_REFERENCE); + addColumnToTable(table, Messages.ChooserComposite_const); if(!info.isExtractExpression()) { addColumnToTable(table, COLUMN_RETURN); } @@ -118,6 +120,34 @@ public class ChooserComposite extends Composite { editor.horizontalAlignment = SWT.CENTER; referenceButtons.add(referenceButton); editor.setEditor(referenceButton, item, columnIndex++); + + // Cosnt Button + editor = new TableEditor(table); + final Button constButton = new Button(table, SWT.CHECK); + + constButton.setSelection(name.isConst()); + constButton.setEnabled(!name.isWriteAccess()); + + constButton.setBackground(table.getBackground()); + constButton.addSelectionListener(new SelectionListener() { + + public void widgetDefaultSelected(SelectionEvent e) { + name.setConst(constButton + .getSelection()); + onVisibilityOrReturnChange(info.getAllUsedNames()); + } + + public void widgetSelected(SelectionEvent e) { + widgetDefaultSelected(e); + } + + }); + constButton.pack(); + editor.minimumWidth = constButton.getSize().x; + editor.horizontalAlignment = SWT.CENTER; +// referenceButtons.add(referenceButton); + editor.setEditor(constButton, item, columnIndex++); + if(info.isExtractExpression()) continue; // Skip the return radiobutton diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java index 41f39644edb..a11840e197d 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Copyright (c) 2008, 2009 Institute for Software, HSR Hochschule fuer Technik * Rapperswil, University of applied sciences and others * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -11,6 +11,7 @@ *******************************************************************************/ package org.eclipse.cdt.internal.ui.refactoring.extractfunction; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -91,6 +92,8 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleDeclSpecifier; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleDeclaration; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTemplateDeclaration; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPNodeFactory; +import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVariableReadWriteFlags; +import org.eclipse.cdt.internal.core.pdom.dom.PDOMName; import org.eclipse.cdt.internal.ui.refactoring.AddDeclarationNodeToClassChange; import org.eclipse.cdt.internal.ui.refactoring.CRefactoring; @@ -155,6 +158,7 @@ public class ExtractFunctionRefactoring extends CRefactoring { return initStatus; container.findAllNames(); + markWriteAccess(); sm.worked(1); if (isProgressMonitorCanceld(sm, initStatus)) @@ -176,7 +180,7 @@ public class ExtractFunctionRefactoring extends CRefactoring { } else if (container.getAllDeclaredInScope().size() == 1) { info.setInScopeDeclaredVariable(container.getAllDeclaredInScope().get(0)); } - + extractedFunctionConstructionHelper = ExtractedFunctionConstructionHelper .createFor(container.getNodesToWrite()); @@ -190,6 +194,19 @@ public class ExtractFunctionRefactoring extends CRefactoring { return status; } + private void markWriteAccess() throws CoreException { + ArrayList paras = container.getNames(); + + for (NameInformation name : paras) { + int flag = CPPVariableReadWriteFlags.getReadWriteFlags(name + .getName()); + if ((flag & PDOMName.WRITE_ACCESS) != 0) { + name.setWriteAccess(true); + } + } + + } + private void checkForNonExtractableStatements(NodeContainer cont, RefactoringStatus status) { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.java index 4bf146e3a34..4c8b1d95f28 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Copyright (c) 2008, 2009 Institute for Software, HSR Hochschule fuer Technik * Rapperswil, University of applied sciences and others * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -39,6 +39,7 @@ public final class Messages extends NLS { public static String ExtractFunctionRefactoring_CreateFunctionCall; public static String ChooserComposite_Return; public static String ChooserComposite_CallByRef; + public static String ChooserComposite_const; public static String ChooserComposite_Name; public static String ChooserComposite_Type; public static String ChooserComposite_NoReturnValue; diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/messages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/messages.properties index 4d60c9f3315..19b02e9be22 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/messages.properties +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/messages.properties @@ -1,5 +1,5 @@ ############################################################################### -# Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik +# Copyright (c) 2008, 2009 Institute for Software, HSR Hochschule fuer Technik # Rapperswil, University of applied sciences and others # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Public License v1.0 @@ -27,6 +27,7 @@ ExtractFunctionRefactoring_CreateMethodCall=Create Method Call ExtractFunctionRefactoring_CreateFunctionCall=Create Function Call ChooserComposite_Return=Return ChooserComposite_CallByRef=Call by Reference +ChooserComposite_const=const ChooserComposite_Name=Name ChooserComposite_Type=Type ChooserComposite_NoReturnValue=No return-value (void)