org.eclipse.jdt.core.dom.CatchClause源码实例Demo

类org.eclipse.jdt.core.dom.CatchClause源码实例Demo

下面列出了org.eclipse.jdt.core.dom.CatchClause 类实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: eclipse.jdt.ls   文件: FlowContext.java
boolean isExceptionCaught(ITypeBinding excpetionType) {
	for (Iterator<List<CatchClause>> exceptions= fExceptionStack.iterator(); exceptions.hasNext(); ) {
		for (Iterator<CatchClause> catchClauses= exceptions.next().iterator(); catchClauses.hasNext(); ) {
			SingleVariableDeclaration caughtException= catchClauses.next().getException();
			IVariableBinding binding= caughtException.resolveBinding();
			if (binding == null) {
				continue;
			}
			ITypeBinding caughtype= binding.getType();
			while (caughtype != null) {
				if (caughtype == excpetionType) {
					return true;
				}
				caughtype= caughtype.getSuperclass();
			}
		}
	}
	return false;
}
 
源代码2 项目: eclipse.jdt.ls   文件: FlowAnalyzer.java
@Override
public boolean visit(TryStatement node) {
	if (traverseNode(node)) {
		fFlowContext.pushExcptions(node);
		for (Iterator<VariableDeclarationExpression> iterator = node.resources().iterator(); iterator.hasNext();) {
			iterator.next().accept(this);
		}
		node.getBody().accept(this);
		fFlowContext.popExceptions();
		List<CatchClause> catchClauses = node.catchClauses();
		for (Iterator<CatchClause> iter = catchClauses.iterator(); iter.hasNext();) {
			iter.next().accept(this);
		}
		Block finallyBlock = node.getFinally();
		if (finallyBlock != null) {
			finallyBlock.accept(this);
		}
	}
	return false;
}
 
源代码3 项目: eclipse.jdt.ls   文件: FlowAnalyzer.java
@Override
public void endVisit(TryStatement node) {
	if (skipNode(node)) {
		return;
	}
	TryFlowInfo info = createTry();
	setFlowInfo(node, info);
	for (Iterator<VariableDeclarationExpression> iterator = node.resources().iterator(); iterator.hasNext();) {
		info.mergeResources(getFlowInfo(iterator.next()), fFlowContext);
	}
	info.mergeTry(getFlowInfo(node.getBody()), fFlowContext);
	for (Iterator<CatchClause> iter = node.catchClauses().iterator(); iter.hasNext();) {
		CatchClause element = iter.next();
		info.mergeCatch(getFlowInfo(element), fFlowContext);
	}
	info.mergeFinally(getFlowInfo(node.getFinally()), fFlowContext);
}
 
源代码4 项目: jdt2famix   文件: AstVisitor.java
@Override
public boolean visit(CatchClause node) {
	if (importer.topFromContainerStack(Method.class) != null) {
		importer.topFromContainerStack(Method.class).incCyclomaticComplexity();
	}

	CaughtException caughtException = new CaughtException();
	ITypeBinding binding = node.getException().getType().resolveBinding();
	if (binding != null) {
		Type caughtType = importer.ensureTypeFromTypeBinding(binding);
		caughtException.setExceptionClass((com.feenk.jdt2famix.model.famix.Class) caughtType);
		caughtException.setDefiningMethod((Method) importer.topOfContainerStack());
		importer.repository().add(caughtException);
	}
	return true;
}
 
private RefactoringStatus checkSelection(VariableDeclaration decl) {
	ASTNode parent= decl.getParent();
	if (parent instanceof MethodDeclaration) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_method_parameter);
	}

	if (parent instanceof CatchClause) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_exceptions_declared);
	}

	if (parent instanceof VariableDeclarationExpression && parent.getLocationInParent() == ForStatement.INITIALIZERS_PROPERTY) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_for_initializers);
	}

	if (parent instanceof VariableDeclarationExpression && parent.getLocationInParent() == TryStatement.RESOURCES_PROPERTY) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_resource_in_try_with_resources);
	}

	if (decl.getInitializer() == null) {
		String message= Messages.format(RefactoringCoreMessages.InlineTempRefactoring_not_initialized, BasicElementLabels.getJavaElementName(decl.getName().getIdentifier()));
		return RefactoringStatus.createFatalErrorStatus(message);
	}

	return checkAssignments(decl);
}
 
