@Override public void endVisit(CompilationUnit node) { postProcessSelectedNodes(internalGetSelectedNodes()); ASTNode enclosingNode= null; superCall: { if (getStatus().hasFatalError()) break superCall; if (!hasSelectedNodes()) { ASTNode coveringNode= getLastCoveringNode(); if (coveringNode instanceof Block) { Block block= (Block)coveringNode; Message[] messages= ASTNodes.getMessages(block, ASTNodes.NODE_ONLY); if (messages.length > 0) { invalidSelection(JavaManipulationMessages.SurroundWithTryCatchAnalyzer_compile_errors, JavaStatusContext.create(getCompilationUnit(), block)); break superCall; invalidSelection(JavaManipulationMessages.SurroundWithTryCatchAnalyzer_doesNotCover); break superCall; enclosingNode= getEnclosingNode(getFirstSelectedNode()); boolean isValidEnclosingNode= enclosingNode instanceof MethodDeclaration || enclosingNode instanceof Initializer; if (fSurroundWithTryCatch) { invalidSelection(JavaManipulationMessages.SurroundWithTryCatchAnalyzer_doesNotContain); break superCall; if (!validSelectedNodes()) { invalidSelection(JavaManipulationMessages.SurroundWithTryCatchAnalyzer_onlyStatements);
/** * Selected nodes in <code>context</code> under <code>selection</code> or null if no valid * selection. * * @param context The context in which the proposal is applied. * @return Selected nodes or null if no valid selection. * @throws CoreException if the analyzer cannot be created */ public static ASTNode[] getValidSelectedNodes(IInvocationContext context) throws CoreException { Selection selection= Selection.createFromStartLength(context.getSelectionOffset(), context.getSelectionLength()); SurroundWithAnalyzer analyzer= new SurroundWithAnalyzer(context.getCompilationUnit(), selection, false); context.getASTRoot().accept(analyzer); if (!analyzer.getStatus().isOK() || !analyzer.hasSelectedNodes()) { return null; } else { return analyzer.getValidSelectedNodes(); } }
private boolean validSelectedNodes() { ASTNode[] nodes= getSelectedNodes(); for (int i= 0; i < nodes.length; i++) { ASTNode node= nodes[i]; boolean isValidNode= node instanceof Statement; if (fSurroundWithTryCatch) { // allow method reference and lambda's expression body also isValidNode= isValidNode || node instanceof MethodReference || node.getLocationInParent() == LambdaExpression.BODY_PROPERTY; } if (!isValidNode) return false; } return true; }
public ASTNode[] getValidSelectedNodes() { if (hasSelectedNodes()) { return internalGetSelectedNodes().toArray(new ASTNode[internalGetSelectedNodes().size()]); } else { return new Statement[0]; } }
public static boolean isApplicable(IInvocationContext context) throws CoreException { ICompilationUnit unit= context.getCompilationUnit(); CompilationUnit ast= SharedASTProviderCore.getAST(unit, SharedASTProviderCore.WAIT_NO, null); if (ast == null) return true; Selection selection= Selection.createFromStartLength(context.getSelectionOffset(), context.getSelectionLength()); SurroundWithAnalyzer analyzer= new SurroundWithAnalyzer(unit, selection, false); context.getASTRoot().accept(analyzer); return analyzer.getStatus().isOK() && analyzer.hasSelectedNodes(); }
@Override public void endVisit(CompilationUnit node) { ASTNode enclosingNode= null; if (!getStatus().hasFatalError() && hasSelectedNodes()) enclosingNode= SurroundWithAnalyzer.getEnclosingNode(getFirstSelectedNode()); super.endVisit(node); if (enclosingNode != null && !getStatus().hasFatalError()) { fExceptions= ExceptionAnalyzer.perform(enclosingNode, getSelection()); if (fExceptions == null || fExceptions.length == 0) { if (enclosingNode instanceof MethodReference) { invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_doesNotContain); } else { fExceptions= new ITypeBinding[] { node.getAST().resolveWellKnownType("java.lang.Exception") }; //$NON-NLS-1$ } } } } }
ASTNode enclosingNode = SurroundWithAnalyzer.getEnclosingNode(selectedNode); if (enclosingNode == null) { return;
@Override public void endVisit(CompilationUnit node) { ASTNode enclosingNode = null; if (!getStatus().hasFatalError() && hasSelectedNodes()) { enclosingNode= getEnclosingNode(getFirstSelectedNode()); } super.endVisit(node); if (enclosingNode != null && !getStatus().hasFatalError()) { fExceptions = ExceptionAnalyzer.perform(enclosingNode, getSelection()); if (fExceptions == null || fExceptions.length == 0) { if (enclosingNode instanceof MethodReference) { invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_doesNotContain); } else { fExceptions = new ITypeBinding[] { node.getAST().resolveWellKnownType("java.lang.Exception") }; //$NON-NLS-1$ } } } } }
public BodyDeclaration getEnclosingBodyDeclaration() { ASTNode node= getFirstSelectedNode(); if (node == null) return null; return ASTNodes.getParent(node, BodyDeclaration.class); }
ASTNode enclosingNode= SurroundWithAnalyzer.getEnclosingNode(selectedNode); if (enclosingNode == null) { return;