mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
FIXED - bug 291190: Refactoring tests use the index without read-locks
https://bugs.eclipse.org/bugs/show_bug.cgi?id=291190
This commit is contained in:
parent
8c5477d2d0
commit
95e3b558d5
9 changed files with 328 additions and 318 deletions
|
@ -301,6 +301,12 @@ public abstract class CRefactoring extends Refactoring {
|
||||||
return fIndex;
|
return fIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public IASTTranslationUnit getUnit() {
|
||||||
|
return unit;
|
||||||
|
}
|
||||||
|
|
||||||
protected ArrayList<IASTName> findAllMarkedNames() {
|
protected ArrayList<IASTName> findAllMarkedNames() {
|
||||||
final ArrayList<IASTName> namesVector = new ArrayList<IASTName>();
|
final ArrayList<IASTName> namesVector = new ArrayList<IASTName>();
|
||||||
|
|
||||||
|
|
|
@ -194,6 +194,10 @@ public class MethodContext {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isInline() {
|
||||||
|
return qname == null;
|
||||||
|
}
|
||||||
|
|
||||||
private static ICPPClassType getClassBinding(ICPPASTQualifiedName qname){
|
private static ICPPClassType getClassBinding(ICPPASTQualifiedName qname){
|
||||||
IASTName classname = qname.getNames()[qname.getNames().length - 2];
|
IASTName classname = qname.getNames()[qname.getNames().length - 2];
|
||||||
ICPPClassType bind = (ICPPClassType)classname.resolveBinding();
|
ICPPClassType bind = (ICPPClassType)classname.resolveBinding();
|
||||||
|
|
|
@ -15,7 +15,6 @@ import java.util.List;
|
||||||
|
|
||||||
import org.eclipse.text.edits.TextEditGroup;
|
import org.eclipse.text.edits.TextEditGroup;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.dom.ast.DOMException;
|
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
|
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
|
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
|
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
|
||||||
|
@ -29,10 +28,12 @@ import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
|
||||||
import org.eclipse.cdt.core.dom.ast.IBasicType;
|
import org.eclipse.cdt.core.dom.ast.IBasicType;
|
||||||
import org.eclipse.cdt.core.dom.ast.IBinding;
|
import org.eclipse.cdt.core.dom.ast.IBinding;
|
||||||
import org.eclipse.cdt.core.dom.ast.IType;
|
import org.eclipse.cdt.core.dom.ast.IType;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.ITypedef;
|
||||||
import org.eclipse.cdt.core.dom.ast.IBasicType.Kind;
|
import org.eclipse.cdt.core.dom.ast.IBasicType.Kind;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTBinaryExpression;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTBinaryExpression;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
|
||||||
import org.eclipse.cdt.core.dom.rewrite.ASTRewrite;
|
import org.eclipse.cdt.core.dom.rewrite.ASTRewrite;
|
||||||
|
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTBinaryExpression;
|
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTBinaryExpression;
|
||||||
|
@ -47,10 +48,8 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTReturnStatement;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleDeclSpecifier;
|
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.CPPASTSimpleDeclaration;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPBasicType;
|
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPBasicType;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPClassType;
|
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPFunction;
|
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPFunction;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPNamespace;
|
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPTypedef;
|
|
||||||
|
|
||||||
import org.eclipse.cdt.internal.ui.refactoring.NodeContainer.NameInformation;
|
import org.eclipse.cdt.internal.ui.refactoring.NodeContainer.NameInformation;
|
||||||
|
|
||||||
|
@ -120,8 +119,11 @@ public class ExtractExpression extends ExtractedFunctionConstructionHelper {
|
||||||
}
|
}
|
||||||
return createSimpleDeclSpecifier(Kind.eVoid);
|
return createSimpleDeclSpecifier(Kind.eVoid);
|
||||||
}
|
}
|
||||||
|
if(declSpecifier.isFrozen()) {
|
||||||
return declSpecifier.copy();
|
return declSpecifier.copy();
|
||||||
|
}else {
|
||||||
|
return declSpecifier;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private IASTDeclSpecifier handleLiteralExpression(IASTLiteralExpression extractedNode) {
|
private IASTDeclSpecifier handleLiteralExpression(IASTLiteralExpression extractedNode) {
|
||||||
|
@ -207,65 +209,39 @@ public class ExtractExpression extends ExtractedFunctionConstructionHelper {
|
||||||
CPPBasicType basicType = (CPPBasicType) expressionType;
|
CPPBasicType basicType = (CPPBasicType) expressionType;
|
||||||
return createSimpleDeclSpecifier(basicType.getKind());
|
return createSimpleDeclSpecifier(basicType.getKind());
|
||||||
|
|
||||||
} else if (expressionType instanceof CPPTypedef) {
|
} else if (expressionType instanceof ITypedef) {
|
||||||
|
|
||||||
return getDeclSpecForType(((CPPTypedef)expressionType));
|
return getDeclSpecForType(((ITypedef)expressionType));
|
||||||
|
|
||||||
} else if (expressionType instanceof CPPClassType) {
|
} else if (expressionType instanceof ICPPClassType) {
|
||||||
|
|
||||||
return getDeclSpecForType((CPPClassType)expressionType);
|
return getDeclSpecForType((ICPPClassType)expressionType);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private CPPASTNamedTypeSpecifier getDeclSpecForType(CPPClassType classType) {
|
private IASTDeclSpecifier getDeclSpecForType(ICPPClassType expressionType) {
|
||||||
|
|
||||||
IASTName name = null;
|
IASTName name = null;
|
||||||
try {
|
|
||||||
IBinding bind = classType.getOwner();
|
char[][] qualifiedNameCharArray = CPPVisitor.getQualifiedNameCharArray(expressionType);
|
||||||
if (bind instanceof CPPNamespace) {
|
name = new CPPASTQualifiedName();
|
||||||
ICPPASTQualifiedName qname = getQname(classType, bind);
|
for (char[] cs : qualifiedNameCharArray) {
|
||||||
qname.addName((IASTName) classType.getDefinition().copy());
|
((ICPPASTQualifiedName) name).addName(new CPPASTName(cs));
|
||||||
name = qname;
|
|
||||||
}else {
|
|
||||||
name = (IASTName) classType.getDefinition();
|
|
||||||
}
|
|
||||||
} catch (DOMException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
name= new CPPASTName(classType.getNameCharArray());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new CPPASTNamedTypeSpecifier(name.copy());
|
return new CPPASTNamedTypeSpecifier(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ICPPASTQualifiedName getQname(IBinding classType, IBinding bind) {
|
private CPPASTNamedTypeSpecifier getDeclSpecForType(ITypedef classType) {
|
||||||
CPPNamespace namespace = (CPPNamespace) bind;
|
|
||||||
char[][] names = namespace.getFullyQualifiedNameCharArray();
|
|
||||||
CPPASTQualifiedName qname = new CPPASTQualifiedName();
|
|
||||||
for (char[] string : names) {
|
|
||||||
qname.addName(new CPPASTName(string));
|
|
||||||
}
|
|
||||||
return qname;
|
|
||||||
}
|
|
||||||
|
|
||||||
private IASTDeclSpecifier getDeclSpecForType(CPPTypedef typedef) {
|
|
||||||
IASTName name = null;
|
IASTName name = null;
|
||||||
try {
|
char[][] qualifiedNameCharArray = CPPVisitor.getQualifiedNameCharArray(classType);
|
||||||
IBinding bind = typedef.getOwner();
|
name = new CPPASTQualifiedName();
|
||||||
if (bind instanceof CPPNamespace) {
|
for (char[] cs : qualifiedNameCharArray) {
|
||||||
ICPPASTQualifiedName qname = getQname(typedef, bind);
|
((ICPPASTQualifiedName) name).addName(new CPPASTName(cs));
|
||||||
qname.addName((IASTName) typedef.getDefinition().copy());
|
|
||||||
name = qname;
|
|
||||||
}else {
|
|
||||||
name = (IASTName) typedef.getDefinition();
|
|
||||||
}
|
|
||||||
} catch (DOMException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
name= new CPPASTName(typedef.getNameCharArray());
|
|
||||||
}
|
}
|
||||||
return new CPPASTNamedTypeSpecifier(name.copy());
|
|
||||||
|
return new CPPASTNamedTypeSpecifier(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IASTDeclSpecifier createSimpleDeclSpecifier(IBasicType.Kind type) {
|
private static IASTDeclSpecifier createSimpleDeclSpecifier(IBasicType.Kind type) {
|
||||||
|
@ -307,8 +283,8 @@ public class ExtractExpression extends ExtractedFunctionConstructionHelper {
|
||||||
return declaration.getDeclSpecifier();
|
return declaration.getDeclSpecifier();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}else if(binding instanceof CPPTypedef) {
|
}else if(binding instanceof ITypedef) {
|
||||||
CPPTypedef typedef = (CPPTypedef) binding;
|
ITypedef typedef = (ITypedef) binding;
|
||||||
return new CPPASTNamedTypeSpecifier(new CPPASTName(typedef.getNameCharArray()));
|
return new CPPASTNamedTypeSpecifier(new CPPASTName(typedef.getNameCharArray()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -57,7 +57,6 @@ import org.eclipse.cdt.core.dom.ast.IASTNode;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
|
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
|
import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTReturnStatement;
|
import org.eclipse.cdt.core.dom.ast.IASTReturnStatement;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
|
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
|
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator;
|
import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTStatement;
|
import org.eclipse.cdt.core.dom.ast.IASTStatement;
|
||||||
|
@ -72,11 +71,9 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclSpecifier;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTOperatorName;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTOperatorName;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleDeclSpecifier;
|
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateId;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateId;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding;
|
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
|
||||||
import org.eclipse.cdt.core.dom.rewrite.ASTRewrite;
|
import org.eclipse.cdt.core.dom.rewrite.ASTRewrite;
|
||||||
|
@ -98,7 +95,6 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTInitializerExpression;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName;
|
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTQualifiedName;
|
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTQualifiedName;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTReturnStatement;
|
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTReturnStatement;
|
||||||
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.CPPASTSimpleDeclaration;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTemplateDeclaration;
|
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.CPPNodeFactory;
|
||||||
|
@ -155,72 +151,83 @@ public class ExtractFunctionRefactoring extends CRefactoring {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public RefactoringStatus checkInitialConditions(IProgressMonitor pm)
|
public RefactoringStatus checkInitialConditions(IProgressMonitor pm)
|
||||||
throws CoreException, OperationCanceledException {
|
throws CoreException, OperationCanceledException {
|
||||||
SubMonitor sm = SubMonitor.convert(pm, 10);
|
SubMonitor sm = SubMonitor.convert(pm, 10);
|
||||||
RefactoringStatus status = super.checkInitialConditions(sm.newChild(6));
|
try {
|
||||||
|
lockIndex();
|
||||||
|
|
||||||
container = findExtractableNodes();
|
try {
|
||||||
sm.worked(1);
|
super.checkInitialConditions(sm.newChild(6));
|
||||||
|
|
||||||
if (isProgressMonitorCanceld(sm, initStatus))
|
container = findExtractableNodes();
|
||||||
return initStatus;
|
sm.worked(1);
|
||||||
|
|
||||||
checkForNonExtractableStatements(container, status);
|
if (isProgressMonitorCanceld(sm, initStatus))
|
||||||
sm.worked(1);
|
return initStatus;
|
||||||
|
|
||||||
if (isProgressMonitorCanceld(sm, initStatus))
|
checkForNonExtractableStatements(container, initStatus);
|
||||||
return initStatus;
|
sm.worked(1);
|
||||||
|
|
||||||
container.findAllNames();
|
if (isProgressMonitorCanceld(sm, initStatus))
|
||||||
markWriteAccess();
|
return initStatus;
|
||||||
sm.worked(1);
|
|
||||||
|
|
||||||
if (isProgressMonitorCanceld(sm, initStatus))
|
container.findAllNames();
|
||||||
return initStatus;
|
markWriteAccess();
|
||||||
|
sm.worked(1);
|
||||||
|
|
||||||
container.getAllAfterUsedNames();
|
if (isProgressMonitorCanceld(sm, initStatus))
|
||||||
info.setAllUsedNames(container.getUsedNamesUnique());
|
return initStatus;
|
||||||
|
|
||||||
if (container.size() < 1) {
|
container.getAllAfterUsedNames();
|
||||||
status.addFatalError(Messages.ExtractFunctionRefactoring_NoStmtSelected);
|
info.setAllUsedNames(container.getUsedNamesUnique());
|
||||||
sm.done();
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (container.getAllDeclaredInScope().size() > 1) {
|
if (container.size() < 1) {
|
||||||
status.addFatalError(Messages.ExtractFunctionRefactoring_TooManySelected);
|
initStatus.addFatalError(Messages.ExtractFunctionRefactoring_NoStmtSelected);
|
||||||
} else if (container.getAllDeclaredInScope().size() == 1) {
|
sm.done();
|
||||||
info.setInScopeDeclaredVariable(container.getAllDeclaredInScope().get(0));
|
return initStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
extractedFunctionConstructionHelper = ExtractedFunctionConstructionHelper
|
if (container.getAllDeclaredInScope().size() > 1) {
|
||||||
|
initStatus.addFatalError(Messages.ExtractFunctionRefactoring_TooManySelected);
|
||||||
|
} else if (container.getAllDeclaredInScope().size() == 1) {
|
||||||
|
info.setInScopeDeclaredVariable(container.getAllDeclaredInScope().get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
extractedFunctionConstructionHelper = ExtractedFunctionConstructionHelper
|
||||||
.createFor(container.getNodesToWrite());
|
.createFor(container.getNodesToWrite());
|
||||||
|
|
||||||
boolean isExtractExpression = container.getNodesToWrite().get(0) instanceof IASTExpression;
|
boolean isExtractExpression = container.getNodesToWrite().get(0) instanceof IASTExpression;
|
||||||
info.setExtractExpression(isExtractExpression);
|
info.setExtractExpression(isExtractExpression);
|
||||||
|
|
||||||
info.setDeclarator(getDeclaration(container.getNodesToWrite().get(0)));
|
info.setDeclarator(getDeclaration(container.getNodesToWrite().get(0)));
|
||||||
MethodContext context = NodeHelper.findMethodContext(container.getNodesToWrite().get(0), getIndex());
|
MethodContext context = NodeHelper.findMethodContext(container.getNodesToWrite().get(0), getIndex());
|
||||||
info.setMethodContext(context);
|
info.setMethodContext(context);
|
||||||
|
|
||||||
if (info.getInScopeDeclaredVariable() != null) {
|
if (info.getInScopeDeclaredVariable() != null) {
|
||||||
info.getInScopeDeclaredVariable().setUserSetIsReturnValue(true);
|
info.getInScopeDeclaredVariable().setUserSetIsReturnValue(true);
|
||||||
}
|
}
|
||||||
for (int i = 0; i < info.getAllUsedNames().size(); i++) {
|
for (int i = 0; i < info.getAllUsedNames().size(); i++) {
|
||||||
if (!info.getAllUsedNames().get(i).isDeclarationInScope()) {
|
if (!info.getAllUsedNames().get(i).isDeclarationInScope()) {
|
||||||
NameInformation name = info.getAllUsedNames().get(i);
|
NameInformation name = info.getAllUsedNames().get(i);
|
||||||
if(!name.isReturnValue()) {
|
if(!name.isReturnValue()) {
|
||||||
name.setUserSetIsReference(name.isReference());
|
name.setUserSetIsReference(name.isReference());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(unit != null) {
|
||||||
|
IIndex index = CCorePlugin.getIndexManager().getIndex(project);
|
||||||
|
unit.setIndex(index);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
if(unit != null) {
|
|
||||||
IIndex index = CCorePlugin.getIndexManager().getIndex(project);
|
|
||||||
unit.setIndex(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
sm.done();
|
sm.done();
|
||||||
return status;
|
}
|
||||||
|
finally {
|
||||||
|
unlockIndex();
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
return initStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void markWriteAccess() throws CoreException {
|
private void markWriteAccess() throws CoreException {
|
||||||
|
@ -279,56 +286,77 @@ public class ExtractFunctionRefactoring extends CRefactoring {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public RefactoringStatus checkFinalConditions(IProgressMonitor pm)
|
public RefactoringStatus checkFinalConditions(IProgressMonitor pm)
|
||||||
throws CoreException, OperationCanceledException {
|
throws CoreException, OperationCanceledException {
|
||||||
RefactoringStatus status = super.checkFinalConditions(pm);
|
RefactoringStatus finalConditions = null;
|
||||||
|
try {
|
||||||
|
lockIndex();
|
||||||
|
try {
|
||||||
|
finalConditions = super.checkFinalConditions(pm);
|
||||||
|
|
||||||
final IASTName astMethodName = new CPPASTName(info.getMethodName()
|
|
||||||
.toCharArray());
|
|
||||||
MethodContext context = NodeHelper.findMethodContext(container.getNodesToWrite().get(0), getIndex());
|
|
||||||
|
|
||||||
if (context.getType() == ContextType.METHOD) {
|
final IASTName astMethodName = new CPPASTName(info.getMethodName()
|
||||||
ICPPASTCompositeTypeSpecifier classDeclaration = (ICPPASTCompositeTypeSpecifier) context
|
.toCharArray());
|
||||||
|
MethodContext context = NodeHelper.findMethodContext(container.getNodesToWrite().get(0), getIndex());
|
||||||
|
|
||||||
|
if (context.getType() == ContextType.METHOD && !context.isInline()) {
|
||||||
|
ICPPASTCompositeTypeSpecifier classDeclaration = (ICPPASTCompositeTypeSpecifier) context
|
||||||
.getMethodDeclaration().getParent();
|
.getMethodDeclaration().getParent();
|
||||||
IASTSimpleDeclaration methodDeclaration = getDeclaration(astMethodName);
|
IASTSimpleDeclaration methodDeclaration = getDeclaration(astMethodName);
|
||||||
|
|
||||||
if (isMethodAllreadyDefined(methodDeclaration, classDeclaration)) {
|
if (isMethodAllreadyDefined(methodDeclaration, classDeclaration, getIndex())) {
|
||||||
status.addError(Messages.ExtractFunctionRefactoring_NameInUse);
|
finalConditions.addError(Messages.ExtractFunctionRefactoring_NameInUse);
|
||||||
return status;
|
return finalConditions;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (NameInformation name : info.getAllUsedNames()) {
|
||||||
|
if (name.isUserSetIsReturnValue()) {
|
||||||
|
info.setReturnVariable(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
finally {
|
||||||
for (NameInformation name : info.getAllUsedNames()) {
|
unlockIndex();
|
||||||
if (name.isUserSetIsReturnValue()) {
|
|
||||||
info.setReturnVariable(name);
|
|
||||||
}
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
}
|
}
|
||||||
|
return finalConditions;
|
||||||
return status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void collectModifications(IProgressMonitor pm,
|
protected void collectModifications(IProgressMonitor pm,
|
||||||
ModificationCollector collector) throws CoreException,
|
ModificationCollector collector) throws CoreException,
|
||||||
OperationCanceledException {
|
OperationCanceledException {
|
||||||
final IASTName astMethodName = new CPPASTName(info.getMethodName()
|
try {
|
||||||
.toCharArray());
|
lockIndex();
|
||||||
|
try {
|
||||||
|
final IASTName astMethodName = new CPPASTName(info.getMethodName()
|
||||||
|
.toCharArray());
|
||||||
|
|
||||||
MethodContext context = NodeHelper.findMethodContext(container.getNodesToWrite().get(0), getIndex());
|
MethodContext context = NodeHelper.findMethodContext(container.getNodesToWrite().get(0), getIndex());
|
||||||
|
|
||||||
// Create Declaration in Class
|
// Create Declaration in Class
|
||||||
if (context.getType() == ContextType.METHOD) {
|
if (context.getType() == ContextType.METHOD && !context.isInline()) {
|
||||||
createMethodDeclaration(astMethodName, context, collector);
|
createMethodDeclaration(astMethodName, context, collector);
|
||||||
}
|
}
|
||||||
// Create Method Definition
|
// Create Method Definition
|
||||||
IASTNode firstNode = container.getNodesToWrite().get(0);
|
IASTNode firstNode = container.getNodesToWrite().get(0);
|
||||||
IPath implPath = new Path(firstNode.getContainingFilename());
|
IPath implPath = new Path(firstNode.getContainingFilename());
|
||||||
final IFile implementationFile = ResourcesPlugin.getWorkspace()
|
final IFile implementationFile = ResourcesPlugin.getWorkspace()
|
||||||
.getRoot().getFileForLocation(implPath);
|
.getRoot().getFileForLocation(implPath);
|
||||||
|
|
||||||
createMethodDefinition(astMethodName, context, firstNode,
|
createMethodDefinition(astMethodName, context, firstNode,
|
||||||
implementationFile, collector);
|
implementationFile, collector);
|
||||||
|
|
||||||
createMethodCalls(astMethodName, implementationFile, context, collector);
|
createMethodCalls(astMethodName, implementationFile, context, collector);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
unlockIndex();
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -476,7 +504,7 @@ public class ExtractFunctionRefactoring extends CRefactoring {
|
||||||
} else {
|
} else {
|
||||||
// Save Name Sequenz Number
|
// Save Name Sequenz Number
|
||||||
IASTName name = (IASTName) node;
|
IASTName name = (IASTName) node;
|
||||||
TrailName trailName = new TrailName();
|
TrailName trailName = new TrailName(name);
|
||||||
int actCount = trailCounter.getObject().intValue();
|
int actCount = trailCounter.getObject().intValue();
|
||||||
if (nameTrail.containsKey(name.getRawSignature())) {
|
if (nameTrail.containsKey(name.getRawSignature())) {
|
||||||
Integer value = nameTrail.get(name.getRawSignature());
|
Integer value = nameTrail.get(name.getRawSignature());
|
||||||
|
@ -486,7 +514,6 @@ public class ExtractFunctionRefactoring extends CRefactoring {
|
||||||
nameTrail.put(name.getRawSignature(), trailCounter.getObject());
|
nameTrail.put(name.getRawSignature(), trailCounter.getObject());
|
||||||
}
|
}
|
||||||
trailName.setNameNumber(actCount);
|
trailName.setNameNumber(actCount);
|
||||||
trailName.setRealName(name);
|
|
||||||
|
|
||||||
if (info.getReturnVariable() != null
|
if (info.getReturnVariable() != null
|
||||||
&& info.getReturnVariable().getName().getRawSignature().equals(
|
&& info.getReturnVariable().getName().getRawSignature().equals(
|
||||||
|
@ -494,33 +521,6 @@ public class ExtractFunctionRefactoring extends CRefactoring {
|
||||||
returnNumber.setObject(Integer.valueOf(actCount));
|
returnNumber.setObject(Integer.valueOf(actCount));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save type informations for the name
|
|
||||||
IBinding bind = name.resolveBinding();
|
|
||||||
IASTName[] declNames = name.getTranslationUnit().getDeclarationsInAST(bind);
|
|
||||||
if (declNames.length > 0) {
|
|
||||||
IASTNode tmpNode = ASTHelper.getDeclarationForNode(declNames[0]);
|
|
||||||
|
|
||||||
IBinding declbind = declNames[0].resolveBinding();
|
|
||||||
if (declbind instanceof ICPPBinding) {
|
|
||||||
ICPPBinding cppBind = (ICPPBinding) declbind;
|
|
||||||
try {
|
|
||||||
trailName.setGloballyQualified(cppBind.isGloballyQualified());
|
|
||||||
} catch (DOMException e) {
|
|
||||||
ILog logger = CUIPlugin.getDefault().getLog();
|
|
||||||
IStatus status = new Status(IStatus.WARNING, CUIPlugin.PLUGIN_ID,
|
|
||||||
IStatus.OK, e.getMessage(), e);
|
|
||||||
|
|
||||||
logger.log(status);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tmpNode != null) {
|
|
||||||
trailName.setDeclaration(tmpNode);
|
|
||||||
} else {
|
|
||||||
hasNameResolvingForSimilarError = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
trail.add(trailName);
|
trail.add(trailName);
|
||||||
return PROCESS_SKIP;
|
return PROCESS_SKIP;
|
||||||
}
|
}
|
||||||
|
@ -536,9 +536,9 @@ public class ExtractFunctionRefactoring extends CRefactoring {
|
||||||
return trail;
|
return trail;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean isStatementInTrail(IASTStatement stmt, final Vector<IASTNode> trail) {
|
protected boolean isStatementInTrail(IASTStatement stmt, final Vector<IASTNode> trail, IIndex index) {
|
||||||
final Container<Boolean> same = new Container<Boolean>(Boolean.TRUE);
|
final Container<Boolean> same = new Container<Boolean>(Boolean.TRUE);
|
||||||
final TrailNodeEqualityChecker equalityChecker = new TrailNodeEqualityChecker(names, namesCounter);
|
final TrailNodeEqualityChecker equalityChecker = new TrailNodeEqualityChecker(names, namesCounter, index);
|
||||||
|
|
||||||
stmt.accept(new CPPASTAllVisitor() {
|
stmt.accept(new CPPASTAllVisitor() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -578,9 +578,9 @@ public class ExtractFunctionRefactoring extends CRefactoring {
|
||||||
|
|
||||||
private boolean isMethodAllreadyDefined(
|
private boolean isMethodAllreadyDefined(
|
||||||
IASTSimpleDeclaration methodDeclaration,
|
IASTSimpleDeclaration methodDeclaration,
|
||||||
ICPPASTCompositeTypeSpecifier classDeclaration) {
|
ICPPASTCompositeTypeSpecifier classDeclaration, IIndex index) {
|
||||||
TrailNodeEqualityChecker equalityChecker = new TrailNodeEqualityChecker(
|
TrailNodeEqualityChecker equalityChecker = new TrailNodeEqualityChecker(
|
||||||
names, namesCounter);
|
names, namesCounter, index);
|
||||||
|
|
||||||
IBinding bind = classDeclaration.getName().resolveBinding();
|
IBinding bind = classDeclaration.getName().resolveBinding();
|
||||||
IASTStandardFunctionDeclarator declarator = (IASTStandardFunctionDeclarator) methodDeclaration
|
IASTStandardFunctionDeclarator declarator = (IASTStandardFunctionDeclarator) methodDeclaration
|
||||||
|
@ -647,8 +647,10 @@ public class ExtractFunctionRefactoring extends CRefactoring {
|
||||||
|
|
||||||
ICPPASTQualifiedName qname = new CPPASTQualifiedName();
|
ICPPASTQualifiedName qname = new CPPASTQualifiedName();
|
||||||
if (context.getType() == ContextType.METHOD) {
|
if (context.getType() == ContextType.METHOD) {
|
||||||
for (int i = 0; i < (context.getMethodQName().getNames().length - 1); i++) {
|
if(context.getMethodQName() != null) {
|
||||||
qname.addName(new CPPASTName(context.getMethodQName().getNames()[i].toCharArray()));
|
for (int i = 0; i < (context.getMethodQName().getNames().length - 1); i++) {
|
||||||
|
qname.addName(new CPPASTName(context.getMethodQName().getNames()[i].toCharArray()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
qname.addName(astMethodName);
|
qname.addName(astMethodName);
|
||||||
|
@ -656,9 +658,8 @@ public class ExtractFunctionRefactoring extends CRefactoring {
|
||||||
IASTFunctionDefinition func = new CPPASTFunctionDefinition();
|
IASTFunctionDefinition func = new CPPASTFunctionDefinition();
|
||||||
func.setParent(unit);
|
func.setParent(unit);
|
||||||
|
|
||||||
ICPPASTSimpleDeclSpecifier dummyDeclSpecifier = new CPPASTSimpleDeclSpecifier();
|
IASTDeclSpecifier returnType = getReturnType();
|
||||||
func.setDeclSpecifier(dummyDeclSpecifier);
|
func.setDeclSpecifier(returnType);
|
||||||
dummyDeclSpecifier.setType(IASTSimpleDeclSpecifier.t_void);
|
|
||||||
|
|
||||||
IASTStandardFunctionDeclarator createdFunctionDeclarator = extractedFunctionConstructionHelper
|
IASTStandardFunctionDeclarator createdFunctionDeclarator = extractedFunctionConstructionHelper
|
||||||
.createFunctionDeclarator(qname, info.getDeclarator(), info
|
.createFunctionDeclarator(qname, info.getDeclarator(), info
|
||||||
|
@ -688,8 +689,6 @@ public class ExtractFunctionRefactoring extends CRefactoring {
|
||||||
subRW = rewriter.insertBefore(insertpoint.getParent(), insertpoint, func, group);
|
subRW = rewriter.insertBefore(insertpoint.getParent(), insertpoint, func, group);
|
||||||
}
|
}
|
||||||
|
|
||||||
subRW.replace(dummyDeclSpecifier, getReturnType(), group);
|
|
||||||
|
|
||||||
extractedFunctionConstructionHelper.constructMethodBody(compound,
|
extractedFunctionConstructionHelper.constructMethodBody(compound,
|
||||||
container.getNodesToWrite(), subRW, group);
|
container.getNodesToWrite(), subRW, group);
|
||||||
|
|
||||||
|
|
|
@ -63,7 +63,7 @@ final class SimilarFinderVisitor extends CPPASTVisitor {
|
||||||
boolean isAllreadyInMainRefactoring = isInSelection(stmt);
|
boolean isAllreadyInMainRefactoring = isInSelection(stmt);
|
||||||
|
|
||||||
if( (!isAllreadyInMainRefactoring)
|
if( (!isAllreadyInMainRefactoring)
|
||||||
&& this.refactoring.isStatementInTrail(stmt, trail)){
|
&& this.refactoring.isStatementInTrail(stmt, trail, this.refactoring.getIndex())){
|
||||||
stmtToReplace.add(stmt);
|
stmtToReplace.add(stmt);
|
||||||
similarContainer.add(stmt);
|
similarContainer.add(stmt);
|
||||||
++i;
|
++i;
|
||||||
|
@ -99,20 +99,19 @@ final class SimilarFinderVisitor extends CPPASTVisitor {
|
||||||
}
|
}
|
||||||
|
|
||||||
if(similarOnReturnWays){
|
if(similarOnReturnWays){
|
||||||
System.out.println(6);
|
IASTNode call = refactoring.getMethodCall(name,
|
||||||
IASTNode call = refactoring.getMethodCall(name,
|
this.refactoring.nameTrail, this.refactoring.names,
|
||||||
this.refactoring.nameTrail, this.refactoring.names,
|
this.refactoring.container, similarContainer);
|
||||||
this.refactoring.container, similarContainer);
|
ASTRewrite rewrite = collector.rewriterForTranslationUnit(stmtToReplace.get(0)
|
||||||
ASTRewrite rewrite = collector.rewriterForTranslationUnit(stmtToReplace.get(0)
|
.getTranslationUnit());
|
||||||
.getTranslationUnit());
|
TextEditGroup editGroup = new TextEditGroup(Messages.SimilarFinderVisitor_replaceDuplicateCode);
|
||||||
TextEditGroup editGroup = new TextEditGroup(Messages.SimilarFinderVisitor_replaceDuplicateCode);
|
rewrite.replace(stmtToReplace.get(0), call, editGroup);
|
||||||
rewrite.replace(stmtToReplace.get(0), call, editGroup);
|
if (stmtToReplace.size() > 1) {
|
||||||
if (stmtToReplace.size() > 1) {
|
for (int i = 1; i < stmtToReplace.size(); ++i) {
|
||||||
for (int i = 1; i < stmtToReplace.size(); ++i) {
|
rewrite.remove(stmtToReplace.get(i), editGroup);
|
||||||
rewrite.remove(stmtToReplace.get(i), editGroup);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
clear();
|
clear();
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,24 +11,26 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.internal.ui.refactoring.extractfunction;
|
package org.eclipse.cdt.internal.ui.refactoring.extractfunction;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.core.dom.ast.DOMException;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
|
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
|
||||||
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.IBinding;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding;
|
||||||
|
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName;
|
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
|
||||||
|
|
||||||
import org.eclipse.cdt.internal.ui.refactoring.utils.ASTHelper;
|
import org.eclipse.cdt.internal.ui.refactoring.utils.ASTHelper;
|
||||||
|
|
||||||
class TrailName extends CPPASTName {
|
class TrailName extends ASTNode{
|
||||||
|
|
||||||
private int nameNumber;
|
private int nameNumber;
|
||||||
private IASTNode declaration = null;
|
private IASTNode declaration = null;
|
||||||
private IASTName realName = null;
|
private IASTName realName = null;
|
||||||
private boolean isGloballyQualified = false;
|
|
||||||
|
|
||||||
@Override
|
public TrailName(IASTName realName) {
|
||||||
public String getRawSignature() {
|
super();
|
||||||
return realName.getRawSignature();
|
this.realName = realName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getNameNumber() {
|
public int getNameNumber() {
|
||||||
|
@ -39,14 +41,6 @@ class TrailName extends CPPASTName {
|
||||||
this.nameNumber = nameNumber;
|
this.nameNumber = nameNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IASTNode getDeclaration() {
|
|
||||||
return declaration;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDeclaration(IASTNode declaration) {
|
|
||||||
this.declaration = declaration;
|
|
||||||
}
|
|
||||||
|
|
||||||
public IASTDeclSpecifier getDeclSpecifier() {
|
public IASTDeclSpecifier getDeclSpecifier() {
|
||||||
return ASTHelper.getDeclarationSpecifier(declaration);
|
return ASTHelper.getDeclarationSpecifier(declaration);
|
||||||
}
|
}
|
||||||
|
@ -55,20 +49,19 @@ class TrailName extends CPPASTName {
|
||||||
return realName;
|
return realName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRealName(IASTName realName) {
|
|
||||||
this.realName = realName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isGloballyQualified() {
|
public boolean isGloballyQualified() {
|
||||||
return isGloballyQualified;
|
IBinding bind = realName.resolveBinding();
|
||||||
|
try {
|
||||||
|
if (bind instanceof ICPPBinding) {
|
||||||
|
ICPPBinding cppBind = (ICPPBinding) bind;
|
||||||
|
return cppBind.isGloballyQualified();
|
||||||
|
}
|
||||||
|
} catch (DOMException e) {
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setGloballyQualified(boolean isGloballyQualified) {
|
public IASTNode copy() {
|
||||||
this.isGloballyQualified = isGloballyQualified;
|
throw new UnsupportedOperationException();
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public char[] toCharArray() {
|
|
||||||
return realName.toCharArray();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,9 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.eclipse.core.runtime.Assert;
|
import org.eclipse.core.runtime.Assert;
|
||||||
|
import org.eclipse.core.runtime.CoreException;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.core.dom.ast.DOMException;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTASMDeclaration;
|
import org.eclipse.cdt.core.dom.ast.IASTASMDeclaration;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
|
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
|
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
|
||||||
|
@ -28,6 +30,7 @@ import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier;
|
import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTExpression;
|
import org.eclipse.cdt.core.dom.ast.IASTExpression;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTFieldReference;
|
import org.eclipse.cdt.core.dom.ast.IASTFieldReference;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
|
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
|
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
import org.eclipse.cdt.core.dom.ast.IASTName;
|
||||||
|
@ -41,6 +44,7 @@ import org.eclipse.cdt.core.dom.ast.IASTStatement;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression;
|
import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
|
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
|
||||||
import org.eclipse.cdt.core.dom.ast.IBinding;
|
import org.eclipse.cdt.core.dom.ast.IBinding;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IType;
|
||||||
import org.eclipse.cdt.core.dom.ast.c.ICASTDeclSpecifier;
|
import org.eclipse.cdt.core.dom.ast.c.ICASTDeclSpecifier;
|
||||||
import org.eclipse.cdt.core.dom.ast.c.ICASTPointer;
|
import org.eclipse.cdt.core.dom.ast.c.ICASTPointer;
|
||||||
import org.eclipse.cdt.core.dom.ast.c.ICASTSimpleDeclSpecifier;
|
import org.eclipse.cdt.core.dom.ast.c.ICASTSimpleDeclSpecifier;
|
||||||
|
@ -59,24 +63,28 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTypenameExpression;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTypenameExpression;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDeclaration;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDeclaration;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTVisibilityLabel;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTVisibilityLabel;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable;
|
||||||
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTDeclSpecifier;
|
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTDeclSpecifier;
|
||||||
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTExplicitTemplateInstantiation;
|
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTExplicitTemplateInstantiation;
|
||||||
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTPointer;
|
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTPointer;
|
||||||
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTSimpleDeclSpecifier;
|
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTSimpleDeclSpecifier;
|
||||||
|
import org.eclipse.cdt.core.index.IIndex;
|
||||||
|
import org.eclipse.cdt.core.index.IIndexName;
|
||||||
|
|
||||||
import org.eclipse.cdt.internal.ui.refactoring.Container;
|
import org.eclipse.cdt.internal.ui.refactoring.Container;
|
||||||
import org.eclipse.cdt.internal.ui.refactoring.EqualityChecker;
|
import org.eclipse.cdt.internal.ui.refactoring.EqualityChecker;
|
||||||
import org.eclipse.cdt.internal.ui.refactoring.utils.ASTHelper;
|
|
||||||
|
|
||||||
public class TrailNodeEqualityChecker implements EqualityChecker<IASTNode> {
|
public class TrailNodeEqualityChecker implements EqualityChecker<IASTNode> {
|
||||||
|
|
||||||
private final Map<String, Integer> names;
|
private final Map<String, Integer> names;
|
||||||
private final Container<Integer> namesCounter;
|
private final Container<Integer> namesCounter;
|
||||||
|
private IIndex index;
|
||||||
|
|
||||||
public TrailNodeEqualityChecker(Map<String, Integer> names, Container<Integer> namesCounter) {
|
public TrailNodeEqualityChecker(Map<String, Integer> names, Container<Integer> namesCounter, IIndex index) {
|
||||||
super();
|
super();
|
||||||
this.names = names;
|
this.names = names;
|
||||||
this.namesCounter = namesCounter;
|
this.namesCounter = namesCounter;
|
||||||
|
this.index = index;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isEquals(IASTNode trailNode, IASTNode node) {
|
public boolean isEquals(IASTNode trailNode, IASTNode node) {
|
||||||
|
@ -165,13 +173,12 @@ public class TrailNodeEqualityChecker implements EqualityChecker<IASTNode> {
|
||||||
ICPPASTNamedTypeSpecifier decl = (ICPPASTNamedTypeSpecifier) node;
|
ICPPASTNamedTypeSpecifier decl = (ICPPASTNamedTypeSpecifier) node;
|
||||||
|
|
||||||
|
|
||||||
boolean isSame = isDeclSpecifierEquals(trailDecl, decl)
|
return isDeclSpecifierEquals(trailDecl, decl)
|
||||||
&& isSameNamedTypeSpecifierName(trailDecl, decl)
|
&& isSameNamedTypeSpecifierName(trailDecl, decl)
|
||||||
&& trailDecl.isTypename() == decl.isTypename()
|
&& trailDecl.isTypename() == decl.isTypename()
|
||||||
&& trailDecl.isExplicit() == decl.isExplicit()
|
&& trailDecl.isExplicit() == decl.isExplicit()
|
||||||
&& trailDecl.isFriend() == decl.isFriend()
|
&& trailDecl.isFriend() == decl.isFriend()
|
||||||
&& trailDecl.isVirtual() == decl.isVirtual();
|
&& trailDecl.isVirtual() == decl.isVirtual();
|
||||||
return isSame;
|
|
||||||
} else if (trailNode instanceof IASTNamedTypeSpecifier) {
|
} else if (trailNode instanceof IASTNamedTypeSpecifier) {
|
||||||
IASTNamedTypeSpecifier trailDecl = (IASTNamedTypeSpecifier) trailNode;
|
IASTNamedTypeSpecifier trailDecl = (IASTNamedTypeSpecifier) trailNode;
|
||||||
IASTNamedTypeSpecifier decl = (IASTNamedTypeSpecifier) node;
|
IASTNamedTypeSpecifier decl = (IASTNamedTypeSpecifier) node;
|
||||||
|
@ -409,52 +416,68 @@ public class TrailNodeEqualityChecker implements EqualityChecker<IASTNode> {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
IBinding bind = name.resolveBinding();
|
if(trailName.isGloballyQualified()) {
|
||||||
IASTName[] declNames = name.getTranslationUnit().getDeclarationsInAST(bind);
|
IBinding realBind = trailName.getRealName().resolveBinding();
|
||||||
if(declNames.length > 0){
|
IBinding nameBind = name.resolveBinding();
|
||||||
IASTNode tmpNode = ASTHelper.getDeclarationForNode(declNames[0]);
|
try {
|
||||||
|
index.acquireReadLock();
|
||||||
if(tmpNode != null){
|
IIndexName[] realDecs = index.findDeclarations(realBind);
|
||||||
if(trailName.isGloballyQualified()){
|
IIndexName[] nameDecs = index.findDeclarations(nameBind);
|
||||||
//global Node
|
if(realDecs.length == nameDecs.length) {
|
||||||
if(tmpNode.equals(trailName.getDeclaration())){
|
for(int i = 0; i < realDecs.length; ++i) {
|
||||||
return true;
|
IASTFileLocation rfl = realDecs[i].getFileLocation();
|
||||||
}
|
IASTFileLocation nfl = nameDecs[i].getFileLocation();
|
||||||
} else {
|
if(rfl.getNodeOffset() == nfl.getNodeOffset() && rfl.getFileName().equals(nfl.getFileName())) {
|
||||||
//localNode
|
continue;
|
||||||
IASTDeclSpecifier decl = ASTHelper.getDeclarationSpecifier(tmpNode);
|
}else {
|
||||||
IASTDeclSpecifier trailDecl = trailName.getDeclSpecifier();
|
return false;
|
||||||
|
|
||||||
IASTDeclarator declarator = ASTHelper.getDeclaratorForNode(declNames[0]);
|
|
||||||
IASTDeclarator trailDeclarator = ASTHelper.getDeclaratorForNode(trailName.getDeclaration());
|
|
||||||
|
|
||||||
IASTPointerOperator[] pointerOperators1 = declarator.getPointerOperators();
|
|
||||||
IASTPointerOperator[] pointerOperators2 = trailDeclarator.getPointerOperators();
|
|
||||||
|
|
||||||
if(trailDecl != null && decl != null
|
|
||||||
&& decl.getStorageClass() == trailDecl.getStorageClass()
|
|
||||||
&& ASTHelper.samePointers(pointerOperators1, pointerOperators2, this)){
|
|
||||||
if (decl instanceof IASTSimpleDeclSpecifier
|
|
||||||
&& trailDecl instanceof IASTSimpleDeclSpecifier) {
|
|
||||||
IASTSimpleDeclSpecifier simpleDecl = (IASTSimpleDeclSpecifier) decl;
|
|
||||||
IASTSimpleDeclSpecifier simpleTrailDecl = (IASTSimpleDeclSpecifier) trailDecl;
|
|
||||||
if(simpleDecl.getType() == simpleTrailDecl.getType()){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
} else if (decl instanceof IASTNamedTypeSpecifier
|
|
||||||
&& trailDecl instanceof IASTNamedTypeSpecifier) {
|
|
||||||
|
|
||||||
IASTNamedTypeSpecifier namedDecl = (IASTNamedTypeSpecifier) decl;
|
|
||||||
IASTNamedTypeSpecifier trailNamedDecl = (IASTNamedTypeSpecifier) trailDecl;
|
|
||||||
if(namedDecl.getName().getRawSignature().equals(trailNamedDecl.getName().getRawSignature())){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
}else {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
} catch (InterruptedException e) {}
|
||||||
|
catch (CoreException e) {}
|
||||||
|
finally {
|
||||||
|
index.releaseReadLock();
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
IType oType = getType(trailName.getRealName().resolveBinding());
|
||||||
|
IType nType = getType(name.resolveBinding());
|
||||||
|
if(oType.isSameType(nType)) {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private IType getType(IBinding binding) {
|
||||||
|
try {
|
||||||
|
if (binding instanceof ICPPVariable) {
|
||||||
|
ICPPVariable var = (ICPPVariable) binding;
|
||||||
|
return var.getType();
|
||||||
|
}
|
||||||
|
} catch (DOMException e) {
|
||||||
|
return new NullType();
|
||||||
|
}
|
||||||
|
return new NullType();
|
||||||
|
}
|
||||||
|
|
||||||
|
private class NullType implements IType{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object clone() {
|
||||||
|
try {
|
||||||
|
return super.clone();
|
||||||
|
} catch (CloneNotSupportedException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isSameType(IType type) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,13 +116,20 @@ public class GenerateGettersAndSettersRefactoring extends CRefactoring {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException,
|
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException,
|
||||||
OperationCanceledException {
|
OperationCanceledException {
|
||||||
RefactoringStatus finalStatus = super.checkFinalConditions(pm);
|
RefactoringStatus finalStatus = null;
|
||||||
if(!context.isImplementationInHeader()) {
|
try {
|
||||||
definitionInsertLocation = findInsertLocation();
|
lockIndex();
|
||||||
if(file.equals(definitionInsertLocation.getInsertFile())) {
|
finalStatus = super.checkFinalConditions(pm);
|
||||||
finalStatus.addInfo(Messages.GenerateGettersAndSettersRefactoring_NoImplFile);
|
if(!context.isImplementationInHeader()) {
|
||||||
|
definitionInsertLocation = findInsertLocation();
|
||||||
|
if(file.equals(definitionInsertLocation.getInsertFile())) {
|
||||||
|
finalStatus.addInfo(Messages.GenerateGettersAndSettersRefactoring_NoImplFile);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} catch (InterruptedException e) {}
|
||||||
|
finally {
|
||||||
|
unlockIndex();
|
||||||
}
|
}
|
||||||
return finalStatus;
|
return finalStatus;
|
||||||
}
|
}
|
||||||
|
@ -206,22 +213,28 @@ public class GenerateGettersAndSettersRefactoring extends CRefactoring {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void collectModifications(IProgressMonitor pm,ModificationCollector collector) throws CoreException, OperationCanceledException {
|
protected void collectModifications(IProgressMonitor pm,ModificationCollector collector) throws CoreException, OperationCanceledException {
|
||||||
ArrayList<IASTNode> getterAndSetters = new ArrayList<IASTNode>();
|
try {
|
||||||
ArrayList<IASTFunctionDefinition> definitions = new ArrayList<IASTFunctionDefinition>();
|
lockIndex();
|
||||||
for(GetterSetterInsertEditProvider currentProvider : context.selectedFunctions){
|
ArrayList<IASTNode> getterAndSetters = new ArrayList<IASTNode>();
|
||||||
if(context.isImplementationInHeader()) {
|
ArrayList<IASTFunctionDefinition> definitions = new ArrayList<IASTFunctionDefinition>();
|
||||||
getterAndSetters.add(currentProvider.getFunctionDefinition(false));
|
for(GetterSetterInsertEditProvider currentProvider : context.selectedFunctions){
|
||||||
}else {
|
if(context.isImplementationInHeader()) {
|
||||||
getterAndSetters.add(currentProvider.getFunctionDeclaration());
|
getterAndSetters.add(currentProvider.getFunctionDefinition(false));
|
||||||
definitions.add(currentProvider.getFunctionDefinition(true));
|
}else {
|
||||||
|
getterAndSetters.add(currentProvider.getFunctionDeclaration());
|
||||||
|
definitions.add(currentProvider.getFunctionDefinition(true));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
if(!context.isImplementationInHeader()) {
|
||||||
if(!context.isImplementationInHeader()) {
|
addDefinition(collector, definitions);
|
||||||
addDefinition(collector, definitions);
|
}
|
||||||
}
|
ICPPASTCompositeTypeSpecifier classDefinition = (ICPPASTCompositeTypeSpecifier) context.existingFields.get(context.existingFields.size()-1).getParent();
|
||||||
ICPPASTCompositeTypeSpecifier classDefinition = (ICPPASTCompositeTypeSpecifier) context.existingFields.get(context.existingFields.size()-1).getParent();
|
|
||||||
|
|
||||||
AddDeclarationNodeToClassChange.createChange(classDefinition, VisibilityEnum.v_public, getterAndSetters, false, collector);
|
AddDeclarationNodeToClassChange.createChange(classDefinition, VisibilityEnum.v_public, getterAndSetters, false, collector);
|
||||||
|
} catch (InterruptedException e) {}
|
||||||
|
finally {
|
||||||
|
unlockIndex();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,6 @@ 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.
|
||||||
|
@ -150,34 +149,32 @@ public class NodeHelper {
|
||||||
private static void getMethodContexWithIndex(IIndex index,
|
private static void getMethodContexWithIndex(IIndex index,
|
||||||
IASTTranslationUnit translationUnit, MethodContext context,
|
IASTTranslationUnit translationUnit, MethodContext context,
|
||||||
IASTName name) throws CoreException {
|
IASTName name) throws CoreException {
|
||||||
if(name instanceof ICPPASTQualifiedName){
|
IBinding bind = name.resolveBinding();
|
||||||
ICPPASTQualifiedName qname =( ICPPASTQualifiedName )name;
|
if (bind instanceof ICPPMethod) {
|
||||||
context.setMethodQName(qname);
|
context.setType(MethodContext.ContextType.METHOD);
|
||||||
IBinding bind = qname.resolveBinding();
|
IIndexName[] decl;
|
||||||
if (bind instanceof ICPPMethod) {
|
decl = index.findDeclarations(bind);
|
||||||
context.setType(MethodContext.ContextType.METHOD);
|
String tuFileLoc = translationUnit.getFileLocation().getFileName();
|
||||||
IIndexName[] decl;
|
if(decl.length == 0) {
|
||||||
decl = index.findDeclarations(bind);
|
context.setMethodDeclarationName(name);
|
||||||
String tuFileLoc = translationUnit.getFileLocation().getFileName();
|
}
|
||||||
for (IIndexName tmpname : decl) {
|
for (IIndexName tmpname : decl) {
|
||||||
IASTTranslationUnit locTu = translationUnit;
|
IASTTranslationUnit locTu = translationUnit;
|
||||||
if(!tuFileLoc.equals(tmpname.getFileLocation().getFileName())) {
|
if(!tuFileLoc.equals(tmpname.getFileLocation().getFileName())) {
|
||||||
locTu = TranslationUnitHelper.loadTranslationUnit(tmpname.getFileLocation().getFileName(), false);
|
locTu = TranslationUnitHelper.loadTranslationUnit(tmpname.getFileLocation().getFileName(), false);
|
||||||
}
|
}
|
||||||
IASTName declName = DeclarationFinder.findDeclarationInTranslationUnit(locTu, tmpname);
|
IASTName declName = DeclarationFinder.findDeclarationInTranslationUnit(locTu, tmpname);
|
||||||
if(declName != null) {
|
if(declName != null) {
|
||||||
IASTNode methoddefinition = declName.getParent().getParent();
|
IASTNode methoddefinition = declName.getParent().getParent();
|
||||||
if (methoddefinition instanceof IASTSimpleDeclaration) {
|
if (methoddefinition instanceof IASTSimpleDeclaration || methoddefinition instanceof IASTFunctionDefinition) {
|
||||||
context.setMethodDeclarationName(declName);
|
context.setMethodDeclarationName(declName);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}else {
|
}
|
||||||
if (name.getParent().getParent().getParent() instanceof ICPPASTCompositeTypeSpecifier) {
|
if(name instanceof ICPPASTQualifiedName){
|
||||||
context.setType(ContextType.METHOD);
|
ICPPASTQualifiedName qname =( ICPPASTQualifiedName )name;
|
||||||
context.setMethodDeclarationName(name);
|
context.setMethodQName(qname);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue