use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project AutoRefactor by JnRouvignac.
the class ASTBuilder method declareExpr.
/**
* Builds a new {@link VariableDeclarationExpression} instance.
*
* @param type
* the type of the variable being declared
* @param varName
* the name of the variable being declared
* @param initializer
* the variable initializer, can be null
* @return a new variable declaration expression
*/
public VariableDeclarationExpression declareExpr(Type type, SimpleName varName, Expression initializer) {
final VariableDeclarationFragment fragment = declareFragment(varName, initializer);
final VariableDeclarationExpression vde = ast.newVariableDeclarationExpression(fragment);
modifiers(vde).add(final0());
vde.setType(type);
return vde;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project bayou by capergroup.
the class DOMVariableDeclarationExpression method handle.
@Override
public DSubTree handle() {
DSubTree tree = new DSubTree();
for (Object o : expression.fragments()) {
VariableDeclarationFragment fragment = (VariableDeclarationFragment) o;
DSubTree t = new DOMVariableDeclarationFragment(fragment, visitor).handle();
tree.addNodes(t.getNodes());
}
return tree;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project eclipse-pmd by acanda.
the class SingularFieldQuickFix method replaceAssignment.
/**
* Replaces the assignment with a variable declaration. If the assignment is the only one in the block for this
* variable, the final modifier is added to the declaration.
*/
@SuppressWarnings("unchecked")
private void replaceAssignment(final VariableDeclarationFragment node, final Assignment assignment, final boolean finalDeclaration) {
final FieldDeclaration fieldDeclaration = (FieldDeclaration) node.getParent();
final VariableDeclarationStatement declaration = (VariableDeclarationStatement) node.getAST().createInstance(VariableDeclarationStatement.class);
declaration.setType(ASTUtil.copy(fieldDeclaration.getType()));
final VariableDeclarationFragment fragment = (VariableDeclarationFragment) node.getAST().createInstance(VariableDeclarationFragment.class);
fragment.setName(ASTUtil.copy(node.getName()));
fragment.setInitializer(ASTUtil.copy(assignment.getRightHandSide()));
declaration.fragments().add(fragment);
if (finalDeclaration) {
final Modifier modifier = (Modifier) node.getAST().createInstance(Modifier.class);
modifier.setKeyword(ModifierKeyword.FINAL_KEYWORD);
declaration.modifiers().add(modifier);
}
ASTUtil.replace(assignment.getParent(), declaration);
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project flux by eclipse.
the class ASTResolving method getPossibleReferenceBinding.
private static ITypeBinding getPossibleReferenceBinding(ASTNode node) {
ASTNode parent = node.getParent();
switch(parent.getNodeType()) {
case ASTNode.ASSIGNMENT:
Assignment assignment = (Assignment) parent;
if (node.equals(assignment.getLeftHandSide())) {
// field write access: xx= expression
return assignment.getRightHandSide().resolveTypeBinding();
}
// read access
return assignment.getLeftHandSide().resolveTypeBinding();
case ASTNode.INFIX_EXPRESSION:
InfixExpression infix = (InfixExpression) parent;
InfixExpression.Operator op = infix.getOperator();
if (op == InfixExpression.Operator.CONDITIONAL_AND || op == InfixExpression.Operator.CONDITIONAL_OR) {
//$NON-NLS-1$
return infix.getAST().resolveWellKnownType("boolean");
} else if (op == InfixExpression.Operator.LEFT_SHIFT || op == InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED || op == InfixExpression.Operator.RIGHT_SHIFT_SIGNED) {
//$NON-NLS-1$
return infix.getAST().resolveWellKnownType("int");
}
if (node.equals(infix.getLeftOperand())) {
// xx operation expression
ITypeBinding rigthHandBinding = infix.getRightOperand().resolveTypeBinding();
if (rigthHandBinding != null) {
return rigthHandBinding;
}
} else {
// expression operation xx
ITypeBinding leftHandBinding = infix.getLeftOperand().resolveTypeBinding();
if (leftHandBinding != null) {
return leftHandBinding;
}
}
if (op != InfixExpression.Operator.EQUALS && op != InfixExpression.Operator.NOT_EQUALS) {
//$NON-NLS-1$
return infix.getAST().resolveWellKnownType("int");
}
break;
case ASTNode.INSTANCEOF_EXPRESSION:
InstanceofExpression instanceofExpression = (InstanceofExpression) parent;
return instanceofExpression.getRightOperand().resolveBinding();
case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
VariableDeclarationFragment frag = (VariableDeclarationFragment) parent;
if (frag.getInitializer().equals(node)) {
return frag.getName().resolveTypeBinding();
}
break;
case ASTNode.SUPER_METHOD_INVOCATION:
SuperMethodInvocation superMethodInvocation = (SuperMethodInvocation) parent;
IMethodBinding superMethodBinding = ASTNodes.getMethodBinding(superMethodInvocation.getName());
if (superMethodBinding != null) {
return getParameterTypeBinding(node, superMethodInvocation.arguments(), superMethodBinding);
}
break;
case ASTNode.METHOD_INVOCATION:
MethodInvocation methodInvocation = (MethodInvocation) parent;
IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
if (methodBinding != null) {
return getParameterTypeBinding(node, methodInvocation.arguments(), methodBinding);
}
break;
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
{
SuperConstructorInvocation superInvocation = (SuperConstructorInvocation) parent;
IMethodBinding superBinding = superInvocation.resolveConstructorBinding();
if (superBinding != null) {
return getParameterTypeBinding(node, superInvocation.arguments(), superBinding);
}
break;
}
case ASTNode.CONSTRUCTOR_INVOCATION:
{
ConstructorInvocation constrInvocation = (ConstructorInvocation) parent;
IMethodBinding constrBinding = constrInvocation.resolveConstructorBinding();
if (constrBinding != null) {
return getParameterTypeBinding(node, constrInvocation.arguments(), constrBinding);
}
break;
}
case ASTNode.CLASS_INSTANCE_CREATION:
{
ClassInstanceCreation creation = (ClassInstanceCreation) parent;
IMethodBinding creationBinding = creation.resolveConstructorBinding();
if (creationBinding != null) {
return getParameterTypeBinding(node, creation.arguments(), creationBinding);
}
break;
}
case ASTNode.PARENTHESIZED_EXPRESSION:
return guessBindingForReference(parent);
case ASTNode.ARRAY_ACCESS:
if (((ArrayAccess) parent).getIndex().equals(node)) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
} else {
ITypeBinding parentBinding = getPossibleReferenceBinding(parent);
if (parentBinding == null) {
//$NON-NLS-1$
parentBinding = parent.getAST().resolveWellKnownType("java.lang.Object");
}
return parentBinding.createArrayType(1);
}
case ASTNode.ARRAY_CREATION:
if (((ArrayCreation) parent).dimensions().contains(node)) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
}
break;
case ASTNode.ARRAY_INITIALIZER:
ASTNode initializerParent = parent.getParent();
int dim = 1;
while (initializerParent instanceof ArrayInitializer) {
initializerParent = initializerParent.getParent();
dim++;
}
Type creationType = null;
if (initializerParent instanceof ArrayCreation) {
creationType = ((ArrayCreation) initializerParent).getType();
} else if (initializerParent instanceof VariableDeclaration) {
VariableDeclaration varDecl = (VariableDeclaration) initializerParent;
creationType = ASTNodes.getType(varDecl);
dim -= varDecl.getExtraDimensions();
} else if (initializerParent instanceof MemberValuePair) {
String name = ((MemberValuePair) initializerParent).getName().getIdentifier();
IMethodBinding annotMember = findAnnotationMember((Annotation) initializerParent.getParent(), name);
if (annotMember != null) {
return getReducedDimensionBinding(annotMember.getReturnType(), dim);
}
}
if (creationType instanceof ArrayType) {
ITypeBinding creationTypeBinding = ((ArrayType) creationType).resolveBinding();
if (creationTypeBinding != null) {
return Bindings.getComponentType(creationTypeBinding, dim);
}
}
break;
case ASTNode.CONDITIONAL_EXPRESSION:
ConditionalExpression expression = (ConditionalExpression) parent;
if (node.equals(expression.getExpression())) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("boolean");
}
if (node.equals(expression.getElseExpression())) {
return expression.getThenExpression().resolveTypeBinding();
}
return expression.getElseExpression().resolveTypeBinding();
case ASTNode.POSTFIX_EXPRESSION:
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
case ASTNode.PREFIX_EXPRESSION:
if (((PrefixExpression) parent).getOperator() == PrefixExpression.Operator.NOT) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("boolean");
}
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
case ASTNode.IF_STATEMENT:
case ASTNode.WHILE_STATEMENT:
case ASTNode.DO_STATEMENT:
if (node instanceof Expression) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("boolean");
}
break;
case ASTNode.SWITCH_STATEMENT:
if (((SwitchStatement) parent).getExpression().equals(node)) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
}
break;
case ASTNode.RETURN_STATEMENT:
MethodDeclaration decl = ASTResolving.findParentMethodDeclaration(parent);
if (decl != null && !decl.isConstructor()) {
return decl.getReturnType2().resolveBinding();
}
LambdaExpression lambdaExpr = ASTResolving.findEnclosingLambdaExpression(parent);
if (lambdaExpr != null) {
IMethodBinding lambdaMethodBinding = lambdaExpr.resolveMethodBinding();
if (lambdaMethodBinding != null && lambdaMethodBinding.getReturnType() != null) {
return lambdaMethodBinding.getReturnType();
}
}
break;
case ASTNode.CAST_EXPRESSION:
return ((CastExpression) parent).getType().resolveBinding();
case ASTNode.THROW_STATEMENT:
case ASTNode.CATCH_CLAUSE:
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("java.lang.Exception");
case ASTNode.FIELD_ACCESS:
if (node.equals(((FieldAccess) parent).getName())) {
return getPossibleReferenceBinding(parent);
}
break;
case ASTNode.SUPER_FIELD_ACCESS:
return getPossibleReferenceBinding(parent);
case ASTNode.QUALIFIED_NAME:
if (node.equals(((QualifiedName) parent).getName())) {
return getPossibleReferenceBinding(parent);
}
break;
case ASTNode.SWITCH_CASE:
if (node.equals(((SwitchCase) parent).getExpression()) && parent.getParent() instanceof SwitchStatement) {
return ((SwitchStatement) parent.getParent()).getExpression().resolveTypeBinding();
}
break;
case ASTNode.ASSERT_STATEMENT:
if (node.getLocationInParent() == AssertStatement.EXPRESSION_PROPERTY) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("boolean");
}
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("java.lang.String");
case ASTNode.SINGLE_MEMBER_ANNOTATION:
{
//$NON-NLS-1$
IMethodBinding annotMember = findAnnotationMember((Annotation) parent, "value");
if (annotMember != null) {
return annotMember.getReturnType();
}
break;
}
case ASTNode.MEMBER_VALUE_PAIR:
{
String name = ((MemberValuePair) parent).getName().getIdentifier();
IMethodBinding annotMember = findAnnotationMember((Annotation) parent.getParent(), name);
if (annotMember != null) {
return annotMember.getReturnType();
}
break;
}
default:
}
return null;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getReplaceConditionalWithIfElseProposals.
private static boolean getReplaceConditionalWithIfElseProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
ASTNode node = covering;
while (!(node instanceof ConditionalExpression) && node instanceof Expression) {
node = node.getParent();
}
if (!(node instanceof ConditionalExpression)) {
node = covering;
while (node != null && !(node instanceof Statement)) {
node = node.getParent();
}
if (node instanceof VariableDeclarationStatement) {
node = (ASTNode) (((VariableDeclarationStatement) node).fragments().get(0));
node = ((VariableDeclarationFragment) node).getInitializer();
}
if (node instanceof ExpressionStatement) {
node = ((ExpressionStatement) node).getExpression();
if (node instanceof Assignment) {
node = ((Assignment) node).getRightHandSide();
}
}
if (node instanceof ReturnStatement) {
node = ((ReturnStatement) node).getExpression();
}
}
if (!(node instanceof ConditionalExpression)) {
return false;
}
covering = node;
StructuralPropertyDescriptor locationInParent = covering.getLocationInParent();
if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY) {
if (covering.getParent().getLocationInParent() != ExpressionStatement.EXPRESSION_PROPERTY) {
return false;
}
} else if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
ASTNode statement = covering.getParent().getParent();
if (!(statement instanceof VariableDeclarationStatement) || statement.getLocationInParent() != Block.STATEMENTS_PROPERTY) {
return false;
}
} else if (locationInParent != ReturnStatement.EXPRESSION_PROPERTY) {
return false;
}
ConditionalExpression conditional = (ConditionalExpression) covering;
// we could produce quick assist
if (resultingCollections == null) {
return true;
}
//
AST ast = covering.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
// prepare new 'if' statement
Expression expression = conditional.getExpression();
while (expression instanceof ParenthesizedExpression) {
expression = ((ParenthesizedExpression) expression).getExpression();
}
IfStatement ifStatement = ast.newIfStatement();
ifStatement.setExpression((Expression) rewrite.createCopyTarget(expression));
if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY) {
Assignment assignment = (Assignment) covering.getParent();
Expression assignee = assignment.getLeftHandSide();
Assignment.Operator op = assignment.getOperator();
ifStatement.setThenStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getThenExpression()));
ifStatement.setElseStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getElseExpression()));
// replace return conditional expression with if/then/else/return
rewrite.replace(covering.getParent().getParent(), ifStatement, null);
} else if (locationInParent == ReturnStatement.EXPRESSION_PROPERTY) {
ifStatement.setThenStatement(createReturnExpression(rewrite, conditional.getThenExpression()));
ifStatement.setElseStatement(createReturnExpression(rewrite, conditional.getElseExpression()));
//
// replace return conditional expression with if/then/else/return
rewrite.replace(conditional.getParent(), ifStatement, null);
} else if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
VariableDeclarationFragment frag = (VariableDeclarationFragment) covering.getParent();
Assignment.Operator op = Assignment.Operator.ASSIGN;
Expression assignee = frag.getName();
ifStatement.setThenStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getThenExpression()));
ifStatement.setElseStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getElseExpression()));
// clear initializer
rewrite.set(frag, VariableDeclarationFragment.INITIALIZER_PROPERTY, null, null);
ASTNode statement = frag.getParent();
rewrite.getListRewrite(statement.getParent(), Block.STATEMENTS_PROPERTY).insertAfter(ifStatement, statement, null);
}
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_replaceConditionalWithIf;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REPLACE_CONDITIONAL_WITH_IF_ELSE);
resultingCollections.add(proposal);
return true;
}
Aggregations