Search in sources :

Example 81 with IMethodBinding

use of org.eclipse.jdt.core.dom.IMethodBinding in project che by eclipse.

the class BindingLabelProvider method getLocalVariableLabel.

private static void getLocalVariableLabel(IVariableBinding binding, long flags, StringBuffer buffer) {
    if (((flags & JavaElementLabels.F_PRE_TYPE_SIGNATURE) != 0)) {
        getTypeLabel(binding.getType(), (flags & JavaElementLabels.T_TYPE_PARAMETERS), buffer);
        buffer.append(' ');
    }
    if (((flags & JavaElementLabels.F_FULLY_QUALIFIED) != 0)) {
        IMethodBinding declaringMethod = binding.getDeclaringMethod();
        if (declaringMethod != null) {
            getMethodLabel(declaringMethod, flags, buffer);
            buffer.append('.');
        }
    }
    buffer.append(binding.getName());
    if (((flags & JavaElementLabels.F_APP_TYPE_SIGNATURE) != 0)) {
        buffer.append(JavaElementLabels.DECL_STRING);
        getTypeLabel(binding.getType(), (flags & JavaElementLabels.T_TYPE_PARAMETERS), buffer);
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding)

Example 82 with IMethodBinding

use of org.eclipse.jdt.core.dom.IMethodBinding in project che by eclipse.

the class IntroduceFactoryRefactoring method getUnimplementedMethods.

private IMethodBinding[] getUnimplementedMethods(ITypeBinding binding) {
    IMethodBinding[] unimplementedMethods = StubUtility2.getUnimplementedMethods(binding, true);
    Arrays.sort(unimplementedMethods, new MethodsSourcePositionComparator(binding));
    return unimplementedMethods;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) MethodsSourcePositionComparator(org.eclipse.jdt.internal.corext.util.MethodsSourcePositionComparator)

Example 83 with IMethodBinding

use of org.eclipse.jdt.core.dom.IMethodBinding 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 84 with IMethodBinding

use of org.eclipse.jdt.core.dom.IMethodBinding in project che by eclipse.

the class IntroduceIndirectionRefactoring method findMethodBindingInHierarchy.

// ********* SMALL HELPERS ********************
/*
	 * Helper method for finding an IMethod inside a binding hierarchy
	 */
private IMethodBinding findMethodBindingInHierarchy(ITypeBinding currentTypeBinding, IMethod methodDeclaration) {
    IMethodBinding[] bindings = currentTypeBinding.getDeclaredMethods();
    for (int i = 0; i < bindings.length; i++) if (methodDeclaration.equals(bindings[i].getJavaElement()))
        return bindings[i];
    ITypeBinding superClass = currentTypeBinding.getSuperclass();
    if (superClass != null) {
        IMethodBinding b = findMethodBindingInHierarchy(superClass, methodDeclaration);
        if (b != null)
            return b;
    }
    ITypeBinding[] interfaces = currentTypeBinding.getInterfaces();
    for (int i = 0; i < interfaces.length; i++) {
        IMethodBinding b = findMethodBindingInHierarchy(interfaces[i], methodDeclaration);
        if (b != null)
            return b;
    }
    return null;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 85 with IMethodBinding

use of org.eclipse.jdt.core.dom.IMethodBinding in project che by eclipse.

the class IntroduceIndirectionRefactoring method qualifyThisExpression.

/**
	 * Attempts to qualify a "this" expression for a method invocation with an appropriate qualifier.
	 * The invoked method is analyzed according to the following specs:
	 *
	 * 'this' must be qualified iff method is declared in an enclosing type or a supertype of an enclosing type
	 *
	 * 1) The method is declared somewhere outside of the cu of the invocation
	 *      1a) inside a supertype of the current type
	 *      1b) inside a supertype of an enclosing type
	 * 2) The method is declared inside of the cu of the invocation
	 * 		2a) inside the type of the invocation
	 * 		2b) outside the type of the invocation
	 *
	 * In case of 1a) and 2b), qualify with the enclosing type.
	 * @param expr a {@link ThisExpression}
	 * @param originalInvocation the original method invocation
	 * @param enclosing the enclosing member of the original method invocation
	 * @param unitRewriter the rewrite
	 * @return resulting status
	 *
	 */
private RefactoringStatus qualifyThisExpression(ThisExpression expr, MethodInvocation originalInvocation, IMember enclosing, CompilationUnitRewrite unitRewriter) {
    RefactoringStatus status = new RefactoringStatus();
    IMethodBinding methodBinding = originalInvocation.resolveMethodBinding();
    MethodDeclaration methodDeclaration = (MethodDeclaration) ASTNodes.findDeclaration(methodBinding, originalInvocation.getRoot());
    ITypeBinding currentTypeBinding = null;
    if (methodDeclaration != null) {
        // Case 1) : Declaring type is inside this cu => use its name if it's declared in an enclosing type
        if (ASTNodes.isParent(originalInvocation, methodDeclaration.getParent()))
            currentTypeBinding = methodBinding.getDeclaringClass();
        else
            currentTypeBinding = ASTNodes.getEnclosingType(originalInvocation);
    } else {
        // Case 2) : Declaring type is outside of this cu => find subclass in this cu
        ASTNode currentTypeDeclaration = getEnclosingTypeDeclaration(originalInvocation);
        currentTypeBinding = ASTNodes.getEnclosingType(currentTypeDeclaration);
        while (currentTypeDeclaration != null && (Bindings.findMethodInHierarchy(currentTypeBinding, methodBinding.getName(), methodBinding.getParameterTypes()) == null)) {
            currentTypeDeclaration = getEnclosingTypeDeclaration(currentTypeDeclaration.getParent());
            currentTypeBinding = ASTNodes.getEnclosingType(currentTypeDeclaration);
        }
    }
    if (currentTypeBinding == null) {
        status.merge(createWarningAboutCall(enclosing, originalInvocation, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_declaring_type_not_found));
        return status;
    }
    currentTypeBinding = currentTypeBinding.getTypeDeclaration();
    ITypeBinding typeOfCall = ASTNodes.getEnclosingType(originalInvocation);
    if (!typeOfCall.equals(currentTypeBinding)) {
        if (currentTypeBinding.isAnonymous()) {
            // Cannot qualify, see bug 115277
            status.merge(createWarningAboutCall(enclosing, originalInvocation, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_anonymous_cannot_qualify));
        } else {
            expr.setQualifier(unitRewriter.getAST().newSimpleName(currentTypeBinding.getName()));
        }
    } else {
    // do not qualify, only use "this.".
    }
    return status;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus)

Aggregations

IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)173 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)110 ASTNode (org.eclipse.jdt.core.dom.ASTNode)46 Expression (org.eclipse.jdt.core.dom.Expression)36 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)33 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)32 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)29 ArrayList (java.util.ArrayList)27 IBinding (org.eclipse.jdt.core.dom.IBinding)25 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)21 CastExpression (org.eclipse.jdt.core.dom.CastExpression)20 SimpleName (org.eclipse.jdt.core.dom.SimpleName)20 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)20 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)19 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)19 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)19 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)17 Image (org.eclipse.swt.graphics.Image)16 AST (org.eclipse.jdt.core.dom.AST)15 Type (org.eclipse.jdt.core.dom.Type)15