mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Bug 319273 Generate getters and setters can't handle function pointers, patch from Tomasz Wesolowski
This commit is contained in:
parent
0db4efab5f
commit
f3cae35c1d
2 changed files with 43 additions and 10 deletions
|
@ -12,6 +12,8 @@
|
||||||
package org.eclipse.cdt.internal.ui.refactoring.gettersandsetters;
|
package org.eclipse.cdt.internal.ui.refactoring.gettersandsetters;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
|
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.IASTFunctionDefinition;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
|
import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
|
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
|
||||||
|
@ -42,7 +44,12 @@ public class FunctionFactory {
|
||||||
IASTFunctionDefinition getter = new CPPASTFunctionDefinition();
|
IASTFunctionDefinition getter = new CPPASTFunctionDefinition();
|
||||||
|
|
||||||
getter.setDeclSpecifier(fieldDeclaration.getDeclSpecifier().copy());
|
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));
|
getter.setBody(getGetterBody(varName));
|
||||||
|
|
||||||
|
@ -61,23 +68,45 @@ public class FunctionFactory {
|
||||||
return compound;
|
return compound;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static CPPASTFunctionDeclarator getGetterDeclarator(String varName,
|
private static IASTDeclarator getGetterDeclarator(String varName,
|
||||||
IASTSimpleDeclaration fieldDeclaration, ICPPASTQualifiedName name) {
|
IASTSimpleDeclaration fieldDeclaration, ICPPASTQualifiedName name) {
|
||||||
|
|
||||||
CPPASTName getterName = new CPPASTName();
|
CPPASTName getterName = new CPPASTName();
|
||||||
String varPartOfGetterName = NameHelper.makeFirstCharUpper(NameHelper.trimFieldName(varName));
|
String varPartOfGetterName = NameHelper.makeFirstCharUpper(NameHelper.trimFieldName(varName));
|
||||||
getterName.setName("get".concat(varPartOfGetterName).toCharArray()); //$NON-NLS-1$
|
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) {
|
if(name != null) {
|
||||||
name.addName(getterName);
|
name.addName(getterName);
|
||||||
declarator.setName(name);
|
functionDeclarator.setName(name);
|
||||||
}else {
|
}else {
|
||||||
declarator.setName(getterName);
|
functionDeclarator.setName(getterName);
|
||||||
}
|
}
|
||||||
for(IASTPointerOperator pointer : fieldDeclaration.getDeclarators()[0].getPointerOperators()){
|
for(IASTPointerOperator pointer : innermost.getPointerOperators()){
|
||||||
declarator.addPointerOperator(pointer.copy());
|
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) {
|
public static IASTFunctionDefinition createSetterDefinition(String varName, IASTSimpleDeclaration fieldDeclaration, ICPPASTQualifiedName name) {
|
||||||
|
|
|
@ -192,7 +192,11 @@ public class GenerateGettersAndSettersRefactoring extends CRefactoring {
|
||||||
if (props.getName().contains(MEMBER_DECLARATION)) {
|
if (props.getName().contains(MEMBER_DECLARATION)) {
|
||||||
final IASTDeclarator[] declarators = fieldDeclaration.getDeclarators();
|
final IASTDeclarator[] declarators = fieldDeclaration.getDeclarators();
|
||||||
if (declarators.length > 0) {
|
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);
|
context.existingFunctionDeclarations.add(fieldDeclaration);
|
||||||
} else {
|
} else {
|
||||||
if(SelectionHelper.isInSameFile(fieldDeclaration, file)){
|
if(SelectionHelper.isInSameFile(fieldDeclaration, file)){
|
||||||
|
|
Loading…
Add table
Reference in a new issue