org.eclipse.jdt.core.dom.ExpressionStatement Java Examples

The following examples show how to use org.eclipse.jdt.core.dom.ExpressionStatement. 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: TryPurifyByException.java    From SimFix with GNU General Public License v2.0 7 votes vote down vote up
private Set<Integer> findAllMethodCall(){
	Set<Integer> methodStmt = new HashSet<>();
	if(_backupBody != null){
		Block body = _backupBody;
		for(int i = 0; i < body.statements().size(); i++){
			ASTNode stmt = (ASTNode) body.statements().get(i);
			if(stmt instanceof ExpressionStatement){
				stmt = ((ExpressionStatement) stmt).getExpression();
				if(stmt instanceof MethodInvocation){
					methodStmt.add(i);
				} else if(stmt instanceof Assignment){
					Assignment assign = (Assignment) stmt;
					if(assign.getRightHandSide() instanceof MethodInvocation){
						methodStmt.add(i);
					}
				}
			}
		}
		
	}
	return methodStmt;
}
 
Example #2
Source File: AssignToVariableAssistProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public AssignToVariableAssistProposal(ICompilationUnit cu, int variableKind, ExpressionStatement node, ITypeBinding typeBinding, int relevance) {
	super("", cu, null, relevance, null); //$NON-NLS-1$

	fVariableKind= variableKind;
	fNodeToAssign= node;
	if (typeBinding.isWildcardType()) {
		typeBinding= ASTResolving.normalizeWildcardType(typeBinding, true, node.getAST());
	}

	fTypeBinding= typeBinding;
	if (variableKind == LOCAL) {
		setDisplayName(CorrectionMessages.AssignToVariableAssistProposal_assigntolocal_description);
		setImage(JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL));
	} else {
		setDisplayName(CorrectionMessages.AssignToVariableAssistProposal_assigntofield_description);
		setImage(JavaPluginImages.get(JavaPluginImages.IMG_FIELD_PRIVATE));
	}
	createImportRewrite((CompilationUnit) node.getRoot());
}
 
Example #3
Source File: SnippetCompletionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static boolean accept(ICompilationUnit cu, CompletionContext completionContext, boolean acceptClass) {
	if (completionContext != null && completionContext.isExtended()) {
		if (completionContext.isInJavadoc()) {
			return false;
		}
		if (completionContext instanceof InternalCompletionContext) {
			InternalCompletionContext internalCompletionContext = (InternalCompletionContext) completionContext;
			ASTNode node = internalCompletionContext.getCompletionNode();
			if (node instanceof CompletionOnKeyword2) {
				return true;
			}
			if (node instanceof CompletionOnFieldType) {
				return true;
			}
			if (acceptClass && node instanceof CompletionOnSingleNameReference) {
				if (completionContext.getEnclosingElement() instanceof IMethod) {
					CompilationUnit ast = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
					org.eclipse.jdt.core.dom.ASTNode astNode = ASTNodeSearchUtil.getAstNode(ast, completionContext.getTokenStart(), completionContext.getTokenEnd() - completionContext.getTokenStart() + 1);
					return (astNode == null || (astNode.getParent() instanceof ExpressionStatement));
				}
				return true;
			}
		}
	}
	return false;
}
 
Example #4
Source File: UnusedCodeFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void removeVariableWithInitializer(ASTRewrite rewrite, ASTNode initializerNode, ASTNode statementNode, TextEditGroup group) {
	boolean performRemove= fForceRemove;
	if (!performRemove) {
		ArrayList<Expression> sideEffectNodes= new ArrayList<Expression>();
		initializerNode.accept(new SideEffectFinder(sideEffectNodes));
		performRemove= sideEffectNodes.isEmpty();
	}
	if (performRemove) {
		removeStatement(rewrite, statementNode, group);
		fRemovedAssignmentsCount++;
	} else {
		ASTNode initNode = rewrite.createMoveTarget(initializerNode);
		ExpressionStatement statement = rewrite.getAST().newExpressionStatement((Expression) initNode);
		rewrite.replace(statementNode, statement, null);
		fAlteredAssignmentsCount++;
	}
}
 
