blob: 90fbc0dd864175b52bd6e2b0e060fd108495820b [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2000, 2003 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.internal.ui.text.correction;
import org.eclipse.swt.graphics.Image;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
import org.eclipse.jdt.internal.corext.dom.ASTNodeConstants;
import org.eclipse.jdt.internal.corext.dom.ASTRewrite;
import org.eclipse.jdt.internal.corext.textmanipulation.GroupDescription;
public class ModifierChangeCompletionProposal extends LinkedCorrectionProposal {
private IBinding fBinding;
private ASTNode fNode;
private int fIncludedModifiers;
private int fExcludedModifiers;
public ModifierChangeCompletionProposal(String label, ICompilationUnit targetCU, IBinding binding, ASTNode node, int includedModifiers, int excludedModifiers, int relevance, Image image) {
super(label, targetCU, null, relevance, image);
fBinding= binding;
fNode= node;
fIncludedModifiers= includedModifiers;
fExcludedModifiers= excludedModifiers;
}
protected ASTRewrite getRewrite() {
CompilationUnit astRoot= ASTResolving.findParentCompilationUnit(fNode);
ASTNode boundNode= astRoot.findDeclaringNode(fBinding);
ASTNode declNode= null;
GroupDescription selectionDescription= null;
if (boundNode != null) {
declNode= boundNode; // is same CU
} else {
selectionDescription= new GroupDescription("selection"); // in different CU, needs selection //$NON-NLS-1$
setSelectionDescription(selectionDescription);
CompilationUnit newRoot= AST.parseCompilationUnit(getCompilationUnit(), true);
declNode= newRoot.findDeclaringNode(fBinding.getKey());
}
if (declNode != null) {
ASTRewrite rewrite= new ASTRewrite(declNode.getParent());
if (declNode instanceof MethodDeclaration) {
MethodDeclaration methodDecl= (MethodDeclaration) declNode;
int newModifiers= (methodDecl.getModifiers() & ~fExcludedModifiers) | fIncludedModifiers;
rewrite.markAsReplaced(methodDecl, ASTNodeConstants.MODIFIERS, new Integer(newModifiers), selectionDescription);
} else if (declNode instanceof VariableDeclarationFragment) {
ASTNode parent= declNode.getParent();
if (parent instanceof FieldDeclaration) {
FieldDeclaration fieldDecl= (FieldDeclaration) parent;
int newModifiers= (fieldDecl.getModifiers() & ~fExcludedModifiers) | fIncludedModifiers;
rewrite.markAsReplaced(fieldDecl, ASTNodeConstants.MODIFIERS, new Integer(newModifiers), selectionDescription);
} else if (parent instanceof VariableDeclarationStatement) {
VariableDeclarationStatement varDecl= (VariableDeclarationStatement) parent;
int newModifiers= (varDecl.getModifiers() & ~fExcludedModifiers) | fIncludedModifiers;
rewrite.markAsReplaced(varDecl, ASTNodeConstants.MODIFIERS, new Integer(newModifiers), selectionDescription);
} else if (parent instanceof VariableDeclarationExpression) {
VariableDeclarationExpression varDecl= (VariableDeclarationExpression) parent;
int newModifiers= (varDecl.getModifiers() & ~fExcludedModifiers) | fIncludedModifiers;
rewrite.markAsReplaced(varDecl, ASTNodeConstants.MODIFIERS, new Integer(newModifiers), selectionDescription);
}
} else if (declNode instanceof SingleVariableDeclaration) {
SingleVariableDeclaration variableDeclaration= (SingleVariableDeclaration) declNode;
int newModifiers= (variableDeclaration.getModifiers() & ~fExcludedModifiers) | fIncludedModifiers;
rewrite.markAsReplaced(variableDeclaration, ASTNodeConstants.MODIFIERS, new Integer(newModifiers), selectionDescription);
} else if (declNode instanceof TypeDeclaration) {
TypeDeclaration typeDecl= (TypeDeclaration) declNode;
int newModifiers= (typeDecl.getModifiers() & ~fExcludedModifiers) | fIncludedModifiers;
rewrite.markAsReplaced(typeDecl, ASTNodeConstants.MODIFIERS, new Integer(newModifiers), selectionDescription);
}
return rewrite;
}
return null;
}
}