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

The following examples show how to use org.eclipse.jdt.core.dom.ThrowStatement. 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: StatementObject.java    From JDeodorant with MIT License 6 votes vote down vote up
public StatementObject(Statement statement, StatementType type, AbstractMethodFragment parent) {
	super(statement, type, parent);
	
	ExpressionExtractor expressionExtractor = new ExpressionExtractor();
       List<Expression> assignments = expressionExtractor.getAssignments(statement);
       List<Expression> postfixExpressions = expressionExtractor.getPostfixExpressions(statement);
       List<Expression> prefixExpressions = expressionExtractor.getPrefixExpressions(statement);
       processVariables(expressionExtractor.getVariableInstructions(statement), assignments, postfixExpressions, prefixExpressions);
	processMethodInvocations(expressionExtractor.getMethodInvocations(statement));
	processClassInstanceCreations(expressionExtractor.getClassInstanceCreations(statement));
	processArrayCreations(expressionExtractor.getArrayCreations(statement));
	//processArrayAccesses(expressionExtractor.getArrayAccesses(statement));
	processLiterals(expressionExtractor.getLiterals(statement));
	if(statement instanceof ThrowStatement) {
		processThrowStatement((ThrowStatement)statement);
	}
	if(statement instanceof ConstructorInvocation) {
		processConstructorInvocation((ConstructorInvocation)statement);
	}
}
 
Example #2
Source File: CFG.java    From JDeodorant with MIT License 6 votes vote down vote up
private CFGNode createNonCompositeNode(StatementObject statement) {
	CFGNode currentNode;
	Statement astStatement = statement.getStatement();
	if(astStatement instanceof ReturnStatement)
		currentNode = new CFGExitNode(statement);
	else if(astStatement instanceof SwitchCase)
		currentNode = new CFGSwitchCaseNode(statement);
	else if(astStatement instanceof BreakStatement)
		currentNode = new CFGBreakNode(statement);
	else if(astStatement instanceof ContinueStatement)
		currentNode = new CFGContinueNode(statement);
	else if(astStatement instanceof ThrowStatement)
		currentNode = new CFGThrowNode(statement);
	else
		currentNode = new CFGNode(statement);
	directlyNestedNodeInBlock(currentNode);
	return currentNode;
}
 
Example #3
Source File: NecessaryParenthesesChecker.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean locationNeedsParentheses(StructuralPropertyDescriptor locationInParent) {
	if (locationInParent instanceof ChildListPropertyDescriptor && locationInParent != InfixExpression.EXTENDED_OPERANDS_PROPERTY) {
		// e.g. argument lists of MethodInvocation, ClassInstanceCreation, dimensions of ArrayCreation ...
		return false;
	}
	if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY
			|| locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY
			|| locationInParent == ReturnStatement.EXPRESSION_PROPERTY
			|| locationInParent == EnhancedForStatement.EXPRESSION_PROPERTY
			|| locationInParent == ForStatement.EXPRESSION_PROPERTY
			|| locationInParent == WhileStatement.EXPRESSION_PROPERTY
			|| locationInParent == DoStatement.EXPRESSION_PROPERTY
			|| locationInParent == AssertStatement.EXPRESSION_PROPERTY
			|| locationInParent == AssertStatement.MESSAGE_PROPERTY
			|| locationInParent == IfStatement.EXPRESSION_PROPERTY
			|| locationInParent == SwitchStatement.EXPRESSION_PROPERTY
			|| locationInParent == SwitchCase.EXPRESSION_PROPERTY
			|| locationInParent == ArrayAccess.INDEX_PROPERTY
			|| locationInParent == ThrowStatement.EXPRESSION_PROPERTY
			|| locationInParent == SynchronizedStatement.EXPRESSION_PROPERTY
			|| locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
		return false;
	}
	return true;
}
 
Example #4
Source File: FullConstraintCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public ITypeConstraint[] create(ThrowStatement node) {
	ConstraintVariable nameVariable= fConstraintVariableFactory.makeExpressionOrTypeVariable(node.getExpression(), getContext());
	ITypeBinding throwable= node.getAST().resolveWellKnownType("java.lang.Throwable"); //$NON-NLS-1$
	return fTypeConstraintFactory.createSubtypeConstraint(
			nameVariable,
			fConstraintVariableFactory.makeRawBindingVariable(throwable));
}
 
