Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergey Prigogin2011-12-29 02:05:01 +0000
committerSergey Prigogin2011-12-29 02:10:22 +0000
commitfbcb28a05b14d5a808f1b9df756f397e984f9977 (patch)
tree4207494f7f9a3d311073116e6f6f28a123346daf
parent302e64aebcadeca88c3712baca0d39a8b6e54a6e (diff)
downloadorg.eclipse.cdt-fbcb28a05b14d5a808f1b9df756f397e984f9977.tar.gz
org.eclipse.cdt-fbcb28a05b14d5a808f1b9df756f397e984f9977.tar.xz
org.eclipse.cdt-fbcb28a05b14d5a808f1b9df756f397e984f9977.zip
Bug 352258 - Generate Getters and Setters can generate code containing
reserved keywords.
-rw-r--r--core/org.eclipse.cdt.ui.tests/resources/refactoring/GenerateGettersAndSetters.rts51
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/gettersandsetters/GenerateGettersAndSettersTest.java36
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/AccessorDescriptor.java123
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/AccessorFactory.java277
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/FieldDescriptor.java121
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GenerateGettersAndSettersInputPage.java41
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GenerateGettersAndSettersRefactoring.java10
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterContext.java201
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterFactory.java235
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterInsertEditProvider.java110
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterLabelProvider.java3
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterNameGenerator.java70
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/Messages.java10
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/Messages.properties10
14 files changed, 720 insertions, 578 deletions
diff --git a/core/org.eclipse.cdt.ui.tests/resources/refactoring/GenerateGettersAndSetters.rts b/core/org.eclipse.cdt.ui.tests/resources/refactoring/GenerateGettersAndSetters.rts
index bac0e169ad0..faac2f0f1cb 100644
--- a/core/org.eclipse.cdt.ui.tests/resources/refactoring/GenerateGettersAndSetters.rts
+++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/GenerateGettersAndSetters.rts
@@ -467,14 +467,14 @@ public:
return name;
}
- int getSystemId() const {
- return systemId;
- }
-
void setName(char* name) {
this->name = name;
}
+ int getSystemId() const {
+ return systemId;
+ }
+
void setSystemId(int systemId) {
this->systemId = systemId;
}
@@ -542,14 +542,14 @@ public:
return i;
}
- bool isOk() const {
- return ok;
- }
-
void setI(int i) {
this->i = i;
}
+ bool isOk() const {
+ return ok;
+ }
+
void setOk(bool ok) {
this->ok = ok;
}
@@ -1451,7 +1451,7 @@ public:
int test;
};
#endif /* A_H_ */
-//!Bug ??? - Getter for an array field
+//!Bug 319278 - Getter for an array field
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
//@.config
filename=A.h
@@ -1479,3 +1479,36 @@ public:
}
};
#endif /* A_H_ */
+//!Bug 352258 - Avoiding reserved names
+//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
+//@.config
+filename=A.h
+getters=mClass
+setters=mClass
+//@A.h
+#ifndef A_H_
+#define A_H_
+
+class getClass {
+private:
+ int /*$*/mClass/*$$*/;
+};
+#endif /* A_H_ */
+//=
+#ifndef A_H_
+#define A_H_
+
+class getClass {
+private:
+ int mClass;
+
+public:
+ int getClass1() const {
+ return mClass;
+ }
+
+ void setClass(int _class) {
+ mClass = _class;
+ }
+};
+#endif /* A_H_ */
diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/gettersandsetters/GenerateGettersAndSettersTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/gettersandsetters/GenerateGettersAndSettersTest.java
index 01d43779f2f..6b9cbf91d44 100644
--- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/gettersandsetters/GenerateGettersAndSettersTest.java
+++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/gettersandsetters/GenerateGettersAndSettersTest.java
@@ -13,9 +13,7 @@
******************************************************************************/
package org.eclipse.cdt.ui.tests.refactoring.gettersandsetters;
-import java.util.ArrayList;
import java.util.Collection;
-import java.util.List;
import java.util.Properties;
import org.eclipse.core.resources.IFile;
@@ -23,12 +21,12 @@ import org.eclipse.core.runtime.CoreException;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
-import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.ui.tests.refactoring.RefactoringTest;
import org.eclipse.cdt.ui.tests.refactoring.TestSourceFile;
+import org.eclipse.cdt.internal.ui.refactoring.gettersandsetters.AccessorDescriptor.AccessorKind;
import org.eclipse.cdt.internal.ui.refactoring.gettersandsetters.GenerateGettersAndSettersRefactoring;
import org.eclipse.cdt.internal.ui.refactoring.gettersandsetters.GetterSetterContext;
@@ -38,11 +36,11 @@ import org.eclipse.cdt.internal.ui.refactoring.gettersandsetters.GetterSetterCon
public class GenerateGettersAndSettersTest extends RefactoringTest {
protected boolean fatalError;
private int warnings;
- private List<String> selectedGetters;
- private List<String> selectedSetters;
+ private int infos;
+ private String[] selectedGetters;
+ private String[] selectedSetters;
private GenerateGettersAndSettersRefactoring refactoring;
private boolean definitionSeparate;
- private int infos;
/**
* @param name
@@ -88,17 +86,11 @@ public class GenerateGettersAndSettersTest extends RefactoringTest {
private void selectFields() {
GetterSetterContext context = refactoring.getContext();
- for (IASTSimpleDeclaration currentDecl : context.existingFields) {
- String name = currentDecl.getDeclarators()[0].getName().getRawSignature();
- if (selectedGetters.contains(name)) {
- selectedGetters.remove(name);
- context.selectedFunctions.add(context.createGetterInserter(currentDecl));
- }
-
- if (selectedSetters.contains(name)) {
- selectedSetters.remove(name);
- context.selectedFunctions.add(context.createSetterInserter(currentDecl));
- }
+ for (String name : selectedGetters) {
+ context.selectAccessorForField(name, AccessorKind.GETTER);
+ }
+ for (String name : selectedSetters) {
+ context.selectAccessorForField(name, AccessorKind.SETTER);
}
}
@@ -111,13 +103,7 @@ public class GenerateGettersAndSettersTest extends RefactoringTest {
String setters = refactoringProperties.getProperty("setters", ""); //$NON-NLS-1$ //$NON-NLS-2$
definitionSeparate = Boolean.valueOf(refactoringProperties.getProperty("definitionSeparate", "false"));
- selectedGetters = new ArrayList<String>();
- for (String getterName : getters.split(",")) { //$NON-NLS-1$
- selectedGetters.add(getterName);
- }
- selectedSetters = new ArrayList<String>();
- for (String setterName : setters.split(",")) { //$NON-NLS-1$
- selectedSetters.add(setterName);
- }
+ selectedGetters = getters.split(",");
+ selectedSetters = setters.split(",");
}
}
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/AccessorDescriptor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/AccessorDescriptor.java
new file mode 100644
index 00000000000..45beb6d1d3c
--- /dev/null
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/AccessorDescriptor.java
@@ -0,0 +1,123 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Google, Inc 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:
+ * Sergey Prigogin (Google) - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.cdt.internal.ui.refactoring.gettersandsetters;
+
+import com.ibm.icu.text.Collator;
+
+import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
+import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
+import org.eclipse.cdt.core.dom.ast.IASTName;
+import org.eclipse.cdt.core.dom.ast.IASTNode;
+import org.eclipse.cdt.core.dom.ast.IASTNode.CopyStyle;
+import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
+
+import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTQualifiedName;
+
+public class AccessorDescriptor implements Comparable<AccessorDescriptor> {
+ public enum AccessorKind {
+ GETTER,
+ SETTER;
+ }
+
+ private static final Collator collator = Collator.getInstance();
+
+ private final AccessorKind kind;
+ private final String accessorName;
+ private final IASTName fieldName;
+ private AccessorFactory accessorFactory;
+ private IASTSimpleDeclaration accessorDeclaration;
+ private IASTSimpleDeclaration existingAccessorDeclaration;
+ private IASTFunctionDefinition existingAccessorDefinition;
+
+ public AccessorDescriptor(AccessorKind kind, String accessorName, FieldDescriptor fieldDescriptor) {
+ GetterSetterContext context = fieldDescriptor.getContext();
+ this.kind = kind;
+ this.accessorName = accessorName;
+ this.fieldName = fieldDescriptor.getFieldName();
+ if (accessorName != null) {
+ this.accessorFactory = AccessorFactory.createFactory(kind, fieldName,
+ fieldDescriptor.getFieldDeclaration(), accessorName);
+ this.accessorDeclaration = accessorFactory.createDeclaration();
+
+ for (IASTFunctionDefinition currentDefinition : context.existingFunctionDefinitions) {
+ if (currentDefinition.getDeclarator().getName().toString().equals(accessorName)) {
+ existingAccessorDefinition = currentDefinition;
+ }
+ }
+ for (IASTSimpleDeclaration currentDeclaration : context.existingFunctionDeclarations) {
+ if (GetterSetterContext.getDeclarationName(currentDeclaration).toString().equals(accessorName)) {
+ existingAccessorDeclaration = currentDeclaration;
+ }
+ }
+ }
+ }
+
+ boolean canBeGenerated() {
+ return accessorName != null && existingAccessorDeclaration == null && existingAccessorDefinition == null;
+ }
+
+ public AccessorKind getKind() {
+ return kind;
+ }
+
+ public IASTName getFieldName() {
+ return fieldName;
+ }
+
+ @Override
+ public int compareTo(AccessorDescriptor other) {
+ int c = collator.compare(fieldName.toString(), other.fieldName.toString());
+ if (c != 0)
+ return c;
+ return kind.ordinal() - other.kind.ordinal();
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (!(obj instanceof AccessorDescriptor))
+ return false;
+ AccessorDescriptor other = (AccessorDescriptor) obj;
+ return fieldName == other.fieldName && kind == other.kind;
+ }
+
+ @Override
+ public String toString() {
+ return accessorName;
+ }
+
+ public IASTSimpleDeclaration getAccessorDeclaration() {
+ return accessorDeclaration;
+ }
+
+ public IASTFunctionDefinition getAccessorDefinition(boolean qualifedName) {
+ ICPPASTQualifiedName qname;
+ if (qualifedName) {
+ qname = getClassName();
+ } else {
+ qname = null;
+ }
+
+ return accessorFactory.createDefinition(qname);
+ }
+
+ private ICPPASTQualifiedName getClassName() {
+ IASTNode node = fieldName.getParent();
+ while (!(node instanceof IASTCompositeTypeSpecifier)) {
+ node = node.getParent();
+ }
+ IASTCompositeTypeSpecifier comp = (IASTCompositeTypeSpecifier) node;
+
+ CPPASTQualifiedName qname = new CPPASTQualifiedName();
+ qname.addName(comp.getName().copy(CopyStyle.withLocations));
+ return qname;
+ }
+}
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/AccessorFactory.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/AccessorFactory.java
new file mode 100644
index 00000000000..9ee966e281a
--- /dev/null
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/AccessorFactory.java
@@ -0,0 +1,277 @@
+/*******************************************************************************
+ * Copyright (c) 2008, 2011 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 - initial API and implementation
+ * Sergey Prigogin (Google)
+ *******************************************************************************/
+package org.eclipse.cdt.internal.ui.refactoring.gettersandsetters;
+
+import java.util.Arrays;
+
+import org.eclipse.cdt.core.dom.ast.IASTArrayDeclarator;
+import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
+import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
+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.IASTNode.CopyStyle;
+import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
+import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
+import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
+import org.eclipse.cdt.core.dom.ast.IType;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTArrayDeclarator;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
+import org.eclipse.cdt.core.dom.rewrite.TypeHelper;
+import org.eclipse.cdt.core.parser.Keywords;
+
+import org.eclipse.cdt.internal.core.dom.parser.c.CASTDeclarator;
+import org.eclipse.cdt.internal.core.dom.parser.c.CASTPointer;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTBinaryExpression;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTCompoundStatement;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTDeclarator;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTExpressionStatement;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFieldReference;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDefinition;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTIdExpression;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTLiteralExpression;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTParameterDeclaration;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTPointer;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTReferenceOperator;
+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.semantics.CPPVisitor;
+
+import org.eclipse.cdt.internal.ui.refactoring.gettersandsetters.AccessorDescriptor.AccessorKind;
+
+public abstract class AccessorFactory {
+ protected final IASTName fieldName;
+ protected final IASTSimpleDeclaration fieldDeclaration;
+ protected final String accessorName;
+ protected boolean passByReference;
+
+ public static AccessorFactory createFactory(AccessorKind kind, IASTName fieldName,
+ IASTSimpleDeclaration fieldDeclaration, String accessorName) {
+ if (kind == AccessorKind.GETTER) {
+ return new GetterFactory(fieldName, fieldDeclaration, accessorName);
+ } else {
+ return new SetterFactory(fieldName, fieldDeclaration, accessorName);
+ }
+ }
+
+ protected AccessorFactory(IASTName fieldName, IASTSimpleDeclaration fieldDeclaration,
+ String accessorName) {
+ this.fieldName = fieldName;
+ this.fieldDeclaration = fieldDeclaration;
+ this.accessorName = accessorName;
+ IType type = CPPVisitor.createType(fieldDeclaration.getDeclSpecifier());
+ passByReference = TypeHelper.shouldBePassedByReference(type, fieldDeclaration.getTranslationUnit());
+ }
+
+ /**
+ * Creates an accessor declaration.
+ */
+ public abstract IASTSimpleDeclaration createDeclaration();
+
+ /**
+ * Creates an accessor definition.
+ *
+ * @param className qualified name of the class containing the accessor
+ */
+ public abstract IASTFunctionDefinition createDefinition(ICPPASTQualifiedName className);
+
+ protected IASTDeclSpecifier getParamOrReturnDeclSpecifier() {
+ IASTDeclSpecifier declSpec = fieldDeclaration.getDeclSpecifier().copy(CopyStyle.withLocations);
+ if (passByReference || fieldDeclaration.getDeclarators()[0] instanceof IASTArrayDeclarator) {
+ declSpec.setConst(true);
+ }
+ return declSpec;
+ }
+
+ private static class GetterFactory extends AccessorFactory {
+ GetterFactory(IASTName fieldName, IASTSimpleDeclaration fieldDeclaration, String getterName) {
+ super(fieldName, fieldDeclaration, getterName);
+ }
+
+ @Override
+ public IASTSimpleDeclaration createDeclaration() {
+ IASTSimpleDeclaration getter = new CPPASTSimpleDeclaration();
+ getter.setDeclSpecifier(getParamOrReturnDeclSpecifier());
+ getter.addDeclarator(getGetterDeclarator(null));
+ return getter;
+ }
+
+ @Override
+ public IASTFunctionDefinition createDefinition(ICPPASTQualifiedName className) {
+ IASTFunctionDefinition getter = new CPPASTFunctionDefinition();
+
+ getter.setDeclSpecifier(getParamOrReturnDeclSpecifier());
+ IASTDeclarator getterDeclarator = getGetterDeclarator(className);
+ // IASTFunctionDefinition expects the outermost IASTFunctionDeclarator in declarator hierarchy
+ while (!(getterDeclarator instanceof IASTFunctionDeclarator)) {
+ getterDeclarator = getterDeclarator.getNestedDeclarator();
+ }
+ getter.setDeclarator((IASTFunctionDeclarator) getterDeclarator);
+ getter.setBody(getGetterBody());
+ return getter;
+ }
+
+ private CPPASTCompoundStatement getGetterBody() {
+ CPPASTCompoundStatement compound = new CPPASTCompoundStatement();
+ CPPASTReturnStatement returnStatement = new CPPASTReturnStatement();
+ CPPASTIdExpression idExpr = new CPPASTIdExpression();
+ CPPASTName returnVal = new CPPASTName();
+ returnVal.setName(fieldName.toCharArray());
+ idExpr.setName(returnVal);
+ returnStatement.setReturnValue(idExpr);
+ compound.addStatement(returnStatement);
+ return compound;
+ }
+
+ private IASTDeclarator getGetterDeclarator(ICPPASTQualifiedName qualifiedName) {
+ CPPASTName getterName = new CPPASTName();
+ getterName.setName(accessorName.toCharArray());
+
+ // Copy declarator hierarchy
+ IASTDeclarator topDeclarator = fieldDeclaration.getDeclarators()[0].copy(CopyStyle.withLocations);
+
+ if (topDeclarator instanceof IASTArrayDeclarator) {
+ boolean isCpp = topDeclarator instanceof ICPPASTArrayDeclarator;
+ IASTDeclarator decl = isCpp ? new CPPASTDeclarator() : new CASTDeclarator();
+ decl.setName(topDeclarator.getName());
+ decl.setNestedDeclarator(topDeclarator.getNestedDeclarator());
+ decl.addPointerOperator(isCpp ? new CPPASTPointer() : new CASTPointer());
+ for (IASTPointerOperator pointer : topDeclarator.getPointerOperators()) {
+ decl.addPointerOperator(pointer);
+ }
+ topDeclarator = decl;
+ }
+ // Find the innermost declarator in hierarchy
+ IASTDeclarator innermost = topDeclarator;
+ while (innermost.getNestedDeclarator() != null) {
+ innermost = innermost.getNestedDeclarator();
+ }
+
+ // Create a new innermost function declarator based on the field declarator
+ CPPASTFunctionDeclarator functionDeclarator = new CPPASTFunctionDeclarator();
+ functionDeclarator.setConst(true);
+ if (qualifiedName != null) {
+ qualifiedName.addName(getterName);
+ functionDeclarator.setName(qualifiedName);
+ } else {
+ functionDeclarator.setName(getterName);
+ }
+ for (IASTPointerOperator pointer : innermost.getPointerOperators()){
+ functionDeclarator.addPointerOperator(pointer.copy(CopyStyle.withLocations));
+ }
+ if (passByReference) {
+ functionDeclarator.addPointerOperator(new CPPASTReferenceOperator(false));
+ }
+
+ // Replace the 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;
+ }
+ }
+ }
+
+ private static class SetterFactory extends AccessorFactory {
+ SetterFactory(IASTName fieldName, IASTSimpleDeclaration fieldDeclaration, String setterName) {
+ super(fieldName, fieldDeclaration, setterName);
+ }
+
+ @Override
+ public IASTSimpleDeclaration createDeclaration() {
+ IASTSimpleDeclaration setter = new CPPASTSimpleDeclaration();
+ setter.setDeclSpecifier(getVoidDeclSpec());
+ setter.addDeclarator(getSetterDeclarator(null));
+ return setter;
+ }
+
+ @Override
+ public IASTFunctionDefinition createDefinition(ICPPASTQualifiedName className) {
+ IASTFunctionDefinition setter = new CPPASTFunctionDefinition();
+ setter.setDeclSpecifier(getVoidDeclSpec());
+ setter.setDeclarator(getSetterDeclarator(className));
+ setter.setBody(getSetterBody());
+ return setter;
+ }
+
+ private CPPASTCompoundStatement getSetterBody() {
+ CPPASTCompoundStatement compound = new CPPASTCompoundStatement();
+ CPPASTExpressionStatement exprStmt = new CPPASTExpressionStatement();
+ CPPASTBinaryExpression binExpr = new CPPASTBinaryExpression();
+ IASTDeclarator innerDeclarator = fieldDeclaration.getDeclarators()[0];
+ while (innerDeclarator.getNestedDeclarator() != null) {
+ innerDeclarator = innerDeclarator.getNestedDeclarator();
+ }
+ IASTName fieldName = innerDeclarator.getName();
+ CPPASTName parameterName = getSetterParameterName();
+ if (Arrays.equals(fieldName.getSimpleID(), parameterName.getSimpleID())) {
+ CPPASTFieldReference fieldRef = new CPPASTFieldReference();
+ CPPASTLiteralExpression litExpr = new CPPASTLiteralExpression();
+ litExpr.setValue(Keywords.cTHIS);
+ fieldRef.setFieldOwner(litExpr);
+ fieldRef.setIsPointerDereference(true);
+ fieldRef.setFieldName(fieldName.copy(CopyStyle.withLocations));
+ binExpr.setOperand1(fieldRef);
+ } else {
+ CPPASTIdExpression idExpr = new CPPASTIdExpression(fieldName.copy(CopyStyle.withLocations));
+ binExpr.setOperand1(idExpr);
+ }
+ binExpr.setOperator(IASTBinaryExpression.op_assign);
+ CPPASTIdExpression idExpr = new CPPASTIdExpression(parameterName);
+ binExpr.setOperand2(idExpr);
+ exprStmt.setExpression(binExpr);
+ compound.addStatement(exprStmt);
+ return compound;
+ }
+
+ private CPPASTFunctionDeclarator getSetterDeclarator(ICPPASTQualifiedName qualifiedName) {
+ CPPASTName setterName = new CPPASTName();
+ setterName.setName(accessorName.toCharArray());
+ CPPASTFunctionDeclarator declarator = new CPPASTFunctionDeclarator();
+ if (qualifiedName != null) {
+ qualifiedName.addName(setterName);
+ declarator.setName(qualifiedName);
+ } else {
+ declarator.setName(setterName);
+ }
+ CPPASTParameterDeclaration parameterDeclaration = new CPPASTParameterDeclaration();
+ IASTDeclarator parameterDeclarator = fieldDeclaration.getDeclarators()[0].copy(CopyStyle.withLocations);
+ parameterDeclarator.setName(getSetterParameterName());
+ if (passByReference) {
+ parameterDeclarator.addPointerOperator(new CPPASTReferenceOperator(false));
+ }
+ parameterDeclaration.setDeclarator(parameterDeclarator);
+ parameterDeclaration.setDeclSpecifier(getParamOrReturnDeclSpecifier());
+ declarator.addParameterDeclaration(parameterDeclaration.copy(CopyStyle.withLocations));
+ return declarator;
+ }
+
+ private CPPASTName getSetterParameterName() {
+ String parameterName = GetterSetterNameGenerator.generateSetterParameterName(fieldName);
+ return new CPPASTName(parameterName.toCharArray());
+ }
+
+ private static CPPASTSimpleDeclSpecifier getVoidDeclSpec() {
+ CPPASTSimpleDeclSpecifier declSpecifier = new CPPASTSimpleDeclSpecifier();
+ declSpecifier.setType(IASTSimpleDeclSpecifier.t_void);
+ return declSpecifier;
+ }
+ }
+}
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/FieldDescriptor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/FieldDescriptor.java
new file mode 100644
index 00000000000..c627a0eee34
--- /dev/null
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/FieldDescriptor.java
@@ -0,0 +1,121 @@
+package org.eclipse.cdt.internal.ui.refactoring.gettersandsetters;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
+import org.eclipse.cdt.core.dom.ast.IASTName;
+import org.eclipse.cdt.core.dom.ast.IASTNode;
+import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
+import org.eclipse.cdt.core.dom.ast.IArrayType;
+import org.eclipse.cdt.core.dom.ast.IBinding;
+import org.eclipse.cdt.core.dom.ast.IPointerType;
+import org.eclipse.cdt.core.dom.ast.IQualifierType;
+import org.eclipse.cdt.core.dom.ast.IType;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPField;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPReferenceType;
+
+import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil;
+
+import org.eclipse.cdt.internal.ui.refactoring.gettersandsetters.AccessorDescriptor.AccessorKind;
+
+class FieldDescriptor {
+ private final IASTName fieldName;
+ private final IASTSimpleDeclaration fieldDeclaration;
+ private final AccessorDescriptor getter;
+ private final AccessorDescriptor setter;
+ private final AccessorDescriptor[] childNodes;
+ private final GetterSetterContext context;
+
+ FieldDescriptor(IASTSimpleDeclaration fieldDeclaration, GetterSetterContext context) {
+ this.fieldName = GetterSetterContext.getDeclarationName(fieldDeclaration);
+ this.fieldDeclaration = fieldDeclaration;
+ this.context = context;
+ Set<String> namesToAvoid = getNamesToAvoid();
+ String name = GetterSetterNameGenerator.generateGetterName(fieldName, namesToAvoid);
+ this.getter = new AccessorDescriptor(AccessorKind.GETTER, name, this);
+ name = GetterSetterNameGenerator.generateSetterName(fieldName, namesToAvoid);
+ if (!isAssignable(fieldDeclaration))
+ name = null;
+ this.setter = new AccessorDescriptor(AccessorKind.SETTER, name, this);
+
+ List<AccessorDescriptor> children = new ArrayList<AccessorDescriptor>(2);
+ if (getter.canBeGenerated()) {
+ children.add(getter);
+ }
+ if (setter.canBeGenerated()) {
+ children.add(setter);
+ }
+ childNodes = children.toArray(new AccessorDescriptor[children.size()]);
+ }
+
+ private Set<String> getNamesToAvoid() {
+ Set<String> namesToAvoid = new HashSet<String>();
+ // Add field names.
+ for (IASTSimpleDeclaration fieldDeclaration : context.existingFields) {
+ namesToAvoid.add(String.valueOf(GetterSetterContext.getDeclarationName(fieldDeclaration).getSimpleID()));
+ }
+ // Add constructor name.
+ if (!context.existingFields.isEmpty()) {
+ IASTNode node = context.existingFields.get(0).getParent();
+ while (!(node instanceof IASTCompositeTypeSpecifier)) {
+ node = node.getParent();
+ }
+ IASTCompositeTypeSpecifier comp = (IASTCompositeTypeSpecifier) node;
+ namesToAvoid.add(String.valueOf(comp.getName().getLastName().getSimpleID()));
+ }
+ return namesToAvoid;
+ }
+
+ private static boolean isAssignable(IASTSimpleDeclaration declaration) {
+ IASTName name = GetterSetterContext.getDeclarationName(declaration);
+ IBinding binding = name.resolveBinding();
+ if (!(binding instanceof ICPPField))
+ return false;
+ ICPPField field = (ICPPField) binding;
+ IType type = field.getType();
+ type = SemanticUtil.getNestedType(type, SemanticUtil.TDEF);
+ if (type instanceof IArrayType || type instanceof ICPPReferenceType)
+ return false;
+ if (type instanceof IPointerType && ((IPointerType) type).isConst())
+ return false;
+ if (type instanceof IQualifierType && ((IQualifierType) type).isConst())
+ return false;
+ return true;
+ }
+
+ @Override
+ public String toString() {
+ return fieldName.toString();
+ }
+
+ AccessorDescriptor[] getChildNodes() {
+ return childNodes;
+ }
+
+ boolean missingGetterOrSetter() {
+ return getter.canBeGenerated() || setter.canBeGenerated();
+ }
+
+ public GetterSetterContext getContext() {
+ return context;
+ }
+
+ public IASTName getFieldName() {
+ return fieldName;
+ }
+
+ public IASTSimpleDeclaration getFieldDeclaration() {
+ return fieldDeclaration;
+ }
+
+ public AccessorDescriptor getGetter() {
+ return getter;
+ }
+
+ public AccessorDescriptor getSetter() {
+ return setter;
+ }
+}
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GenerateGettersAndSettersInputPage.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GenerateGettersAndSettersInputPage.java
index 3f4877de98a..857a1dff291 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GenerateGettersAndSettersInputPage.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GenerateGettersAndSettersInputPage.java
@@ -33,8 +33,7 @@ import org.eclipse.ui.dialogs.PreferencesUtil;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.internal.ui.preferences.NameStylePreferencePage;
-import org.eclipse.cdt.internal.ui.refactoring.gettersandsetters.GetterSetterContext.FieldWrapper;
-import org.eclipse.cdt.internal.ui.refactoring.gettersandsetters.GetterSetterInsertEditProvider.AccessorKind;
+import org.eclipse.cdt.internal.ui.refactoring.gettersandsetters.AccessorDescriptor.AccessorKind;
public class GenerateGettersAndSettersInputPage extends UserInputWizardPage implements IPreferenceChangeListener {
private GetterSetterContext context;
@@ -42,7 +41,7 @@ public class GenerateGettersAndSettersInputPage extends UserInputWizardPage impl
private GetterSetterLabelProvider labelProvider;
public GenerateGettersAndSettersInputPage(GetterSetterContext context) {
- super(Messages.GettersAndSetters_Name);
+ super(Messages.GenerateGettersAndSettersInputPage_Name);
this.context = context;
IEclipsePreferences node = InstanceScope.INSTANCE.getNode(CUIPlugin.PLUGIN_ID);
// We are listening for changes in the Name Style preferences
@@ -51,8 +50,8 @@ public class GenerateGettersAndSettersInputPage extends UserInputWizardPage impl
@Override
public void createControl(Composite parent) {
- setTitle(Messages.GettersAndSetters_Name);
- setMessage(Messages.GenerateGettersAndSettersInputPage_header);
+ setTitle(Messages.GenerateGettersAndSettersInputPage_Name);
+ setMessage(Messages.GenerateGettersAndSettersInputPage_Header);
Composite comp = new Composite(parent, SWT.NONE);
comp.setLayout(new GridLayout(2, false));
@@ -149,15 +148,15 @@ public class GenerateGettersAndSettersInputPage extends UserInputWizardPage impl
return btComp;
}
- private void selectAccessors(AccessorKind type) {
+ private void selectAccessors(AccessorKind kind) {
for (Object treeItem : context.getElements(null)) {
- if (treeItem instanceof FieldWrapper) {
- FieldWrapper field = (FieldWrapper) treeItem;
+ if (treeItem instanceof FieldDescriptor) {
+ FieldDescriptor field = (FieldDescriptor) treeItem;
Object[] children = context.getChildren(field);
for (Object child : children) {
- if (child instanceof GetterSetterInsertEditProvider) {
- GetterSetterInsertEditProvider accessor = (GetterSetterInsertEditProvider) child;
- if (accessor.getType() == type) {
+ if (child instanceof AccessorDescriptor) {
+ AccessorDescriptor accessor = (AccessorDescriptor) child;
+ if (accessor.getKind() == kind) {
variableSelectionView.setChecked(accessor, true);
}
}
@@ -174,11 +173,11 @@ public class GenerateGettersAndSettersInputPage extends UserInputWizardPage impl
variableSelectionView.setLabelProvider(labelProvider);
variableSelectionView.setAutoExpandLevel(3);
- variableSelectionView.setInput(""); //$NON-NLS-1$
+ variableSelectionView.setInput(context);
if (context.selectedName != null) {
String rawSignature = context.selectedName.getRawSignature();
for (Object obj : variableSelectionView.getVisibleExpandedElements()) {
- if (obj instanceof FieldWrapper) {
+ if (obj instanceof FieldDescriptor) {
if (obj.toString().contains(rawSignature)) {
variableSelectionView.setSubtreeChecked(obj, true);
}
@@ -197,13 +196,13 @@ public class GenerateGettersAndSettersInputPage extends UserInputWizardPage impl
}
private void updateSelectedFunctions() {
- context.selectedFunctions.clear();
+ context.selectedAccessors.clear();
for (Object element : variableSelectionView.getCheckedElements()) {
- if (element instanceof GetterSetterInsertEditProvider) {
- context.selectedFunctions.add((GetterSetterInsertEditProvider) element);
+ if (element instanceof AccessorDescriptor) {
+ context.selectedAccessors.add((AccessorDescriptor) element);
}
}
- setPageComplete(!context.selectedFunctions.isEmpty());
+ setPageComplete(!context.selectedAccessors.isEmpty());
}
@Override
@@ -213,15 +212,19 @@ public class GenerateGettersAndSettersInputPage extends UserInputWizardPage impl
}
if (GetterSetterNameGenerator.getGenerateGetterSettersPreferenceKeys().contains(event.getKey())) {
- context.refresh();
+ context.recreateFieldDescriptors();
variableSelectionView.refresh();
+ variableSelectionView.setInput(context); // Set input to trigger node expansion.
+ for (Object element : context.selectedAccessors) {
+ variableSelectionView.setChecked(element, true);
+ }
}
}
@Override
public void dispose() {
- super.dispose();
IEclipsePreferences node = InstanceScope.INSTANCE.getNode(CUIPlugin.PLUGIN_ID);
node.removePreferenceChangeListener(this);
+ super.dispose();
}
}
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 a613276f522..7f6d0c36d2b 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
@@ -159,7 +159,7 @@ public class GenerateGettersAndSettersRefactoring extends CRefactoring2 {
if (compositeTypeSpecifier != null) {
findDeclarations(compositeTypeSpecifier);
} else {
- initStatus.addFatalError(Messages.GenerateGettersAndSettersRefactoring_NoCassDefFound);
+ initStatus.addFatalError(Messages.GenerateGettersAndSettersRefactoring_NoClassDefFound);
}
}
@@ -230,17 +230,17 @@ public class GenerateGettersAndSettersRefactoring extends CRefactoring2 {
throws CoreException, OperationCanceledException {
List<IASTNode> getterAndSetters = new ArrayList<IASTNode>();
List<IASTFunctionDefinition> definitions = new ArrayList<IASTFunctionDefinition>();
- for (GetterSetterInsertEditProvider currentProvider : context.selectedFunctions) {
+ for (AccessorDescriptor accessor : context.selectedAccessors) {
if (context.isDefinitionSeparate()) {
- getterAndSetters.add(currentProvider.getFunctionDeclaration());
- IASTFunctionDefinition functionDefinition = currentProvider.getFunctionDefinition(true);
+ getterAndSetters.add(accessor.getAccessorDeclaration());
+ IASTFunctionDefinition functionDefinition = accessor.getAccessorDefinition(true);
// Standalone definitions in a header file have to be declared inline.
if (definitionInsertLocation.getTranslationUnit().isHeaderUnit()) {
functionDefinition.getDeclSpecifier().setInline(true);
}
definitions.add(functionDefinition);
} else {
- getterAndSetters.add(currentProvider.getFunctionDefinition(false));
+ getterAndSetters.add(accessor.getAccessorDefinition(false));
}
}
if (context.isDefinitionSeparate()) {
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterContext.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterContext.java
index 09697a87b08..e915b736b81 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterContext.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterContext.java
@@ -13,6 +13,7 @@
package org.eclipse.cdt.internal.ui.refactoring.gettersandsetters;
import java.util.ArrayList;
+import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
@@ -23,54 +24,24 @@ 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;
-import org.eclipse.cdt.core.dom.ast.IArrayType;
-import org.eclipse.cdt.core.dom.ast.IBinding;
-import org.eclipse.cdt.core.dom.ast.IPointerType;
-import org.eclipse.cdt.core.dom.ast.IQualifierType;
-import org.eclipse.cdt.core.dom.ast.IType;
-import org.eclipse.cdt.core.dom.ast.cpp.ICPPField;
-import org.eclipse.cdt.core.dom.ast.cpp.ICPPReferenceType;
-import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil;
-
-import org.eclipse.cdt.internal.ui.refactoring.gettersandsetters.GetterSetterInsertEditProvider.AccessorKind;
+import org.eclipse.cdt.internal.ui.refactoring.gettersandsetters.AccessorDescriptor.AccessorKind;
public class GetterSetterContext implements ITreeContentProvider {
- public ArrayList<IASTSimpleDeclaration> existingFields = new ArrayList<IASTSimpleDeclaration>();
- public ArrayList<IASTFunctionDefinition> existingFunctionDefinitions = new ArrayList<IASTFunctionDefinition>();
- public ArrayList<IASTSimpleDeclaration> existingFunctionDeclarations = new ArrayList<IASTSimpleDeclaration>();
- public SortedSet<GetterSetterInsertEditProvider> selectedFunctions = new TreeSet<GetterSetterInsertEditProvider>();
- public IASTName selectedName;
- private ArrayList<FieldWrapper> wrappedFields;
+ final List<IASTSimpleDeclaration> existingFields = new ArrayList<IASTSimpleDeclaration>();
+ final List<IASTFunctionDefinition> existingFunctionDefinitions = new ArrayList<IASTFunctionDefinition>();
+ final List<IASTSimpleDeclaration> existingFunctionDeclarations = new ArrayList<IASTSimpleDeclaration>();
+ final SortedSet<AccessorDescriptor> selectedAccessors = new TreeSet<AccessorDescriptor>();
+ IASTName selectedName;
+ private List<FieldDescriptor> fieldDescriptors;
private boolean definitionSeparate;
+ private static final Object[] NO_CHILDREN = {};
@Override
public Object[] getChildren(Object parentElement) {
- ArrayList<GetterSetterInsertEditProvider> children = new ArrayList<GetterSetterInsertEditProvider>();
- if (parentElement instanceof FieldWrapper) {
- FieldWrapper wrapper = (FieldWrapper) parentElement;
-
- if (wrapper.getChildNodes().isEmpty()) {
- if (!wrapper.getter.exists()) {
- wrapper.childNodes.add(createGetterInserter(wrapper.field));
- }
- if (!wrapper.setter.exists() && isAssignable(wrapper.field)) {
- wrapper.childNodes.add(createSetterInserter(wrapper.field));
- }
- }
- children = wrapper.getChildNodes();
- }
- return children.toArray();
- }
-
- public GetterSetterInsertEditProvider createGetterInserter(IASTSimpleDeclaration simpleDeclaration) {
- IASTName fieldName = getDeclarationName(simpleDeclaration);
- return new GetterSetterInsertEditProvider(fieldName, simpleDeclaration, AccessorKind.GETTER);
- }
-
- public GetterSetterInsertEditProvider createSetterInserter(IASTSimpleDeclaration simpleDeclaration) {
- IASTName fieldName = getDeclarationName(simpleDeclaration);
- return new GetterSetterInsertEditProvider(fieldName, simpleDeclaration, AccessorKind.SETTER);
+ if (!(parentElement instanceof FieldDescriptor))
+ return NO_CHILDREN;
+ return ((FieldDescriptor) parentElement).getChildNodes();
}
@Override
@@ -80,31 +51,18 @@ public class GetterSetterContext implements ITreeContentProvider {
@Override
public boolean hasChildren(Object element) {
- if (element instanceof FieldWrapper) {
- FieldWrapper wrapper = (FieldWrapper) element;
- return wrapper.missingGetterOrSetter();
+ if (element instanceof FieldDescriptor) {
+ FieldDescriptor descriptor = (FieldDescriptor) element;
+ return descriptor.missingGetterOrSetter();
}
return false;
}
@Override
public Object[] getElements(Object inputElement) {
- return getWrappedFields().toArray();
+ return getFieldDescriptors().toArray();
}
- public void refresh() {
- // We only recreate the function declarations instead of recreating
- // GetterSetterInsertEditProviders. That way, selectedFunctions is still valid.
- // Also, the objects inside the TreeViewer are still the same, which is convenient because
- // that way we don't need to save then restore the collapsed/expanded+checked/unchecked
- // state of the TreeViewer.
- for (FieldWrapper wrapper : wrappedFields) {
- for (GetterSetterInsertEditProvider provider : wrapper.childNodes) {
- provider.createFunctionDeclaration();
- }
- }
- }
-
@Override
public void dispose() {
}
@@ -112,7 +70,35 @@ public class GetterSetterContext implements ITreeContentProvider {
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
-
+
+ public void recreateFieldDescriptors() {
+ // Delete field descriptors so that they are recreated by the next getFieldDescriptors call.
+ fieldDescriptors = null;
+ SortedSet<AccessorDescriptor> oldSelected = new TreeSet<AccessorDescriptor>(selectedAccessors);
+ selectedAccessors.clear();
+ for (FieldDescriptor descriptor : getFieldDescriptors()) {
+ for (AccessorDescriptor accessor : descriptor.getChildNodes()) {
+ if (oldSelected.contains(accessor)) {
+ selectedAccessors.add(accessor);
+ }
+ }
+ }
+ }
+
+ public void selectAccessorForField(String name, AccessorKind kind) {
+ for (FieldDescriptor descriptor : getFieldDescriptors()) {
+ if (name.equals(String.valueOf(descriptor.getFieldName().getSimpleID()))) {
+ for (Object child : descriptor.getChildNodes()) {
+ AccessorDescriptor accessor = (AccessorDescriptor) child;
+ if (accessor.getKind() == kind) {
+ selectedAccessors.add(accessor);
+ break;
+ }
+ }
+ }
+ }
+ }
+
public boolean isDefinitionSeparate() {
return definitionSeparate;
}
@@ -121,105 +107,24 @@ public class GetterSetterContext implements ITreeContentProvider {
this.definitionSeparate = definitionSeparate;
}
- private ArrayList<FieldWrapper> getWrappedFields() {
- if (wrappedFields == null) {
- wrappedFields = new ArrayList<FieldWrapper>();
+ private List<FieldDescriptor> getFieldDescriptors() {
+ if (fieldDescriptors == null) {
+ fieldDescriptors = new ArrayList<FieldDescriptor>();
for (IASTSimpleDeclaration field : existingFields) {
- FieldWrapper wrapper = new FieldWrapper();
- wrapper.field = field;
- wrapper.getter = getGetterForField(field);
- wrapper.setter = getSetterForField(field);
- if (wrapper.missingGetterOrSetter()) {
- wrappedFields.add(wrapper);
+ FieldDescriptor descriptor = new FieldDescriptor(field, this);
+ if (descriptor.missingGetterOrSetter()) {
+ fieldDescriptors.add(descriptor);
}
}
}
- return wrappedFields;
- }
-
- private FunctionWrapper getGetterForField(IASTSimpleDeclaration currentField) {
- FunctionWrapper wrapper = new FunctionWrapper();
- String name = GetterSetterNameGenerator.generateGetterName(getDeclarationName(currentField));
- setFunctionToWrapper(wrapper, name);
- return wrapper;
+ return fieldDescriptors;
}
- private FunctionWrapper getSetterForField(IASTSimpleDeclaration field) {
- FunctionWrapper wrapper = new FunctionWrapper();
- String name = GetterSetterNameGenerator.generateSetterName(getDeclarationName(field));
- setFunctionToWrapper(wrapper, name);
- return wrapper;
- }
-
- private static IASTName getDeclarationName(IASTSimpleDeclaration declaration) {
+ static IASTName getDeclarationName(IASTSimpleDeclaration declaration) {
IASTDeclarator declarator = declaration.getDeclarators()[0];
while (declarator.getNestedDeclarator() != null) {
declarator = declarator.getNestedDeclarator();
}
return declarator.getName();
}
-
- private static boolean isAssignable(IASTSimpleDeclaration declaration) {
- IASTName name = getDeclarationName(declaration);
- IBinding binding = name.resolveBinding();
- if (!(binding instanceof ICPPField))
- return false;
- ICPPField field = (ICPPField) binding;
- IType type = field.getType();
- type = SemanticUtil.getNestedType(type, SemanticUtil.TDEF);
- if (type instanceof IArrayType || type instanceof ICPPReferenceType)
- return false;
- if (type instanceof IPointerType && ((IPointerType) type).isConst())
- return false;
- if (type instanceof IQualifierType && ((IQualifierType) type).isConst())
- return false;
- return true;
- }
-
- private void setFunctionToWrapper(FunctionWrapper wrapper, String accessorName) {
- for (IASTFunctionDefinition currentDefinition : existingFunctionDefinitions) {
- if (currentDefinition.getDeclarator().getName().toString().equals(accessorName)) {
- wrapper.functionDefinition = currentDefinition;
- }
- }
-
- for (IASTSimpleDeclaration currentDeclaration : existingFunctionDeclarations) {
- if (getDeclarationName(currentDeclaration).toString().equals(accessorName)) {
- wrapper.functionDeclaration = currentDeclaration;
- }
- }
- }
-
- protected class FieldWrapper {
- protected IASTSimpleDeclaration field;
- protected FunctionWrapper getter;
- protected FunctionWrapper setter;
- protected ArrayList<GetterSetterInsertEditProvider> childNodes = new ArrayList<GetterSetterInsertEditProvider>(2);
-
- @Override
- public String toString() {
- IASTDeclarator declarator = field.getDeclarators()[0];
- while (declarator.getNestedDeclarator() != null) {
- declarator = declarator.getNestedDeclarator();
- }
- return declarator.getName().toString();
- }
-
- public ArrayList<GetterSetterInsertEditProvider> getChildNodes() {
- return childNodes;
- }
-
- public boolean missingGetterOrSetter() {
- return !getter.exists() || !setter.exists();
- }
- }
-
- protected class FunctionWrapper {
- protected IASTSimpleDeclaration functionDeclaration;
- protected IASTFunctionDefinition functionDefinition;
-
- public boolean exists() {
- return functionDeclaration != null || functionDefinition != null;
- }
- }
}
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterFactory.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterFactory.java
deleted file mode 100644
index 070d7c2d1f9..00000000000
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterFactory.java
+++ /dev/null
@@ -1,235 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2008, 2011 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 - initial API and implementation
- * Sergey Prigogin (Google)
- *******************************************************************************/
-package org.eclipse.cdt.internal.ui.refactoring.gettersandsetters;
-
-import java.util.Arrays;
-
-import org.eclipse.cdt.core.dom.ast.IASTArrayDeclarator;
-import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
-import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
-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.IASTNode.CopyStyle;
-import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
-import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
-import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
-import org.eclipse.cdt.core.dom.ast.IType;
-import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTArrayDeclarator;
-import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
-import org.eclipse.cdt.core.dom.rewrite.TypeHelper;
-import org.eclipse.cdt.core.parser.Keywords;
-
-import org.eclipse.cdt.internal.core.dom.parser.c.CASTDeclarator;
-import org.eclipse.cdt.internal.core.dom.parser.c.CASTPointer;
-import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTBinaryExpression;
-import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTCompoundStatement;
-import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTDeclarator;
-import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTExpressionStatement;
-import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFieldReference;
-import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator;
-import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDefinition;
-import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTIdExpression;
-import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTLiteralExpression;
-import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName;
-import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTParameterDeclaration;
-import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTPointer;
-import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTReferenceOperator;
-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.semantics.CPPVisitor;
-
-public class GetterSetterFactory {
- private final IASTName fieldName;
- private final IASTSimpleDeclaration fieldDeclaration;
- private boolean passByReference;
-
- public GetterSetterFactory(IASTName fieldName, IASTSimpleDeclaration fieldDeclaration) {
- this.fieldName = fieldName;
- this.fieldDeclaration = fieldDeclaration;
- IType type = CPPVisitor.createType(fieldDeclaration.getDeclSpecifier());
- passByReference = TypeHelper.shouldBePassedByReference(type, fieldDeclaration.getTranslationUnit());
- }
-
- public IASTFunctionDefinition createGetterDefinition(ICPPASTQualifiedName qualifiedName) {
- IASTFunctionDefinition getter = new CPPASTFunctionDefinition();
-
- getter.setDeclSpecifier(getParamOrReturnDeclSpecifier());
- IASTDeclarator getterDeclarator = getGetterDeclarator(qualifiedName);
- // IASTFunctionDefinition expects the outermost IASTFunctionDeclarator in declarator hierarchy
- while (!(getterDeclarator instanceof IASTFunctionDeclarator)) {
- getterDeclarator = getterDeclarator.getNestedDeclarator();
- }
- getter.setDeclarator((IASTFunctionDeclarator) getterDeclarator);
- getter.setBody(getGetterBody());
- return getter;
- }
-
- private CPPASTCompoundStatement getGetterBody() {
- CPPASTCompoundStatement compound = new CPPASTCompoundStatement();
- CPPASTReturnStatement returnStatement = new CPPASTReturnStatement();
- CPPASTIdExpression idExpr = new CPPASTIdExpression();
- CPPASTName returnVal = new CPPASTName();
- returnVal.setName(fieldName.toCharArray());
- idExpr.setName(returnVal);
- returnStatement.setReturnValue(idExpr);
- compound.addStatement(returnStatement);
- return compound;
- }
-
- private IASTDeclarator getGetterDeclarator(ICPPASTQualifiedName qualifiedName) {
- CPPASTName getterName = new CPPASTName();
- getterName.setName(GetterSetterNameGenerator.generateGetterName(fieldName).toCharArray());
-
- // Copy declarator hierarchy
- IASTDeclarator topDeclarator = fieldDeclaration.getDeclarators()[0].copy(CopyStyle.withLocations);
-
- if (topDeclarator instanceof IASTArrayDeclarator) {
- boolean isCpp = topDeclarator instanceof ICPPASTArrayDeclarator;
- IASTDeclarator decl = isCpp ? new CPPASTDeclarator() : new CASTDeclarator();
- decl.setName(topDeclarator.getName());
- decl.setNestedDeclarator(topDeclarator.getNestedDeclarator());
- decl.addPointerOperator(isCpp ? new CPPASTPointer() : new CASTPointer());
- for (IASTPointerOperator pointer : topDeclarator.getPointerOperators()) {
- decl.addPointerOperator(pointer);
- }
- topDeclarator = decl;
- }
- // Find the innermost declarator in hierarchy
- IASTDeclarator innermost = topDeclarator;
- while (innermost.getNestedDeclarator() != null) {
- innermost = innermost.getNestedDeclarator();
- }
-
- // Create a new innermost function declarator based on the field declarator
- CPPASTFunctionDeclarator functionDeclarator = new CPPASTFunctionDeclarator();
- functionDeclarator.setConst(true);
- if (qualifiedName != null) {
- qualifiedName.addName(getterName);
- functionDeclarator.setName(qualifiedName);
- } else {
- functionDeclarator.setName(getterName);
- }
- for (IASTPointerOperator pointer : innermost.getPointerOperators()){
- functionDeclarator.addPointerOperator(pointer.copy(CopyStyle.withLocations));
- }
- if (passByReference) {
- functionDeclarator.addPointerOperator(new CPPASTReferenceOperator(false));
- }
-
- // Replace the 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;
- }
- }
-
- public IASTFunctionDefinition createSetterDefinition(ICPPASTQualifiedName qualifiedName) {
- IASTFunctionDefinition setter = new CPPASTFunctionDefinition();
- setter.setDeclSpecifier(getVoidDeclSpec());
- setter.setDeclarator(getSetterDeclarator(qualifiedName));
- setter.setBody(getSetterBody());
- return setter;
- }
-
- private CPPASTCompoundStatement getSetterBody() {
- CPPASTCompoundStatement compound = new CPPASTCompoundStatement();
- CPPASTExpressionStatement exprStmt = new CPPASTExpressionStatement();
- CPPASTBinaryExpression binExpr = new CPPASTBinaryExpression();
- IASTDeclarator innerDeclarator = fieldDeclaration.getDeclarators()[0];
- while (innerDeclarator.getNestedDeclarator() != null) {
- innerDeclarator = innerDeclarator.getNestedDeclarator();
- }
- IASTName fieldName = innerDeclarator.getName();
- CPPASTName parameterName = getSetterParameterName();
- if (Arrays.equals(fieldName.getSimpleID(), parameterName.getSimpleID())) {
- CPPASTFieldReference fieldRef = new CPPASTFieldReference();
- CPPASTLiteralExpression litExpr = new CPPASTLiteralExpression();
- litExpr.setValue(Keywords.cTHIS);
- fieldRef.setFieldOwner(litExpr);
- fieldRef.setIsPointerDereference(true);
- fieldRef.setFieldName(fieldName.copy(CopyStyle.withLocations));
- binExpr.setOperand1(fieldRef);
- } else {
- CPPASTIdExpression idExpr = new CPPASTIdExpression(fieldName.copy(CopyStyle.withLocations));
- binExpr.setOperand1(idExpr);
- }
- binExpr.setOperator(IASTBinaryExpression.op_assign);
- CPPASTIdExpression idExpr = new CPPASTIdExpression(parameterName);
- binExpr.setOperand2(idExpr);
- exprStmt.setExpression(binExpr);
- compound.addStatement(exprStmt);
- return compound;
- }
-
- private CPPASTFunctionDeclarator getSetterDeclarator(ICPPASTQualifiedName qualifiedName) {
- CPPASTName setterName = new CPPASTName();
- setterName.setName(GetterSetterNameGenerator.generateSetterName(fieldName).toCharArray());
- CPPASTFunctionDeclarator declarator = new CPPASTFunctionDeclarator();
- if (qualifiedName != null) {
- qualifiedName.addName(setterName);
- declarator.setName(qualifiedName);
- } else {
- declarator.setName(setterName);
- }
- CPPASTParameterDeclaration parameterDeclaration = new CPPASTParameterDeclaration();
- IASTDeclarator parameterDeclarator = fieldDeclaration.getDeclarators()[0].copy(CopyStyle.withLocations);
- parameterDeclarator.setName(getSetterParameterName());
- if (passByReference) {
- parameterDeclarator.addPointerOperator(new CPPASTReferenceOperator(false));
- }
- parameterDeclaration.setDeclarator(parameterDeclarator);
- parameterDeclaration.setDeclSpecifier(getParamOrReturnDeclSpecifier());
- declarator.addParameterDeclaration(parameterDeclaration.copy(CopyStyle.withLocations));
- return declarator;
- }
-
- private CPPASTName getSetterParameterName() {
- String parameterName = GetterSetterNameGenerator.generateSetterParameterName(fieldName);
- return new CPPASTName(parameterName.toCharArray());
- }
-
- private static CPPASTSimpleDeclSpecifier getVoidDeclSpec() {
- CPPASTSimpleDeclSpecifier declSpecifier = new CPPASTSimpleDeclSpecifier();
- declSpecifier.setType(IASTSimpleDeclSpecifier.t_void);
- return declSpecifier;
- }
-
- public IASTSimpleDeclaration createGetterDeclaration() {
- IASTSimpleDeclaration getter = new CPPASTSimpleDeclaration();
- getter.setDeclSpecifier(getParamOrReturnDeclSpecifier());
- getter.addDeclarator(getGetterDeclarator(null));
- return getter;
- }
-
- private IASTDeclSpecifier getParamOrReturnDeclSpecifier() {
- IASTDeclSpecifier declSpec = fieldDeclaration.getDeclSpecifier().copy(CopyStyle.withLocations);
- if (passByReference || fieldDeclaration.getDeclarators()[0] instanceof IASTArrayDeclarator) {
- declSpec.setConst(true);
- }
- return declSpec;
- }
-
- public IASTSimpleDeclaration createSetterDeclaration() {
- IASTSimpleDeclaration setter = new CPPASTSimpleDeclaration();
- setter.setDeclSpecifier(getVoidDeclSpec());
- setter.addDeclarator(getSetterDeclarator(null));
- return setter;
- }
-}
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterInsertEditProvider.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterInsertEditProvider.java
deleted file mode 100644
index 0956d700277..00000000000
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterInsertEditProvider.java
+++ /dev/null
@@ -1,110 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2008, 2011 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 - initial API and implementation
- * Sergey Prigogin (Google)
- *******************************************************************************/
-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.IASTName;
-import org.eclipse.cdt.core.dom.ast.IASTNode;
-import org.eclipse.cdt.core.dom.ast.IASTNode.CopyStyle;
-import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
-import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
-
-import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTQualifiedName;
-
-public class GetterSetterInsertEditProvider implements Comparable<GetterSetterInsertEditProvider> {
- public enum AccessorKind {
- GETTER,
- SETTER;
- }
-
- private IASTSimpleDeclaration functionDeclaration;
- private AccessorKind kind;
- private IASTSimpleDeclaration fieldDeclaration;
- private GetterSetterFactory getterSetterFactory;
-
- public GetterSetterInsertEditProvider(IASTName fieldName, IASTSimpleDeclaration fieldDeclaration,
- AccessorKind kind) {
- this.kind = kind;
- this.fieldDeclaration = fieldDeclaration;
- this.getterSetterFactory = new GetterSetterFactory(fieldName, fieldDeclaration);
-
- createFunctionDeclaration();
- }
-
- public void createFunctionDeclaration() {
- switch (this.kind) {
- case GETTER:
- this.functionDeclaration = getterSetterFactory.createGetterDeclaration();
- break;
- case SETTER:
- this.functionDeclaration = getterSetterFactory.createSetterDeclaration();
- break;
- }
- }
-
- @Override
- public String toString() {
- IASTDeclarator declarator = functionDeclaration.getDeclarators()[0];
- while (declarator.getNestedDeclarator() != null) {
- declarator = declarator.getNestedDeclarator();
- }
- return declarator.getName().toString();
- }
-
- public IASTFunctionDefinition getFunctionDefinition(boolean qualifedName) {
- IASTFunctionDefinition definition = null;
- ICPPASTQualifiedName qname;
- if (qualifedName) {
- qname = getClassName();
- } else {
- qname = null;
- }
-
- switch (kind) {
- case GETTER:
- definition = getterSetterFactory.createGetterDefinition(qname);
- break;
- case SETTER:
- definition = getterSetterFactory.createSetterDefinition(qname);
- break;
- }
- return definition;
- }
-
- private ICPPASTQualifiedName getClassName() {
- IASTNode node = fieldDeclaration.getParent();
- while (!(node instanceof IASTCompositeTypeSpecifier)) {
- node = node.getParent();
- }
- IASTCompositeTypeSpecifier comp = (IASTCompositeTypeSpecifier) node;
-
- CPPASTQualifiedName qname = new CPPASTQualifiedName();
- qname.addName(comp.getName().copy(CopyStyle.withLocations));
- return qname;
- }
-
- public IASTSimpleDeclaration getFunctionDeclaration() {
- return functionDeclaration;
- }
-
- public AccessorKind getType() {
- return kind;
- }
-
- @Override
- public int compareTo(GetterSetterInsertEditProvider o) {
- return toString().compareTo(o.toString());
- }
-}
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterLabelProvider.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterLabelProvider.java
index 927579fa0eb..a5ba6dcb1d5 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterLabelProvider.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterLabelProvider.java
@@ -22,10 +22,9 @@ import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider;
* @author Emanuel Graf IFS
*/
public class GetterSetterLabelProvider extends LabelProvider {
-
@Override
public Image getImage(Object element) {
- if (element instanceof GetterSetterInsertEditProvider) {
+ if (element instanceof AccessorDescriptor) {
return CElementImageProvider.getMethodImageDescriptor(ASTAccessVisibility.PUBLIC).createImage();
}
return null;
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterNameGenerator.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterNameGenerator.java
index 229fe842b5f..f13586f9af9 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterNameGenerator.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterNameGenerator.java
@@ -16,11 +16,12 @@ import java.util.Set;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.IPreferencesService;
-
+import org.eclipse.cdt.core.CConventions;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IBasicType;
import org.eclipse.cdt.core.dom.ast.IType;
+import org.eclipse.cdt.core.dom.ast.gnu.cpp.GPPLanguage;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.PreferenceConstants;
@@ -29,11 +30,6 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.cdt.internal.ui.util.NameComposer;
public class GetterSetterNameGenerator {
-
- // Do not instantiate.
- private GetterSetterNameGenerator() {
- }
-
private static Set<String> generateGetterSettersPreferenceKeys = new HashSet<String>();
static {
generateGetterSettersPreferenceKeys.add(PreferenceConstants.NAME_STYLE_GETTER_CAPITALIZATION);
@@ -51,18 +47,30 @@ public class GetterSetterNameGenerator {
generateGetterSettersPreferenceKeys.add(PreferenceConstants.NAME_STYLE_VARIABLE_SUFFIX);
}
+ // Do not instantiate.
+ private GetterSetterNameGenerator() {
+ }
+
public static Set<String> getGenerateGetterSettersPreferenceKeys() {
return generateGetterSettersPreferenceKeys;
}
-
- public static String generateGetterName(IASTName fieldName) {
+
+ /**
+ * Generates getter name for a given field name.
+ *
+ * @param fieldName the name of the field
+ * @param namesToAvoid the set of names to avoid
+ * @return the generated getter name, or <code>null</code> if a valid name could not be
+ * generated.
+ */
+ public static String generateGetterName(IASTName fieldName, Set<String> namesToAvoid) {
IPreferencesService preferences = Platform.getPreferencesService();
int capitalization = preferences.getInt(CUIPlugin.PLUGIN_ID,
PreferenceConstants.NAME_STYLE_GETTER_CAPITALIZATION,
PreferenceConstants.NAME_STYLE_CAPITALIZATION_CAMEL_CASE, null);
String wordDelimiter = preferences.getString(CUIPlugin.PLUGIN_ID,
PreferenceConstants.NAME_STYLE_GETTER_WORD_DELIMITER, "", null); //$NON-NLS-1$
- String prefix = isBooleanDecaratorName(fieldName) ?
+ String prefix = isBooleanDeclaratorName(fieldName) ?
preferences.getString(CUIPlugin.PLUGIN_ID,
PreferenceConstants.NAME_STYLE_GETTER_PREFIX_FOR_BOOLEAN, "is", null) : //$NON-NLS-1$
preferences.getString(CUIPlugin.PLUGIN_ID,
@@ -71,10 +79,11 @@ public class GetterSetterNameGenerator {
PreferenceConstants.NAME_STYLE_GETTER_SUFFIX, "", null); //$NON-NLS-1$
NameComposer composer = new NameComposer(capitalization, wordDelimiter, prefix, suffix);
String name = NameComposer.trimFieldName(fieldName.toString());
- return composer.compose(name);
+ name = composer.compose(name);
+ return adjustName(name, namesToAvoid);
}
- private static boolean isBooleanDecaratorName(IASTName name) {
+ private static boolean isBooleanDeclaratorName(IASTName name) {
if (IASTDeclarator.DECLARATOR_NAME.equals(name.getPropertyInParent())) {
IASTDeclarator declarator = (IASTDeclarator) name.getParent();
IType type = CPPVisitor.createType(declarator);
@@ -85,7 +94,15 @@ public class GetterSetterNameGenerator {
return false;
}
- public static String generateSetterName(IASTName fieldName) {
+ /**
+ * Generates setter name for a given field name.
+ *
+ * @param fieldName the name of the field
+ * @param namesToAvoid the set of names to avoid
+ * @return the generated setter name, or <code>null</code> if a valid name could not be
+ * generated.
+ */
+ public static String generateSetterName(IASTName fieldName, Set<String> namesToAvoid) {
IPreferencesService preferences = Platform.getPreferencesService();
int capitalization = preferences.getInt(CUIPlugin.PLUGIN_ID,
PreferenceConstants.NAME_STYLE_SETTER_CAPITALIZATION,
@@ -98,10 +115,11 @@ public class GetterSetterNameGenerator {
PreferenceConstants.NAME_STYLE_SETTER_SUFFIX, "", null); //$NON-NLS-1$
NameComposer composer = new NameComposer(capitalization, wordDelimiter, prefix, suffix);
String name = NameComposer.trimFieldName(fieldName.toString());
- return composer.compose(name);
+ name = composer.compose(name);
+ return adjustName(name, namesToAvoid);
}
- public static String generateSetterParameterName(IASTName fieldName) {
+ public static String generateSetterParameterName(IASTName fieldName){
IPreferencesService preferences = Platform.getPreferencesService();
int capitalization = preferences.getInt(CUIPlugin.PLUGIN_ID,
PreferenceConstants.NAME_STYLE_VARIABLE_CAPITALIZATION,
@@ -114,6 +132,28 @@ public class GetterSetterNameGenerator {
PreferenceConstants.NAME_STYLE_VARIABLE_SUFFIX, "", null); //$NON-NLS-1$
NameComposer composer = new NameComposer(capitalization, wordDelimiter, prefix, suffix);
String name = NameComposer.trimFieldName(fieldName.toString());
- return composer.compose(name);
+ name = composer.compose(name);
+ if (!CConventions.validateIdentifier(name, GPPLanguage.getDefault()).isOK())
+ name = '_' + name;
+ return name;
+ }
+
+ /**
+ * Checks is the given name is valid and, if not, tries to adjust it by adding a numeric suffix
+ * to it.
+ *
+ * @param name the name to check and, possibly, adjust
+ * @param namesToAvoid the set of names to avoid
+ * @return the adjusted name, or <code>null</code> if a valid name could not be generated.
+ */
+ private static String adjustName(String name, Set<String> namesToAvoid) {
+ String originalName = name;
+ for (int i = 1; i < 100; i++) {
+ if (!namesToAvoid.contains(name) && CConventions.validateIdentifier(name, GPPLanguage.getDefault()).isOK()) {
+ return name;
+ }
+ name = originalName + i;
+ }
+ return null;
}
}
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/Messages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/Messages.java
index be3a7e838e7..f6cac1f695d 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/Messages.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/Messages.java
@@ -15,17 +15,17 @@ import org.eclipse.osgi.util.NLS;
public final class Messages extends NLS {
public static String GenerateGettersAndSettersInputPage_DeselectAll;
- public static String GenerateGettersAndSettersInputPage_header;
+ public static String GenerateGettersAndSettersInputPage_Header;
+ public static String GenerateGettersAndSettersInputPage_LinkDescription;
+ public static String GenerateGettersAndSettersInputPage_LinkTooltip;
+ public static String GenerateGettersAndSettersInputPage_Name;
public static String GenerateGettersAndSettersInputPage_SeparateDefinition;
public static String GenerateGettersAndSettersInputPage_SelectAll;
public static String GenerateGettersAndSettersInputPage_SelectGetters;
public static String GenerateGettersAndSettersInputPage_SelectSetters;
- public static String GenerateGettersAndSettersInputPage_LinkDescription;
- public static String GenerateGettersAndSettersInputPage_LinkTooltip;
- public static String GenerateGettersAndSettersRefactoring_NoCassDefFound;
+ public static String GenerateGettersAndSettersRefactoring_NoClassDefFound;
public static String GenerateGettersAndSettersRefactoring_NoFields;
public static String GenerateGettersAndSettersRefactoring_NoImplFile;
- public static String GettersAndSetters_Name;
static {
NLS.initializeMessages(Messages.class.getName(), Messages.class);
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/Messages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/Messages.properties
index e4ca8314022..f57203d0fe7 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/Messages.properties
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/Messages.properties
@@ -9,15 +9,15 @@
# Contributors:
# Institute for Software - initial API and implementation
###############################################################################
-GettersAndSetters_Name=Generate getters and setters
GenerateGettersAndSettersInputPage_DeselectAll=Deselect All
-GenerateGettersAndSettersInputPage_header=Select getters and setters to create:
+GenerateGettersAndSettersInputPage_Header=Select getters and setters to create:
+GenerateGettersAndSettersInputPage_LinkDescription=The names of getters and setters may be configured on the <a>Name Style</a> preference page.
+GenerateGettersAndSettersInputPage_LinkTooltip=Show the name style preferences.
+GenerateGettersAndSettersInputPage_Name=Generate getters and setters
GenerateGettersAndSettersInputPage_SeparateDefinition=Definition separate from declaration
GenerateGettersAndSettersInputPage_SelectAll=Select All
GenerateGettersAndSettersInputPage_SelectGetters=Select Getters
GenerateGettersAndSettersInputPage_SelectSetters=Select Setters
-GenerateGettersAndSettersInputPage_LinkDescription=The names of getters and setters may be configured on the <a>Name Style</a> preference page.
-GenerateGettersAndSettersInputPage_LinkTooltip=Show the name style preferences.
-GenerateGettersAndSettersRefactoring_NoCassDefFound=No class definition found
+GenerateGettersAndSettersRefactoring_NoClassDefFound=No class definition found
GenerateGettersAndSettersRefactoring_NoFields=The class does not contain any fields.
GenerateGettersAndSettersRefactoring_NoImplFile=No implementation file found. Inserting definition into the header file.

Back to the top