Search in sources :

Example 11 with ClassInstanceCreation

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

the class ConvertAnonymousToNestedRefactoring method getSuperConstructorBinding.

private IMethodBinding getSuperConstructorBinding() {
    //workaround for missing java core functionality - finding a
    // super constructor for an anonymous class creation
    IMethodBinding anonConstr = ((ClassInstanceCreation) fAnonymousInnerClassNode.getParent()).resolveConstructorBinding();
    if (anonConstr == null)
        return null;
    ITypeBinding superClass = anonConstr.getDeclaringClass().getSuperclass();
    IMethodBinding[] superMethods = superClass.getDeclaredMethods();
    for (int i = 0; i < superMethods.length; i++) {
        IMethodBinding superMethod = superMethods[i];
        if (superMethod.isConstructor() && parameterTypesMatch(superMethod, anonConstr))
            return superMethod;
    }
    //there's no way - it must be there
    Assert.isTrue(false);
    return null;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 12 with ClassInstanceCreation

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

the class ConvertAnonymousToNestedRefactoring method createNewConstructor.

private MethodDeclaration createNewConstructor(CompilationUnitRewrite rewrite, IVariableBinding[] bindings, String[] fieldNames) throws JavaModelException {
    ClassInstanceCreation instanceCreation = (ClassInstanceCreation) fAnonymousInnerClassNode.getParent();
    if (instanceCreation.arguments().isEmpty() && bindings.length == 0)
        return null;
    IJavaProject project = fCu.getJavaProject();
    AST ast = rewrite.getAST();
    ImportRewrite importRewrite = rewrite.getImportRewrite();
    ASTRewrite astRewrite = rewrite.getASTRewrite();
    MethodDeclaration newConstructor = ast.newMethodDeclaration();
    newConstructor.setConstructor(true);
    newConstructor.setJavadoc(null);
    newConstructor.modifiers().addAll(ASTNodeFactory.newModifiers(ast, fVisibility));
    newConstructor.setName(ast.newSimpleName(fClassName));
    addLinkedPosition(KEY_TYPE_NAME, newConstructor.getName(), astRewrite, false);
    newConstructor.setBody(ast.newBlock());
    List<Statement> newStatements = newConstructor.getBody().statements();
    List<SingleVariableDeclaration> newParameters = newConstructor.parameters();
    List<String> newParameterNames = new ArrayList<String>();
    // add parameters for elements passed with the instance creation
    if (!instanceCreation.arguments().isEmpty()) {
        IMethodBinding constructorBinding = getSuperConstructorBinding();
        if (constructorBinding != null) {
            SuperConstructorInvocation superConstructorInvocation = ast.newSuperConstructorInvocation();
            ITypeBinding[] parameterTypes = constructorBinding.getParameterTypes();
            String[][] parameterNames = StubUtility.suggestArgumentNamesWithProposals(project, constructorBinding);
            for (int i = 0; i < parameterNames.length; i++) {
                String[] nameProposals = parameterNames[i];
                String paramName = nameProposals[0];
                SingleVariableDeclaration param = newParameterDeclaration(ast, importRewrite, paramName, parameterTypes[i]);
                newParameters.add(param);
                newParameterNames.add(paramName);
                SimpleName newSIArgument = ast.newSimpleName(paramName);
                superConstructorInvocation.arguments().add(newSIArgument);
                if (fLinkedProposalModel != null) {
                    LinkedProposalPositionGroup positionGroup = fLinkedProposalModel.getPositionGroup(KEY_PARAM_NAME_CONST + String.valueOf(i), true);
                    positionGroup.addPosition(astRewrite.track(param.getName()), false);
                    positionGroup.addPosition(astRewrite.track(newSIArgument), false);
                    for (int k = 0; k < nameProposals.length; k++) {
                        positionGroup.addProposal(nameProposals[k], null, nameProposals.length - k);
                    }
                }
            }
            newStatements.add(superConstructorInvocation);
        }
    }
    // add parameters for all outer variables used
    boolean useThisAccess = useThisForFieldAccess();
    for (int i = 0; i < bindings.length; i++) {
        String baseName = StubUtility.getBaseName(bindings[i], project);
        String[] paramNameProposals = StubUtility.getVariableNameSuggestions(NamingConventions.VK_PARAMETER, project, baseName, 0, newParameterNames, true);
        String paramName = paramNameProposals[0];
        SingleVariableDeclaration param = newParameterDeclaration(ast, importRewrite, paramName, bindings[i].getType());
        newParameters.add(param);
        newParameterNames.add(paramName);
        String fieldName = fieldNames[i];
        SimpleName fieldNameNode = ast.newSimpleName(fieldName);
        SimpleName paramNameNode = ast.newSimpleName(paramName);
        newStatements.add(newFieldAssignment(ast, fieldNameNode, paramNameNode, useThisAccess || newParameterNames.contains(fieldName)));
        if (fLinkedProposalModel != null) {
            LinkedProposalPositionGroup positionGroup = fLinkedProposalModel.getPositionGroup(KEY_PARAM_NAME_EXT + String.valueOf(i), true);
            positionGroup.addPosition(astRewrite.track(param.getName()), false);
            positionGroup.addPosition(astRewrite.track(paramNameNode), false);
            for (int k = 0; k < paramNameProposals.length; k++) {
                positionGroup.addProposal(paramNameProposals[k], null, paramNameProposals.length - k);
            }
            fLinkedProposalModel.getPositionGroup(KEY_FIELD_NAME_EXT + i, true).addPosition(astRewrite.track(fieldNameNode), false);
        }
    }
    addExceptionsToNewConstructor(newConstructor, importRewrite);
    if (doAddComments()) {
        try {
            String[] allParamNames = newParameterNames.toArray(new String[newParameterNames.size()]);
            String string = CodeGeneration.getMethodComment(fCu, fClassName, fClassName, allParamNames, new String[0], null, new String[0], null, StubUtility.getLineDelimiterUsed(fCu));
            if (string != null) {
                Javadoc javadoc = (Javadoc) astRewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
                newConstructor.setJavadoc(javadoc);
            }
        } catch (CoreException exception) {
            throw new JavaModelException(exception);
        }
    }
    return newConstructor;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) JavaModelException(org.eclipse.jdt.core.JavaModelException) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Statement(org.eclipse.jdt.core.dom.Statement) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) IJavaProject(org.eclipse.jdt.core.IJavaProject) CoreException(org.eclipse.core.runtime.CoreException) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) LinkedProposalPositionGroup(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup)

