Search in sources :

Example 31 with SingleVariableDeclaration

use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project processing by processing.

the class CompletionGenerator method checkForTypes.

public static CompletionCandidate[] checkForTypes(ASTNode node) {
    List<VariableDeclarationFragment> vdfs = null;
    switch(node.getNodeType()) {
        case ASTNode.TYPE_DECLARATION:
            return new CompletionCandidate[] { new CompletionCandidate((TypeDeclaration) node) };
        case ASTNode.METHOD_DECLARATION:
            MethodDeclaration md = (MethodDeclaration) node;
            log(getNodeAsString(md));
            List<ASTNode> params = (List<ASTNode>) md.getStructuralProperty(MethodDeclaration.PARAMETERS_PROPERTY);
            CompletionCandidate[] cand = new CompletionCandidate[params.size() + 1];
            cand[0] = new CompletionCandidate(md);
            for (int i = 0; i < params.size(); i++) {
                //        cand[i + 1] = new CompletionCandidate(params.get(i).toString(), "", "",
                //                                              CompletionCandidate.LOCAL_VAR);
                cand[i + 1] = new CompletionCandidate((SingleVariableDeclaration) params.get(i));
            }
            return cand;
        case ASTNode.SINGLE_VARIABLE_DECLARATION:
            return new CompletionCandidate[] { new CompletionCandidate((SingleVariableDeclaration) node) };
        case ASTNode.FIELD_DECLARATION:
            vdfs = ((FieldDeclaration) node).fragments();
            break;
        case ASTNode.VARIABLE_DECLARATION_STATEMENT:
            vdfs = ((VariableDeclarationStatement) node).fragments();
            break;
        case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
            vdfs = ((VariableDeclarationExpression) node).fragments();
            break;
        default:
            break;
    }
    if (vdfs != null) {
        CompletionCandidate[] ret = new CompletionCandidate[vdfs.size()];
        int i = 0;
        for (VariableDeclarationFragment vdf : vdfs) {
            //        ret[i++] = new CompletionCandidate(getNodeAsString2(vdf), "", "",
            //                                           CompletionCandidate.LOCAL_VAR);
            ret[i++] = new CompletionCandidate(vdf);
        }
        return ret;
    }
    return null;
}
Also used : VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) List(java.util.List)

Example 32 with SingleVariableDeclaration

use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project AutoRefactor by JnRouvignac.

the class MapEliminateKeySetCallsRefactoring method visit.

@Override
public boolean visit(EnhancedForStatement enhancedFor) {
    final Expression foreachExpr = enhancedFor.getExpression();
    if (isKeySetMethod(foreachExpr)) {
        // From 'for (K key : map.keySet()) { }'
        // -> mapExpression become 'map', parameter become 'K key'
        final Expression mapExpression = ((MethodInvocation) foreachExpr).getExpression();
        if (mapExpression == null) {
            // not implemented
            return VISIT_SUBTREE;
        }
        final SingleVariableDeclaration parameter = enhancedFor.getParameter();
        final List<MethodInvocation> getValueMis = collectMapGetValueCalls(mapExpression, parameter, enhancedFor.getBody());
        if (!getValueMis.isEmpty() && haveSameTypeBindings(getValueMis)) {
            replaceEntryIterationByKeyIteration(enhancedFor, mapExpression, parameter, getValueMis);
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 33 with SingleVariableDeclaration

use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project AutoRefactor by JnRouvignac.

the class ReplaceQualifiedNamesBySimpleNamesRefactoring method visit.

@Override
public boolean visit(MethodDeclaration node) {
    // Method parameters
    for (final SingleVariableDeclaration parameter : parameters(node)) {
        if (maybeReplaceFqnsWithSimpleNames(parameter) == DO_NOT_VISIT_SUBTREE) {
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    // Method return value
    if (maybeReplaceFqnsWithSimpleNames(node.getReturnType2()) == DO_NOT_VISIT_SUBTREE) {
        return DO_NOT_VISIT_SUBTREE;
    }
    // Method body
    final Set<String> localIdentifiers = new HashSet<String>();
    for (final SingleVariableDeclaration localParameter : parameters(node)) {
        localIdentifiers.add(localParameter.getName().getIdentifier());
    }
    localIdentifiers.addAll(getLocalVariableIdentifiers(node.getBody(), true));
    return maybeReplaceFqnsWithSimpleNames(node.getBody(), localIdentifiers);
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) HashSet(java.util.HashSet)

Example 34 with SingleVariableDeclaration

use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project AutoRefactor by JnRouvignac.

the class ASTBuilder method catch0.

/**
 * Builds a new {@link CatchClause} instance.
 *
 * @param exceptionTypeName the exception type name
 * @param caughtExceptionName the local name for the caught exception
 * @param stmts the statements to add to the catch clause
 * @return a new catch clause
 */
public CatchClause catch0(String exceptionTypeName, String caughtExceptionName, Statement... stmts) {
    final CatchClause cc = ast.newCatchClause();
    final SingleVariableDeclaration svd = ast.newSingleVariableDeclaration();
    svd.setType(simpleType(exceptionTypeName));
    svd.setName(ast.newSimpleName(caughtExceptionName));
    cc.setException(svd);
    final Block block = ast.newBlock();
    addAll(statements(block), stmts);
    cc.setBody(block);
    return cc;
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) Block(org.eclipse.jdt.core.dom.Block) CatchClause(org.eclipse.jdt.core.dom.CatchClause)

Example 35 with SingleVariableDeclaration

use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project AutoRefactor by JnRouvignac.

the class ASTBuilder method declareSingleVariable.

/**
 * Builds a new {@link SingleVariableDeclaration} instance.
 *
 * @param varName
 *            the name of the variable being declared
 * @param type
 *            the type of the variable being declared
 * @return a new single variable declaration
 */
public SingleVariableDeclaration declareSingleVariable(String varName, Type type) {
    final SingleVariableDeclaration svd = ast.newSingleVariableDeclaration();
    svd.setName(simpleName(varName));
    svd.setType(type);
    return svd;
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration)

Aggregations

SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)121 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)41 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)37 Type (org.eclipse.jdt.core.dom.Type)37 ASTNode (org.eclipse.jdt.core.dom.ASTNode)33 Block (org.eclipse.jdt.core.dom.Block)33 AST (org.eclipse.jdt.core.dom.AST)32 SimpleName (org.eclipse.jdt.core.dom.SimpleName)27 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)27 ArrayList (java.util.ArrayList)26 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)25 List (java.util.List)24 Expression (org.eclipse.jdt.core.dom.Expression)22 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)18 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)18 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)17 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)17 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)16 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)16 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)16