public String initialize(JavaScriptUnit root, ASTNode node) { fAST = root.getAST(); if (!(node instanceof Name)) { return SearchMessages.ExceptionOccurrencesFinder_no_exception; } fSelectedName = ASTNodes.getTopMostName((Name) node); ASTNode parent = fSelectedName.getParent(); FunctionDeclaration decl = resolveMethodDeclaration(parent); if (decl != null && methodThrowsException(decl, fSelectedName)) { fException = fSelectedName.resolveTypeBinding(); fStart = decl.getBody(); } else if (parent instanceof Type) { parent = parent.getParent(); if (parent instanceof SingleVariableDeclaration && parent.getParent() instanceof CatchClause) { CatchClause catchClause = (CatchClause) parent.getParent(); TryStatement tryStatement = (TryStatement) catchClause.getParent(); if (tryStatement != null) { IVariableBinding var = catchClause.getException().resolveBinding(); if (var != null && var.getType() != null) { fException = var.getType(); fStart = tryStatement.getBody(); } } } } if (fException == null || fStart == null) return SearchMessages.ExceptionOccurrencesFinder_no_exception; return null; }
private FunctionDeclaration resolveMethodDeclaration(ASTNode node) { if (node instanceof FunctionDeclaration) return (FunctionDeclaration) node; JSdoc doc = (JSdoc) ASTNodes.getParent(node, ASTNode.JSDOC); if (doc == null) return null; if (doc.getParent() instanceof FunctionDeclaration) return (FunctionDeclaration) doc.getParent(); return null; }
private AbstractTypeDeclaration getContainingTypeDeclarationNode() throws JavaScriptModelException { AbstractTypeDeclaration result = (AbstractTypeDeclaration) ASTNodes.getParent( getSelectedExpression().getAssociatedNode(), AbstractTypeDeclaration.class); Assert.isNotNull(result); return result; }
public String getElementName() { if (fSelectedName != null) { return ASTNodes.asString(fSelectedName); } return null; }
private String getConstantTypeName() throws JavaScriptModelException { return ASTNodes.asString(getConstantType()); }
public Change createChange(IProgressMonitor monitor) throws CoreException { final Map arguments = new HashMap(); String project = null; IJavaScriptProject javaProject = fCu.getJavaScriptProject(); if (javaProject != null) project = javaProject.getElementName(); int flags = JavaScriptRefactoringDescriptor.JAR_REFACTORING | JavaScriptRefactoringDescriptor.JAR_SOURCE_ATTACHMENT; if (JdtFlags.getVisibilityCode(fVisibility) != Modifier.PRIVATE) flags |= RefactoringDescriptor.STRUCTURAL_CHANGE; String pattern = ""; // $NON-NLS-1$ try { pattern = BindingLabelProvider.getBindingLabel( getContainingTypeBinding(), JavaScriptElementLabels.ALL_FULLY_QUALIFIED) + "."; //$NON-NLS-1$ } catch (JavaScriptModelException exception) { JavaScriptPlugin.log(exception); } final String expression = ASTNodes.asString(fSelectedExpression.getAssociatedExpression()); final String description = Messages.format( RefactoringCoreMessages.ExtractConstantRefactoring_descriptor_description_short, fConstantName); final String header = Messages.format( RefactoringCoreMessages.ExtractConstantRefactoring_descriptor_description, new String[] {pattern + fConstantName, expression}); final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(project, this, header); comment.addSetting( Messages.format( RefactoringCoreMessages.ExtractConstantRefactoring_constant_name_pattern, fConstantName)); comment.addSetting( Messages.format( RefactoringCoreMessages.ExtractConstantRefactoring_constant_expression_pattern, expression)); String visibility = fVisibility; if ("".equals(visibility)) // $NON-NLS-1$ visibility = RefactoringCoreMessages.ExtractConstantRefactoring_default_visibility; comment.addSetting( Messages.format( RefactoringCoreMessages.ExtractConstantRefactoring_visibility_pattern, visibility)); if (fReplaceAllOccurrences) comment.addSetting(RefactoringCoreMessages.ExtractConstantRefactoring_replace_occurrences); if (fQualifyReferencesWithDeclaringClassName) comment.addSetting(RefactoringCoreMessages.ExtractConstantRefactoring_qualify_references); final JDTRefactoringDescriptor descriptor = new JDTRefactoringDescriptor( IJavaScriptRefactorings.EXTRACT_CONSTANT, project, description, comment.asString(), arguments, flags); arguments.put(JDTRefactoringDescriptor.ATTRIBUTE_INPUT, descriptor.elementToHandle(fCu)); arguments.put(JDTRefactoringDescriptor.ATTRIBUTE_NAME, fConstantName); arguments.put( JDTRefactoringDescriptor.ATTRIBUTE_SELECTION, new Integer(fSelectionStart).toString() + " " + new Integer(fSelectionLength).toString()); // $NON-NLS-1$ arguments.put(ATTRIBUTE_REPLACE, Boolean.valueOf(fReplaceAllOccurrences).toString()); arguments.put( ATTRIBUTE_QUALIFY, Boolean.valueOf(fQualifyReferencesWithDeclaringClassName).toString()); arguments.put( ATTRIBUTE_VISIBILITY, new Integer(JdtFlags.getVisibilityCode(fVisibility)).toString()); return new RefactoringDescriptorChange( descriptor, RefactoringCoreMessages.ExtractConstantRefactoring_name, new Change[] {fChange}); }