Example 13 with ClassInstanceCreation

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

the class ConvertAnonymousToNestedRefactoring method getTypeParameters.

private ITypeBinding[] getTypeParameters() {
    final List<ITypeBinding> parameters = new ArrayList<ITypeBinding>(4);
    final ClassInstanceCreation creation = (ClassInstanceCreation) fAnonymousInnerClassNode.getParent();
    if (fDeclareStatic) {
        final TypeVariableFinder finder = new TypeVariableFinder();
        creation.accept(finder);
        return finder.getResult();
    } else {
        final MethodDeclaration declaration = getEnclosingMethodDeclaration(creation);
        if (declaration != null) {
            ITypeBinding binding = null;
            TypeParameter parameter = null;
            for (final Iterator<TypeParameter> iterator = declaration.typeParameters().iterator(); iterator.hasNext(); ) {
                parameter = iterator.next();
                binding = parameter.resolveBinding();
                if (binding != null)
                    parameters.add(binding);
            }
        }
    }
    final TypeVariableFinder finder = new TypeVariableFinder();
    creation.accept(finder);
    final ITypeBinding[] variables = finder.getResult();
    final List<ITypeBinding> remove = new ArrayList<ITypeBinding>(4);
    boolean match = false;
    ITypeBinding binding = null;
    ITypeBinding variable = null;
    for (final Iterator<ITypeBinding> iterator = parameters.iterator(); iterator.hasNext(); ) {
        match = false;
        binding = iterator.next();
        for (int index = 0; index < variables.length; index++) {
            variable = variables[index];
            if (variable.equals(binding))
                match = true;
        }
        if (!match)
            remove.add(binding);
    }
    parameters.removeAll(remove);
    final ITypeBinding[] result = new ITypeBinding[parameters.size()];
    parameters.toArray(result);
    return result;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ArrayList(java.util.ArrayList)

Example 14 with ClassInstanceCreation

use of org.eclipse.jdt.core.dom.ClassInstanceCreation in project flux by eclipse.

the class ASTNodes method getTargetType.

/**
	 * Derives the target type defined at the location of the given expression if the target context
	 * supports poly expressions.
	 * 
	 * @param expression the expression at whose location the target type is required
	 * @return the type binding of the target type defined at the location of the given expression
	 *         if the target context supports poly expressions, or <code>null</code> if the target
	 *         type could not be derived
	 * 
	 * @since 3.10
	 */
public static ITypeBinding getTargetType(Expression expression) {
    ASTNode parent = expression.getParent();
    StructuralPropertyDescriptor locationInParent = expression.getLocationInParent();
    if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY || locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY) {
        return ((VariableDeclaration) parent).getName().resolveTypeBinding();
    } else if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY) {
        return ((Assignment) parent).getLeftHandSide().resolveTypeBinding();
    } else if (locationInParent == ReturnStatement.EXPRESSION_PROPERTY) {
        return getTargetTypeForReturnStmt((ReturnStatement) parent);
    } else if (locationInParent == ArrayInitializer.EXPRESSIONS_PROPERTY) {
        return getTargetTypeForArrayInitializer((ArrayInitializer) parent);
    } else if (locationInParent == MethodInvocation.ARGUMENTS_PROPERTY) {
        MethodInvocation methodInvocation = (MethodInvocation) parent;
        IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
        if (methodBinding != null) {
            return getParameterTypeBinding(expression, methodInvocation.arguments(), methodBinding);
        }
    } else if (locationInParent == SuperMethodInvocation.ARGUMENTS_PROPERTY) {
        SuperMethodInvocation superMethodInvocation = (SuperMethodInvocation) parent;
        IMethodBinding superMethodBinding = superMethodInvocation.resolveMethodBinding();
        if (superMethodBinding != null) {
            return getParameterTypeBinding(expression, superMethodInvocation.arguments(), superMethodBinding);
        }
    } else if (locationInParent == ConstructorInvocation.ARGUMENTS_PROPERTY) {
        ConstructorInvocation constructorInvocation = (ConstructorInvocation) parent;
        IMethodBinding constructorBinding = constructorInvocation.resolveConstructorBinding();
        if (constructorBinding != null) {
            return getParameterTypeBinding(expression, constructorInvocation.arguments(), constructorBinding);
        }
    } else if (locationInParent == SuperConstructorInvocation.ARGUMENTS_PROPERTY) {
        SuperConstructorInvocation superConstructorInvocation = (SuperConstructorInvocation) parent;
        IMethodBinding superConstructorBinding = superConstructorInvocation.resolveConstructorBinding();
        if (superConstructorBinding != null) {
            return getParameterTypeBinding(expression, superConstructorInvocation.arguments(), superConstructorBinding);
        }
    } else if (locationInParent == ClassInstanceCreation.ARGUMENTS_PROPERTY) {
        ClassInstanceCreation creation = (ClassInstanceCreation) parent;
        IMethodBinding creationBinding = creation.resolveConstructorBinding();
        if (creationBinding != null) {
            return getParameterTypeBinding(expression, creation.arguments(), creationBinding);
        }
    } else if (locationInParent == EnumConstantDeclaration.ARGUMENTS_PROPERTY) {
        EnumConstantDeclaration enumConstantDecl = (EnumConstantDeclaration) parent;
        IMethodBinding enumConstructorBinding = enumConstantDecl.resolveConstructorBinding();
        if (enumConstructorBinding != null) {
            return getParameterTypeBinding(expression, enumConstantDecl.arguments(), enumConstructorBinding);
        }
    } else if (locationInParent == LambdaExpression.BODY_PROPERTY) {
        IMethodBinding methodBinding = ((LambdaExpression) parent).resolveMethodBinding();
        if (methodBinding != null) {
            return methodBinding.getReturnType();
        }
    } else if (locationInParent == ConditionalExpression.THEN_EXPRESSION_PROPERTY || locationInParent == ConditionalExpression.ELSE_EXPRESSION_PROPERTY) {
        return getTargetType((ConditionalExpression) parent);
    } else if (locationInParent == CastExpression.EXPRESSION_PROPERTY) {
        return ((CastExpression) parent).getType().resolveBinding();
    } else if (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
        return getTargetType((ParenthesizedExpression) parent);
    }
    return null;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) Assignment(org.eclipse.jdt.core.dom.Assignment) EnumConstantDeclaration(org.eclipse.jdt.core.dom.EnumConstantDeclaration) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 15 with ClassInstanceCreation

