Search in sources :

Example 1 with SimpleType

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

the class UnresolvedElementsSubProcessor method addSimilarTypeProposals.

private static void addSimilarTypeProposals(int kind, ICompilationUnit cu, Name node, int relevance, Collection<ICommandAccess> proposals) throws CoreException {
    SimilarElement[] elements = SimilarElementsRequestor.findSimilarElement(cu, node, kind);
    // try to resolve type in context -> highest severity
    String resolvedTypeName = null;
    ITypeBinding binding = ASTResolving.guessBindingForTypeReference(node);
    if (binding != null) {
        ITypeBinding simpleBinding = binding;
        if (simpleBinding.isArray()) {
            simpleBinding = simpleBinding.getElementType();
        }
        simpleBinding = simpleBinding.getTypeDeclaration();
        if (!simpleBinding.isRecovered()) {
            resolvedTypeName = simpleBinding.getQualifiedName();
            CUCorrectionProposal proposal = createTypeRefChangeProposal(cu, resolvedTypeName, node, relevance + 2, elements.length);
            proposals.add(proposal);
            if (proposal instanceof AddImportCorrectionProposal)
                proposal.setRelevance(relevance + elements.length + 2);
            if (binding.isParameterizedType() && (node.getParent() instanceof SimpleType || node.getParent() instanceof NameQualifiedType) && !(node.getParent().getParent() instanceof Type)) {
                proposals.add(createTypeRefChangeFullProposal(cu, binding, node, relevance + 5));
            }
        }
    } else {
        ASTNode normalizedNode = ASTNodes.getNormalizedNode(node);
        if (!(normalizedNode.getParent() instanceof Type) && node.getParent() != normalizedNode) {
            ITypeBinding normBinding = ASTResolving.guessBindingForTypeReference(normalizedNode);
            if (normBinding != null && !normBinding.isRecovered()) {
                proposals.add(createTypeRefChangeFullProposal(cu, normBinding, normalizedNode, relevance + 5));
            }
        }
    }
    // add all similar elements
    for (int i = 0; i < elements.length; i++) {
        SimilarElement elem = elements[i];
        if ((elem.getKind() & SimilarElementsRequestor.ALL_TYPES) != 0) {
            String fullName = elem.getName();
            if (!fullName.equals(resolvedTypeName)) {
                proposals.add(createTypeRefChangeProposal(cu, fullName, node, relevance, elements.length));
            }
        }
    }
}
Also used : AddImportCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.AddImportCorrectionProposal) SimpleType(org.eclipse.jdt.core.dom.SimpleType) IType(org.eclipse.jdt.core.IType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType)

Example 2 with SimpleType

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

the class Java50Fix method getRawReference.

private static SimpleType getRawReference(SimpleName name, CompilationUnit compilationUnit) {
    SimpleName[] names = LinkedNodeFinder.findByNode(compilationUnit, name);
    for (int j = 0; j < names.length; j++) {
        if (names[j].getParent() instanceof VariableDeclarationFragment) {
            VariableDeclarationFragment fragment = (VariableDeclarationFragment) names[j].getParent();
            if (fragment.getParent() instanceof VariableDeclarationStatement) {
                VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
                ASTNode result = (ASTNode) statement.getStructuralProperty(VariableDeclarationStatement.TYPE_PROPERTY);
                if (isRawTypeReference(result))
                    return (SimpleType) result;
            } else if (fragment.getParent() instanceof FieldDeclaration) {
                FieldDeclaration declaration = (FieldDeclaration) fragment.getParent();
                ASTNode result = (ASTNode) declaration.getStructuralProperty(FieldDeclaration.TYPE_PROPERTY);
                if (isRawTypeReference(result))
                    return (SimpleType) result;
            }
        } else if (names[j].getParent() instanceof SingleVariableDeclaration) {
            SingleVariableDeclaration declaration = (SingleVariableDeclaration) names[j].getParent();
            ASTNode result = (ASTNode) declaration.getStructuralProperty(SingleVariableDeclaration.TYPE_PROPERTY);
            if (isRawTypeReference(result))
                return (SimpleType) result;
        } else if (names[j].getParent() instanceof MethodDeclaration) {
            MethodDeclaration methodDecl = (MethodDeclaration) names[j].getParent();
            ASTNode result = (ASTNode) methodDecl.getStructuralProperty(MethodDeclaration.RETURN_TYPE2_PROPERTY);
            if (isRawTypeReference(result))
                return (SimpleType) result;
        }
    }
    return null;
}
Also used : SimpleType(org.eclipse.jdt.core.dom.SimpleType) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 3 with SimpleType

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

the class Java50Fix method createRawTypeReferenceFix.

