Search in sources :

Example 1 with Dimension

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

the class ASTNodeFactory method newCreationType.

public static Type newCreationType(AST ast, ITypeBinding typeBinding, ImportRewrite importRewrite, ImportRewriteContext importContext) {
    if (typeBinding.isParameterizedType()) {
        Type baseType = newCreationType(ast, typeBinding.getTypeDeclaration(), importRewrite, importContext);
        ParameterizedType parameterizedType = ast.newParameterizedType(baseType);
        for (ITypeBinding typeArgument : typeBinding.getTypeArguments()) {
            parameterizedType.typeArguments().add(newCreationType(ast, typeArgument, importRewrite, importContext));
        }
        return parameterizedType;
    } else if (typeBinding.isParameterizedType()) {
        Type elementType = newCreationType(ast, typeBinding.getElementType(), importRewrite, importContext);
        ArrayType arrayType = ast.newArrayType(elementType, 0);
        while (typeBinding.isArray()) {
            Dimension dimension = ast.newDimension();
            IAnnotationBinding[] typeAnnotations = typeBinding.getTypeAnnotations();
            for (IAnnotationBinding typeAnnotation : typeAnnotations) {
                dimension.annotations().add(importRewrite.addAnnotation(typeAnnotation, ast, importContext));
            }
            arrayType.dimensions().add(dimension);
            typeBinding = typeBinding.getComponentType();
        }
        return arrayType;
    } else if (typeBinding.isWildcardType()) {
        ITypeBinding bound = typeBinding.getBound();
        typeBinding = (bound != null) ? bound : typeBinding.getErasure();
        return newCreationType(ast, typeBinding, importRewrite, importContext);
    } else {
        return importRewrite.addImport(typeBinding, ast, importContext);
    }
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) UnionType(org.eclipse.jdt.core.dom.UnionType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) IAnnotationBinding(org.eclipse.jdt.core.dom.IAnnotationBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) Dimension(org.eclipse.jdt.core.dom.Dimension)

Example 2 with Dimension

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

the class ASTNodeFactory method newType.

/**
	 * Returns the new type node corresponding to the type of the given declaration
	 * including the extra dimensions. If the type is a {@link UnionType}, use the LUB type.
	 * If the <code>importRewrite</code> is <code>null</code>, the type may be fully-qualified.
	 * 
	 * @param ast The AST to create the resulting type with.
	 * @param declaration The variable declaration to get the type from
	 * @param importRewrite the import rewrite to use, or <code>null</code>
	 * @param context the import rewrite context, or <code>null</code>
	 * @return a new type node created with the given AST.
	 * 
	 * @since 3.7.1
	 */
public static Type newType(AST ast, VariableDeclaration declaration, ImportRewrite importRewrite, ImportRewriteContext context) {
    if (declaration instanceof VariableDeclarationFragment && declaration.getParent() instanceof LambdaExpression) {
        return newType((LambdaExpression) declaration.getParent(), (VariableDeclarationFragment) declaration, ast, importRewrite, context);
    }
    Type type = ASTNodes.getType(declaration);
    if (declaration instanceof SingleVariableDeclaration) {
        Type type2 = ((SingleVariableDeclaration) declaration).getType();
        if (type2 instanceof UnionType) {
            ITypeBinding typeBinding = type2.resolveBinding();
            if (typeBinding != null) {
                if (importRewrite != null) {
                    type = importRewrite.addImport(typeBinding, ast, context);
                    return type;
                } else {
                    String qualifiedName = typeBinding.getQualifiedName();
                    if (qualifiedName.length() > 0) {
                        type = ast.newSimpleType(ast.newName(qualifiedName));
                        return type;
                    }
                }
            }
            // XXX: fallback for intersection types or unresolved types: take first type of union
            type = (Type) ((UnionType) type2).types().get(0);
            return type;
        }
    }
    type = (Type) ASTNode.copySubtree(ast, type);
    List<Dimension> extraDimensions = declaration.extraDimensions();
    if (!extraDimensions.isEmpty()) {
        ArrayType arrayType;
        if (type instanceof ArrayType) {
            arrayType = (ArrayType) type;
        } else {
            arrayType = ast.newArrayType(type, 0);
            type = arrayType;
        }
        arrayType.dimensions().addAll(ASTNode.copySubtrees(ast, extraDimensions));
    }
    return type;
}
Also used : ArrayType(org.eclipse.jdt.core.dom.ArrayType) UnionType(org.eclipse.jdt.core.dom.UnionType) Type(org.eclipse.jdt.core.dom.Type) UnionType(org.eclipse.jdt.core.dom.UnionType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) Dimension(org.eclipse.jdt.core.dom.Dimension) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 3 with Dimension

use of org.eclipse.jdt.core.dom.Dimension in project generator by mybatis.

the class TypeStringifier method visit.

@Override
@SuppressWarnings("unchecked")
public boolean visit(ArrayType node) {
    node.getElementType().accept(this);
    List<Dimension> dimensions = node.dimensions();
    for (Dimension dimension : dimensions) {
        dimension.accept(this);
    }
    return false;
}
Also used : Dimension(org.eclipse.jdt.core.dom.Dimension)

