Search in sources :

Example 1 with ArrayInitializer

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

the class QuickAssistProcessor method getArrayInitializerToArrayCreation.

private static boolean getArrayInitializerToArrayCreation(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    if (!(node instanceof ArrayInitializer)) {
        return false;
    }
    ArrayInitializer initializer = (ArrayInitializer) node;
    ASTNode parent = initializer.getParent();
    while (parent instanceof ArrayInitializer) {
        initializer = (ArrayInitializer) parent;
        parent = parent.getParent();
    }
    ITypeBinding typeBinding = initializer.resolveTypeBinding();
    if (!(parent instanceof VariableDeclaration) || typeBinding == null || !typeBinding.isArray()) {
        return false;
    }
    if (resultingCollections == null) {
        return true;
    }
    AST ast = node.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    String label = CorrectionMessages.QuickAssistProcessor_typetoarrayInitializer_description;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.ADD_TYPE_TO_ARRAY_INITIALIZER);
    ImportRewrite imports = proposal.createImportRewrite(context.getASTRoot());
    ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, imports);
    String typeName = imports.addImport(typeBinding, importRewriteContext);
    ArrayCreation creation = ast.newArrayCreation();
    creation.setInitializer((ArrayInitializer) rewrite.createMoveTarget(initializer));
    creation.setType((ArrayType) ASTNodeFactory.newType(ast, typeName));
    rewrite.replace(initializer, creation, null);
    resultingCollections.add(proposal);
    return true;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 2 with ArrayInitializer

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

the class MissingAnnotationAttributesProposal method newDefaultExpression.

private Expression newDefaultExpression(AST ast, ITypeBinding type, ImportRewriteContext context) {
    if (type.isPrimitive()) {
        String name = type.getName();
        if ("boolean".equals(name)) {
            //$NON-NLS-1$
            return ast.newBooleanLiteral(false);
        } else {
            //$NON-NLS-1$
            return ast.newNumberLiteral("0");
        }
    }
    if (type == ast.resolveWellKnownType("java.lang.String")) {
        //$NON-NLS-1$
        return ast.newStringLiteral();
    }
    if (type.isArray()) {
        ArrayInitializer initializer = ast.newArrayInitializer();
        initializer.expressions().add(newDefaultExpression(ast, type.getElementType(), context));
        return initializer;
    }
    if (type.isAnnotation()) {
        MarkerAnnotation annotation = ast.newMarkerAnnotation();
        annotation.setTypeName(ast.newName(getImportRewrite().addImport(type, context)));
        return annotation;
    }
    return ast.newNullLiteral();
}
Also used : MarkerAnnotation(org.eclipse.jdt.core.dom.MarkerAnnotation) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 3 with ArrayInitializer

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

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

the class PromoteTempToFieldRefactoring method getTempInitializerCopy.

private Expression getTempInitializerCopy(ASTRewrite rewrite) {
    final Expression initializer = (Expression) rewrite.createCopyTarget(getTempInitializer());
    if (initializer instanceof ArrayInitializer && ASTNodes.getDimensions(fTempDeclarationNode) > 0) {
        ArrayCreation arrayCreation = rewrite.getAST().newArrayCreation();
        arrayCreation.setType((ArrayType) ASTNodeFactory.newType(rewrite.getAST(), fTempDeclarationNode));
        arrayCreation.setInitializer((ArrayInitializer) initializer);
        return arrayCreation;
    }
    return initializer;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 5 with ArrayInitializer

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

the class ConvertAnonymousToNestedRefactoring method copyArguments.

private void copyArguments(CompilationUnitRewrite rewrite, ClassInstanceCreation newClassCreation) {
    Iterator<Expression> iter = ((ClassInstanceCreation) fAnonymousInnerClassNode.getParent()).arguments().iterator();
    if (!iter.hasNext())
        return;
    IMethodBinding superConstructorBinding = getSuperConstructorBinding();
    ITypeBinding[] parameterTypes = superConstructorBinding.getParameterTypes();
    List<Expression> arguments = newClassCreation.arguments();
    ASTRewrite astRewrite = rewrite.getASTRewrite();
    int last = parameterTypes.length - 1;
    for (int i = 0; i < last; i++) {
        arguments.add((Expression) astRewrite.createCopyTarget(iter.next()));
    }
    if (superConstructorBinding.isVarargs()) {
        AST ast = astRewrite.getAST();
        ArrayCreation arrayCreation = ast.newArrayCreation();
        arrayCreation.setType((ArrayType) rewrite.getImportRewrite().addImport(parameterTypes[last], ast));
        ArrayInitializer initializer = ast.newArrayInitializer();
        arrayCreation.setInitializer(initializer);
        arguments.add(arrayCreation);
        arguments = initializer.expressions();
    }
    while (iter.hasNext()) {
        arguments.add((Expression) astRewrite.createCopyTarget(iter.next()));
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) AST(org.eclipse.jdt.core.dom.AST) Expression(org.eclipse.jdt.core.dom.Expression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Aggregations

ArrayInitializer (org.eclipse.jdt.core.dom.ArrayInitializer)27 Expression (org.eclipse.jdt.core.dom.Expression)12 ASTNode (org.eclipse.jdt.core.dom.ASTNode)11 ArrayCreation (org.eclipse.jdt.core.dom.ArrayCreation)10 AST (org.eclipse.jdt.core.dom.AST)9 CastExpression (org.eclipse.jdt.core.dom.CastExpression)9 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)8 Assignment (org.eclipse.jdt.core.dom.Assignment)7 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)7 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)7 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)7 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)6 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)6 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)6 Type (org.eclipse.jdt.core.dom.Type)6 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)5 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)5 List (java.util.List)4 ArrayType (org.eclipse.jdt.core.dom.ArrayType)4 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)4