use of org.eclipse.jdt.core.dom.FieldDeclaration in project che by eclipse.
the class PromoteTempToFieldRefactoring method addFieldDeclaration.
private void addFieldDeclaration(ASTRewrite rewrite) {
FieldDeclaration[] fields = getFieldDeclarations();
ASTNode parent = getMethodDeclaration().getParent();
ChildListPropertyDescriptor descriptor = ASTNodes.getBodyDeclarationsProperty(parent);
int insertIndex;
if (fields.length == 0)
insertIndex = 0;
else
insertIndex = ASTNodes.getBodyDeclarations(parent).indexOf(fields[fields.length - 1]) + 1;
final FieldDeclaration declaration = createNewFieldDeclaration(rewrite);
rewrite.getListRewrite(parent, descriptor).insertAt(declaration, insertIndex, null);
}
use of org.eclipse.jdt.core.dom.FieldDeclaration in project che by eclipse.
the class AssignToVariableAssistProposal method doAddField.
private ASTRewrite doAddField() {
boolean isParamToField = fNodeToAssign.getNodeType() == ASTNode.SINGLE_VARIABLE_DECLARATION;
ASTNode newTypeDecl = ASTResolving.findParentType(fNodeToAssign);
if (newTypeDecl == null) {
return null;
}
Expression expression = isParamToField ? ((SingleVariableDeclaration) fNodeToAssign).getName() : ((ExpressionStatement) fNodeToAssign).getExpression();
AST ast = newTypeDecl.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
createImportRewrite((CompilationUnit) fNodeToAssign.getRoot());
BodyDeclaration bodyDecl = ASTResolving.findParentBodyDeclaration(fNodeToAssign);
Block body;
if (bodyDecl instanceof MethodDeclaration) {
body = ((MethodDeclaration) bodyDecl).getBody();
} else if (bodyDecl instanceof Initializer) {
body = ((Initializer) bodyDecl).getBody();
} else {
return null;
}
IJavaProject project = getCompilationUnit().getJavaProject();
boolean isAnonymous = newTypeDecl.getNodeType() == ASTNode.ANONYMOUS_CLASS_DECLARATION;
boolean isStatic = Modifier.isStatic(bodyDecl.getModifiers()) && !isAnonymous;
boolean isConstructorParam = isParamToField && fNodeToAssign.getParent() instanceof MethodDeclaration && ((MethodDeclaration) fNodeToAssign.getParent()).isConstructor();
int modifiers = Modifier.PRIVATE;
if (isStatic) {
modifiers |= Modifier.STATIC;
} else if (isConstructorParam) {
// String saveActionsKey= AbstractSaveParticipantPreferenceConfiguration.EDITOR_SAVE_PARTICIPANT_PREFIX + CleanUpPostSaveListener.POSTSAVELISTENER_ID;
// IScopeContext[] scopes= { InstanceScope.INSTANCE, new ProjectScope(project.getProject()) };
// boolean safeActionsEnabled= Platform.getPreferencesService().getBoolean(JavaPlugin.getPluginId(), saveActionsKey, false, scopes);
}
if (/*CleanUpOptions.TRUE.equals(PreferenceConstants.getPreference(
CleanUpPreferenceUtil.SAVE_PARTICIPANT_KEY_PREFIX + CleanUpConstants.CLEANUP_ON_SAVE_ADDITIONAL_OPTIONS, project))
&&*/
CleanUpOptions.TRUE.equals(PreferenceConstants.getPreference(CleanUpPreferenceUtil.SAVE_PARTICIPANT_KEY_PREFIX + CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL, project)) && CleanUpOptions.TRUE.equals(PreferenceConstants.getPreference(CleanUpPreferenceUtil.SAVE_PARTICIPANT_KEY_PREFIX + CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PRIVATE_FIELDS, project))) {
int constructors = 0;
if (newTypeDecl instanceof AbstractTypeDeclaration) {
List<BodyDeclaration> bodyDeclarations = ((AbstractTypeDeclaration) newTypeDecl).bodyDeclarations();
for (BodyDeclaration decl : bodyDeclarations) {
if (decl instanceof MethodDeclaration && ((MethodDeclaration) decl).isConstructor()) {
constructors++;
}
}
}
if (constructors == 1) {
modifiers |= Modifier.FINAL;
}
}
VariableDeclarationFragment newDeclFrag = addFieldDeclaration(rewrite, newTypeDecl, modifiers, expression);
String varName = newDeclFrag.getName().getIdentifier();
Assignment assignment = ast.newAssignment();
assignment.setRightHandSide((Expression) rewrite.createCopyTarget(expression));
boolean needsThis = StubUtility.useThisForFieldAccess(project);
if (isParamToField) {
needsThis |= varName.equals(((SimpleName) expression).getIdentifier());
}
SimpleName accessName = ast.newSimpleName(varName);
if (needsThis) {
FieldAccess fieldAccess = ast.newFieldAccess();
fieldAccess.setName(accessName);
if (isStatic) {
String typeName = ((AbstractTypeDeclaration) newTypeDecl).getName().getIdentifier();
fieldAccess.setExpression(ast.newSimpleName(typeName));
} else {
fieldAccess.setExpression(ast.newThisExpression());
}
assignment.setLeftHandSide(fieldAccess);
} else {
assignment.setLeftHandSide(accessName);
}
ASTNode selectionNode;
if (isParamToField) {
// assign parameter to field
ExpressionStatement statement = ast.newExpressionStatement(assignment);
int insertIdx = findAssignmentInsertIndex(body.statements());
rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY).insertAt(statement, insertIdx, null);
selectionNode = statement;
} else {
if (needsSemicolon(expression)) {
rewrite.replace(expression, ast.newExpressionStatement(assignment), null);
} else {
rewrite.replace(expression, assignment, null);
}
selectionNode = fNodeToAssign;
}
addLinkedPosition(rewrite.track(newDeclFrag.getName()), false, KEY_NAME);
if (!isParamToField) {
FieldDeclaration fieldDeclaration = (FieldDeclaration) newDeclFrag.getParent();
addLinkedPosition(rewrite.track(fieldDeclaration.getType()), false, KEY_TYPE);
}
addLinkedPosition(rewrite.track(accessName), true, KEY_NAME);
IVariableBinding variableBinding = newDeclFrag.resolveBinding();
if (variableBinding != null) {
SimpleName[] linkedNodes = LinkedNodeFinder.findByBinding(fNodeToAssign.getRoot(), variableBinding);
for (int i = 0; i < linkedNodes.length; i++) {
addLinkedPosition(rewrite.track(linkedNodes[i]), false, KEY_NAME);
}
}
setEndPosition(rewrite.track(selectionNode));
return rewrite;
}
use of org.eclipse.jdt.core.dom.FieldDeclaration in project che by eclipse.
the class AssignToVariableAssistProposal method addFieldDeclaration.
private VariableDeclarationFragment addFieldDeclaration(ASTRewrite rewrite, ASTNode newTypeDecl, int modifiers, Expression expression) {
if (fExistingFragment != null) {
return fExistingFragment;
}
ChildListPropertyDescriptor property = ASTNodes.getBodyDeclarationsProperty(newTypeDecl);
List<BodyDeclaration> decls = ASTNodes.getBodyDeclarations(newTypeDecl);
AST ast = newTypeDecl.getAST();
String[] varNames = suggestFieldNames(fTypeBinding, expression, modifiers);
for (int i = 0; i < varNames.length; i++) {
addLinkedPositionProposal(KEY_NAME, varNames[i], null);
}
String varName = varNames[0];
VariableDeclarationFragment newDeclFrag = ast.newVariableDeclarationFragment();
newDeclFrag.setName(ast.newSimpleName(varName));
FieldDeclaration newDecl = ast.newFieldDeclaration(newDeclFrag);
Type type = evaluateType(ast);
newDecl.setType(type);
newDecl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, modifiers));
ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(getLinkedProposalModel(), rewrite, newDecl.modifiers(), false);
int insertIndex = findFieldInsertIndex(decls, fNodeToAssign.getStartPosition());
rewrite.getListRewrite(newTypeDecl, property).insertAt(newDecl, insertIndex, null);
return newDeclFrag;
}
use of org.eclipse.jdt.core.dom.FieldDeclaration in project che by eclipse.
the class ASTNodeDeleteUtil method propagateFieldDeclarationNodeDeletions.
private static void propagateFieldDeclarationNodeDeletions(final List<ASTNode> removed, final CompilationUnitRewrite rewrite, final TextEditGroup group) {
Set<ASTNode> removedNodes = getRemovedNodes(removed, rewrite);
for (Iterator<ASTNode> iter = removedNodes.iterator(); iter.hasNext(); ) {
ASTNode node = iter.next();
if (node instanceof VariableDeclarationFragment) {
if (node.getParent() instanceof FieldDeclaration) {
FieldDeclaration fd = (FieldDeclaration) node.getParent();
if (!removed.contains(fd) && removedNodes.containsAll(fd.fragments()))
rewrite.getASTRewrite().remove(fd, group);
rewrite.getImportRemover().registerRemovedNode(fd);
}
}
}
}
use of org.eclipse.jdt.core.dom.FieldDeclaration in project che by eclipse.
the class TypeChangeCorrectionProposal method getRewrite.
@Override
protected ASTRewrite getRewrite() throws CoreException {
ASTNode boundNode = fAstRoot.findDeclaringNode(fBinding);
ASTNode declNode = null;
CompilationUnit newRoot = fAstRoot;
if (boundNode != null) {
// is same CU
declNode = boundNode;
} else {
newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
declNode = newRoot.findDeclaringNode(fBinding.getKey());
}
if (declNode != null) {
AST ast = declNode.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
ImportRewrite imports = createImportRewrite(newRoot);
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(newRoot, declNode.getStartPosition(), imports);
Type type = imports.addImport(fNewType, ast, context);
if (declNode instanceof MethodDeclaration) {
MethodDeclaration methodDecl = (MethodDeclaration) declNode;
Type origReturnType = methodDecl.getReturnType2();
rewrite.set(methodDecl, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
DimensionRewrite.removeAllChildren(methodDecl, MethodDeclaration.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
// add javadoc tag
Javadoc javadoc = methodDecl.getJavadoc();
if (javadoc != null && origReturnType != null && origReturnType.isPrimitiveType() && ((PrimitiveType) origReturnType).getPrimitiveTypeCode() == PrimitiveType.VOID) {
TagElement returnTag = JavadocTagsSubProcessor.findTag(javadoc, TagElement.TAG_RETURN, null);
if (returnTag == null) {
returnTag = ast.newTagElement();
returnTag.setTagName(TagElement.TAG_RETURN);
TextElement commentStart = ast.newTextElement();
returnTag.fragments().add(commentStart);
//$NON-NLS-1$
addLinkedPosition(rewrite.track(commentStart), false, "comment_start");
ListRewrite tagsRewriter = rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
JavadocTagsSubProcessor.insertTag(tagsRewriter, returnTag, null);
}
}
} else if (declNode instanceof AnnotationTypeMemberDeclaration) {
AnnotationTypeMemberDeclaration methodDecl = (AnnotationTypeMemberDeclaration) declNode;
rewrite.set(methodDecl, AnnotationTypeMemberDeclaration.TYPE_PROPERTY, type, null);
} else if (declNode instanceof VariableDeclarationFragment) {
ASTNode parent = declNode.getParent();
if (parent instanceof FieldDeclaration) {
FieldDeclaration fieldDecl = (FieldDeclaration) parent;
if (fieldDecl.fragments().size() > 1 && (fieldDecl.getParent() instanceof AbstractTypeDeclaration)) {
// split
VariableDeclarationFragment placeholder = (VariableDeclarationFragment) rewrite.createMoveTarget(declNode);
FieldDeclaration newField = ast.newFieldDeclaration(placeholder);
newField.setType(type);
AbstractTypeDeclaration typeDecl = (AbstractTypeDeclaration) fieldDecl.getParent();
ListRewrite listRewrite = rewrite.getListRewrite(typeDecl, typeDecl.getBodyDeclarationsProperty());
if (fieldDecl.fragments().indexOf(declNode) == 0) {
// if it as the first in the list-> insert before
listRewrite.insertBefore(newField, parent, null);
} else {
listRewrite.insertAfter(newField, parent, null);
}
} else {
rewrite.set(fieldDecl, FieldDeclaration.TYPE_PROPERTY, type, null);
DimensionRewrite.removeAllChildren(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
}
} else if (parent instanceof VariableDeclarationStatement) {
VariableDeclarationStatement varDecl = (VariableDeclarationStatement) parent;
if (varDecl.fragments().size() > 1 && (varDecl.getParent() instanceof Block)) {
// split
VariableDeclarationFragment placeholder = (VariableDeclarationFragment) rewrite.createMoveTarget(declNode);
VariableDeclarationStatement newStat = ast.newVariableDeclarationStatement(placeholder);
newStat.setType(type);
ListRewrite listRewrite = rewrite.getListRewrite(varDecl.getParent(), Block.STATEMENTS_PROPERTY);
if (varDecl.fragments().indexOf(declNode) == 0) {
// if it as the first in the list-> insert before
listRewrite.insertBefore(newStat, parent, null);
} else {
listRewrite.insertAfter(newStat, parent, null);
}
} else {
rewrite.set(varDecl, VariableDeclarationStatement.TYPE_PROPERTY, type, null);
DimensionRewrite.removeAllChildren(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
}
} else if (parent instanceof VariableDeclarationExpression) {
VariableDeclarationExpression varDecl = (VariableDeclarationExpression) parent;
rewrite.set(varDecl, VariableDeclarationExpression.TYPE_PROPERTY, type, null);
DimensionRewrite.removeAllChildren(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
}
} else if (declNode instanceof SingleVariableDeclaration) {
SingleVariableDeclaration variableDeclaration = (SingleVariableDeclaration) declNode;
rewrite.set(variableDeclaration, SingleVariableDeclaration.TYPE_PROPERTY, type, null);
DimensionRewrite.removeAllChildren(declNode, SingleVariableDeclaration.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
}
// set up linked mode
//$NON-NLS-1$
final String KEY_TYPE = "type";
addLinkedPosition(rewrite.track(type), true, KEY_TYPE);
if (fTypeProposals != null) {
for (int i = 0; i < fTypeProposals.length; i++) {
addLinkedPositionProposal(KEY_TYPE, fTypeProposals[i]);
}
}
return rewrite;
}
return null;
}
Aggregations