boolean isExceptionCaught(ITypeBinding excpetionType) {
	for (Iterator<List<CatchClause>> exceptions= fExceptionStack.iterator(); exceptions.hasNext(); ) {
		for (Iterator<CatchClause> catchClauses= exceptions.next().iterator(); catchClauses.hasNext(); ) {
			SingleVariableDeclaration caughtException= catchClauses.next().getException();
			IVariableBinding binding= caughtException.resolveBinding();
			if (binding == null)
				continue;
			ITypeBinding caughtype= binding.getType();
			while (caughtype != null) {
				if (caughtype == excpetionType)
					return true;
				caughtype= caughtype.getSuperclass();
			}
		}
	}
	return false;
}
 
@Override
public boolean visit(TryStatement node) {
	if (traverseNode(node)) {
		fFlowContext.pushExcptions(node);
		node.getBody().accept(this);
		fFlowContext.popExceptions();
		List<CatchClause> catchClauses= node.catchClauses();
		for (Iterator<CatchClause> iter= catchClauses.iterator(); iter.hasNext();) {
			iter.next().accept(this);
		}
		Block finallyBlock= node.getFinally();
		if (finallyBlock != null) {
			finallyBlock.accept(this);
		}
	}
	return false;
}
 
@Override
public ITypeConstraint[] create(CatchClause node) {
	SingleVariableDeclaration exception= node.getException();
	ConstraintVariable nameVariable= fConstraintVariableFactory.makeExpressionOrTypeVariable(exception.getName(), getContext());

	ITypeConstraint[] defines= fTypeConstraintFactory.createDefinesConstraint(
			nameVariable,
			fConstraintVariableFactory.makeTypeVariable(exception.getType()));

	ITypeBinding throwable= node.getAST().resolveWellKnownType("java.lang.Throwable"); //$NON-NLS-1$
	ITypeConstraint[] catchBound= fTypeConstraintFactory.createSubtypeConstraint(
			nameVariable,
			fConstraintVariableFactory.makeRawBindingVariable(throwable));

	ArrayList<ITypeConstraint> result= new ArrayList<ITypeConstraint>();
	result.addAll(Arrays.asList(defines));
	result.addAll(Arrays.asList(catchBound));
	return result.toArray(new ITypeConstraint[result.size()]);
}
 
@Override
public void endVisit(TryStatement node) {
	ASTNode firstSelectedNode= getFirstSelectedNode();
	if (getSelection().getEndVisitSelectionMode(node) == Selection.AFTER) {
		if (firstSelectedNode == node.getBody() || firstSelectedNode == node.getFinally()) {
			invalidSelection(RefactoringCoreMessages.StatementAnalyzer_try_statement);
		} else {
			List<CatchClause> catchClauses= node.catchClauses();
			for (Iterator<CatchClause> iterator= catchClauses.iterator(); iterator.hasNext();) {
				CatchClause element= iterator.next();
				if (element == firstSelectedNode || element.getBody() == firstSelectedNode) {
					invalidSelection(RefactoringCoreMessages.StatementAnalyzer_try_statement);
				} else if (element.getException() == firstSelectedNode) {
					invalidSelection(RefactoringCoreMessages.StatementAnalyzer_catch_argument);
				}
			}
		}
	}
	super.endVisit(node);
}
 
protected TryStatement copyTryStatement(ASTRewrite sourceRewriter, AST ast, TryStatement tryStatementParent) {
	TryStatement newTryStatement = ast.newTryStatement();
	ListRewrite resourceRewrite = sourceRewriter.getListRewrite(newTryStatement, TryStatement.RESOURCES_PROPERTY);
	List<VariableDeclarationExpression> resources = tryStatementParent.resources();
	for(VariableDeclarationExpression expression : resources) {
		resourceRewrite.insertLast(expression, null);
	}
	ListRewrite catchClauseRewrite = sourceRewriter.getListRewrite(newTryStatement, TryStatement.CATCH_CLAUSES_PROPERTY);
	List<CatchClause> catchClauses = tryStatementParent.catchClauses();
	for(CatchClause catchClause : catchClauses) {
		catchClauseRewrite.insertLast(catchClause, null);
	}
	if(tryStatementParent.getFinally() != null) {
		sourceRewriter.set(newTryStatement, TryStatement.FINALLY_PROPERTY, tryStatementParent.getFinally(), null);
	}
	return newTryStatement;
}
 
源代码11 项目: SimFix   文件: TryPurifyByException.java
private ASTNode tryCatchStmt(AST ast, ASTNode node){
	Block block = ast.newBlock();
	block.statements().add(ASTNode.copySubtree(ast, node));
	TryStatement tryStatement = ast.newTryStatement();
	tryStatement.setBody(block);
	CatchClause catchClause = ast.newCatchClause();
	SingleVariableDeclaration svd = ast.newSingleVariableDeclaration();
	svd.setType(ast.newSimpleType(ast.newSimpleName("Exception")));
	svd.setName(ast.newSimpleName("mException"));
	catchClause.setException(svd);
	tryStatement.catchClauses().add(catchClause);
	return tryStatement;
}
 