Example #5
Source File: StyledStringVisitor.java    From JDeodorant with MIT License 5 votes vote down vote up
public boolean visit(ThrowStatement stmnt){
	/*
	 * ThrowStatement: throw Expression ;
	 */
	styledString.append("throw", new StyledStringStyler(keywordStyle));
	appendSpace();
	handleExpression((Expression) stmnt.getExpression());
	appendSemicolon();
	return false;
}
 
Example #6
Source File: AbstractMethodFragment.java    From JDeodorant with MIT License 5 votes vote down vote up
protected void processThrowStatement(ThrowStatement throwStatement) {
	Expression expression = throwStatement.getExpression();
	if(expression instanceof ClassInstanceCreation) {
		ClassInstanceCreation creation = (ClassInstanceCreation)expression;
		ITypeBinding typeBinding = creation.getType().resolveBinding();
		addExceptionInThrowStatement(typeBinding.getQualifiedName());
	}
}
 
Example #7
Source File: MethodExitsFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(ThrowStatement node) {
	ITypeBinding exception= node.getExpression().resolveTypeBinding();
	if (isExitPoint(exception)) {
		// mark 'throw'
		fResult.add(new OccurrenceLocation(node.getStartPosition(), 5, 0, fExitDescription));
	}
	return true;
}
 
Example #8
Source File: ExceptionOccurrencesFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(ThrowStatement node) {
	if (matches(node.getExpression().resolveTypeBinding())) {
		// mark 'throw'
		fResult.add(new OccurrenceLocation(node.getStartPosition(), 5, 0, fDescription));
	}
	return super.visit(node);
}
 
Example #9
Source File: ExceptionAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(ThrowStatement node) {
	ITypeBinding exception= node.getExpression().resolveTypeBinding();
	if (!isSelected(node) || exception == null || Bindings.isRuntimeException(exception)) // Safety net for null bindings when compiling fails.
		return true;

	addException(exception, node.getAST());
	return true;
}
 
Example #10
Source File: CodeSearch.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
public boolean visit(ThrowStatement node) {
	int start = _unit.getLineNumber(node.getStartPosition());
	if(start == _extendedLine){
		_extendedStatement = node;
		return false;
	}
	return true;
}
 
Example #11
Source File: ExceptionAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(ThrowStatement node) {
	ITypeBinding exception= node.getExpression().resolveTypeBinding();
	if (exception == null)		// Safety net for null bindings when compiling fails.
		return true;

	addException(exception, node.getAST());
	return true;
}
 
Example #12
Source File: FlowAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void endVisit(ThrowStatement node) {
	if (skipNode(node))
		return;
	ThrowFlowInfo info= createThrow();
	setFlowInfo(node, info);
	Expression expression= node.getExpression();
	info.merge(getFlowInfo(expression), fFlowContext);
}
 
Example #13
Source File: LocalCorrectionsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static ThrowStatement getThrowForUnsupportedCase(Expression switchExpr, AST ast, ASTRewrite astRewrite) {
	ThrowStatement newThrowStatement = ast.newThrowStatement();
	ClassInstanceCreation newCic = ast.newClassInstanceCreation();
	newCic.setType(ast.newSimpleType(ast.newSimpleName("UnsupportedOperationException"))); //$NON-NLS-1$
	InfixExpression newInfixExpr = ast.newInfixExpression();
	StringLiteral newStringLiteral = ast.newStringLiteral();
	newStringLiteral.setLiteralValue("Unimplemented case: "); //$NON-NLS-1$
	newInfixExpr.setLeftOperand(newStringLiteral);
	newInfixExpr.setOperator(InfixExpression.Operator.PLUS);
	newInfixExpr.setRightOperand((Expression) astRewrite.createCopyTarget(switchExpr));
	newCic.arguments().add(newInfixExpr);
	newThrowStatement.setExpression(newCic);
	return newThrowStatement;
}
 
Example #14
Source File: CodeBlock.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
private ThrowStmt visit(ThrowStatement node) {
	int startLine = _cunit.getLineNumber(node.getStartPosition());
	int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength());
	ThrowStmt throwStmt = new ThrowStmt(startLine, endLine, node);
	
	Expr expression = (Expr) process(node.getExpression());
	expression.setParent(throwStmt);
	throwStmt.setExpression(expression);
	
	return throwStmt;
}
 