Example #5
Source File: AccessAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean visit(PostfixExpression node) {
	Expression operand= node.getOperand();
	if (!considerBinding(resolveBinding(operand), operand))
		return true;

	ASTNode parent= node.getParent();
	if (!(parent instanceof ExpressionStatement)) {
		fStatus.addError(RefactoringCoreMessages.SelfEncapsulateField_AccessAnalyzer_cannot_convert_postfix_expression,
			JavaStatusContext.create(fCUnit, SourceRangeFactory.create(node)));
		return false;
	}
	fRewriter.replace(node,
		createInvocation(node.getAST(), node.getOperand(), node.getOperator().toString()),
		createGroupDescription(POSTFIX_ACCESS));
	return false;
}
 
Example #6
Source File: ExtractConstantRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean canReplace(IASTFragment fragment) {
	ASTNode node= fragment.getAssociatedNode();
	ASTNode parent= node.getParent();
	if (parent instanceof VariableDeclarationFragment) {
		VariableDeclarationFragment vdf= (VariableDeclarationFragment) parent;
		if (node.equals(vdf.getName()))
			return false;
	}
	if (parent instanceof ExpressionStatement)
		return false;
	if (parent instanceof SwitchCase) {
		if (node instanceof Name) {
			Name name= (Name) node;
			ITypeBinding typeBinding= name.resolveTypeBinding();
			if (typeBinding != null) {
				return !typeBinding.isEnum();
			}
		}
	}
	return true;
}
 
Example #7
Source File: VariableDeclarationFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private MethodDeclaration getWritingConstructor(SimpleName name) {
Assignment assignement= (Assignment)ASTNodes.getParent(name, Assignment.class);
if (assignement == null)
	return null;

ASTNode expression= assignement.getParent();
if (!(expression instanceof ExpressionStatement))
	return null;

ASTNode block= expression.getParent();
if (!(block instanceof Block))
	return null;

ASTNode methodDeclaration= block.getParent();
if (!(methodDeclaration instanceof MethodDeclaration))
	return null;

      return (MethodDeclaration)methodDeclaration;
     }
 
Example #8
Source File: ExtractTempRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private IExpressionFragment getSelectedExpression() throws JavaModelException {
	if (fSelectedExpression != null) {
		return fSelectedExpression;
	}
	IASTFragment selectedFragment = ASTFragmentFactory.createFragmentForSourceRange(new SourceRange(fSelectionStart, fSelectionLength), fCompilationUnitNode, fCu);

	if (selectedFragment instanceof IExpressionFragment && !Checks.isInsideJavadoc(selectedFragment.getAssociatedNode())) {
		fSelectedExpression = (IExpressionFragment) selectedFragment;
	} else if (selectedFragment != null) {
		if (selectedFragment.getAssociatedNode() instanceof ExpressionStatement) {
			ExpressionStatement exprStatement = (ExpressionStatement) selectedFragment.getAssociatedNode();
			Expression expression = exprStatement.getExpression();
			fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(expression);
		} else if (selectedFragment.getAssociatedNode() instanceof Assignment) {
			Assignment assignment = (Assignment) selectedFragment.getAssociatedNode();
			fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(assignment);
		}
	}

	if (fSelectedExpression != null && Checks.isEnumCase(fSelectedExpression.getAssociatedExpression().getParent())) {
		fSelectedExpression = null;
	}

	return fSelectedExpression;
}
 
Example #9
Source File: IntroduceIndirectionRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static ASTNode checkNode(ASTNode node) {
	if (node == null)
		return null;
	if (node.getNodeType() == ASTNode.SIMPLE_NAME) {
		node= node.getParent();
	} else if (node.getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
		node= ((ExpressionStatement) node).getExpression();
	}
	switch (node.getNodeType()) {
		case ASTNode.METHOD_INVOCATION:
		case ASTNode.METHOD_DECLARATION:
		case ASTNode.SUPER_METHOD_INVOCATION:
			return node;
	}
	return null;
}
 
