use of org.eclipse.jdt.core.dom.VariableDeclarationStatement in project che by eclipse.
the class ExtractMethodRefactoring method createCallNodes.
//---- Code generation -----------------------------------------------------------------------
private ASTNode[] createCallNodes(SnippetFinder.Match duplicate, int modifiers) {
List<ASTNode> result = new ArrayList<ASTNode>(2);
IVariableBinding[] locals = fAnalyzer.getCallerLocals();
for (int i = 0; i < locals.length; i++) {
result.add(createDeclaration(locals[i], null));
}
MethodInvocation invocation = fAST.newMethodInvocation();
invocation.setName(fAST.newSimpleName(fMethodName));
ASTNode typeNode = ASTResolving.findParentType(fAnalyzer.getEnclosingBodyDeclaration());
RefactoringStatus status = new RefactoringStatus();
while (fDestination != typeNode) {
fAnalyzer.checkInput(status, fMethodName, typeNode);
if (!status.isOK()) {
SimpleName destinationTypeName = fAST.newSimpleName(ASTNodes.getEnclosingType(fDestination).getName());
if ((modifiers & Modifier.STATIC) == 0) {
ThisExpression thisExpression = fAST.newThisExpression();
thisExpression.setQualifier(destinationTypeName);
invocation.setExpression(thisExpression);
} else {
invocation.setExpression(destinationTypeName);
}
break;
}
typeNode = typeNode.getParent();
}
List<Expression> arguments = invocation.arguments();
for (int i = 0; i < fParameterInfos.size(); i++) {
ParameterInfo parameter = fParameterInfos.get(i);
arguments.add(ASTNodeFactory.newName(fAST, getMappedName(duplicate, parameter)));
}
if (fLinkedProposalModel != null) {
LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
nameGroup.addPosition(fRewriter.track(invocation.getName()), false);
}
ASTNode call;
int returnKind = fAnalyzer.getReturnKind();
switch(returnKind) {
case ExtractMethodAnalyzer.ACCESS_TO_LOCAL:
IVariableBinding binding = fAnalyzer.getReturnLocal();
if (binding != null) {
VariableDeclarationStatement decl = createDeclaration(getMappedBinding(duplicate, binding), invocation);
call = decl;
} else {
Assignment assignment = fAST.newAssignment();
assignment.setLeftHandSide(ASTNodeFactory.newName(fAST, getMappedBinding(duplicate, fAnalyzer.getReturnValue()).getName()));
assignment.setRightHandSide(invocation);
call = assignment;
}
break;
case ExtractMethodAnalyzer.RETURN_STATEMENT_VALUE:
ReturnStatement rs = fAST.newReturnStatement();
rs.setExpression(invocation);
call = rs;
break;
default:
call = invocation;
}
if (call instanceof Expression && !fAnalyzer.isExpressionSelected()) {
call = fAST.newExpressionStatement((Expression) call);
}
result.add(call);
// return;
if (returnKind == ExtractMethodAnalyzer.RETURN_STATEMENT_VOID && !fAnalyzer.isLastStatementSelected()) {
result.add(fAST.newReturnStatement());
}
return result.toArray(new ASTNode[result.size()]);
}
use of org.eclipse.jdt.core.dom.VariableDeclarationStatement 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.VariableDeclarationStatement in project che by eclipse.
the class Java50Fix method getRawReference.
private static SimpleType getRawReference(SimpleName name, CompilationUnit compilationUnit) {
SimpleName[] names = LinkedNodeFinder.findByNode(compilationUnit, name);
for (int j = 0; j < names.length; j++) {
if (names[j].getParent() instanceof VariableDeclarationFragment) {
VariableDeclarationFragment fragment = (VariableDeclarationFragment) names[j].getParent();
if (fragment.getParent() instanceof VariableDeclarationStatement) {
VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
ASTNode result = (ASTNode) statement.getStructuralProperty(VariableDeclarationStatement.TYPE_PROPERTY);
if (isRawTypeReference(result))
return (SimpleType) result;
} else if (fragment.getParent() instanceof FieldDeclaration) {
FieldDeclaration declaration = (FieldDeclaration) fragment.getParent();
ASTNode result = (ASTNode) declaration.getStructuralProperty(FieldDeclaration.TYPE_PROPERTY);
if (isRawTypeReference(result))
return (SimpleType) result;
}
} else if (names[j].getParent() instanceof SingleVariableDeclaration) {
SingleVariableDeclaration declaration = (SingleVariableDeclaration) names[j].getParent();
ASTNode result = (ASTNode) declaration.getStructuralProperty(SingleVariableDeclaration.TYPE_PROPERTY);
if (isRawTypeReference(result))
return (SimpleType) result;
} else if (names[j].getParent() instanceof MethodDeclaration) {
MethodDeclaration methodDecl = (MethodDeclaration) names[j].getParent();
ASTNode result = (ASTNode) methodDecl.getStructuralProperty(MethodDeclaration.RETURN_TYPE2_PROPERTY);
if (isRawTypeReference(result))
return (SimpleType) result;
}
}
return null;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationStatement in project che by eclipse.
the class VariableDeclarationRewrite method rewriteModifiers.
public static void rewriteModifiers(final VariableDeclarationStatement declarationNode, final VariableDeclarationFragment[] toChange, final int includedModifiers, final int excludedModifiers, ASTRewrite rewrite, final TextEditGroup group) {
final List<VariableDeclarationFragment> fragmentsToChange = Arrays.asList(toChange);
AST ast = declarationNode.getAST();
List<VariableDeclarationFragment> fragments = declarationNode.fragments();
Iterator<VariableDeclarationFragment> iter = fragments.iterator();
ListRewrite blockRewrite = null;
ASTNode parentStatement = declarationNode.getParent();
if (parentStatement instanceof SwitchStatement) {
blockRewrite = rewrite.getListRewrite(parentStatement, SwitchStatement.STATEMENTS_PROPERTY);
} else if (parentStatement instanceof Block) {
blockRewrite = rewrite.getListRewrite(parentStatement, Block.STATEMENTS_PROPERTY);
} else {
// should not happen. VariableDeclaration's can not be in a control statement body
Assert.isTrue(false);
}
VariableDeclarationFragment lastFragment = iter.next();
ASTNode lastStatement = declarationNode;
boolean modifiersModified = false;
if (fragmentsToChange.contains(lastFragment)) {
ModifierRewrite modifierRewrite = ModifierRewrite.create(rewrite, declarationNode);
modifierRewrite.setModifiers(includedModifiers, excludedModifiers, group);
modifiersModified = true;
}
ListRewrite fragmentsRewrite = null;
while (iter.hasNext()) {
VariableDeclarationFragment currentFragment = iter.next();
if (fragmentsToChange.contains(lastFragment) != fragmentsToChange.contains(currentFragment)) {
VariableDeclarationStatement newStatement = ast.newVariableDeclarationStatement((VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment));
newStatement.setType((Type) rewrite.createCopyTarget(declarationNode.getType()));
ModifierRewrite modifierRewrite = ModifierRewrite.create(rewrite, newStatement);
if (fragmentsToChange.contains(currentFragment)) {
modifierRewrite.copyAllAnnotations(declarationNode, group);
int newModifiers = (declarationNode.getModifiers() & ~excludedModifiers) | includedModifiers;
modifierRewrite.setModifiers(newModifiers, excludedModifiers, group);
} else {
modifierRewrite.copyAllModifiers(declarationNode, group, modifiersModified);
}
blockRewrite.insertAfter(newStatement, lastStatement, group);
fragmentsRewrite = rewrite.getListRewrite(newStatement, VariableDeclarationStatement.FRAGMENTS_PROPERTY);
lastStatement = newStatement;
} else if (fragmentsRewrite != null) {
ASTNode fragment0 = rewrite.createMoveTarget(currentFragment);
fragmentsRewrite.insertLast(fragment0, group);
}
lastFragment = currentFragment;
}
}
use of org.eclipse.jdt.core.dom.VariableDeclarationStatement in project che by eclipse.
the class ConvertForLoopOperation method convertBody.
private void convertBody(Statement body, final IBinding indexBinding, final IBinding arrayBinding, final String parameterName, final ASTRewrite rewrite, final TextEditGroup editGroup, final LinkedProposalPositionGroup pg) {
final AST ast = body.getAST();
body.accept(new GenericVisitor() {
@Override
public boolean visit(ArrayAccess node) {
IBinding binding = getBinding(node.getArray());
if (arrayBinding.equals(binding)) {
IBinding index = getBinding(node.getIndex());
if (indexBinding.equals(index)) {
replaceAccess(node);
}
}
return super.visit(node);
}
private void replaceAccess(ASTNode node) {
if (fElementDeclaration != null && node.getLocationInParent() == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
VariableDeclarationFragment fragment = (VariableDeclarationFragment) node.getParent();
IBinding targetBinding = fragment.getName().resolveBinding();
if (targetBinding != null) {
VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
if (statement.fragments().size() == 1) {
rewrite.remove(statement, editGroup);
} else {
ListRewrite listRewrite = rewrite.getListRewrite(statement, VariableDeclarationStatement.FRAGMENTS_PROPERTY);
listRewrite.remove(fragment, editGroup);
}
} else {
SimpleName name = ast.newSimpleName(parameterName);
rewrite.replace(node, name, editGroup);
pg.addPosition(rewrite.track(name), true);
}
} else {
SimpleName name = ast.newSimpleName(parameterName);
rewrite.replace(node, name, editGroup);
pg.addPosition(rewrite.track(name), true);
}
}
});
}
Aggregations