Example #15
Source File: FlowAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void endVisit(ThrowStatement node) {
	if (skipNode(node)) {
		return;
	}
	ThrowFlowInfo info = createThrow();
	setFlowInfo(node, info);
	Expression expression = node.getExpression();
	info.merge(getFlowInfo(expression), fFlowContext);
}
 
Example #16
Source File: ExceptionAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean visit(ThrowStatement node) {
	ITypeBinding exception = node.getExpression().resolveTypeBinding();
	if (exception == null) {
		return true;
	}

	addException(exception, node.getAST());
	return true;
}
 
Example #17
Source File: ExceptionAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean visit(ThrowStatement node) {
	ITypeBinding exception = node.getExpression().resolveTypeBinding();
	if (!isSelected(node) || exception == null || Bindings.isRuntimeException(exception)) {
		return true;
	}

	addException(exception, node.getAST());
	return true;
}
 
Example #18
Source File: AstVisitor.java    From jdt2famix with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(ThrowStatement node) {
	ITypeBinding binding = node.getExpression().resolveTypeBinding();
	if (binding != null) {
		ThrownException thrownException = new ThrownException();
		Type thrownType = importer.ensureTypeFromTypeBinding(binding);
		thrownException.setExceptionClass((com.feenk.jdt2famix.model.famix.Class) thrownType);
		thrownException.setDefiningMethod((Method) importer.topOfContainerStack());
		importer.repository().add(thrownException);
	}
	return true;
}
 
Example #19
Source File: LocalCorrectionsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static ThrowStatement getThrowForUnexpectedDefault(Expression switchExpression, AST ast, ASTRewrite astRewrite) {
	ThrowStatement newThrowStatement = ast.newThrowStatement();
	ClassInstanceCreation newCic = ast.newClassInstanceCreation();
	newCic.setType(ast.newSimpleType(ast.newSimpleName("IllegalArgumentException"))); //$NON-NLS-1$
	InfixExpression newInfixExpr = ast.newInfixExpression();
	StringLiteral newStringLiteral = ast.newStringLiteral();
	newStringLiteral.setLiteralValue("Unexpected value: "); //$NON-NLS-1$
	newInfixExpr.setLeftOperand(newStringLiteral);
	newInfixExpr.setOperator(InfixExpression.Operator.PLUS);
	newInfixExpr.setRightOperand((Expression) astRewrite.createCopyTarget(switchExpression));
	newCic.arguments().add(newInfixExpr);
	newThrowStatement.setExpression(newCic);
	return newThrowStatement;
}
 
Example #20
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean visit(final ThrowStatement node) {
  this.appendToBuffer("throw ");
  node.getExpression().accept(this);
  ASTNode _parent = node.getParent();
  boolean _not = (!(_parent instanceof Block));
  if (_not) {
    this.appendToBuffer(";");
  }
  return false;
}
 
Example #21
Source File: ConstraintCollector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public boolean visit(ThrowStatement node) {
	add(fCreator.create(node));
	return true;
}
 
Example #22
Source File: AbstractExceptionAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public abstract boolean visit(ThrowStatement node);
 
Example #23
Source File: LocalCorrectionsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
private static void createMissingDefaultProposal(IInvocationContext context, ASTNode parent, Collection<ChangeCorrectionProposal> proposals) {
	List<Statement> statements;
	Expression expression;
	if (parent instanceof SwitchStatement) {
		SwitchStatement switchStatement = (SwitchStatement) parent;
		statements = switchStatement.statements();
		expression = switchStatement.getExpression();
	} else if (parent instanceof SwitchExpression) {
		SwitchExpression switchExpression = (SwitchExpression) parent;
		statements = switchExpression.statements();
		expression = switchExpression.getExpression();
	} else {
		return;
	}
	AST ast = parent.getAST();
	ASTRewrite astRewrite = ASTRewrite.create(ast);
	ListRewrite listRewrite;
	if (parent instanceof SwitchStatement) {
		listRewrite = astRewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY);
	} else {
		listRewrite = astRewrite.getListRewrite(parent, SwitchExpression.STATEMENTS_PROPERTY);
	}
	String label = CorrectionMessages.LocalCorrectionsSubProcessor_add_default_case_description;
	LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(), astRewrite, IProposalRelevance.ADD_MISSING_DEFAULT_CASE);

	SwitchCase newSwitchCase = ast.newSwitchCase();
	listRewrite.insertLast(newSwitchCase, null);

	if (ASTHelper.isSwitchCaseExpressionsSupportedInAST(ast)) {
		if (statements.size() > 0) {
			Statement firstStatement = statements.get(0);
			SwitchCase switchCase = (SwitchCase) firstStatement;
			boolean isArrow = switchCase.isSwitchLabeledRule();
			newSwitchCase.setSwitchLabeledRule(isArrow);
			if (isArrow || parent instanceof SwitchExpression) {
				ThrowStatement newThrowStatement = getThrowForUnexpectedDefault(expression, ast, astRewrite);
				listRewrite.insertLast(newThrowStatement, null);
				proposal.addLinkedPosition(astRewrite.track(newThrowStatement), true, null);
			} else {
				listRewrite.insertLast(ast.newBreakStatement(), null);
			}
		} else {
			listRewrite.insertLast(ast.newBreakStatement(), null);
		}
	} else {
		newSwitchCase.setExpression(null);
		listRewrite.insertLast(ast.newBreakStatement(), null);
	}

	proposals.add(proposal);
}
 
