Java Code Examples for org.eclipse.jdt.core.dom.AST#newExpressionStatement()
The following examples show how to use
org.eclipse.jdt.core.dom.AST#newExpressionStatement() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CreateSuperCallResolution.java From spotbugs with GNU Lesser General Public License v2.1 | 6 votes |
@Override protected void repairBug(ASTRewrite rewrite, CompilationUnit workingUnit, BugInstance bug) throws BugResolutionException { Assert.isNotNull(rewrite); Assert.isNotNull(workingUnit); Assert.isNotNull(bug); TypeDeclaration type = getTypeDeclaration(workingUnit, bug.getPrimaryClass()); MethodDeclaration method = getMethodDeclaration(type, bug.getPrimaryMethod()); AST ast = rewrite.getAST(); SuperMethodInvocation superCall = createSuperMethodInvocation(rewrite, method); ExpressionStatement statement = ast.newExpressionStatement(superCall); Block methodBody = method.getBody(); ListRewrite listRewrite = rewrite.getListRewrite(methodBody, Block.STATEMENTS_PROPERTY); if (isInsertFirst()) { listRewrite.insertFirst(statement, null); } else { listRewrite.insertLast(statement, null); } }
Example 2
Source File: QuickAssistProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private static Block getBlockBodyForLambda(Expression bodyExpr, ITypeBinding returnTypeBinding, AST ast) { Statement statementInBlockBody; if (ast.resolveWellKnownType("void").isEqualTo(returnTypeBinding)) { //$NON-NLS-1$ ExpressionStatement expressionStatement = ast.newExpressionStatement(bodyExpr); statementInBlockBody = expressionStatement; } else { ReturnStatement returnStatement = ast.newReturnStatement(); returnStatement.setExpression(bodyExpr); statementInBlockBody = returnStatement; } Block blockBody = ast.newBlock(); blockBody.statements().add(statementInBlockBody); return blockBody; }
Example 3
Source File: ExtractFieldRefactoring.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private Statement createNewAssignmentStatement() throws JavaModelException { AST ast = fCURewrite.getAST(); Assignment assignment = ast.newAssignment(); SimpleName fieldName = ast.newSimpleName(fFieldName); ASTRewrite rewrite = fCURewrite.getASTRewrite(); if (fLinkedProposalModel != null) { fLinkedProposalModel.getPositionGroup(KEY_NAME, true).addPosition(rewrite.track(fieldName), true); } assignment.setLeftHandSide(wrapAsFieldAccessExpression(fieldName)); assignment.setRightHandSide(getSelectedExpression().createCopyTarget(rewrite, true)); return ast.newExpressionStatement(assignment); }
Example 4
Source File: MoveInnerToTopRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private void createConstructor(final AbstractTypeDeclaration declaration, final ASTRewrite rewrite) throws CoreException { Assert.isNotNull(declaration); Assert.isNotNull(rewrite); final AST ast= declaration.getAST(); final MethodDeclaration constructor= ast.newMethodDeclaration(); constructor.setConstructor(true); constructor.setName(ast.newSimpleName(declaration.getName().getIdentifier())); final String comment= CodeGeneration.getMethodComment(fType.getCompilationUnit(), fType.getElementName(), fType.getElementName(), getNewConstructorParameterNames(), new String[0], null, null, StubUtility.getLineDelimiterUsed(fType.getJavaProject())); if (comment != null && comment.length() > 0) { final Javadoc doc= (Javadoc) rewrite.createStringPlaceholder(comment, ASTNode.JAVADOC); constructor.setJavadoc(doc); } if (fCreateInstanceField) { final SingleVariableDeclaration variable= ast.newSingleVariableDeclaration(); final String name= getNameForEnclosingInstanceConstructorParameter(); variable.setName(ast.newSimpleName(name)); variable.setType(createEnclosingType(ast)); constructor.parameters().add(variable); final Block body= ast.newBlock(); final Assignment assignment= ast.newAssignment(); if (fCodeGenerationSettings.useKeywordThis || fEnclosingInstanceFieldName.equals(fNameForEnclosingInstanceConstructorParameter)) { final FieldAccess access= ast.newFieldAccess(); access.setExpression(ast.newThisExpression()); access.setName(ast.newSimpleName(fEnclosingInstanceFieldName)); assignment.setLeftHandSide(access); } else assignment.setLeftHandSide(ast.newSimpleName(fEnclosingInstanceFieldName)); assignment.setRightHandSide(ast.newSimpleName(name)); final Statement statement= ast.newExpressionStatement(assignment); body.statements().add(statement); constructor.setBody(body); } else constructor.setBody(ast.newBlock()); rewrite.getListRewrite(declaration, declaration.getBodyDeclarationsProperty()).insertFirst(constructor, null); }
Example 5
Source File: ParameterObjectFactory.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public ExpressionStatement createInitializer(ParameterInfo pi, String paramName, CompilationUnitRewrite cuRewrite) { AST ast= cuRewrite.getAST(); VariableDeclarationFragment fragment= ast.newVariableDeclarationFragment(); fragment.setName(ast.newSimpleName(pi.getOldName())); fragment.setInitializer(createFieldReadAccess(pi, paramName, ast, cuRewrite.getCu().getJavaProject(), false, null)); VariableDeclarationExpression declaration= ast.newVariableDeclarationExpression(fragment); IVariableBinding variable= pi.getOldBinding(); declaration.setType(importBinding(pi.getNewTypeBinding(), cuRewrite)); int modifiers= variable.getModifiers(); List<Modifier> newModifiers= ast.newModifiers(modifiers); declaration.modifiers().addAll(newModifiers); return ast.newExpressionStatement(declaration); }
Example 6
Source File: ConvertAnonymousToNestedRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private Statement newFieldAssignment(AST ast, SimpleName fieldNameNode, Expression initializer, boolean useThisAccess) { Assignment assignment= ast.newAssignment(); if (useThisAccess) { FieldAccess access= ast.newFieldAccess(); access.setExpression(ast.newThisExpression()); access.setName(fieldNameNode); assignment.setLeftHandSide(access); } else { assignment.setLeftHandSide(fieldNameNode); } assignment.setOperator(Assignment.Operator.ASSIGN); assignment.setRightHandSide(initializer); return ast.newExpressionStatement(assignment); }
Example 7
Source File: PromoteTempToFieldRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private Statement createNewAssignmentStatement(ASTRewrite rewrite) { AST ast= getAST(); Assignment assignment= ast.newAssignment(); SimpleName fieldName= ast.newSimpleName(fFieldName); addLinkedName(rewrite, fieldName, true); assignment.setLeftHandSide(fieldName); assignment.setRightHandSide(getTempInitializerCopy(rewrite)); return ast.newExpressionStatement(assignment); }
Example 8
Source File: AdvancedQuickAssistProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static Statement createAssignmentStatement(ASTRewrite rewrite, Assignment.Operator assignmentOperator, Expression origAssignee, Expression origAssigned) { AST ast= rewrite.getAST(); Assignment elseAssignment= ast.newAssignment(); elseAssignment.setOperator(assignmentOperator); elseAssignment.setLeftHandSide((Expression) rewrite.createCopyTarget(origAssignee)); elseAssignment.setRightHandSide((Expression) rewrite.createCopyTarget(origAssigned)); ExpressionStatement statement= ast.newExpressionStatement(elseAssignment); return statement; }
Example 9
Source File: AssignToVariableAssistProposal.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private ASTRewrite doAddField(ASTRewrite rewrite, ASTNode nodeToAssign, ITypeBinding typeBinding, int index) { boolean isParamToField= nodeToAssign.getNodeType() == ASTNode.SINGLE_VARIABLE_DECLARATION; ASTNode newTypeDecl= ASTResolving.findParentType(nodeToAssign); if (newTypeDecl == null) { return null; } Expression expression= isParamToField ? ((SingleVariableDeclaration) nodeToAssign).getName() : ((ExpressionStatement) nodeToAssign).getExpression(); AST ast= newTypeDecl.getAST(); createImportRewrite((CompilationUnit) nodeToAssign.getRoot()); BodyDeclaration bodyDecl= ASTResolving.findParentBodyDeclaration(nodeToAssign); Block body; if (bodyDecl instanceof MethodDeclaration) { body= ((MethodDeclaration) bodyDecl).getBody(); } else if (bodyDecl instanceof Initializer) { body= ((Initializer) bodyDecl).getBody(); } else { return null; } IJavaProject project= getCompilationUnit().getJavaProject(); boolean isAnonymous= newTypeDecl.getNodeType() == ASTNode.ANONYMOUS_CLASS_DECLARATION; boolean isStatic= Modifier.isStatic(bodyDecl.getModifiers()) && !isAnonymous; int modifiers= Modifier.PRIVATE; if (isStatic) { modifiers |= Modifier.STATIC; } VariableDeclarationFragment newDeclFrag= addFieldDeclaration(rewrite, newTypeDecl, modifiers, expression, nodeToAssign, typeBinding, index); String varName= newDeclFrag.getName().getIdentifier(); Assignment assignment= ast.newAssignment(); assignment.setRightHandSide((Expression) rewrite.createCopyTarget(expression)); boolean needsThis= StubUtility.useThisForFieldAccess(project); if (isParamToField) { needsThis |= varName.equals(((SimpleName) expression).getIdentifier()); } SimpleName accessName= ast.newSimpleName(varName); if (needsThis) { FieldAccess fieldAccess= ast.newFieldAccess(); fieldAccess.setName(accessName); if (isStatic) { String typeName= ((AbstractTypeDeclaration) newTypeDecl).getName().getIdentifier(); fieldAccess.setExpression(ast.newSimpleName(typeName)); } else { fieldAccess.setExpression(ast.newThisExpression()); } assignment.setLeftHandSide(fieldAccess); } else { assignment.setLeftHandSide(accessName); } ASTNode selectionNode; if (isParamToField) { // assign parameter to field ExpressionStatement statement= ast.newExpressionStatement(assignment); int insertIdx= findAssignmentInsertIndex(body.statements(), nodeToAssign) + index; rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY).insertAt(statement, insertIdx, null); selectionNode= statement; } else { if (needsSemicolon(expression)) { rewrite.replace(expression, ast.newExpressionStatement(assignment), null); } else { rewrite.replace(expression, assignment, null); } selectionNode= nodeToAssign; } addLinkedPosition(rewrite.track(newDeclFrag.getName()), false, KEY_NAME + index); if (!isParamToField) { FieldDeclaration fieldDeclaration= (FieldDeclaration) newDeclFrag.getParent(); addLinkedPosition(rewrite.track(fieldDeclaration.getType()), false, KEY_TYPE); } addLinkedPosition(rewrite.track(accessName), true, KEY_NAME + index); IVariableBinding variableBinding= newDeclFrag.resolveBinding(); if (variableBinding != null) { SimpleName[] linkedNodes= LinkedNodeFinder.findByBinding(nodeToAssign.getRoot(), variableBinding); for (int i= 0; i < linkedNodes.length; i++) { addLinkedPosition(rewrite.track(linkedNodes[i]), false, KEY_NAME + index); } } setEndPosition(rewrite.track(selectionNode)); return rewrite; }
Example 10
Source File: StubUtility2.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
public static MethodDeclaration createDelegationStub(ICompilationUnit unit, ASTRewrite rewrite, ImportRewrite imports, ImportRewriteContext context, IMethodBinding delegate, IVariableBinding delegatingField, CodeGenerationSettings settings) throws CoreException { Assert.isNotNull(delegate); Assert.isNotNull(delegatingField); Assert.isNotNull(settings); AST ast= rewrite.getAST(); MethodDeclaration decl= ast.newMethodDeclaration(); decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, delegate.getModifiers() & ~Modifier.SYNCHRONIZED & ~Modifier.ABSTRACT & ~Modifier.NATIVE)); decl.setName(ast.newSimpleName(delegate.getName())); decl.setConstructor(false); createTypeParameters(imports, context, ast, delegate, decl); decl.setReturnType2(imports.addImport(delegate.getReturnType(), ast, context)); List<SingleVariableDeclaration> params= createParameters(unit.getJavaProject(), imports, context, ast, delegate, null, decl); createThrownExceptions(decl, delegate, imports, context, ast); Block body= ast.newBlock(); decl.setBody(body); String delimiter= StubUtility.getLineDelimiterUsed(unit); Statement statement= null; MethodInvocation invocation= ast.newMethodInvocation(); invocation.setName(ast.newSimpleName(delegate.getName())); List<Expression> arguments= invocation.arguments(); for (int i= 0; i < params.size(); i++) arguments.add(ast.newSimpleName(params.get(i).getName().getIdentifier())); if (settings.useKeywordThis) { FieldAccess access= ast.newFieldAccess(); access.setExpression(ast.newThisExpression()); access.setName(ast.newSimpleName(delegatingField.getName())); invocation.setExpression(access); } else invocation.setExpression(ast.newSimpleName(delegatingField.getName())); if (delegate.getReturnType().isPrimitive() && delegate.getReturnType().getName().equals("void")) {//$NON-NLS-1$ statement= ast.newExpressionStatement(invocation); } else { ReturnStatement returnStatement= ast.newReturnStatement(); returnStatement.setExpression(invocation); statement= returnStatement; } body.statements().add(statement); ITypeBinding declaringType= delegatingField.getDeclaringClass(); if (declaringType == null) { // can be null for return decl; } String qualifiedName= declaringType.getQualifiedName(); IPackageBinding packageBinding= declaringType.getPackage(); if (packageBinding != null) { if (packageBinding.getName().length() > 0 && qualifiedName.startsWith(packageBinding.getName())) qualifiedName= qualifiedName.substring(packageBinding.getName().length()); } if (settings.createComments) { /* * TODO: have API for delegate method comments This is an inlined * version of * {@link CodeGeneration#getMethodComment(ICompilationUnit, String, MethodDeclaration, IMethodBinding, String)} */ delegate= delegate.getMethodDeclaration(); String declaringClassQualifiedName= delegate.getDeclaringClass().getQualifiedName(); String linkToMethodName= delegate.getName(); String[] parameterTypesQualifiedNames= StubUtility.getParameterTypeNamesForSeeTag(delegate); String string= StubUtility.getMethodComment(unit, qualifiedName, decl, delegate.isDeprecated(), linkToMethodName, declaringClassQualifiedName, parameterTypesQualifiedNames, true, delimiter); if (string != null) { Javadoc javadoc= (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC); decl.setJavadoc(javadoc); } } return decl; }
Example 11
Source File: PolymorphismRefactoring.java From JDeodorant with MIT License | 4 votes |
protected void generateSettersForAssignedFields() { AST contextAST = sourceTypeDeclaration.getAST(); Set<VariableDeclarationFragment> assignedFields = new LinkedHashSet<VariableDeclarationFragment>(); assignedFields.addAll(typeCheckElimination.getAssignedFields()); assignedFields.addAll(typeCheckElimination.getSuperAssignedFields()); for(VariableDeclarationFragment fragment : assignedFields) { IMethodBinding setterMethodBinding = null; if(typeCheckElimination.getSuperAssignedFields().contains(fragment)) { for(IVariableBinding fieldBinding : typeCheckElimination.getSuperAssignedFieldBindings()) { if(fieldBinding.isEqualTo(fragment.resolveBinding())) { setterMethodBinding = typeCheckElimination.getSetterMethodBindingOfSuperAssignedField(fieldBinding); break; } } } else { setterMethodBinding = findSetterMethodInContext(fragment.resolveBinding()); } if(setterMethodBinding == null) { FieldDeclaration fieldDeclaration = (FieldDeclaration)fragment.getParent(); if(!fragment.equals(typeCheckElimination.getTypeField())) { ASTRewrite sourceRewriter = ASTRewrite.create(sourceTypeDeclaration.getAST()); MethodDeclaration newMethodDeclaration = contextAST.newMethodDeclaration(); sourceRewriter.set(newMethodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, contextAST.newPrimitiveType(PrimitiveType.VOID), null); ListRewrite methodDeclarationModifiersRewrite = sourceRewriter.getListRewrite(newMethodDeclaration, MethodDeclaration.MODIFIERS2_PROPERTY); methodDeclarationModifiersRewrite.insertLast(contextAST.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD), null); String methodName = fragment.getName().getIdentifier(); methodName = "set" + methodName.substring(0,1).toUpperCase() + methodName.substring(1,methodName.length()); sourceRewriter.set(newMethodDeclaration, MethodDeclaration.NAME_PROPERTY, contextAST.newSimpleName(methodName), null); ListRewrite methodDeclarationParametersRewrite = sourceRewriter.getListRewrite(newMethodDeclaration, MethodDeclaration.PARAMETERS_PROPERTY); SingleVariableDeclaration parameter = contextAST.newSingleVariableDeclaration(); sourceRewriter.set(parameter, SingleVariableDeclaration.TYPE_PROPERTY, fieldDeclaration.getType(), null); sourceRewriter.set(parameter, SingleVariableDeclaration.NAME_PROPERTY, fragment.getName(), null); methodDeclarationParametersRewrite.insertLast(parameter, null); Block methodDeclarationBody = contextAST.newBlock(); ListRewrite methodDeclarationBodyStatementsRewrite = sourceRewriter.getListRewrite(methodDeclarationBody, Block.STATEMENTS_PROPERTY); Assignment assignment = contextAST.newAssignment(); sourceRewriter.set(assignment, Assignment.RIGHT_HAND_SIDE_PROPERTY, fragment.getName(), null); sourceRewriter.set(assignment, Assignment.OPERATOR_PROPERTY, Assignment.Operator.ASSIGN, null); FieldAccess fieldAccess = contextAST.newFieldAccess(); sourceRewriter.set(fieldAccess, FieldAccess.EXPRESSION_PROPERTY, contextAST.newThisExpression(), null); sourceRewriter.set(fieldAccess, FieldAccess.NAME_PROPERTY, fragment.getName(), null); sourceRewriter.set(assignment, Assignment.LEFT_HAND_SIDE_PROPERTY, fieldAccess, null); ExpressionStatement expressionStatement = contextAST.newExpressionStatement(assignment); methodDeclarationBodyStatementsRewrite.insertLast(expressionStatement, null); sourceRewriter.set(newMethodDeclaration, MethodDeclaration.BODY_PROPERTY, methodDeclarationBody, null); ListRewrite contextBodyRewrite = sourceRewriter.getListRewrite(sourceTypeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); contextBodyRewrite.insertLast(newMethodDeclaration, null); try { TextEdit sourceEdit = sourceRewriter.rewriteAST(); ICompilationUnit sourceICompilationUnit = (ICompilationUnit)sourceCompilationUnit.getJavaElement(); CompilationUnitChange change = compilationUnitChanges.get(sourceICompilationUnit); change.getEdit().addChild(sourceEdit); change.addTextEditGroup(new TextEditGroup("Create setter method for assigned field", new TextEdit[] {sourceEdit})); } catch (JavaModelException e) { e.printStackTrace(); } } } } }