1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-24 09:25:31 +02:00

Bug 319111 - Generate getters and setters fails on complex field types

This commit is contained in:
Alena Laskavaia 2010-08-11 02:37:42 +00:00
parent 78fdb615a6
commit 1735b0d4ed
3 changed files with 33 additions and 9 deletions

View file

@ -15,6 +15,7 @@ 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.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
@ -128,12 +129,17 @@ public class FunctionFactory {
CPPASTLiteralExpression litExpr = new CPPASTLiteralExpression();
litExpr.setValue(Keywords.cTHIS);
fieldRef.setFieldOwner(litExpr);
fieldRef.setFieldName(fieldDeclaration.getDeclarators()[0].getName().copy());
IASTDeclarator innerDeclarator = fieldDeclaration.getDeclarators()[0];
while (innerDeclarator.getNestedDeclarator() != null) {
innerDeclarator = innerDeclarator.getNestedDeclarator();
}
IASTName fieldName = innerDeclarator.getName();
fieldRef.setFieldName(fieldName.copy());
fieldRef.setIsPointerDereference(true);
binExpr.setOperand1(fieldRef);
binExpr.setOperator(IASTBinaryExpression.op_assign);
CPPASTIdExpression idExpr = new CPPASTIdExpression();
idExpr.setName(fieldDeclaration.getDeclarators()[0].getName().copy());
idExpr.setName(fieldName.copy());
binExpr.setOperand2(idExpr);
exprStmt.setExpression(binExpr);
compound.addStatement(exprStmt);

View file

@ -18,6 +18,7 @@ import java.util.TreeSet;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
@ -55,12 +56,12 @@ public class GetterAndSetterContext implements ITreeContentProvider{
}
public GetterSetterInsertEditProvider createGetterInserter(IASTSimpleDeclaration simpleDeclaration) {
String varName = simpleDeclaration.getDeclarators()[0].getName().toString();
String varName = getFieldDeclarationName(simpleDeclaration).toString();
return new GetterSetterInsertEditProvider(varName, simpleDeclaration, Type.getter);
}
public GetterSetterInsertEditProvider createSetterInserter(IASTSimpleDeclaration simpleDeclaration) {
String varName = simpleDeclaration.getDeclarators()[0].getName().toString();
String varName = getFieldDeclarationName(simpleDeclaration).toString();
return new GetterSetterInsertEditProvider(varName, simpleDeclaration, Type.setter);
}
@ -114,17 +115,25 @@ public class GetterAndSetterContext implements ITreeContentProvider{
private FunctionWrapper getGetterForField(IASTSimpleDeclaration currentField) {
FunctionWrapper wrapper = new FunctionWrapper();
String trimmedName = NameHelper.trimFieldName(currentField.getDeclarators()[0].getName().toString());
String trimmedName = NameHelper.trimFieldName(getFieldDeclarationName(currentField).toString());
String getterName = "get" + NameHelper.makeFirstCharUpper(trimmedName); //$NON-NLS-1$
setFunctionToWrapper(wrapper, getterName);
return wrapper;
}
private IASTName getFieldDeclarationName(IASTSimpleDeclaration fieldDeclaration) {
IASTDeclarator declarator = fieldDeclaration.getDeclarators()[0];
while (declarator.getNestedDeclarator() != null) {
declarator = declarator.getNestedDeclarator();
}
return declarator.getName();
}
private FunctionWrapper getSetterForField(IASTSimpleDeclaration currentField) {
FunctionWrapper wrapper = new FunctionWrapper();
String trimmedName = NameHelper.trimFieldName(currentField.getDeclarators()[0].getName().toString());
String trimmedName = NameHelper.trimFieldName(getFieldDeclarationName(currentField).toString());
String setterName = "set" + NameHelper.makeFirstCharUpper(trimmedName); //$NON-NLS-1$
setFunctionToWrapper(wrapper, setterName);
@ -139,7 +148,7 @@ public class GetterAndSetterContext implements ITreeContentProvider{
}
for(IASTSimpleDeclaration currentDeclaration : existingFunctionDeclarations){
if(currentDeclaration.getDeclarators()[0].getName().toString().endsWith(getterName)){
if(getFieldDeclarationName(currentDeclaration).toString().endsWith(getterName)){
wrapper.functionDeclaration = currentDeclaration;
}
}
@ -154,7 +163,11 @@ public class GetterAndSetterContext implements ITreeContentProvider{
@Override
public String toString(){
return field.getDeclarators()[0].getName().toString();
IASTDeclarator declarator = field.getDeclarators()[0];
while (declarator.getNestedDeclarator() != null) {
declarator = declarator.getNestedDeclarator();
}
return declarator.getName().toString();
}
public ArrayList<GetterSetterInsertEditProvider> getChildNodes() {

View file

@ -12,6 +12,7 @@
package org.eclipse.cdt.internal.ui.refactoring.gettersandsetters;
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
@ -52,7 +53,11 @@ public class GetterSetterInsertEditProvider implements Comparable<GetterSetterIn
@Override
public String toString(){
return functionDeclaration.getDeclarators()[0].getName().toString();
IASTDeclarator declarator = functionDeclaration.getDeclarators()[0];
while (declarator.getNestedDeclarator() != null) {
declarator = declarator.getNestedDeclarator();
}
return declarator.getName().toString();
}
public IASTFunctionDefinition getFunctionDefinition(boolean qualifedName) {