diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/FunctionFactory.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/FunctionFactory.java index e9cdba169de..fe0b65afb2f 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/FunctionFactory.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/FunctionFactory.java @@ -12,6 +12,8 @@ package org.eclipse.cdt.internal.ui.refactoring.gettersandsetters; import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression; +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator; import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition; import org.eclipse.cdt.core.dom.ast.IASTPointerOperator; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier; @@ -42,7 +44,12 @@ public class FunctionFactory { IASTFunctionDefinition getter = new CPPASTFunctionDefinition(); getter.setDeclSpecifier(fieldDeclaration.getDeclSpecifier().copy()); - getter.setDeclarator(getGetterDeclarator(varName, fieldDeclaration, name)); + IASTDeclarator getterDeclarator = getGetterDeclarator(varName, fieldDeclaration, name); + // IASTFunctionDefinition. expects the outermost IASTFunctionDeclarator in declarator hierarchy + while (!(getterDeclarator instanceof IASTFunctionDeclarator)) { + getterDeclarator = getterDeclarator.getNestedDeclarator(); + } + getter.setDeclarator((IASTFunctionDeclarator) getterDeclarator); getter.setBody(getGetterBody(varName)); @@ -61,23 +68,45 @@ public class FunctionFactory { return compound; } - private static CPPASTFunctionDeclarator getGetterDeclarator(String varName, + private static IASTDeclarator getGetterDeclarator(String varName, IASTSimpleDeclaration fieldDeclaration, ICPPASTQualifiedName name) { + CPPASTName getterName = new CPPASTName(); String varPartOfGetterName = NameHelper.makeFirstCharUpper(NameHelper.trimFieldName(varName)); getterName.setName("get".concat(varPartOfGetterName).toCharArray()); //$NON-NLS-1$ - CPPASTFunctionDeclarator declarator = new CPPASTFunctionDeclarator(); - declarator.setConst(true); + + // copy declarator hierarchy + IASTDeclarator topDeclarator = fieldDeclaration.getDeclarators()[0].copy(); + + // find the innermost declarator in hierarchy + IASTDeclarator innermost = topDeclarator; + while (innermost.getNestedDeclarator() != null) { + innermost = innermost.getNestedDeclarator(); + } + + // create a new innermost function declarator basing on the field declarator + CPPASTFunctionDeclarator functionDeclarator = new CPPASTFunctionDeclarator(); + functionDeclarator.setConst(true); if(name != null) { name.addName(getterName); - declarator.setName(name); + functionDeclarator.setName(name); }else { - declarator.setName(getterName); + functionDeclarator.setName(getterName); } - for(IASTPointerOperator pointer : fieldDeclaration.getDeclarators()[0].getPointerOperators()){ - declarator.addPointerOperator(pointer.copy()); + for(IASTPointerOperator pointer : innermost.getPointerOperators()){ + functionDeclarator.addPointerOperator(pointer.copy()); + } + + // replace innermost with functionDeclarator and return the whole declarator tree + if (innermost == topDeclarator) { + // no tree + return functionDeclarator; + } else { + IASTDeclarator parent = (IASTDeclarator) innermost.getParent(); + parent.setNestedDeclarator(functionDeclarator); + return topDeclarator; + } - return declarator; } public static IASTFunctionDefinition createSetterDefinition(String varName, IASTSimpleDeclaration fieldDeclaration, ICPPASTQualifiedName name) { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GenerateGettersAndSettersRefactoring.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GenerateGettersAndSettersRefactoring.java index 1e4aff4f2c4..1454d5d4ba0 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GenerateGettersAndSettersRefactoring.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GenerateGettersAndSettersRefactoring.java @@ -192,7 +192,11 @@ public class GenerateGettersAndSettersRefactoring extends CRefactoring { if (props.getName().contains(MEMBER_DECLARATION)) { final IASTDeclarator[] declarators = fieldDeclaration.getDeclarators(); if (declarators.length > 0) { - if ((declarators[0] instanceof IASTFunctionDeclarator)) { + IASTDeclarator innermostDeclarator = declarators[0]; + while (innermostDeclarator.getNestedDeclarator() != null) { + innermostDeclarator = innermostDeclarator.getNestedDeclarator(); + } + if ((innermostDeclarator instanceof IASTFunctionDeclarator)) { context.existingFunctionDeclarations.add(fieldDeclaration); } else { if(SelectionHelper.isInSameFile(fieldDeclaration, file)){