Search in sources :

Example 16 with ParameterizedType

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

the class ConvertAnonymousToNestedRefactoring method createNewClassInstanceCreation.

private ASTNode createNewClassInstanceCreation(CompilationUnitRewrite rewrite, ITypeBinding[] parameters) {
    AST ast = fAnonymousInnerClassNode.getAST();
    ClassInstanceCreation newClassCreation = ast.newClassInstanceCreation();
    newClassCreation.setAnonymousClassDeclaration(null);
    Type type = null;
    SimpleName newNameNode = ast.newSimpleName(fClassName);
    if (parameters.length > 0) {
        final ParameterizedType parameterized = ast.newParameterizedType(ast.newSimpleType(newNameNode));
        for (int index = 0; index < parameters.length; index++) parameterized.typeArguments().add(ast.newSimpleType(ast.newSimpleName(parameters[index].getName())));
        type = parameterized;
    } else
        type = ast.newSimpleType(newNameNode);
    newClassCreation.setType(type);
    copyArguments(rewrite, newClassCreation);
    addArgumentsForLocalsUsedInInnerClass(newClassCreation);
    addLinkedPosition(KEY_TYPE_NAME, newNameNode, rewrite.getASTRewrite(), true);
    return newClassCreation;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) AST(org.eclipse.jdt.core.dom.AST) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) Type(org.eclipse.jdt.core.dom.Type) SimpleName(org.eclipse.jdt.core.dom.SimpleName)

Example 17 with ParameterizedType

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

the class ExtractToNullCheckedLocalProposal method newType.

/**
	 * Create a fresh type reference
	 * @param typeBinding the type we want to refer to
	 * @param ast AST for creating new nodes
	 * @param imports use this for optimal type names
	 * @return a fully features non-null type reference (can be parameterized and/or array).
	 */
public static Type newType(ITypeBinding typeBinding, AST ast, ImportRewrite imports) {
    // unwrap array type:
    int dimensions = typeBinding.getDimensions();
    if (dimensions > 0)
        typeBinding = typeBinding.getElementType();
    // unwrap parameterized type:
    ITypeBinding[] typeArguments = typeBinding.getTypeArguments();
    typeBinding = typeBinding.getErasure();
    // create leaf type:
    Type elementType = (typeBinding.isPrimitive()) ? ast.newPrimitiveType(PrimitiveType.toCode(typeBinding.getName())) : ast.newSimpleType(ast.newName(imports.addImport(typeBinding)));
    // re-wrap as parameterized type:
    if (typeArguments.length > 0) {
        ParameterizedType parameterizedType = ast.newParameterizedType(elementType);
        for (ITypeBinding typeArgument : typeArguments) parameterizedType.typeArguments().add(newType(typeArgument, ast, imports));
        elementType = parameterizedType;
    }
    // re-wrap as array type:
    if (dimensions > 0)
        return ast.newArrayType(elementType, dimensions);
    else
        return elementType;
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 18 with ParameterizedType

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

the class AbstractSerialVersionOperation method rewriteAST.

/**
	 * {@inheritDoc}
	 */
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel positionGroups) throws CoreException {
    final ASTRewrite rewrite = cuRewrite.getASTRewrite();
    VariableDeclarationFragment fragment = null;
    for (int i = 0; i < fNodes.length; i++) {
        final ASTNode node = fNodes[i];
        final AST ast = node.getAST();
        fragment = ast.newVariableDeclarationFragment();
        fragment.setName(ast.newSimpleName(NAME_FIELD));
        final FieldDeclaration declaration = ast.newFieldDeclaration(fragment);
        declaration.setType(ast.newPrimitiveType(PrimitiveType.LONG));
        declaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL));
        if (!addInitializer(fragment, node))
            continue;
        if (fragment.getInitializer() != null) {
            final TextEditGroup editGroup = createTextEditGroup(FixMessages.SerialVersion_group_description, cuRewrite);
            if (node instanceof AbstractTypeDeclaration)
                rewrite.getListRewrite(node, ((AbstractTypeDeclaration) node).getBodyDeclarationsProperty()).insertAt(declaration, 0, editGroup);
            else if (node instanceof AnonymousClassDeclaration)
                rewrite.getListRewrite(node, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(declaration, 0, editGroup);
            else if (node instanceof ParameterizedType) {
                final ParameterizedType type = (ParameterizedType) node;
                final ASTNode parent = type.getParent();
                if (parent instanceof ClassInstanceCreation) {
                    final ClassInstanceCreation creation = (ClassInstanceCreation) parent;
                    final AnonymousClassDeclaration anonymous = creation.getAnonymousClassDeclaration();
                    if (anonymous != null)
                        rewrite.getListRewrite(anonymous, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(declaration, 0, editGroup);
                }
            } else
                Assert.isTrue(false);
            addLinkedPositions(rewrite, fragment, positionGroups);
        }
        final String comment = CodeGeneration.getFieldComment(fUnit, declaration.getType().toString(), NAME_FIELD, StubUtility.getLineDelimiterUsed(fUnit));
        if (comment != null && comment.length() > 0) {
            final Javadoc doc = (Javadoc) rewrite.createStringPlaceholder(comment, ASTNode.JAVADOC);
            declaration.setJavadoc(doc);
        }
    }
    if (fragment == null)
        return;
    positionGroups.setEndPosition(rewrite.track(fragment));
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) AST(org.eclipse.jdt.core.dom.AST) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) Javadoc(org.eclipse.jdt.core.dom.Javadoc) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 19 with ParameterizedType

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