use of org.eclipse.jdt.core.dom.ClassInstanceCreation in project flux by eclipse.

the class QuickAssistProcessor method getInferDiamondArgumentsProposal.

public static boolean getInferDiamondArgumentsProposal(IInvocationContext context, ASTNode node, IProblemLocation[] locations, Collection<ICommandAccess> resultingCollections) {
    // don't add if already added as quick fix
    if (containsMatchingProblem(locations, IProblem.DiamondNotBelow17))
        return false;
    ParameterizedType createdType = null;
    if (node instanceof Name) {
        Name name = ASTNodes.getTopMostName((Name) node);
        if (name.getLocationInParent() == SimpleType.NAME_PROPERTY || name.getLocationInParent() == NameQualifiedType.NAME_PROPERTY) {
            ASTNode type = name.getParent();
            if (type.getLocationInParent() == ParameterizedType.TYPE_PROPERTY) {
                createdType = (ParameterizedType) type.getParent();
                if (createdType.getLocationInParent() != ClassInstanceCreation.TYPE_PROPERTY) {
                    return false;
                }
            }
        }
    } else if (node instanceof ParameterizedType) {
        createdType = (ParameterizedType) node;
        if (createdType.getLocationInParent() != ClassInstanceCreation.TYPE_PROPERTY) {
            return false;
        }
    } else if (node instanceof ClassInstanceCreation) {
        ClassInstanceCreation creation = (ClassInstanceCreation) node;
        Type type = creation.getType();
        if (type instanceof ParameterizedType) {
            createdType = (ParameterizedType) type;
        }
    }
    //		}
    return true;
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) IType(org.eclipse.jdt.core.IType) UnionType(org.eclipse.jdt.core.dom.UnionType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name)

Aggregations

ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)32 ASTNode (org.eclipse.jdt.core.dom.ASTNode)18 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)16 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)12 SimpleName (org.eclipse.jdt.core.dom.SimpleName)12 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)9 Type (org.eclipse.jdt.core.dom.Type)9 Expression (org.eclipse.jdt.core.dom.Expression)8 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)8 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)8 ArrayList (java.util.ArrayList)6 IType (org.eclipse.jdt.core.IType)6 SuperConstructorInvocation (org.eclipse.jdt.core.dom.SuperConstructorInvocation)6 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 AST (org.eclipse.jdt.core.dom.AST)5 ArrayType (org.eclipse.jdt.core.dom.ArrayType)5 CastExpression (org.eclipse.jdt.core.dom.CastExpression)5 SimpleType (org.eclipse.jdt.core.dom.SimpleType)5 VariableDeclaration (org.eclipse.jdt.core.dom.VariableDeclaration)5 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)4