Example 4 with Dimension

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

the class SelfEncapsulateFieldRefactoring method createSetterMethod.

private MethodDeclaration createSetterMethod(AST ast, ASTRewrite rewriter, String lineDelimiter) throws CoreException {
    FieldDeclaration field = (FieldDeclaration) ASTNodes.getParent(fFieldDeclaration, FieldDeclaration.class);
    Type type = field.getType();
    MethodDeclaration result = ast.newMethodDeclaration();
    result.setName(ast.newSimpleName(fSetterName));
    result.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiers()));
    if (fSetterMustReturnValue) {
        result.setReturnType2((Type) rewriter.createCopyTarget(type));
    }
    SingleVariableDeclaration param = ast.newSingleVariableDeclaration();
    result.parameters().add(param);
    param.setName(ast.newSimpleName(fArgName));
    param.setType((Type) rewriter.createCopyTarget(type));
    List<Dimension> extraDimensions = DimensionRewrite.copyDimensions(fFieldDeclaration.extraDimensions(), rewriter);
    param.extraDimensions().addAll(extraDimensions);
    Block block = ast.newBlock();
    result.setBody(block);
    String fieldAccess = createFieldAccess();
    String body = CodeGeneration.getSetterMethodBodyContent(fField.getCompilationUnit(), getTypeName(field.getParent()), fSetterName, fieldAccess, fArgName, lineDelimiter);
    if (body != null) {
        ASTNode setterNode = rewriter.createStringPlaceholder(body, ASTNode.BLOCK);
        block.statements().add(setterNode);
    } else {
        Assignment ass = ast.newAssignment();
        ass.setLeftHandSide((Expression) rewriter.createStringPlaceholder(fieldAccess, ASTNode.QUALIFIED_NAME));
        ass.setRightHandSide(ast.newSimpleName(fArgName));
        block.statements().add(ass);
    }
    if (fSetterMustReturnValue) {
        ReturnStatement rs = ast.newReturnStatement();
        rs.setExpression(ast.newSimpleName(fArgName));
        block.statements().add(rs);
    }
    if (fGenerateJavadoc) {
        String string = CodeGeneration.getSetterComment(fField.getCompilationUnit(), getTypeName(field.getParent()), fSetterName, fField.getElementName(), ASTNodes.asString(type), fArgName, StubUtility.getBaseName(fField), lineDelimiter);
        if (string != null) {
            Javadoc javadoc = (Javadoc) fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC);
            result.setJavadoc(javadoc);
        }
    }
    return result;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) IType(org.eclipse.jdt.core.IType) Type(org.eclipse.jdt.core.dom.Type) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) Javadoc(org.eclipse.jdt.core.dom.Javadoc) Dimension(org.eclipse.jdt.core.dom.Dimension) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 5 with Dimension

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

the class PromoteTempToFieldRefactoring method createNewFieldDeclaration.

private FieldDeclaration createNewFieldDeclaration(ASTRewrite rewrite) {
    AST ast = getAST();
    VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
    SimpleName variableName = ast.newSimpleName(fFieldName);
    fragment.setName(variableName);
    addLinkedName(rewrite, variableName, false);
    List<Dimension> extraDimensions = DimensionRewrite.copyDimensions(fTempDeclarationNode.extraDimensions(), rewrite);
    fragment.extraDimensions().addAll(extraDimensions);
    if (fInitializeIn == INITIALIZE_IN_FIELD && tempHasInitializer()) {
        Expression initializer = (Expression) rewrite.createCopyTarget(getTempInitializer());
        fragment.setInitializer(initializer);
    }
    FieldDeclaration fieldDeclaration = ast.newFieldDeclaration(fragment);
    VariableDeclarationStatement vds = getTempDeclarationStatement();
    Type type = (Type) rewrite.createCopyTarget(vds.getType());
    fieldDeclaration.setType(type);
    fieldDeclaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getModifiers()));
    return fieldDeclaration;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) SimpleName(org.eclipse.jdt.core.dom.SimpleName) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Dimension(org.eclipse.jdt.core.dom.Dimension) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Aggregations

Dimension (org.eclipse.jdt.core.dom.Dimension)10 ArrayType (org.eclipse.jdt.core.dom.ArrayType)8 Type (org.eclipse.jdt.core.dom.Type)7 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)5 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)5 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)4 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)4 UnionType (org.eclipse.jdt.core.dom.UnionType)4 IAnnotationBinding (org.eclipse.jdt.core.dom.IAnnotationBinding)3 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)3 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)2 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 IType (org.eclipse.jdt.core.IType)1 AST (org.eclipse.jdt.core.dom.AST)1 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 Annotation (org.eclipse.jdt.core.dom.Annotation)1 Assignment (org.eclipse.jdt.core.dom.Assignment)1 Block (org.eclipse.jdt.core.dom.Block)1