Example #24
Source File: GenericVisitor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void endVisit(ThrowStatement node) {
	endVisitNode(node);
}
 
Example #25
Source File: GenericVisitor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public boolean visit(ThrowStatement node) {
	return visitNode(node);
}
 
Example #26
Source File: ControlStatementsFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean satisfiesPrecondition(Statement controlStatement, ChildPropertyDescriptor childDescriptor, boolean onlyReturnAndThrows, boolean cleanUpCheck) {
	Object child= controlStatement.getStructuralProperty(childDescriptor);

	if (!(child instanceof Block))
		return false;

	Block block= (Block)child;
	List<Statement> list= block.statements();
	if (list.size() != 1)
		return false;

	ASTNode singleStatement= list.get(0);

	if (onlyReturnAndThrows)
		if (!(singleStatement instanceof ReturnStatement) && !(singleStatement instanceof ThrowStatement))
			return false;

	if (controlStatement instanceof IfStatement) {
		// if (true) {
		//  while (true)
		// 	 if (false)
		//    ;
		// } else
		//   ;

		if (((IfStatement)controlStatement).getThenStatement() != child)
			return true;//can always remove blocks in else part

		IfStatement ifStatement= (IfStatement)controlStatement;
		if (ifStatement.getElseStatement() == null)
			return true;//can always remove if no else part

		return !hasUnblockedIf((Statement)singleStatement, onlyReturnAndThrows, cleanUpCheck);
	} else {
		//if (true)
		// while (true) {
		//  if (false)
		//   ;
		// }
		//else
		// ;
		if (!hasUnblockedIf((Statement)singleStatement, onlyReturnAndThrows, cleanUpCheck))
			return true;

		ASTNode currentChild= controlStatement;
		ASTNode parent= currentChild.getParent();
		while (true) {
			Statement body= null;
			if (parent instanceof IfStatement) {
				body= ((IfStatement)parent).getThenStatement();
				if (body == currentChild && ((IfStatement)parent).getElseStatement() != null)//->currentChild is an unblocked then part
					return false;
			} else if (parent instanceof WhileStatement) {
				body= ((WhileStatement)parent).getBody();
			} else if (parent instanceof DoStatement) {
				body= ((DoStatement)parent).getBody();
			} else if (parent instanceof ForStatement) {
				body= ((ForStatement)parent).getBody();
			} else if (parent instanceof EnhancedForStatement) {
				body= ((EnhancedForStatement)parent).getBody();
			} else {
				return true;
			}
			if (body != currentChild)//->parents child is a block
				return true;

			currentChild= parent;
			parent= currentChild.getParent();
		}
	}
}
 
Example #27
Source File: AbstractExceptionAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public abstract boolean visit(ThrowStatement node);
 
Example #28
Source File: ThrownExceptionVisitor.java    From JDeodorant with MIT License 4 votes vote down vote up
public boolean visit(ThrowStatement node) {
	Expression expression = node.getExpression();
	typeBindings.add(expression.resolveTypeBinding());
	return super.visit(node);
}
 
Example #29
Source File: StatementCollector.java    From JDeodorant with MIT License 4 votes vote down vote up
public boolean visit(ThrowStatement node) {
	statementList.add(node);
	return false;
}
 
Example #30
Source File: ExtractStatementsVisitor.java    From JDeodorant with MIT License 4 votes vote down vote up
public boolean visit(ThrowStatement node) {
	statementsList.add(node);
	
	return false;
}