Example #10
Source File: ExtractFieldRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private IExpressionFragment getSelectedExpression() throws JavaModelException {
	if (fSelectedExpression != null) {
		return fSelectedExpression;
	}
	IASTFragment selectedFragment = ASTFragmentFactory.createFragmentForSourceRange(new SourceRange(fSelectionStart, fSelectionLength), fCompilationUnitNode, fCu);

	if (selectedFragment instanceof IExpressionFragment && !Checks.isInsideJavadoc(selectedFragment.getAssociatedNode())) {
		fSelectedExpression = (IExpressionFragment) selectedFragment;
	} else if (selectedFragment != null) {
		if (selectedFragment.getAssociatedNode() instanceof ExpressionStatement) {
			ExpressionStatement exprStatement = (ExpressionStatement) selectedFragment.getAssociatedNode();
			Expression expression = exprStatement.getExpression();
			fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(expression);
		} else if (selectedFragment.getAssociatedNode() instanceof Assignment) {
			Assignment assignment = (Assignment) selectedFragment.getAssociatedNode();
			fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(assignment);
		}
	}

	if (fSelectedExpression != null && Checks.isEnumCase(fSelectedExpression.getAssociatedExpression().getParent())) {
		fSelectedExpression = null;
	}

	return fSelectedExpression;
}
 
Example #11
Source File: ReplaceInvocationsRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static ASTNode checkNode(ASTNode node) {
		if (node == null)
			return null;
		if (node.getNodeType() == ASTNode.SIMPLE_NAME) {
			node= node.getParent();
		} else if (node.getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
			node= ((ExpressionStatement)node).getExpression();
		}
		switch(node.getNodeType()) {
			case ASTNode.METHOD_DECLARATION:
			case ASTNode.METHOD_INVOCATION:
// not yet...
//			case ASTNode.SUPER_METHOD_INVOCATION:
//			case ASTNode.CONSTRUCTOR_INVOCATION:
				return node;
		}
		return null;
	}
 
Example #12
Source File: ExtractConstantRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static boolean canReplace(IASTFragment fragment) {
	ASTNode node = fragment.getAssociatedNode();
	ASTNode parent = node.getParent();
	if (parent instanceof VariableDeclarationFragment) {
		VariableDeclarationFragment vdf = (VariableDeclarationFragment) parent;
		if (node.equals(vdf.getName())) {
			return false;
		}
	}
	if (parent instanceof ExpressionStatement) {
		return false;
	}
	if (parent instanceof SwitchCase) {
		if (node instanceof Name) {
			Name name = (Name) node;
			ITypeBinding typeBinding = name.resolveTypeBinding();
			if (typeBinding != null) {
				return !typeBinding.isEnum();
			}
		}
	}
	return true;
}
 