public List<ImplementationCodeSmell> detectEmptyCatchBlock() {
	MethodControlFlowVisitor visitor = new MethodControlFlowVisitor();
	methodMetrics.getMethod().getMethodDeclaration().accept(visitor);
	for (TryStatement tryStatement : visitor.getTryStatements()) {
		for (Object catchClause : tryStatement.catchClauses()) {
			if (!hasBody((CatchClause) catchClause)) {
				addToSmells(initializeCodeSmell(EMPTY_CATCH_CLAUSE));
			}
		}
	}
	return smells;
}
 
源代码13 项目: eclipse.jdt.ls   文件: CodeScopeBuilder.java
@Override
public boolean visit(CatchClause node) {
	// open a new scope for the exception declaration.
	fScopes.add(fScope);
	fScope = new Scope(fScope, node.getStartPosition(), node.getLength());
	return true;
}
 
源代码14 项目: eclipse.jdt.ls   文件: FlowContext.java
void pushExcptions(TryStatement node) {
	List<CatchClause> catchClauses= node.catchClauses();
	if (catchClauses == null) {
		catchClauses= EMPTY_CATCH_CLAUSE;
	}
	fExceptionStack.add(catchClauses);
}
 
源代码15 项目: eclipse.jdt.ls   文件: FlowAnalyzer.java
@Override
public void endVisit(CatchClause node) {
	if (skipNode(node)) {
		return;
	}
	processSequential(node, node.getException(), node.getBody());
}
 
源代码16 项目: eclipse.jdt.ls   文件: AbstractExceptionAnalyzer.java
@Override
public boolean visit(TryStatement node) {
	fCurrentExceptions = new ArrayList<>(1);
	fTryStack.push(fCurrentExceptions);

	// visit try block
	node.getBody().accept(this);

	List<VariableDeclarationExpression> resources = node.resources();
	for (Iterator<VariableDeclarationExpression> iterator = resources.iterator(); iterator.hasNext();) {
		iterator.next().accept(this);
	}

	// Remove those exceptions that get catch by following catch blocks
	List<CatchClause> catchClauses = node.catchClauses();
	if (!catchClauses.isEmpty()) {
		handleCatchArguments(catchClauses);
	}
	List<ITypeBinding> current = fTryStack.pop();
	fCurrentExceptions = fTryStack.peek();
	for (Iterator<ITypeBinding> iter = current.iterator(); iter.hasNext();) {
		addException(iter.next(), node.getAST());
	}

	// visit catch and finally
	for (Iterator<CatchClause> iter = catchClauses.iterator(); iter.hasNext();) {
		iter.next().accept(this);
	}
	if (node.getFinally() != null) {
		node.getFinally().accept(this);
	}

	// return false. We have visited the body by ourselves.
	return false;
}
 
源代码17 项目: eclipse.jdt.ls   文件: AbstractExceptionAnalyzer.java
private void handleCatchArguments(List<CatchClause> catchClauses) {
	for (Iterator<CatchClause> iter = catchClauses.iterator(); iter.hasNext();) {
		Type type = iter.next().getException().getType();
		if (type instanceof UnionType) {
			List<Type> types = ((UnionType) type).types();
			for (Iterator<Type> iterator = types.iterator(); iterator.hasNext();) {
				removeCaughtExceptions(iterator.next().resolveBinding());
			}
		} else {
			removeCaughtExceptions(type.resolveBinding());
		}
	}
}
 
源代码18 项目: xtext-xtend   文件: JavaASTFlattener.java
@Override
public boolean visit(final SingleVariableDeclaration it) {
  if ((((it.getParent() instanceof MethodDeclaration) || (it.getParent() instanceof CatchClause)) || 
    (it.getParent() instanceof EnhancedForStatement))) {
    final Function1<IExtendedModifier, Boolean> _function = (IExtendedModifier it_1) -> {
      return Boolean.valueOf(it_1.isAnnotation());
    };
    this.appendModifiers(it, IterableExtensions.<IExtendedModifier>filter(Iterables.<IExtendedModifier>filter(it.modifiers(), IExtendedModifier.class), _function));
  } else {
    this.appendModifiers(it, it.modifiers());
  }
  it.getType().accept(this);
  this.appendExtraDimensions(it.getExtraDimensions());
  boolean _isVarargs = it.isVarargs();
  if (_isVarargs) {
    this.appendToBuffer("...");
  }
  this.appendSpaceToBuffer();
  it.getName().accept(this);
  Expression _initializer = it.getInitializer();
  boolean _tripleNotEquals = (_initializer != null);
  if (_tripleNotEquals) {
    this.appendToBuffer("=");
    it.getInitializer().accept(this);
  }
  return false;
}
 