public static Java50Fix createRawTypeReferenceFix(CompilationUnit compilationUnit, IProblemLocation problem) {
    List<CompilationUnitRewriteOperation> operations = new ArrayList<CompilationUnitRewriteOperation>();
    SimpleType node = createRawTypeReferenceOperations(compilationUnit, new IProblemLocation[] { problem }, operations);
    if (operations.size() == 0)
        return null;
    return new Java50Fix(Messages.format(FixMessages.Java50Fix_AddTypeArguments_description, BasicElementLabels.getJavaElementName(node.getName().getFullyQualifiedName())), compilationUnit, operations.toArray(new CompilationUnitRewriteOperation[operations.size()]));
}
Also used : SimpleType(org.eclipse.jdt.core.dom.SimpleType) ArrayList(java.util.ArrayList)

Example 4 with SimpleType

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

the class ASTNodes method getTypeName.

/**
	 * Returns the simple name of the type, followed by array dimensions.
	 * Skips qualifiers, type arguments, and type annotations.
	 * <p>
	 * Does <b>not</b> work for WildcardTypes, etc.!
	 * 
	 * @param type a type that has a simple name
	 * @return the simple name, followed by array dimensions
	 * @see #getSimpleNameIdentifier(Name)
	 * @since 3.10
	 */