Example #13
Source File: ExtractTempRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private IExpressionFragment getSelectedExpression() throws JavaModelException {
	if (fSelectedExpression != null)
		return fSelectedExpression;
	IASTFragment selectedFragment= ASTFragmentFactory.createFragmentForSourceRange(new SourceRange(fSelectionStart, fSelectionLength), fCompilationUnitNode, fCu);

	if (selectedFragment instanceof IExpressionFragment && !Checks.isInsideJavadoc(selectedFragment.getAssociatedNode())) {
		fSelectedExpression= (IExpressionFragment) selectedFragment;
	} else if (selectedFragment != null) {
		if (selectedFragment.getAssociatedNode() instanceof ExpressionStatement) {
			ExpressionStatement exprStatement= (ExpressionStatement) selectedFragment.getAssociatedNode();
			Expression expression= exprStatement.getExpression();
			fSelectedExpression= (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(expression);
		} else if (selectedFragment.getAssociatedNode() instanceof Assignment) {
			Assignment assignment= (Assignment) selectedFragment.getAssociatedNode();
			fSelectedExpression= (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(assignment);
		}
	}

	if (fSelectedExpression != null && Checks.isEnumCase(fSelectedExpression.getAssociatedExpression().getParent())) {
		fSelectedExpression= null;
	}

	return fSelectedExpression;
}
 
Example #14
Source File: ExtractTempRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean canReplace(IASTFragment fragment) {
	ASTNode node= fragment.getAssociatedNode();
	ASTNode parent= node.getParent();
	if (parent instanceof VariableDeclarationFragment) {
		VariableDeclarationFragment vdf= (VariableDeclarationFragment) parent;
		if (node.equals(vdf.getName()))
			return false;
	}
	if (isMethodParameter(node))
		return false;
	if (isThrowableInCatchBlock(node))
		return false;
	if (parent instanceof ExpressionStatement)
		return false;
	if (isLeftValue(node))
		return false;
	if (isReferringToLocalVariableFromFor((Expression) node))
		return false;
	if (isUsedInForInitializerOrUpdater((Expression) node))
		return false;
	if (parent instanceof SwitchCase)
		return false;
	return true;
}
 
Example #15
Source File: MethodObject.java    From JDeodorant with MIT License 6 votes vote down vote up
public FieldInstructionObject isSetter() {
	if(getMethodBody() != null) {
 	List<AbstractStatement> abstractStatements = getMethodBody().getCompositeStatement().getStatements();
 	if(abstractStatements.size() == 1 && abstractStatements.get(0) instanceof StatementObject) {
 		StatementObject statementObject = (StatementObject)abstractStatements.get(0);
 		Statement statement = statementObject.getStatement();
 		if(statement instanceof ExpressionStatement) {
 			ExpressionStatement expressionStatement = (ExpressionStatement)statement;
 			if(expressionStatement.getExpression() instanceof Assignment && statementObject.getFieldInstructions().size() == 1 && statementObject.getMethodInvocations().size() == 0 &&
     				statementObject.getLocalVariableDeclarations().size() == 0 && statementObject.getLocalVariableInstructions().size() == 1 && this.constructorObject.parameterList.size() == 1) {
 				Assignment assignment = (Assignment)expressionStatement.getExpression();
 				if((assignment.getLeftHandSide() instanceof SimpleName || assignment.getLeftHandSide() instanceof FieldAccess) && assignment.getRightHandSide() instanceof SimpleName)
 					return statementObject.getFieldInstructions().get(0);
 			}
 		}
 	}
	}
	return null;
}
 
Example #16
Source File: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static ASTNode getInlineableMethodNode(ASTNode node, IJavaElement unit) {
	if (node == null)
		return null;
	switch (node.getNodeType()) {
		case ASTNode.SIMPLE_NAME:
			StructuralPropertyDescriptor locationInParent= node.getLocationInParent();
			if (locationInParent == MethodDeclaration.NAME_PROPERTY) {
				return node.getParent();
			} else if (locationInParent == MethodInvocation.NAME_PROPERTY
					|| locationInParent == SuperMethodInvocation.NAME_PROPERTY) {
				return unit instanceof ICompilationUnit ? node.getParent() : null; // don't start on invocations in binary
			}
			return null;
		case ASTNode.EXPRESSION_STATEMENT:
			node= ((ExpressionStatement)node).getExpression();
	}
	switch (node.getNodeType()) {
		case ASTNode.METHOD_DECLARATION:
			return node;
		case ASTNode.METHOD_INVOCATION:
		case ASTNode.SUPER_METHOD_INVOCATION:
		case ASTNode.CONSTRUCTOR_INVOCATION:
			return unit instanceof ICompilationUnit ? node : null; // don't start on invocations in binary
	}
	return null;
}
 
Example #17
Source File: PDGNodeMapping.java    From JDeodorant with MIT License 6 votes vote down vote up
public boolean isVoidMethodCallDifferenceCoveringEntireStatement() {
	boolean expression1IsVoidMethodCallDifference = false;
	boolean expression2IsVoidMethodCallDifference = false;
	for(ASTNodeDifference difference : nodeDifferences) {
		Expression expr1 = ASTNodeDifference.getParentExpressionOfMethodNameOrTypeName(difference.getExpression1().getExpression());
		Expression expr2 = ASTNodeDifference.getParentExpressionOfMethodNameOrTypeName(difference.getExpression2().getExpression());
		for(PreconditionViolation violation : getPreconditionViolations()) {
			if(violation instanceof ExpressionPreconditionViolation && violation.getType().equals(PreconditionViolationType.EXPRESSION_DIFFERENCE_IS_VOID_METHOD_CALL)) {
				ExpressionPreconditionViolation expressionViolation = (ExpressionPreconditionViolation)violation;
				Expression expression = ASTNodeDifference.getParentExpressionOfMethodNameOrTypeName(expressionViolation.getExpression().getExpression());
				if(expression.equals(expr1)) {
					if(expr1.getParent() instanceof ExpressionStatement) {
						expression1IsVoidMethodCallDifference = true;
					}
				}
				if(expression.equals(expr2)) {
					if(expr2.getParent() instanceof ExpressionStatement) {
						expression2IsVoidMethodCallDifference = true;
					}
				}
			}
		}
	}
	return expression1IsVoidMethodCallDifference && expression2IsVoidMethodCallDifference;
}
 
Example #18
Source File: QuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean getAssignToVariableProposals(IInvocationContext context, ASTNode node, IProblemLocation[] locations, Collection<ICommandAccess> resultingCollections) {
	Statement statement= ASTResolving.findParentStatement(node);
	if (!(statement instanceof ExpressionStatement)) {
		return false;
	}
	ExpressionStatement expressionStatement= (ExpressionStatement) statement;

	Expression expression= expressionStatement.getExpression();
	if (expression.getNodeType() == ASTNode.ASSIGNMENT) {
		return false; // too confusing and not helpful
	}

	ITypeBinding typeBinding= expression.resolveTypeBinding();
	typeBinding= Bindings.normalizeTypeBinding(typeBinding);
	if (typeBinding == null) {
		return false;
	}
	if (resultingCollections == null) {
		return true;
	}

	// don't add if already added as quick fix
	if (containsMatchingProblem(locations, IProblem.UnusedObjectAllocation))
		return false;
	
	ICompilationUnit cu= context.getCompilationUnit();

	AssignToVariableAssistProposal localProposal= new AssignToVariableAssistProposal(cu, AssignToVariableAssistProposal.LOCAL, expressionStatement, typeBinding, IProposalRelevance.ASSIGN_TO_LOCAL);
	localProposal.setCommandId(ASSIGN_TO_LOCAL_ID);
	resultingCollections.add(localProposal);

	ASTNode type= ASTResolving.findParentType(expression);
	if (type != null) {
		AssignToVariableAssistProposal fieldProposal= new AssignToVariableAssistProposal(cu, AssignToVariableAssistProposal.FIELD, expressionStatement, typeBinding, IProposalRelevance.ASSIGN_TO_FIELD);
		fieldProposal.setCommandId(ASSIGN_TO_FIELD_ID);
		resultingCollections.add(fieldProposal);
	}
	return true;

}
 
Example #19
Source File: StyledStringVisitor.java    From JDeodorant with MIT License 5 votes vote down vote up
public boolean visit(ExpressionStatement expr) {
	/*
	 * ExpressionStatement: StatementExpression ;
	 */
	handleExpression((Expression) expr.getExpression());
	appendSemicolon();
	return false;
}
 
Example #20
Source File: QuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static ITypeBinding extractExpressionType(ExpressionStatement statement) {
	Expression expression= statement.getExpression();
	if (expression.getNodeType() == ASTNode.METHOD_INVOCATION
			|| expression.getNodeType() == ASTNode.FIELD_ACCESS) {
		return expression.resolveTypeBinding();
	}
	return null;
}
 
Example #21
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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 #22
Source File: NewVariableCorrectionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isAssigned(Statement statement, SimpleName name) {
	if (statement instanceof ExpressionStatement) {
		ExpressionStatement exstat= (ExpressionStatement) statement;
		if (exstat.getExpression() instanceof Assignment) {
			Assignment assignment= (Assignment) exstat.getExpression();
			return assignment.getLeftHandSide() == name;
		}
	}
	return false;
}
 
Example #23
Source File: ASTNodeDeleteUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static ASTNode[] getNodesToDelete(IJavaElement element, CompilationUnit cuNode) throws JavaModelException {
	// fields are different because you don't delete the whole declaration but only a fragment of it
	if (element.getElementType() == IJavaElement.FIELD) {
		if (JdtFlags.isEnum((IField) element))
			return new ASTNode[] { ASTNodeSearchUtil.getEnumConstantDeclaration((IField) element, cuNode)};
		else
			return new ASTNode[] { ASTNodeSearchUtil.getFieldDeclarationFragmentNode((IField) element, cuNode)};
	}
	if (element.getElementType() == IJavaElement.TYPE && ((IType) element).isLocal()) {
		IType type= (IType) element;
		if (type.isAnonymous()) {
			if (type.getParent().getElementType() == IJavaElement.FIELD) {
				EnumConstantDeclaration enumDecl= ASTNodeSearchUtil.getEnumConstantDeclaration((IField) element.getParent(), cuNode);
				if (enumDecl != null && enumDecl.getAnonymousClassDeclaration() != null)  {
					return new ASTNode[] { enumDecl.getAnonymousClassDeclaration() };
				}
			}
			ClassInstanceCreation creation= ASTNodeSearchUtil.getClassInstanceCreationNode(type, cuNode);
			if (creation != null) {
				if (creation.getLocationInParent() == ExpressionStatement.EXPRESSION_PROPERTY) {
					return new ASTNode[] { creation.getParent() };
				} else if (creation.getLocationInParent() == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
					return new ASTNode[] { creation};
				}
				return new ASTNode[] { creation.getAnonymousClassDeclaration() };
			}
			return new ASTNode[0];
		} else {
			ASTNode[] nodes= ASTNodeSearchUtil.getDeclarationNodes(element, cuNode);
			// we have to delete the TypeDeclarationStatement
			nodes[0]= nodes[0].getParent();
			return nodes;
		}
	}
	return ASTNodeSearchUtil.getDeclarationNodes(element, cuNode);
}
 
Example #24
Source File: MethodDeclarationUtility.java    From JDeodorant with MIT License 5 votes vote down vote up
public static SimpleName isSetter(MethodDeclaration methodDeclaration) {
	Block methodBody = methodDeclaration.getBody();
	List<SingleVariableDeclaration> parameters = methodDeclaration.parameters();
	if(methodBody != null) {
		List<Statement> statements = methodBody.statements();
		if(statements.size() == 1 && parameters.size() == 1) {
			Statement statement = statements.get(0);
    		if(statement instanceof ExpressionStatement) {
    			ExpressionStatement expressionStatement = (ExpressionStatement)statement;
    			Expression expressionStatementExpression = expressionStatement.getExpression();
    			if(expressionStatementExpression instanceof Assignment) {
    				Assignment assignment = (Assignment)expressionStatementExpression;
    				Expression rightHandSide = assignment.getRightHandSide();
    				if(rightHandSide instanceof SimpleName) {
    					SimpleName rightHandSideSimpleName = (SimpleName)rightHandSide;
    					if(rightHandSideSimpleName.resolveBinding().isEqualTo(parameters.get(0).resolveBinding())) {
    						Expression leftHandSide = assignment.getLeftHandSide();
    						if(leftHandSide instanceof SimpleName) {
    		    				return (SimpleName)leftHandSide;
    		    			}
    		    			else if(leftHandSide instanceof FieldAccess) {
    		    				FieldAccess fieldAccess = (FieldAccess)leftHandSide;
    		    				return fieldAccess.getName();
    		    			}
    					}
    				}
    			}
    		}
		}
	}
	return null;
}
 
Example #25
Source File: IntroduceParameterObjectProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void updateBody(MethodDeclaration methodDeclaration, final CompilationUnitRewrite cuRewrite, RefactoringStatus result) throws CoreException {
	// ensure that the parameterObject is imported
	fParameterObjectFactory.createType(fCreateAsTopLevel, cuRewrite, methodDeclaration.getStartPosition());
	if (cuRewrite.getCu().equals(getCompilationUnit()) && !fParameterClassCreated) {
		createParameterClass(methodDeclaration, cuRewrite);
		fParameterClassCreated= true;
	}
	Block body= methodDeclaration.getBody();
	final List<SingleVariableDeclaration> parameters= methodDeclaration.parameters();
	if (body != null) { // abstract methods don't have bodies
		final ASTRewrite rewriter= cuRewrite.getASTRewrite();
		ListRewrite bodyStatements= rewriter.getListRewrite(body, Block.STATEMENTS_PROPERTY);
		List<ParameterInfo> managedParams= getParameterInfos();
		for (Iterator<ParameterInfo> iter= managedParams.iterator(); iter.hasNext();) {
			final ParameterInfo pi= iter.next();
			if (isValidField(pi)) {
				if (isReadOnly(pi, body, parameters, null)) {
					body.accept(new ASTVisitor(false) {

						@Override
						public boolean visit(SimpleName node) {
							updateSimpleName(rewriter, pi, node, parameters, cuRewrite.getCu().getJavaProject());
							return false;
						}

					});
					pi.setInlined(true);
				} else {
					ExpressionStatement initializer= fParameterObjectFactory.createInitializer(pi, getParameterName(), cuRewrite);
					bodyStatements.insertFirst(initializer, null);
				}
			}
		}
	}


}
 
Example #26
Source File: ParameterObjectFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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 #27
Source File: Utils.java    From txtUML with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isSuperMethodInvocation(Statement statement) {
	if (statement instanceof ExpressionStatement) {
		Expression expression = ((ExpressionStatement) statement).getExpression();
		return expression instanceof SuperMethodInvocation;
	}
	return false;
}
 
Example #28
Source File: PreconditionExaminer.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean isMethodCallDifferenceCoveringEntireStatement(ASTNodeDifference difference) {
	boolean expression1IsMethodCallDifference = false;
	boolean expression2IsMethodCallDifference = false;
	Expression expr1 = ASTNodeDifference.getParentExpressionOfMethodNameOrTypeName(difference.getExpression1().getExpression());
	Expression expr2 = ASTNodeDifference.getParentExpressionOfMethodNameOrTypeName(difference.getExpression2().getExpression());
	if(expr1.getParent() instanceof ExpressionStatement) {
		expression1IsMethodCallDifference = true;
	}
	if(expr2.getParent() instanceof ExpressionStatement) {
		expression2IsMethodCallDifference = true;
	}
	return expression1IsMethodCallDifference && expression2IsMethodCallDifference;
}
 
Example #29
Source File: GenStatement.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
private static Statement genPrinter(Expression expression) {
	MethodInvocation methodInvocation = ast.newMethodInvocation();
	methodInvocation.setExpression(ast.newName("System.out"));
	methodInvocation.setName(ast.newSimpleName("println"));
	methodInvocation.arguments().add(expression);
	ExpressionStatement expressionStatement = ast.newExpressionStatement(methodInvocation);
	return expressionStatement;
}
 
Example #30
Source File: Utils.java    From txtUML with Eclipse Public License 1.0 5 votes vote down vote up
public static SuperMethodInvocation getSuperMethodInvocationFromStatement(Statement statement) {
	if (statement instanceof ExpressionStatement) {
		Expression expression = ((ExpressionStatement) statement).getExpression();
		if (expression instanceof SuperMethodInvocation) {
			SuperMethodInvocation methodInvocation = (SuperMethodInvocation) expression;
			return methodInvocation;
		}
	}
	return null;
}