源代码19 项目: xtext-xtend   文件: JavaASTFlattener.java
@Override
public boolean visit(final CatchClause node) {
  int _nodeType = node.getException().getType().getNodeType();
  boolean _tripleEquals = (_nodeType == 84);
  if (_tripleEquals) {
    this.appendToBuffer(" catch (");
    final List<ASTNode> types = this._aSTFlattenerUtils.genericChildListProperty(node.getException().getType(), "types");
    if (types!=null) {
      final Procedure2<ASTNode, Integer> _function = (ASTNode child, Integer index) -> {
        child.accept(this);
        int _size = types.size();
        int _minus = (_size - 1);
        boolean _lessThan = ((index).intValue() < _minus);
        if (_lessThan) {
          this.appendSpaceToBuffer();
          this.appendToBuffer("|");
          this.appendSpaceToBuffer();
        }
      };
      IterableExtensions.<ASTNode>forEach(types, _function);
    }
    this.appendSpaceToBuffer();
    this.appendToBuffer(this._aSTFlattenerUtils.toSimpleName(node.getException().getName()));
    this.appendToBuffer(") ");
    node.getBody().accept(this);
  } else {
    this.appendToBuffer(" catch (");
    node.getException().accept(this);
    this.appendToBuffer(") ");
    node.getBody().accept(this);
  }
  return false;
}
 
@Override
public final void endVisit(final CatchClause node) {
	final SingleVariableDeclaration declaration= node.getException();
	if (declaration != null) {
		final ConstraintVariable2 descendant= (ConstraintVariable2) declaration.getProperty(PROPERTY_CONSTRAINT_VARIABLE);
		if (descendant != null) {
			final ITypeBinding binding= node.getAST().resolveWellKnownType("java.lang.Throwable"); //$NON-NLS-1$
			if (binding != null) {
				final ConstraintVariable2 ancestor= fModel.createImmutableTypeVariable(binding);
				if (ancestor != null)
					fModel.createSubtypeConstraint(descendant, ancestor);
			}
		}
	}
}
 
@Override
public boolean visit(CatchClause node) {
	SingleVariableDeclaration exception= node.getException();
	IVariableBinding variableBinding= exception.resolveBinding();
	VariableVariable2 cv= fTCModel.makeDeclaredVariableVariable(variableBinding, fCU);
	setConstraintVariable(exception, cv);
	return true;
}
 
@Override
public void endVisit(TryStatement node) {
	if (skipNode(node))
		return;
	TryFlowInfo info= createTry();
	setFlowInfo(node, info);
	info.mergeTry(getFlowInfo(node.getBody()), fFlowContext);
	for (Iterator<CatchClause> iter= node.catchClauses().iterator(); iter.hasNext();) {
		CatchClause element= iter.next();
		info.mergeCatch(getFlowInfo(element), fFlowContext);
	}
	info.mergeFinally(getFlowInfo(node.getFinally()), fFlowContext);
}
 
@Override
public boolean visit(TryStatement node) {
	fCurrentExceptions= new ArrayList<ITypeBinding>(1);
	fTryStack.push(fCurrentExceptions);

	// visit try block
	node.getBody().accept(this);

	List<VariableDeclarationExpression> resources= node.resources();
	for (Iterator<VariableDeclarationExpression> iterator= resources.iterator(); iterator.hasNext();) {
		iterator.next().accept(this);
	}

	// Remove those exceptions that get catch by following catch blocks
	List<CatchClause> catchClauses= node.catchClauses();
	if (!catchClauses.isEmpty())
		handleCatchArguments(catchClauses);
	List<ITypeBinding> current= fTryStack.pop();
	fCurrentExceptions= fTryStack.peek();
	for (Iterator<ITypeBinding> iter= current.iterator(); iter.hasNext();) {
		addException(iter.next(), node.getAST());
	}

	// visit catch and finally
	for (Iterator<CatchClause> iter= catchClauses.iterator(); iter.hasNext(); ) {
		iter.next().accept(this);
	}
	if (node.getFinally() != null)
		node.getFinally().accept(this);

	// return false. We have visited the body by ourselves.
	return false;
}
 
