use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.
the class ExtractMethodRefactoring method createDeclaration.
private VariableDeclarationStatement createDeclaration(IVariableBinding binding, Expression intilizer) {
VariableDeclaration original = ASTNodes.findVariableDeclaration(binding, fAnalyzer.getEnclosingBodyDeclaration());
VariableDeclarationFragment fragment = fAST.newVariableDeclarationFragment();
fragment.setName((SimpleName) ASTNode.copySubtree(fAST, original.getName()));
fragment.setInitializer(intilizer);
VariableDeclarationStatement result = fAST.newVariableDeclarationStatement(fragment);
result.modifiers().addAll(ASTNode.copySubtrees(fAST, ASTNodes.getModifiers(original)));
result.setType(ASTNodeFactory.newType(fAST, original, fImportRewriter, new ContextSensitiveImportRewriteContext(original, fImportRewriter)));
return result;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.
the class PromoteTempToFieldRefactoring method getNamesOfFieldsInDeclaringType.
private String[] getNamesOfFieldsInDeclaringType() {
final AbstractTypeDeclaration type = getEnclosingType();
if (type instanceof TypeDeclaration) {
FieldDeclaration[] fields = ((TypeDeclaration) type).getFields();
List<String> result = new ArrayList<String>(fields.length);
for (int i = 0; i < fields.length; i++) {
for (Iterator<VariableDeclarationFragment> iter = fields[i].fragments().iterator(); iter.hasNext(); ) {
VariableDeclarationFragment field = iter.next();
result.add(field.getName().getIdentifier());
}
}
return result.toArray(new String[result.size()]);
}
return new String[] {};
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.
the class PromoteTempToFieldRefactoring method addLocalDeclarationSplit.
private void addLocalDeclarationSplit(ASTRewrite rewrite) {
VariableDeclarationStatement tempDeclarationStatement = getTempDeclarationStatement();
ASTNode parentStatement = tempDeclarationStatement.getParent();
ListRewrite listRewrite;
if (parentStatement instanceof SwitchStatement) {
listRewrite = rewrite.getListRewrite(parentStatement, SwitchStatement.STATEMENTS_PROPERTY);
} else if (parentStatement instanceof Block) {
listRewrite = rewrite.getListRewrite(parentStatement, Block.STATEMENTS_PROPERTY);
} else {
// should not happen. VariableDeclaration's can not be in a control statement body
listRewrite = null;
Assert.isTrue(false);
}
int statementIndex = listRewrite.getOriginalList().indexOf(tempDeclarationStatement);
Assert.isTrue(statementIndex != -1);
Statement newStatement = createNewAssignmentStatement(rewrite);
List<VariableDeclarationFragment> fragments = tempDeclarationStatement.fragments();
int fragmentIndex = fragments.indexOf(fTempDeclarationNode);
Assert.isTrue(fragmentIndex != -1);
if (fragments.size() == 1) {
rewrite.replace(tempDeclarationStatement, newStatement, null);
return;
}
for (int i1 = fragmentIndex, n = fragments.size(); i1 < n; i1++) {
VariableDeclarationFragment fragment = fragments.get(i1);
rewrite.remove(fragment, null);
}
if (fragmentIndex == 0)
rewrite.remove(tempDeclarationStatement, null);
Assert.isTrue(tempHasInitializer());
listRewrite.insertAt(newStatement, statementIndex + 1, null);
if (fragmentIndex + 1 < fragments.size()) {
VariableDeclarationFragment firstFragmentAfter = fragments.get(fragmentIndex + 1);
VariableDeclarationFragment copyfirstFragmentAfter = (VariableDeclarationFragment) rewrite.createCopyTarget(firstFragmentAfter);
VariableDeclarationStatement statement = getAST().newVariableDeclarationStatement(copyfirstFragmentAfter);
Type type = (Type) rewrite.createCopyTarget(tempDeclarationStatement.getType());
statement.setType(type);
List<IExtendedModifier> modifiers = tempDeclarationStatement.modifiers();
if (modifiers.size() > 0) {
ListRewrite modifiersRewrite = rewrite.getListRewrite(tempDeclarationStatement, VariableDeclarationStatement.MODIFIERS2_PROPERTY);
ASTNode firstModifier = (ASTNode) modifiers.get(0);
ASTNode lastModifier = (ASTNode) modifiers.get(modifiers.size() - 1);
ASTNode modifiersCopy = modifiersRewrite.createCopyTarget(firstModifier, lastModifier);
statement.modifiers().add(modifiersCopy);
}
for (int i = fragmentIndex + 2; i < fragments.size(); i++) {
VariableDeclarationFragment fragment = fragments.get(i);
VariableDeclarationFragment fragmentCopy = (VariableDeclarationFragment) rewrite.createCopyTarget(fragment);
statement.fragments().add(fragmentCopy);
}
listRewrite.insertAt(statement, statementIndex + 2, null);
}
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.
the class ExtractTempRefactoring method getForInitializedVariables.
// return List<IVariableBinding>
private static List<IVariableBinding> getForInitializedVariables(VariableDeclarationExpression variableDeclarations) {
List<IVariableBinding> forInitializerVariables = new ArrayList<IVariableBinding>(1);
for (Iterator<VariableDeclarationFragment> iter = variableDeclarations.fragments().iterator(); iter.hasNext(); ) {
VariableDeclarationFragment fragment = iter.next();
IVariableBinding binding = fragment.resolveBinding();
if (binding != null)
forInitializerVariables.add(binding);
}
return forInitializerVariables;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.
the class InlineConstantRefactoring method findDeclaration.
private RefactoringStatus findDeclaration() throws JavaModelException {
fDeclarationSelectedChecked = true;
fDeclarationSelected = false;
ASTNode parent = fSelectedConstantName.getParent();
if (parent instanceof VariableDeclarationFragment) {
VariableDeclarationFragment parentDeclaration = (VariableDeclarationFragment) parent;
if (parentDeclaration.getName() == fSelectedConstantName) {
fDeclarationSelected = true;
fDeclarationCuRewrite = fSelectionCuRewrite;
fDeclaration = (VariableDeclarationFragment) fSelectedConstantName.getParent();
return null;
}
}
VariableDeclarationFragment declaration = (VariableDeclarationFragment) fSelectionCuRewrite.getRoot().findDeclaringNode(fSelectedConstantName.resolveBinding());
if (declaration != null) {
fDeclarationCuRewrite = fSelectionCuRewrite;
fDeclaration = declaration;
return null;
}
if (fField.getCompilationUnit() == null)
return RefactoringStatus.createStatus(RefactoringStatus.FATAL, RefactoringCoreMessages.InlineConstantRefactoring_binary_file, null, Corext.getPluginId(), RefactoringStatusCodes.DECLARED_IN_CLASSFILE, null);
fDeclarationCuRewrite = new CompilationUnitRewrite(fField.getCompilationUnit());
fDeclaration = ASTNodeSearchUtil.getFieldDeclarationFragmentNode(fField, fDeclarationCuRewrite.getRoot());
return null;
}
Aggregations