public static String getTypeName(Type type) {
    final StringBuffer buffer = new StringBuffer();
    ASTVisitor visitor = new ASTVisitor() {

        @Override
        public boolean visit(PrimitiveType node) {
            buffer.append(node.getPrimitiveTypeCode().toString());
            return false;
        }

        @Override
        public boolean visit(SimpleType node) {
            buffer.append(getSimpleNameIdentifier(node.getName()));
            return false;
        }

        @Override
        public boolean visit(QualifiedType node) {
            buffer.append(node.getName().getIdentifier());
            return false;
        }

        @Override
        public boolean visit(NameQualifiedType node) {
            buffer.append(node.getName().getIdentifier());
            return false;
        }

        @Override
        public boolean visit(ParameterizedType node) {
            node.getType().accept(this);
            return false;
        }

        @Override
        public void endVisit(ArrayType node) {
            for (int i = 0; i < node.dimensions().size(); i++) {
                //$NON-NLS-1$
                buffer.append("[]");
            }
        }
    };
    type.accept(visitor);
    return buffer.toString();
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 5 with SimpleType

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

the class QuickAssistProcessor method getConvertToStringBufferProposal.

private static LinkedCorrectionProposal getConvertToStringBufferProposal(IInvocationContext context, AST ast, InfixExpression oldInfixExpression) {
    String bufferOrBuilderName;
    ICompilationUnit cu = context.getCompilationUnit();
    if (JavaModelUtil.is50OrHigher(cu.getJavaProject())) {
        //$NON-NLS-1$
        bufferOrBuilderName = "StringBuilder";
    } else {
        //$NON-NLS-1$
        bufferOrBuilderName = "StringBuffer";
    }
    ASTRewrite rewrite = ASTRewrite.create(ast);
    SimpleName existingBuffer = getEnclosingAppendBuffer(oldInfixExpression);
    String mechanismName = BasicElementLabels.getJavaElementName(existingBuffer == null ? bufferOrBuilderName : existingBuffer.getIdentifier());
    String label = Messages.format(CorrectionMessages.QuickAssistProcessor_convert_to_string_buffer_description, mechanismName);
    //Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.CONVERT_TO_STRING_BUFFER);
    proposal.setCommandId(CONVERT_TO_STRING_BUFFER_ID);
    Statement insertAfter;
    String bufferName;
    //$NON-NLS-1$
    String groupID = "nameId";
    ListRewrite listRewrite;
    Statement enclosingStatement = ASTResolving.findParentStatement(oldInfixExpression);
    if (existingBuffer != null) {
        if (ASTNodes.isControlStatementBody(enclosingStatement.getLocationInParent())) {
            Block newBlock = ast.newBlock();
            listRewrite = rewrite.getListRewrite(newBlock, Block.STATEMENTS_PROPERTY);
            insertAfter = null;
            rewrite.replace(enclosingStatement, newBlock, null);
        } else {
            listRewrite = rewrite.getListRewrite(enclosingStatement.getParent(), (ChildListPropertyDescriptor) enclosingStatement.getLocationInParent());
            insertAfter = enclosingStatement;
        }
        bufferName = existingBuffer.getIdentifier();
    } else {
        // create buffer
        VariableDeclarationFragment frag = ast.newVariableDeclarationFragment();
        // check if name is already in use and provide alternative
        List<String> fExcludedVariableNames = Arrays.asList(ASTResolving.getUsedVariableNames(oldInfixExpression));
        SimpleType bufferType = ast.newSimpleType(ast.newName(bufferOrBuilderName));
        ClassInstanceCreation newBufferExpression = ast.newClassInstanceCreation();
        //StubUtility.getVariableNameSuggestions(NamingConventions.VK_LOCAL, cu.getJavaProject(), bufferOrBuilderName, 0, fExcludedVariableNames, true);
        String[] newBufferNames = new String[] {};
        bufferName = newBufferNames[0];
        SimpleName bufferNameDeclaration = ast.newSimpleName(bufferName);
        frag.setName(bufferNameDeclaration);
        proposal.addLinkedPosition(rewrite.track(bufferNameDeclaration), true, groupID);
        for (int i = 0; i < newBufferNames.length; i++) {
            proposal.addLinkedPositionProposal(groupID, newBufferNames[i], null);
        }
        newBufferExpression.setType(bufferType);
        frag.setInitializer(newBufferExpression);
        VariableDeclarationStatement bufferDeclaration = ast.newVariableDeclarationStatement(frag);
        bufferDeclaration.setType(ast.newSimpleType(ast.newName(bufferOrBuilderName)));
        insertAfter = bufferDeclaration;
        Statement statement = ASTResolving.findParentStatement(oldInfixExpression);
        if (ASTNodes.isControlStatementBody(statement.getLocationInParent())) {
            Block newBlock = ast.newBlock();
            listRewrite = rewrite.getListRewrite(newBlock, Block.STATEMENTS_PROPERTY);
            listRewrite.insertFirst(bufferDeclaration, null);
            listRewrite.insertLast(rewrite.createMoveTarget(statement), null);
            rewrite.replace(statement, newBlock, null);
        } else {
            listRewrite = rewrite.getListRewrite(statement.getParent(), (ChildListPropertyDescriptor) statement.getLocationInParent());
            listRewrite.insertBefore(bufferDeclaration, statement, null);
        }
    }
    List<Expression> operands = new ArrayList<Expression>();
    collectInfixPlusOperands(oldInfixExpression, operands);
    Statement lastAppend = insertAfter;
    for (Iterator<Expression> iter = operands.iterator(); iter.hasNext(); ) {
        Expression operand = iter.next();
        MethodInvocation appendIncovationExpression = ast.newMethodInvocation();
        //$NON-NLS-1$
        appendIncovationExpression.setName(ast.newSimpleName("append"));
        SimpleName bufferNameReference = ast.newSimpleName(bufferName);
        // If there was an existing name, don't offer to rename it
        if (existingBuffer == null) {
            proposal.addLinkedPosition(rewrite.track(bufferNameReference), true, groupID);
        }
        appendIncovationExpression.setExpression(bufferNameReference);
        appendIncovationExpression.arguments().add(rewrite.createCopyTarget(operand));
        ExpressionStatement appendExpressionStatement = ast.newExpressionStatement(appendIncovationExpression);
        if (lastAppend == null) {
            listRewrite.insertFirst(appendExpressionStatement, null);
        } else {
            listRewrite.insertAfter(appendExpressionStatement, lastAppend, null);
        }
        lastAppend = appendExpressionStatement;
    }
    if (existingBuffer != null) {
        proposal.setEndPosition(rewrite.track(lastAppend));
        if (insertAfter != null) {
            rewrite.remove(enclosingStatement, null);
        }
    } else {
        // replace old expression with toString
        MethodInvocation bufferToString = ast.newMethodInvocation();
        //$NON-NLS-1$
        bufferToString.setName(ast.newSimpleName("toString"));
        SimpleName bufferNameReference = ast.newSimpleName(bufferName);
        bufferToString.setExpression(bufferNameReference);
        proposal.addLinkedPosition(rewrite.track(bufferNameReference), true, groupID);
        rewrite.replace(oldInfixExpression, bufferToString, null);
        proposal.setEndPosition(rewrite.track(bufferToString));
    }
    return proposal;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) ChildListPropertyDescriptor(org.eclipse.jdt.core.dom.ChildListPropertyDescriptor) SimpleType(org.eclipse.jdt.core.dom.SimpleType) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement)

Aggregations

SimpleType (org.eclipse.jdt.core.dom.SimpleType)23 ASTNode (org.eclipse.jdt.core.dom.ASTNode)13 SimpleName (org.eclipse.jdt.core.dom.SimpleName)13 Type (org.eclipse.jdt.core.dom.Type)10 ArrayType (org.eclipse.jdt.core.dom.ArrayType)9 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)9 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)8 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)8 ArrayList (java.util.ArrayList)7 Name (org.eclipse.jdt.core.dom.Name)7 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)7 List (java.util.List)5 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)5 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)5 Expression (org.eclipse.jdt.core.dom.Expression)5 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 IType (org.eclipse.jdt.core.IType)4 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)4 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)4