private void handleCatchArguments(List<CatchClause> catchClauses) {
	for (Iterator<CatchClause> iter= catchClauses.iterator(); iter.hasNext(); ) {
		Type type= iter.next().getException().getType();
		if (type instanceof UnionType) {
			List<Type> types= ((UnionType) type).types();
			for (Iterator<Type> iterator= types.iterator(); iterator.hasNext();) {
				removeCaughtExceptions(iterator.next().resolveBinding());
			}
		} else {
			removeCaughtExceptions(type.resolveBinding());
		}
	}
}
 
@Override
public boolean visit(CatchClause node) {
	if (isInside(node)) {
		node.getBody().accept(this);
		node.getException().accept(this);
	}
	return false;
}
 
@Override
public boolean visit(CatchClause node) {
	// open a new scope for the exception declaration.
	fScopes.add(fScope);
	fScope= new Scope(fScope, node.getStartPosition(), node.getLength());
	return true;
}
 
@Override
public boolean visit(TryStatement node) {
	int currentSize= fCaughtExceptions.size();
	List<CatchClause> catchClauses= node.catchClauses();
	for (Iterator<CatchClause> iter= catchClauses.iterator(); iter.hasNext();) {
		Type type= iter.next().getException().getType();
		if (type instanceof UnionType) {
			List<Type> types= ((UnionType) type).types();
			for (Iterator<Type> iterator= types.iterator(); iterator.hasNext();) {
				addCaughtException(iterator.next());
			}
		} else {
			addCaughtException(type);
		}
	}

	node.getBody().accept(this);

	handleResourceDeclarations(node);

	int toRemove= fCaughtExceptions.size() - currentSize;
	for (int i= toRemove; i > 0; i--) {
		fCaughtExceptions.remove(currentSize);
	}

	// visit catch and finally
	for (Iterator<CatchClause> iter= catchClauses.iterator(); iter.hasNext();) {
		iter.next().accept(this);
	}
	if (node.getFinally() != null)
		node.getFinally().accept(this);

	// return false. We have visited the body by ourselves.
	return false;
}
 
private static void setCatchClauseBody(CatchClause newCatchClause, ASTRewrite rewrite, CatchClause catchClause) {
		// Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=350285
		
//		newCatchClause.setBody((Block) rewrite.createCopyTarget(catchClause.getBody()));
		
		//newCatchClause#setBody() destroys the formatting, hence copy statement by statement.
		List<Statement> statements= catchClause.getBody().statements();
		for (Iterator<Statement> iterator2= statements.iterator(); iterator2.hasNext();) {
			newCatchClause.getBody().statements().add(rewrite.createCopyTarget(iterator2.next()));
		}
	}
 
private ITypeBinding getPossibleSuperTypeBinding(ASTNode node) {
	if (fTypeKind == K_ANNOTATION) {
		return null;
	}

	AST ast= node.getAST();
	node= ASTNodes.getNormalizedNode(node);
	ASTNode parent= node.getParent();
	switch (parent.getNodeType()) {
		case ASTNode.METHOD_DECLARATION:
			if (node.getLocationInParent() == MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY) {
				return ast.resolveWellKnownType("java.lang.Exception"); //$NON-NLS-1$
			}
			break;
		case ASTNode.THROW_STATEMENT :
			return ast.resolveWellKnownType("java.lang.Exception"); //$NON-NLS-1$
		case ASTNode.SINGLE_VARIABLE_DECLARATION:
			if (parent.getLocationInParent() == CatchClause.EXCEPTION_PROPERTY) {
				return ast.resolveWellKnownType("java.lang.Exception"); //$NON-NLS-1$
			}
			break;
		case ASTNode.VARIABLE_DECLARATION_STATEMENT:
		case ASTNode.FIELD_DECLARATION:
			return null; // no guessing for LHS types, cannot be a supertype of a known type
		case ASTNode.PARAMETERIZED_TYPE:
			return null; // Inheritance doesn't help: A<X> z= new A<String>(); ->
	}
	ITypeBinding binding= ASTResolving.guessBindingForTypeReference(node);
	if (binding != null && !binding.isRecovered()) {
		return binding;
	}
	return null;
}
 
源代码30 项目: JDeodorant   文件: CloneInstanceMapper.java
private ASTNode getParent(ASTNode controlNode) {
	if(!(controlNode.getParent() instanceof Block))
		return controlNode.getParent();
	while(controlNode.getParent() instanceof Block) {
		controlNode = controlNode.getParent();
	}
	if(controlNode.getParent() instanceof CatchClause) {
		CatchClause catchClause = (CatchClause)controlNode.getParent();
		return catchClause.getParent();
	}
	return controlNode.getParent();
}
 
源代码评论
动弹
沙发等你来抢
 类所在包
 类方法
 同包方法