Search in sources :

Example 96 with IMethodBinding

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

the class PromoteTempToFieldRefactoring method checkTempTypeForLocalTypeUsage.

private RefactoringStatus checkTempTypeForLocalTypeUsage() {
    VariableDeclarationStatement vds = getTempDeclarationStatement();
    if (vds == null)
        return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_cannot_promote);
    Type type = vds.getType();
    ITypeBinding binding = type.resolveBinding();
    if (binding == null)
        return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_cannot_promote);
    IMethodBinding declaringMethodBinding = getMethodDeclaration().resolveBinding();
    ITypeBinding[] methodTypeParameters = declaringMethodBinding == null ? new ITypeBinding[0] : declaringMethodBinding.getTypeParameters();
    LocalTypeAndVariableUsageAnalyzer analyzer = new LocalTypeAndVariableUsageAnalyzer(methodTypeParameters);
    type.accept(analyzer);
    boolean usesLocalTypes = !analyzer.getUsageOfEnclosingNodes().isEmpty();
    fTempTypeUsesClassTypeVariables = analyzer.getClassTypeVariablesUsed();
    if (usesLocalTypes)
        return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_uses_type_declared_locally);
    return null;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement)

Example 97 with IMethodBinding

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

the class ReplaceInvocationsRefactoring method createChange.

@Override
public Change createChange(IProgressMonitor pm) throws CoreException {
    // TODO: update for fSelectionStart == -1
    final Map<String, String> arguments = new HashMap<String, String>();
    String project = null;
    IJavaProject javaProject = fSelectionTypeRoot.getJavaProject();
    if (javaProject != null)
        project = javaProject.getElementName();
    final IMethodBinding binding = fSourceProvider.getDeclaration().resolveBinding();
    int flags = RefactoringDescriptor.STRUCTURAL_CHANGE | JavaRefactoringDescriptor.JAR_REFACTORING | JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
    if (!Modifier.isPrivate(binding.getModifiers()))
        flags |= RefactoringDescriptor.MULTI_CHANGE;
    final String description = Messages.format(RefactoringCoreMessages.ReplaceInvocationsRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(binding.getName()));
    final String header = Messages.format(RefactoringCoreMessages.ReplaceInvocationsRefactoring_descriptor_description, new String[] { BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED), BindingLabelProvider.getBindingLabel(binding.getDeclaringClass(), JavaElementLabels.ALL_FULLY_QUALIFIED) });
    final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(project, this, header);
    comment.addSetting(Messages.format(RefactoringCoreMessages.ReplaceInvocationsRefactoring_original_pattern, BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED)));
    if (!fTargetProvider.isSingle())
        comment.addSetting(RefactoringCoreMessages.ReplaceInvocationsRefactoring_replace_references);
    //REVIEW Unregistered ID!
    final JavaRefactoringDescriptor descriptor = new JavaRefactoringDescriptor(ID_REPLACE_INVOCATIONS, project, description, comment.asString(), arguments, flags) {
    };
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fSelectionTypeRoot));
    //$NON-NLS-1$
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION, new Integer(fSelectionStart).toString() + " " + new Integer(fSelectionLength).toString());
    arguments.put(ATTRIBUTE_MODE, new Integer(fTargetProvider.isSingle() ? 0 : 1).toString());
    return new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.ReplaceInvocationsRefactoring_change_name, fChangeManager.getAllChanges());
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) IJavaProject(org.eclipse.jdt.core.IJavaProject) HashMap(java.util.HashMap) DynamicValidationRefactoringChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange) JavaRefactoringDescriptor(org.eclipse.jdt.core.refactoring.descriptors.JavaRefactoringDescriptor) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment)

Example 98 with IMethodBinding

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

the class IntroduceFactoryRefactoring method createFactoryMethod.

/**
	 * Creates and returns a new MethodDeclaration that represents the factory method to be used in
	 * place of direct calls to the constructor in question.
	 * 
	 * @param ast An AST used as a factory for various AST nodes
	 * @param ctorBinding binding for the constructor being wrapped
	 * @param unitRewriter the ASTRewrite to be used
	 * @return the new method declaration
	 * @throws CoreException if an exception occurs while accessing its corresponding resource
	 */
