use of org.eclipse.jdt.core.dom.VariableDeclarationStatement in project che by eclipse.
the class CallInliner method computeRealArguments.
private void computeRealArguments() {
List<Expression> arguments = Invocations.getArguments(fInvocation);
Set<Expression> canNotInline = crossCheckArguments(arguments);
boolean needsVarargBoxing = needsVarargBoxing(arguments);
int varargIndex = fSourceProvider.getVarargIndex();
AST ast = fInvocation.getAST();
Expression[] realArguments = new Expression[needsVarargBoxing ? varargIndex + 1 : arguments.size()];
for (int i = 0; i < (needsVarargBoxing ? varargIndex : arguments.size()); i++) {
Expression expression = arguments.get(i);
ParameterData parameter = fSourceProvider.getParameterData(i);
if (canInline(expression, parameter) && !canNotInline.contains(expression)) {
realArguments[i] = expression;
} else {
String name = fInvocationScope.createName(parameter.getName(), true);
realArguments[i] = ast.newSimpleName(name);
VariableDeclarationStatement local = createLocalDeclaration(parameter.getTypeBinding(), name, (Expression) fRewrite.createCopyTarget(expression));
if (parameter.isFinal()) {
local.modifiers().add(fInvocation.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD));
}
fLocals.add(local);
}
}
if (needsVarargBoxing) {
ParameterData parameter = fSourceProvider.getParameterData(varargIndex);
String name = fInvocationScope.createName(parameter.getName(), true);
realArguments[varargIndex] = ast.newSimpleName(name);
Type type = fImportRewrite.addImport(parameter.getTypeBinding(), ast);
VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
fragment.setName(ast.newSimpleName(name));
ArrayInitializer initializer = ast.newArrayInitializer();
for (int i = varargIndex; i < arguments.size(); i++) {
initializer.expressions().add(fRewrite.createCopyTarget(arguments.get(i)));
}
fragment.setInitializer(initializer);
VariableDeclarationStatement decl = ast.newVariableDeclarationStatement(fragment);
decl.setType(type);
fLocals.add(decl);
}
fContext.compilationUnit = fCUnit;
fContext.arguments = realArguments;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationStatement in project che by eclipse.
the class CallInliner method createLocalDeclaration.
private VariableDeclarationStatement createLocalDeclaration(ITypeBinding type, String name, Expression initializer) {
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fTargetNode, fImportRewrite);
String typeName = fImportRewrite.addImport(type, context);
VariableDeclarationStatement decl = (VariableDeclarationStatement) ASTNodeFactory.newStatement(fInvocation.getAST(), //$NON-NLS-1$ //$NON-NLS-2$
typeName + " " + name + ";");
((VariableDeclarationFragment) decl.fragments().get(0)).setInitializer(initializer);
return decl;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationStatement in project che by eclipse.
the class RemoveDeclarationCorrectionProposal method removeVariableReferences.
/**
* Remove the field or variable declaration including the initializer.
* @param rewrite the ast rewrite
* @param reference the reference
*/
private void removeVariableReferences(ASTRewrite rewrite, SimpleName reference) {
ASTNode parent = reference.getParent();
while (parent instanceof QualifiedName) {
parent = parent.getParent();
}
if (parent instanceof FieldAccess) {
parent = parent.getParent();
}
int nameParentType = parent.getNodeType();
if (nameParentType == ASTNode.ASSIGNMENT) {
Assignment assignment = (Assignment) parent;
Expression rightHand = assignment.getRightHandSide();
ASTNode assignParent = assignment.getParent();
if (assignParent.getNodeType() == ASTNode.EXPRESSION_STATEMENT && rightHand.getNodeType() != ASTNode.ASSIGNMENT) {
removeVariableWithInitializer(rewrite, rightHand, assignParent);
} else {
rewrite.replace(assignment, rewrite.createCopyTarget(rightHand), null);
}
} else if (nameParentType == ASTNode.SINGLE_VARIABLE_DECLARATION) {
rewrite.remove(parent, null);
} else if (nameParentType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
VariableDeclarationFragment frag = (VariableDeclarationFragment) parent;
ASTNode varDecl = frag.getParent();
List<VariableDeclarationFragment> fragments;
if (varDecl instanceof VariableDeclarationExpression) {
fragments = ((VariableDeclarationExpression) varDecl).fragments();
} else if (varDecl instanceof FieldDeclaration) {
fragments = ((FieldDeclaration) varDecl).fragments();
} else {
fragments = ((VariableDeclarationStatement) varDecl).fragments();
}
if (fragments.size() == 1) {
rewrite.remove(varDecl, null);
} else {
// don't try to preserve
rewrite.remove(frag, null);
}
}
}
use of org.eclipse.jdt.core.dom.VariableDeclarationStatement in project flux 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 flux by eclipse.
the class QuickAssistProcessor method getConvertToStringBufferProposal.
private static LinkedCorrectionProposal getConvertToStringBufferProposal(IInvocationContext context, AST ast, InfixExpression oldInfixExpression) {
String bufferOrBuilderName;
ICompilationUnit cu = context.getCompilationUnit();
if (JavaModelUtil.is50OrHigher(cu.getJavaProject())) {
//$NON-NLS-1$
bufferOrBuilderName = "StringBuilder";
} else {
//$NON-NLS-1$
bufferOrBuilderName = "StringBuffer";
}
ASTRewrite rewrite = ASTRewrite.create(ast);
SimpleName existingBuffer = getEnclosingAppendBuffer(oldInfixExpression);
String mechanismName = BasicElementLabels.getJavaElementName(existingBuffer == null ? bufferOrBuilderName : existingBuffer.getIdentifier());
String label = Messages.format(CorrectionMessages.QuickAssistProcessor_convert_to_string_buffer_description, mechanismName);
//Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.CONVERT_TO_STRING_BUFFER);
proposal.setCommandId(CONVERT_TO_STRING_BUFFER_ID);
Statement insertAfter;
String bufferName;
//$NON-NLS-1$
String groupID = "nameId";
ListRewrite listRewrite;
Statement enclosingStatement = ASTResolving.findParentStatement(oldInfixExpression);
if (existingBuffer != null) {
if (ASTNodes.isControlStatementBody(enclosingStatement.getLocationInParent())) {
Block newBlock = ast.newBlock();
listRewrite = rewrite.getListRewrite(newBlock, Block.STATEMENTS_PROPERTY);
insertAfter = null;
rewrite.replace(enclosingStatement, newBlock, null);
} else {
listRewrite = rewrite.getListRewrite(enclosingStatement.getParent(), (ChildListPropertyDescriptor) enclosingStatement.getLocationInParent());
insertAfter = enclosingStatement;
}
bufferName = existingBuffer.getIdentifier();
} else {
// create buffer
VariableDeclarationFragment frag = ast.newVariableDeclarationFragment();
// check if name is already in use and provide alternative
List<String> fExcludedVariableNames = Arrays.asList(ASTResolving.getUsedVariableNames(oldInfixExpression));
SimpleType bufferType = ast.newSimpleType(ast.newName(bufferOrBuilderName));
ClassInstanceCreation newBufferExpression = ast.newClassInstanceCreation();
//StubUtility.getVariableNameSuggestions(NamingConventions.VK_LOCAL, cu.getJavaProject(), bufferOrBuilderName, 0, fExcludedVariableNames, true);
String[] newBufferNames = new String[] {};
bufferName = newBufferNames[0];
SimpleName bufferNameDeclaration = ast.newSimpleName(bufferName);
frag.setName(bufferNameDeclaration);
proposal.addLinkedPosition(rewrite.track(bufferNameDeclaration), true, groupID);
for (int i = 0; i < newBufferNames.length; i++) {
proposal.addLinkedPositionProposal(groupID, newBufferNames[i], null);
}
newBufferExpression.setType(bufferType);
frag.setInitializer(newBufferExpression);
VariableDeclarationStatement bufferDeclaration = ast.newVariableDeclarationStatement(frag);
bufferDeclaration.setType(ast.newSimpleType(ast.newName(bufferOrBuilderName)));
insertAfter = bufferDeclaration;
Statement statement = ASTResolving.findParentStatement(oldInfixExpression);
if (ASTNodes.isControlStatementBody(statement.getLocationInParent())) {
Block newBlock = ast.newBlock();
listRewrite = rewrite.getListRewrite(newBlock, Block.STATEMENTS_PROPERTY);
listRewrite.insertFirst(bufferDeclaration, null);
listRewrite.insertLast(rewrite.createMoveTarget(statement), null);
rewrite.replace(statement, newBlock, null);
} else {
listRewrite = rewrite.getListRewrite(statement.getParent(), (ChildListPropertyDescriptor) statement.getLocationInParent());
listRewrite.insertBefore(bufferDeclaration, statement, null);
}
}
List<Expression> operands = new ArrayList<Expression>();
collectInfixPlusOperands(oldInfixExpression, operands);
Statement lastAppend = insertAfter;
for (Iterator<Expression> iter = operands.iterator(); iter.hasNext(); ) {
Expression operand = iter.next();
MethodInvocation appendIncovationExpression = ast.newMethodInvocation();
//$NON-NLS-1$
appendIncovationExpression.setName(ast.newSimpleName("append"));
SimpleName bufferNameReference = ast.newSimpleName(bufferName);
// If there was an existing name, don't offer to rename it
if (existingBuffer == null) {
proposal.addLinkedPosition(rewrite.track(bufferNameReference), true, groupID);
}
appendIncovationExpression.setExpression(bufferNameReference);
appendIncovationExpression.arguments().add(rewrite.createCopyTarget(operand));
ExpressionStatement appendExpressionStatement = ast.newExpressionStatement(appendIncovationExpression);
if (lastAppend == null) {
listRewrite.insertFirst(appendExpressionStatement, null);
} else {
listRewrite.insertAfter(appendExpressionStatement, lastAppend, null);
}
lastAppend = appendExpressionStatement;
}
if (existingBuffer != null) {
proposal.setEndPosition(rewrite.track(lastAppend));
if (insertAfter != null) {
rewrite.remove(enclosingStatement, null);
}
} else {
// replace old expression with toString
MethodInvocation bufferToString = ast.newMethodInvocation();
//$NON-NLS-1$
bufferToString.setName(ast.newSimpleName("toString"));
SimpleName bufferNameReference = ast.newSimpleName(bufferName);
bufferToString.setExpression(bufferNameReference);
proposal.addLinkedPosition(rewrite.track(bufferNameReference), true, groupID);
rewrite.replace(oldInfixExpression, bufferToString, null);
proposal.setEndPosition(rewrite.track(bufferToString));
}
return proposal;
}
Aggregations