Search in sources :

Example 16 with VariableDeclaration

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

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

the class ASTNodes method isExplicitlyTypedLambda.

private static boolean isExplicitlyTypedLambda(Expression expression) {
    if (!(expression instanceof LambdaExpression))
        return false;
    LambdaExpression lambda = (LambdaExpression) expression;
    List<VariableDeclaration> parameters = lambda.parameters();
    if (parameters.isEmpty())
        return true;
    return parameters.get(0) instanceof SingleVariableDeclaration;
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 18 with VariableDeclaration

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

the class ObsoleteLambdaExpressionRatherThanComparatorCleanUp method maybeRefactorLambdaExpression.

private boolean maybeRefactorLambdaExpression(final LambdaExpression visited, final Set<String> classesToUseWithImport) {
    ITypeBinding targetType = ASTNodes.getTargetType(visited);
    if (ASTNodes.hasType(targetType, Comparator.class.getCanonicalName()) && targetType.getTypeArguments() != null && targetType.getTypeArguments().length == 1 && visited.parameters() != null && visited.parameters().size() == 2) {
        VariableDeclaration object1 = (VariableDeclaration) visited.parameters().get(0);
        VariableDeclaration object2 = (VariableDeclaration) visited.parameters().get(1);
        if (visited.getBody() instanceof Statement) {
            return maybeRefactorBody(visited, targetType.getTypeArguments()[0], classesToUseWithImport, object1, object2, ASTNodes.asList((Statement) visited.getBody()));
        }
        if (visited.getBody() instanceof Expression) {
            SimpleName name1 = object1.getName();
            SimpleName name2 = object2.getName();
            return maybeRefactorExpression(visited, targetType.getTypeArguments()[0], classesToUseWithImport, name1, name2, (Expression) visited.getBody());
        }
    }
    return true;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) Statement(org.eclipse.jdt.core.dom.Statement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) Comparator(java.util.Comparator)

Example 19 with VariableDeclaration

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

the class AbstractClassSubstituteCleanUp method maybeRefactorBlock.

/**
 * Maybe refactor the block.
 *
 * @param node                   The node
 * @param classesToUseWithImport The classes to use with import
 * @param importsToAdd           The imports to add
 * @return True to visit subtree
 */
protected boolean maybeRefactorBlock(final Block node, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
    ObjectInstantiationVisitor classCreationVisitor = new ObjectInstantiationVisitor(node);
    node.accept(classCreationVisitor);
    for (ClassInstanceCreation instanceCreation : classCreationVisitor.getObjectInstantiations()) {
        List<VariableDeclaration> varDecls = new ArrayList<>();
        List<MethodInvocation> methodCallsToRefactor = new ArrayList<>();
        if (canInstantiationBeRefactored(instanceCreation) && canBeRefactored(node, instanceCreation, instanceCreation.resolveTypeBinding(), varDecls, methodCallsToRefactor) && canCodeBeRefactored()) {
            replaceClass(instanceCreation, varDecls, methodCallsToRefactor, classesToUseWithImport, importsToAdd);
            return false;
        }
    }
    return true;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ArrayList(java.util.ArrayList) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 20 with VariableDeclaration

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

the class InlineTempRefactoring method removeTemp.

private void removeTemp(CompilationUnitRewrite cuRewrite) {
    VariableDeclaration variableDeclaration = getVariableDeclaration();
    TextEditGroup groupDesc = cuRewrite.createGroupDescription(RefactoringCoreMessages.InlineTempRefactoring_remove_edit_name);
    ASTNode parent = variableDeclaration.getParent();
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    TightSourceRangeComputer sourceRangeComputer = new TightSourceRangeComputer();
    rewrite.setTargetSourceRangeComputer(sourceRangeComputer);
    if (parent instanceof VariableDeclarationStatement && ((VariableDeclarationStatement) parent).fragments().size() == 1) {
        sourceRangeComputer.addTightSourceNode(parent);
        rewrite.remove(parent, groupDesc);
    } else {
        sourceRangeComputer.addTightSourceNode(variableDeclaration);
        rewrite.remove(variableDeclaration, groupDesc);
    }
}
Also used : TightSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Aggregations

VariableDeclaration (org.eclipse.jdt.core.dom.VariableDeclaration)40 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)22 ASTNode (org.eclipse.jdt.core.dom.ASTNode)19 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)15 ArrayList (java.util.ArrayList)12 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)12 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)10 SimpleName (org.eclipse.jdt.core.dom.SimpleName)9 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)8 Expression (org.eclipse.jdt.core.dom.Expression)8 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)8 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)8 Type (org.eclipse.jdt.core.dom.Type)7 AST (org.eclipse.jdt.core.dom.AST)6 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)6 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)6 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)6 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)6 ArrayCreation (org.eclipse.jdt.core.dom.ArrayCreation)5 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)5