private MethodDeclaration createFactoryMethod(AST ast, IMethodBinding ctorBinding, ASTRewrite unitRewriter) throws CoreException {
    MethodDeclaration newMethod = ast.newMethodDeclaration();
    SimpleName newMethodName = ast.newSimpleName(fNewMethodName);
    ClassInstanceCreation newCtorCall = ast.newClassInstanceCreation();
    ReturnStatement ret = ast.newReturnStatement();
    Block body = ast.newBlock();
    List<Statement> stmts = body.statements();
    String retTypeName = ctorBinding.getName();
    createFactoryMethodSignature(ast, newMethod);
    newMethod.setName(newMethodName);
    newMethod.setBody(body);
    ITypeBinding declaringClass = fCtorBinding.getDeclaringClass();
    ITypeBinding[] ctorOwnerTypeParameters = declaringClass.getTypeParameters();
    setMethodReturnType(newMethod, retTypeName, ctorOwnerTypeParameters, ast);
    newMethod.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.STATIC | Modifier.PUBLIC));
    setCtorTypeArguments(newCtorCall, retTypeName, ctorOwnerTypeParameters, ast);
    createFactoryMethodConstructorArgs(ast, newCtorCall);
    if (Modifier.isAbstract(declaringClass.getModifiers())) {
        AnonymousClassDeclaration decl = ast.newAnonymousClassDeclaration();
        IMethodBinding[] unimplementedMethods = getUnimplementedMethods(declaringClass);
        CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(fCUHandle.getJavaProject());
        ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fFactoryCU, decl.getStartPosition(), fImportRewriter);
        for (int i = 0; i < unimplementedMethods.length; i++) {
            IMethodBinding unImplementedMethod = unimplementedMethods[i];
            MethodDeclaration newMethodDecl = StubUtility2.createImplementationStub(fCUHandle, unitRewriter, fImportRewriter, context, unImplementedMethod, unImplementedMethod.getDeclaringClass().getName(), settings, false);
            decl.bodyDeclarations().add(newMethodDecl);
        }
        newCtorCall.setAnonymousClassDeclaration(decl);
    }
    ret.setExpression(newCtorCall);
    stmts.add(ret);
    return newMethod;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) CodeGenerationSettings(org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Statement(org.eclipse.jdt.core.dom.Statement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block)

Example 99 with IMethodBinding

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

the class InferTypeArgumentsConstraintCreator method endVisit.

@Override
public void endVisit(MethodInvocation node) {
    IMethodBinding methodBinding = node.resolveMethodBinding();
    if (methodBinding == null)
        return;
    Expression receiver;
    if (JdtFlags.isStatic(methodBinding))
        receiver = null;
    else
        receiver = node.getExpression();
    if (isSpecialCloneInvocation(methodBinding, receiver)) {
        ConstraintVariable2 expressionCv = getConstraintVariable(receiver);
        // [retVal] =^= [receiver]:
        setConstraintVariable(node, expressionCv);
    } else if ("getClass".equals(methodBinding.getName()) && methodBinding.getParameterTypes().length == 0) {
        //$NON-NLS-1$
        //special case: see JLS3 4.3.2
        ITypeBinding returnType = node.resolveTypeBinding();
        ITypeBinding returnTypeDeclaration = returnType.getTypeDeclaration();
        ParameterizedTypeVariable2 expressionCv = fTCModel.makeParameterizedTypeVariable(returnTypeDeclaration);
        setConstraintVariable(node, expressionCv);
        ConstraintVariable2 classTypeVariable = fTCModel.getElementVariable(expressionCv, returnTypeDeclaration.getTypeParameters()[0]);
        //type of expression 'e.getClass()' is 'Class<? extends X>' where X is the static type of e
        ITypeBinding capture = returnType.getTypeArguments()[0];
        ITypeBinding wildcard = capture.getWildcard();
        if (wildcard.getBound() == null)
            // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=234619
            return;
        ImmutableTypeVariable2 wildcardType = fTCModel.makeImmutableTypeVariable(wildcard, /*no boxing*/
        null);
        fTCModel.createSubtypeConstraint(classTypeVariable, wildcardType);
    //			ITypeBinding bound= wildcard.getBound();
    //			ImmutableTypeVariable2 boundType= fTCModel.makeImmutableTypeVariable(bound, node.getAST());
    //			fTCModel.createSubtypeConstraint(classTypeVariable, boundType);
    } else {
        Map<String, IndependentTypeVariable2> methodTypeVariables = createMethodTypeArguments(methodBinding);
        doVisitMethodInvocationReturnType(node, methodBinding, receiver, methodTypeVariables);
        doVisitMethodInvocationArguments(methodBinding, node.arguments(), receiver, methodTypeVariables, /*no created type*/
        null);
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ImmutableTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ImmutableTypeVariable2) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ParameterizedTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ParameterizedTypeVariable2) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2) Map(java.util.Map) HashMap(java.util.HashMap)

Example 100 with IMethodBinding

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

the class SourceProvider method updateMethodTypeVariable.

private void updateMethodTypeVariable(ASTRewrite rewriter, CallContext context) {
    IMethodBinding method = Invocations.resolveBinding(context.invocation);
    if (method == null)
        return;
    rewriteReferences(rewriter, method.getTypeArguments(), fAnalyzer.getMethodTypeParameterReferences());
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding)

Aggregations

IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)164 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)103 ASTNode (org.eclipse.jdt.core.dom.ASTNode)46 Expression (org.eclipse.jdt.core.dom.Expression)34 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)33 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)32 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)28 ArrayList (java.util.ArrayList)27 IBinding (org.eclipse.jdt.core.dom.IBinding)24 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)20 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)20 CastExpression (org.eclipse.jdt.core.dom.CastExpression)19 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)19 SimpleName (org.eclipse.jdt.core.dom.SimpleName)19 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)18 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)18 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