the class Java50Fix method createRawTypeReferenceOperations.

private static SimpleType createRawTypeReferenceOperations(CompilationUnit compilationUnit, IProblemLocation[] locations, List<CompilationUnitRewriteOperation> operations) {
    if (hasFatalError(compilationUnit))
        return null;
    List<SimpleType> result = new ArrayList<SimpleType>();
    for (int i = 0; i < locations.length; i++) {
        IProblemLocation problem = locations[i];
        if (isRawTypeReferenceProblem(problem.getProblemId())) {
            ASTNode node = problem.getCoveredNode(compilationUnit);
            if (node instanceof ClassInstanceCreation) {
                Type rawReference = (Type) node.getStructuralProperty(ClassInstanceCreation.TYPE_PROPERTY);
                if (isRawTypeReference(rawReference)) {
                    result.add((SimpleType) rawReference);
                }
            } else if (node instanceof SimpleName) {
                ASTNode rawReference = node.getParent();
                if (isRawTypeReference(rawReference)) {
                    ASTNode parent = rawReference.getParent();
                    if (!(parent instanceof ArrayType || parent instanceof ParameterizedType))
                        result.add((SimpleType) rawReference);
                }
            } else if (node instanceof MethodInvocation) {
                MethodInvocation invocation = (MethodInvocation) node;
                SimpleType rawReference = getRawReference(invocation, compilationUnit);
                if (rawReference != null) {
                    result.add(rawReference);
                }
            }
        }
    }
    if (result.size() == 0)
        return null;
    SimpleType[] types = result.toArray(new SimpleType[result.size()]);
    operations.add(new AddTypeParametersOperation(types));
    return types[0];
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) 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) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 20 with ParameterizedType

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

the class PotentialProgrammingProblemsFix method getSelectedName.

private static SimpleName getSelectedName(CompilationUnit compilationUnit, IProblemLocation problem) {
    final ASTNode selection = problem.getCoveredNode(compilationUnit);
    if (selection == null)
        return null;
    Name name = null;
    if (selection instanceof SimpleType) {
        name = ((SimpleType) selection).getName();
    } else if (selection instanceof NameQualifiedType) {
        name = ((NameQualifiedType) selection).getName();
    } else if (selection instanceof QualifiedType) {
        name = ((QualifiedType) selection).getName();
    } else if (selection instanceof ParameterizedType) {
        final ParameterizedType type = (ParameterizedType) selection;
        final Type raw = type.getType();
        if (raw instanceof SimpleType)
            name = ((SimpleType) raw).getName();
        else if (raw instanceof NameQualifiedType)
            name = ((NameQualifiedType) raw).getName();
        else if (raw instanceof QualifiedType)
            name = ((QualifiedType) raw).getName();
    } else if (selection instanceof Name) {
        name = (Name) selection;
    }
    if (name == null)
        return null;
    if (name.isSimpleName()) {
        return (SimpleName) name;
    } else {
        return ((QualifiedName) name).getName();
    }
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) IType(org.eclipse.jdt.core.IType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName)

Aggregations

ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)37 Type (org.eclipse.jdt.core.dom.Type)23 SimpleType (org.eclipse.jdt.core.dom.SimpleType)15 ArrayType (org.eclipse.jdt.core.dom.ArrayType)14 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)12 IType (org.eclipse.jdt.core.IType)9 ASTNode (org.eclipse.jdt.core.dom.ASTNode)9 WildcardType (java.lang.reflect.WildcardType)8 org.eclipse.jdt.core.dom (org.eclipse.jdt.core.dom)8 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)7 QualifiedType (org.eclipse.jdt.core.dom.QualifiedType)7 SimpleName (org.eclipse.jdt.core.dom.SimpleName)6 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)5 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)5 Name (org.eclipse.jdt.core.dom.Name)5 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)4 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)4 UnionType (org.eclipse.jdt.core.dom.UnionType)4 WildcardType (org.eclipse.jdt.core.dom.WildcardType)4