Search in sources :

Example 1 with ExtractMethodDescriptor

use of org.eclipse.jdt.core.refactoring.descriptors.ExtractMethodDescriptor in project che by eclipse.

the class ExtractMethodRefactoring method getRefactoringDescriptor.

private ExtractMethodDescriptor getRefactoringDescriptor() {
    final Map<String, String> arguments = new HashMap<String, String>();
    String project = null;
    IJavaProject javaProject = fCUnit.getJavaProject();
    if (javaProject != null)
        project = javaProject.getElementName();
    ITypeBinding type = null;
    if (fDestination instanceof AbstractTypeDeclaration) {
        final AbstractTypeDeclaration decl = (AbstractTypeDeclaration) fDestination;
        type = decl.resolveBinding();
    } else if (fDestination instanceof AnonymousClassDeclaration) {
        final AnonymousClassDeclaration decl = (AnonymousClassDeclaration) fDestination;
        type = decl.resolveBinding();
    }
    IMethodBinding method = null;
    final BodyDeclaration enclosing = fAnalyzer.getEnclosingBodyDeclaration();
    if (enclosing instanceof MethodDeclaration) {
        final MethodDeclaration node = (MethodDeclaration) enclosing;
        method = node.resolveBinding();
    }
    final int flags = RefactoringDescriptor.STRUCTURAL_CHANGE | JavaRefactoringDescriptor.JAR_REFACTORING | JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
    final String description = Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(fMethodName));
    final String label = method != null ? BindingLabelProvider.getBindingLabel(method, JavaElementLabels.ALL_FULLY_QUALIFIED) : '{' + JavaElementLabels.ELLIPSIS_STRING + '}';
    final String header = Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_descriptor_description, new String[] { BasicElementLabels.getJavaElementName(getSignature()), label, BindingLabelProvider.getBindingLabel(type, JavaElementLabels.ALL_FULLY_QUALIFIED) });
    final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(project, this, header);
    comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_name_pattern, BasicElementLabels.getJavaElementName(fMethodName)));
    comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_destination_pattern, BindingLabelProvider.getBindingLabel(type, JavaElementLabels.ALL_FULLY_QUALIFIED)));
    String visibility = JdtFlags.getVisibilityString(fVisibility);
    if (//$NON-NLS-1$
    "".equals(visibility))
        visibility = RefactoringCoreMessages.ExtractMethodRefactoring_default_visibility;
    comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_visibility_pattern, visibility));
    if (fThrowRuntimeExceptions)
        comment.addSetting(RefactoringCoreMessages.ExtractMethodRefactoring_declare_thrown_exceptions);
    if (fReplaceDuplicates)
        comment.addSetting(RefactoringCoreMessages.ExtractMethodRefactoring_replace_occurrences);
    if (fGenerateJavadoc)
        comment.addSetting(RefactoringCoreMessages.ExtractMethodRefactoring_generate_comment);
    final ExtractMethodDescriptor descriptor = RefactoringSignatureDescriptorFactory.createExtractMethodDescriptor(project, description, comment.asString(), arguments, flags);
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fCUnit));
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME, fMethodName);
    //$NON-NLS-1$
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION, new Integer(fSelectionStart).toString() + " " + new Integer(fSelectionLength).toString());
    arguments.put(ATTRIBUTE_VISIBILITY, new Integer(fVisibility).toString());
    arguments.put(ATTRIBUTE_DESTINATION, new Integer(fDestinationIndex).toString());
    arguments.put(ATTRIBUTE_EXCEPTIONS, Boolean.valueOf(fThrowRuntimeExceptions).toString());
    arguments.put(ATTRIBUTE_COMMENTS, Boolean.valueOf(fGenerateJavadoc).toString());
    arguments.put(ATTRIBUTE_REPLACE, Boolean.valueOf(fReplaceDuplicates).toString());
    return descriptor;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) HashMap(java.util.HashMap) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment) ExtractMethodDescriptor(org.eclipse.jdt.core.refactoring.descriptors.ExtractMethodDescriptor) IJavaProject(org.eclipse.jdt.core.IJavaProject) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 2 with ExtractMethodDescriptor

use of org.eclipse.jdt.core.refactoring.descriptors.ExtractMethodDescriptor in project eclipse.jdt.ls by eclipse.

the class ExtractMethodRefactoring method getRefactoringDescriptor.

