1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-28 19:35:36 +02:00

FIXED - bug 246061: [Refactoring] why can I select a visibility when extracting a const

https://bugs.eclipse.org/bugs/show_bug.cgi?id=246061
This commit is contained in:
Emanuel Graf 2008-09-10 14:36:22 +00:00
parent 4e8f520b7c
commit 06eb3b7f29
9 changed files with 101 additions and 27 deletions

View file

@ -456,18 +456,17 @@ filename=A.h
//@A.h //@A.h
class X { class X {
void method() { void method() {
int a= /*$*/1/*$$*/; int a= /*$*/42/*$$*/;
} }
}; };
//= //=
namespace
{
const int theAnswer = 1;
}
class X { class X {
void method() { void method() {
int a= theAnswer; int a= theAnswer;
} }
public:
static const int theAnswer = 42;
}; };

View file

@ -21,7 +21,7 @@ import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.cdt.ui.tests.refactoring.RefactoringTest; import org.eclipse.cdt.ui.tests.refactoring.RefactoringTest;
import org.eclipse.cdt.ui.tests.refactoring.TestSourceFile; import org.eclipse.cdt.ui.tests.refactoring.TestSourceFile;
import org.eclipse.cdt.internal.ui.refactoring.NameNVisibilityInformation; import org.eclipse.cdt.internal.ui.refactoring.extractconstant.ExtractConstantInfo;
import org.eclipse.cdt.internal.ui.refactoring.extractconstant.ExtractConstantRefactoring; import org.eclipse.cdt.internal.ui.refactoring.extractconstant.ExtractConstantRefactoring;
import org.eclipse.cdt.internal.ui.refactoring.utils.VisibilityEnum; import org.eclipse.cdt.internal.ui.refactoring.utils.VisibilityEnum;
@ -40,7 +40,7 @@ public class ExtractConstantRefactoringTest extends RefactoringTest {
@Override @Override
protected void runTest() throws Throwable { protected void runTest() throws Throwable {
IFile refFile = project.getFile(fileName); IFile refFile = project.getFile(fileName);
NameNVisibilityInformation info = new NameNVisibilityInformation(); ExtractConstantInfo info = new ExtractConstantInfo();
ExtractConstantRefactoring refactoring = new ExtractConstantRefactoring( refFile, selection, info); ExtractConstantRefactoring refactoring = new ExtractConstantRefactoring( refFile, selection, info);
try { try {
refactoring.lockIndex(); refactoring.lockIndex();

View file

@ -12,9 +12,9 @@
package org.eclipse.cdt.internal.ui.refactoring; package org.eclipse.cdt.internal.ui.refactoring;
import org.eclipse.cdt.core.dom.ast.DOMException; import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IScope; import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
@ -57,10 +57,10 @@ public class MethodContext {
return declarationName; return declarationName;
} }
public IASTSimpleDeclaration getMethodDeclaration(){ public IASTDeclaration getMethodDeclaration(){
IASTNode parent = declarationName.getParent().getParent(); IASTNode parent = declarationName.getParent().getParent();
if (parent instanceof IASTSimpleDeclaration) { if (parent instanceof IASTDeclaration) {
return (IASTSimpleDeclaration) parent; return (IASTDeclaration) parent;
} }
return null; return null;
} }
@ -199,4 +199,19 @@ public class MethodContext {
ICPPClassType bind = (ICPPClassType)classname.resolveBinding(); ICPPClassType bind = (ICPPClassType)classname.resolveBinding();
return bind; return bind;
} }
public static boolean isSameClass(IASTName declName1,
IASTName declName2) {
ICPPClassType bind1 = getClassBinding(declName1);
ICPPClassType bind2 = getClassBinding(declName2);
return bind1.equals(bind2);
}
private static ICPPClassType getClassBinding(IASTName declName1) {
if (declName1.getParent().getParent().getParent() instanceof ICPPASTCompositeTypeSpecifier) {
ICPPASTCompositeTypeSpecifier compTypeSpec = (ICPPASTCompositeTypeSpecifier) declName1.getParent().getParent().getParent();
return (ICPPClassType) compTypeSpec.getName().resolveBinding();
}
return null;
}
} }

View file

@ -0,0 +1,34 @@
/*******************************************************************************
* Copyright (c) 2008 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
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Institute for Software (IFS)- initial API and implementation
******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.extractconstant;
import org.eclipse.cdt.internal.ui.refactoring.MethodContext;
import org.eclipse.cdt.internal.ui.refactoring.NameNVisibilityInformation;
/**
* @author Emanuel Graf IFS
*
*/
public class ExtractConstantInfo extends NameNVisibilityInformation{
private MethodContext mContext;
public MethodContext getMContext() {
return mContext;
}
public void setMContext(MethodContext context) {
mContext = context;
}
}

View file

@ -62,7 +62,6 @@ import org.eclipse.cdt.internal.ui.refactoring.AddDeclarationNodeToClassChange;
import org.eclipse.cdt.internal.ui.refactoring.CRefactoring; import org.eclipse.cdt.internal.ui.refactoring.CRefactoring;
import org.eclipse.cdt.internal.ui.refactoring.MethodContext; import org.eclipse.cdt.internal.ui.refactoring.MethodContext;
import org.eclipse.cdt.internal.ui.refactoring.ModificationCollector; import org.eclipse.cdt.internal.ui.refactoring.ModificationCollector;
import org.eclipse.cdt.internal.ui.refactoring.NameNVisibilityInformation;
import org.eclipse.cdt.internal.ui.refactoring.utils.NodeHelper; import org.eclipse.cdt.internal.ui.refactoring.utils.NodeHelper;
import org.eclipse.cdt.internal.ui.refactoring.utils.SelectionHelper; import org.eclipse.cdt.internal.ui.refactoring.utils.SelectionHelper;
import org.eclipse.cdt.internal.ui.refactoring.utils.TranslationUnitHelper; import org.eclipse.cdt.internal.ui.refactoring.utils.TranslationUnitHelper;
@ -78,9 +77,9 @@ public class ExtractConstantRefactoring extends CRefactoring {
private IASTLiteralExpression target = null; private IASTLiteralExpression target = null;
private final ArrayList<IASTExpression> literalsToReplace = new ArrayList<IASTExpression>(); private final ArrayList<IASTExpression> literalsToReplace = new ArrayList<IASTExpression>();
private final NameNVisibilityInformation info; private final ExtractConstantInfo info;
public ExtractConstantRefactoring(IFile file, ISelection selection, NameNVisibilityInformation info){ public ExtractConstantRefactoring(IFile file, ISelection selection, ExtractConstantInfo info){
super(file,selection, null); super(file,selection, null);
this.info = info; this.info = info;
name = Messages.ExtractConstantRefactoring_ExtractConst; name = Messages.ExtractConstantRefactoring_ExtractConst;
@ -120,6 +119,7 @@ public class ExtractConstantRefactoring extends CRefactoring {
info.addNamesToUsedNames(findAllDeclaredNames()); info.addNamesToUsedNames(findAllDeclaredNames());
info.setName(getDefaultName(target)); info.setName(getDefaultName(target));
info.setMContext(NodeHelper.findMethodContext(target, getIndex()));
sm.done(); sm.done();
return initStatus; return initStatus;
} }
@ -254,7 +254,7 @@ public class ExtractConstantRefactoring extends CRefactoring {
protected void collectModifications(IProgressMonitor pm, ModificationCollector collector) protected void collectModifications(IProgressMonitor pm, ModificationCollector collector)
throws CoreException, OperationCanceledException{ throws CoreException, OperationCanceledException{
MethodContext context = NodeHelper.findMethodContext(target, getIndex()); MethodContext context = info.getMContext();
Collection<IASTExpression> locLiteralsToReplace = new ArrayList<IASTExpression>(); Collection<IASTExpression> locLiteralsToReplace = new ArrayList<IASTExpression>();
if(context.getType() == MethodContext.ContextType.METHOD){ if(context.getType() == MethodContext.ContextType.METHOD){
@ -262,9 +262,15 @@ public class ExtractConstantRefactoring extends CRefactoring {
for (IASTExpression expression : literalsToReplace) { for (IASTExpression expression : literalsToReplace) {
MethodContext exprContext = NodeHelper.findMethodContext(expression, getIndex()); MethodContext exprContext = NodeHelper.findMethodContext(expression, getIndex());
if(exprContext.getType() == MethodContext.ContextType.METHOD){ if(exprContext.getType() == MethodContext.ContextType.METHOD){
if(context.getMethodQName() != null) {
if( MethodContext.isSameClass(exprContext.getMethodQName(), context.getMethodQName())){ if( MethodContext.isSameClass(exprContext.getMethodQName(), context.getMethodQName())){
locLiteralsToReplace.add(expression); locLiteralsToReplace.add(expression);
} }
}else {
if( MethodContext.isSameClass(exprContext.getMethodDeclarationName(), context.getMethodDeclarationName())){
locLiteralsToReplace.add(expression);
}
}
} }
} }

View file

@ -20,7 +20,6 @@ import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.internal.ui.refactoring.CRefactoring; import org.eclipse.cdt.internal.ui.refactoring.CRefactoring;
import org.eclipse.cdt.internal.ui.refactoring.NameNVisibilityInformation;
import org.eclipse.cdt.internal.ui.refactoring.RefactoringRunner; import org.eclipse.cdt.internal.ui.refactoring.RefactoringRunner;
/** /**
@ -35,7 +34,7 @@ public class ExtractConstantRefactoringRunner extends RefactoringRunner {
@Override @Override
public void run() { public void run() {
NameNVisibilityInformation info = new NameNVisibilityInformation(); ExtractConstantInfo info = new ExtractConstantInfo();
CRefactoring refactoring = new ExtractConstantRefactoring(file,selection,info); CRefactoring refactoring = new ExtractConstantRefactoring(file,selection,info);
ExtractConstantRefactoringWizard wizard = new ExtractConstantRefactoringWizard(refactoring, info); ExtractConstantRefactoringWizard wizard = new ExtractConstantRefactoringWizard(refactoring, info);
RefactoringWizardOpenOperation operator = new RefactoringWizardOpenOperation(wizard); RefactoringWizardOpenOperation operator = new RefactoringWizardOpenOperation(wizard);

View file

@ -14,7 +14,7 @@ package org.eclipse.cdt.internal.ui.refactoring.extractconstant;
import org.eclipse.ltk.core.refactoring.Refactoring; import org.eclipse.ltk.core.refactoring.Refactoring;
import org.eclipse.ltk.ui.refactoring.RefactoringWizard; import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
import org.eclipse.cdt.internal.ui.refactoring.NameNVisibilityInformation; import org.eclipse.cdt.internal.ui.refactoring.MethodContext.ContextType;
import org.eclipse.cdt.internal.ui.refactoring.dialogs.ExtractInputPage; import org.eclipse.cdt.internal.ui.refactoring.dialogs.ExtractInputPage;
/** /**
@ -23,16 +23,16 @@ import org.eclipse.cdt.internal.ui.refactoring.dialogs.ExtractInputPage;
public class ExtractConstantRefactoringWizard extends RefactoringWizard { public class ExtractConstantRefactoringWizard extends RefactoringWizard {
private ExtractInputPage page; private ExtractInputPage page;
private final NameNVisibilityInformation info; private final ExtractConstantInfo info;
public ExtractConstantRefactoringWizard(Refactoring refactoring, NameNVisibilityInformation info) { public ExtractConstantRefactoringWizard(Refactoring refactoring, ExtractConstantInfo info) {
super(refactoring, WIZARD_BASED_USER_INTERFACE); super(refactoring, WIZARD_BASED_USER_INTERFACE);
this.info = info; this.info = info;
} }
@Override @Override
protected void addUserInputPages() { protected void addUserInputPages() {
page = new InputPage(Messages.ExtractConstantRefactoring_ExtractConst, info); page = new InputPage(Messages.ExtractConstantRefactoring_ExtractConst, info, info.getMContext().getType() == ContextType.METHOD);
addPage(page); addPage(page);
} }

View file

@ -14,6 +14,7 @@ package org.eclipse.cdt.internal.ui.refactoring.extractconstant;
import java.util.ArrayList; import java.util.ArrayList;
import org.eclipse.osgi.util.NLS; import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.cdt.internal.ui.refactoring.NameNVisibilityInformation; import org.eclipse.cdt.internal.ui.refactoring.NameNVisibilityInformation;
import org.eclipse.cdt.internal.ui.refactoring.dialogs.ExtractInputPage; import org.eclipse.cdt.internal.ui.refactoring.dialogs.ExtractInputPage;
@ -21,12 +22,26 @@ import org.eclipse.cdt.internal.ui.refactoring.dialogs.ExtractInputPage;
public class InputPage extends ExtractInputPage { public class InputPage extends ExtractInputPage {
private final ArrayList<String> usedNames; private final ArrayList<String> usedNames;
private boolean showVisibilityPane;
public InputPage(String name, NameNVisibilityInformation info) { public InputPage(String name, NameNVisibilityInformation info) {
this(name, info, true);
}
public InputPage(String name, NameNVisibilityInformation info, boolean showVisibilityPane) {
super(name, info); super(name, info);
label = Messages.InputPage_ConstName; label = Messages.InputPage_ConstName;
errorLabel = Messages.InputPage_EnterContName; errorLabel = Messages.InputPage_EnterContName;
usedNames = info.getUsedNames(); usedNames = info.getUsedNames();
this.showVisibilityPane = showVisibilityPane;
}
@Override
public void createControl(Composite parent) {
super.createControl(parent);
control.getVisibiltyGroup().setVisible(showVisibilityPane);
} }
@Override @Override

View file

@ -37,6 +37,7 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTranslationUnit;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor; import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.cdt.internal.ui.refactoring.MethodContext; import org.eclipse.cdt.internal.ui.refactoring.MethodContext;
import org.eclipse.cdt.internal.ui.refactoring.MethodContext.ContextType;
/** /**
* General class for common Node operations. * General class for common Node operations.
@ -133,7 +134,7 @@ public class NodeHelper {
ICPPASTQualifiedName qname =( ICPPASTQualifiedName )name; ICPPASTQualifiedName qname =( ICPPASTQualifiedName )name;
context.setMethodQName(qname); context.setMethodQName(qname);
IBinding bind = qname.resolveBinding(); IBinding bind = qname.resolveBinding();
IASTName[] decl = translationUnit.getDeclarationsInAST(bind);//TODO HSR works only for names in the current translationUnit IASTName[] decl = translationUnit.getDeclarationsInAST(bind);
for (IASTName tmpname : decl) { for (IASTName tmpname : decl) {
IASTNode methoddefinition = tmpname.getParent().getParent(); IASTNode methoddefinition = tmpname.getParent().getParent();
if (methoddefinition instanceof IASTSimpleDeclaration) { if (methoddefinition instanceof IASTSimpleDeclaration) {
@ -172,6 +173,11 @@ public class NodeHelper {
} }
} }
} }
}else {
if (name.getParent().getParent().getParent() instanceof ICPPASTCompositeTypeSpecifier) {
context.setType(ContextType.METHOD);
context.setMethodDeclarationName(name);
}
} }
} }