private ExtractMethodDescriptor getRefactoringDescriptor() {
    final Map<String, String> arguments = new HashMap<>();
    String project = null;
    IJavaProject javaProject = fCUnit.getJavaProject();
    if (javaProject != null) {
        project = javaProject.getElementName();
    }
    ITypeBinding type = null;
    if (fDestination instanceof AbstractTypeDeclaration) {
        final AbstractTypeDeclaration decl = (AbstractTypeDeclaration) fDestination;
        type = decl.resolveBinding();
    } else if (fDestination instanceof AnonymousClassDeclaration) {
        final AnonymousClassDeclaration decl = (AnonymousClassDeclaration) fDestination;
        type = decl.resolveBinding();
    }
    IMethodBinding method = null;
    final BodyDeclaration enclosing = fAnalyzer.getEnclosingBodyDeclaration();
    if (enclosing instanceof MethodDeclaration) {
        final MethodDeclaration node = (MethodDeclaration) enclosing;
        method = node.resolveBinding();
    }
    final int flags = RefactoringDescriptor.STRUCTURAL_CHANGE | JavaRefactoringDescriptor.JAR_REFACTORING | JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
    final String description = Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(fMethodName));
    final String label = method != null ? BindingLabelProvider.getBindingLabel(method, JavaElementLabels.ALL_FULLY_QUALIFIED) : '{' + JavaElementLabels.ELLIPSIS_STRING + '}';
    final String header = Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_descriptor_description, new String[] { BasicElementLabels.getJavaElementName(getSignature()), label, BindingLabelProvider.getBindingLabel(type, JavaElementLabels.ALL_FULLY_QUALIFIED) });
    final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(project, this, header);
    comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_name_pattern, BasicElementLabels.getJavaElementName(fMethodName)));
    comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_destination_pattern, BindingLabelProvider.getBindingLabel(type, JavaElementLabels.ALL_FULLY_QUALIFIED)));
    String visibility = JdtFlags.getVisibilityString(fVisibility);
    if ("".equals(visibility)) {
        visibility = RefactoringCoreMessages.ExtractMethodRefactoring_default_visibility;
    }
    comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_visibility_pattern, visibility));
    if (fThrowRuntimeExceptions) {
        comment.addSetting(RefactoringCoreMessages.ExtractMethodRefactoring_declare_thrown_exceptions);
    }
    if (fReplaceDuplicates) {
        comment.addSetting(RefactoringCoreMessages.ExtractMethodRefactoring_replace_occurrences);
    }
    if (fGenerateJavadoc) {
        comment.addSetting(RefactoringCoreMessages.ExtractMethodRefactoring_generate_comment);
    }
    final ExtractMethodDescriptor descriptor = RefactoringSignatureDescriptorFactory.createExtractMethodDescriptor(project, description, comment.asString(), arguments, flags);
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fCUnit));
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME, fMethodName);
    // $NON-NLS-1$
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION, String.valueOf(fSelectionStart) + " " + String.valueOf(fSelectionLength));
    arguments.put(ATTRIBUTE_VISIBILITY, new Integer(fVisibility).toString());
    arguments.put(ATTRIBUTE_DESTINATION, new Integer(fDestinationIndex).toString());
    arguments.put(ATTRIBUTE_EXCEPTIONS, Boolean.valueOf(fThrowRuntimeExceptions).toString());
    arguments.put(ATTRIBUTE_COMMENTS, Boolean.valueOf(fGenerateJavadoc).toString());
    arguments.put(ATTRIBUTE_REPLACE, Boolean.valueOf(fReplaceDuplicates).toString());
    return descriptor;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) HashMap(java.util.HashMap) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) JDTRefactoringDescriptorComment(org.eclipse.jdt.ls.core.internal.corext.refactoring.JDTRefactoringDescriptorComment) ExtractMethodDescriptor(org.eclipse.jdt.core.refactoring.descriptors.ExtractMethodDescriptor) IJavaProject(org.eclipse.jdt.core.IJavaProject) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

HashMap (java.util.HashMap)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)2 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)2 AnonymousClassDeclaration (org.eclipse.jdt.core.dom.AnonymousClassDeclaration)2 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)2 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)2 ExtractMethodDescriptor (org.eclipse.jdt.core.refactoring.descriptors.ExtractMethodDescriptor)2 JDTRefactoringDescriptorComment (org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment)1 JDTRefactoringDescriptorComment (org.eclipse.jdt.ls.core.internal.corext.refactoring.JDTRefactoringDescriptorComment)1