Search in sources :

Example 21 with VariableDeclarationFragment

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

the class SelfEncapsulateFieldRefactoring method makeDeclarationPrivate.

private TextEditGroup makeDeclarationPrivate(ASTRewrite rewriter, FieldDeclaration decl) {
    TextEditGroup description = new TextEditGroup(RefactoringCoreMessages.SelfEncapsulateField_change_visibility);
    VariableDeclarationFragment[] vdfs = new VariableDeclarationFragment[] { fFieldDeclaration };
    int includedModifiers = Modifier.PRIVATE;
    int excludedModifiers = Modifier.PROTECTED | Modifier.PUBLIC;
    VariableDeclarationRewrite.rewriteModifiers(decl, vdfs, includedModifiers, excludedModifiers, rewriter, description);
    return description;
}
Also used : VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 22 with VariableDeclarationFragment

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

the class ConvertAnonymousToNestedRefactoring method updateAndMoveBodyDeclarations.

private void updateAndMoveBodyDeclarations(CompilationUnitRewrite rewriter, IVariableBinding[] bindings, String[] fieldNames, List<BodyDeclaration> newBodyDeclarations, MethodDeclaration newConstructorDecl) {
    final ASTRewrite astRewrite = rewriter.getASTRewrite();
    final AST ast = astRewrite.getAST();
    final boolean useThisAccess = useThisForFieldAccess();
    int fieldInsertIndex = newConstructorDecl != null ? newBodyDeclarations.lastIndexOf(newConstructorDecl) : newBodyDeclarations.size();
    for (Iterator<BodyDeclaration> iterator = fAnonymousInnerClassNode.bodyDeclarations().iterator(); iterator.hasNext(); ) {
        BodyDeclaration body = iterator.next();
        for (int i = 0; i < bindings.length; i++) {
            SimpleName[] names = LinkedNodeFinder.findByBinding(body, bindings[i]);
            String fieldName = fieldNames[i];
            for (int k = 0; k < names.length; k++) {
                SimpleName newNode = ast.newSimpleName(fieldName);
                if (useThisAccess) {
                    FieldAccess access = ast.newFieldAccess();
                    access.setExpression(ast.newThisExpression());
                    access.setName(newNode);
                    astRewrite.replace(names[k], access, null);
                } else {
                    astRewrite.replace(names[k], newNode, null);
                }
                addLinkedPosition(KEY_FIELD_NAME_EXT + i, newNode, astRewrite, false);
            }
        }
        if (body instanceof Initializer || body instanceof FieldDeclaration) {
            newBodyDeclarations.add(fieldInsertIndex++, (BodyDeclaration) astRewrite.createMoveTarget(body));
        } else {
            newBodyDeclarations.add((BodyDeclaration) astRewrite.createMoveTarget(body));
        }
    }
    if (newConstructorDecl != null) {
        // move initialization of existing fields to constructor if an outer is referenced
        List<Statement> bodyStatements = newConstructorDecl.getBody().statements();
        List<VariableDeclarationFragment> fieldsToInitializeInConstructor = getFieldsToInitializeInConstructor();
        for (Iterator<VariableDeclarationFragment> iter = fieldsToInitializeInConstructor.iterator(); iter.hasNext(); ) {
            VariableDeclarationFragment fragment = iter.next();
            Expression initializer = fragment.getInitializer();
            Expression replacement = (Expression) astRewrite.get(fragment, VariableDeclarationFragment.INITIALIZER_PROPERTY);
            if (replacement == initializer) {
                replacement = (Expression) astRewrite.createMoveTarget(initializer);
            }
            astRewrite.remove(initializer, null);
            SimpleName fieldNameNode = ast.newSimpleName(fragment.getName().getIdentifier());
            bodyStatements.add(newFieldAssignment(ast, fieldNameNode, replacement, useThisAccess));
        }
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Statement(org.eclipse.jdt.core.dom.Statement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer) Initializer(org.eclipse.jdt.core.dom.Initializer) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess)

Example 23 with VariableDeclarationFragment

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

the class CallInliner method computeRealArguments.

private void computeRealArguments() {
    List<Expression> arguments = Invocations.getArguments(fInvocation);
    Set<Expression> canNotInline = crossCheckArguments(arguments);
    boolean needsVarargBoxing = needsVarargBoxing(arguments);
    int varargIndex = fSourceProvider.getVarargIndex();
    AST ast = fInvocation.getAST();
    Expression[] realArguments = new Expression[needsVarargBoxing ? varargIndex + 1 : arguments.size()];
    for (int i = 0; i < (needsVarargBoxing ? varargIndex : arguments.size()); i++) {
        Expression expression = arguments.get(i);
        ParameterData parameter = fSourceProvider.getParameterData(i);
        if (canInline(expression, parameter) && !canNotInline.contains(expression)) {
            realArguments[i] = expression;
        } else {
            String name = fInvocationScope.createName(parameter.getName(), true);
            realArguments[i] = ast.newSimpleName(name);
            VariableDeclarationStatement local = createLocalDeclaration(parameter.getTypeBinding(), name, (Expression) fRewrite.createCopyTarget(expression));
            if (parameter.isFinal()) {
                local.modifiers().add(fInvocation.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD));
            }
            fLocals.add(local);
        }
    }
    if (needsVarargBoxing) {
        ParameterData parameter = fSourceProvider.getParameterData(varargIndex);
        String name = fInvocationScope.createName(parameter.getName(), true);
        realArguments[varargIndex] = ast.newSimpleName(name);
        Type type = fImportRewrite.addImport(parameter.getTypeBinding(), ast);
        VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
        fragment.setName(ast.newSimpleName(name));
        ArrayInitializer initializer = ast.newArrayInitializer();
        for (int i = varargIndex; i < arguments.size(); i++) {
            initializer.expressions().add(fRewrite.createCopyTarget(arguments.get(i)));
        }
        fragment.setInitializer(initializer);
        VariableDeclarationStatement decl = ast.newVariableDeclarationStatement(fragment);
        decl.setType(type);
        fLocals.add(decl);
    }
    fContext.compilationUnit = fCUnit;
    fContext.arguments = realArguments;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Type(org.eclipse.jdt.core.dom.Type) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 24 with VariableDeclarationFragment

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

the class CallInliner method createLocalDeclaration.

private VariableDeclarationStatement createLocalDeclaration(ITypeBinding type, String name, Expression initializer) {
    ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fTargetNode, fImportRewrite);
    String typeName = fImportRewrite.addImport(type, context);
    VariableDeclarationStatement decl = (VariableDeclarationStatement) ASTNodeFactory.newStatement(fInvocation.getAST(), //$NON-NLS-1$ //$NON-NLS-2$
    typeName + " " + name + ";");
    ((VariableDeclarationFragment) decl.fragments().get(0)).setInitializer(initializer);
    return decl;
}
Also used : ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement)

Example 25 with VariableDeclarationFragment

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

the class ConvertAnonymousToNestedRefactoring method createFieldsForAccessedLocals.

private void createFieldsForAccessedLocals(CompilationUnitRewrite rewrite, IVariableBinding[] varBindings, String[] fieldNames, List<BodyDeclaration> newBodyDeclarations) throws CoreException {
    final ImportRewrite importRewrite = rewrite.getImportRewrite();
    final ASTRewrite astRewrite = rewrite.getASTRewrite();
    final AST ast = astRewrite.getAST();
    for (int i = 0; i < varBindings.length; i++) {
        VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
        fragment.setInitializer(null);
        fragment.setName(ast.newSimpleName(fieldNames[i]));
        FieldDeclaration field = ast.newFieldDeclaration(fragment);
        ITypeBinding varType = varBindings[i].getType();
        field.setType(importRewrite.addImport(varType, ast));
        field.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.PRIVATE | Modifier.FINAL));
        if (doAddComments()) {
            String string = CodeGeneration.getFieldComment(rewrite.getCu(), varType.getName(), fieldNames[i], StubUtility.getLineDelimiterUsed(fCu));
            if (string != null) {
                Javadoc javadoc = (Javadoc) astRewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
                field.setJavadoc(javadoc);
            }
        }
        newBodyDeclarations.add(field);
        addLinkedPosition(KEY_FIELD_NAME_EXT + i, fragment.getName(), astRewrite, false);
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Javadoc(org.eclipse.jdt.core.dom.Javadoc) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Aggregations

VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)82 ASTNode (org.eclipse.jdt.core.dom.ASTNode)39 AST (org.eclipse.jdt.core.dom.AST)35 Expression (org.eclipse.jdt.core.dom.Expression)30 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)26 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)24 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)24 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)22 Type (org.eclipse.jdt.core.dom.Type)22 SimpleName (org.eclipse.jdt.core.dom.SimpleName)21 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)21 Assignment (org.eclipse.jdt.core.dom.Assignment)19 ArrayList (java.util.ArrayList)15 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)14 Block (org.eclipse.jdt.core.dom.Block)13 CastExpression (org.eclipse.jdt.core.dom.CastExpression)13 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)13 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)13 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)12 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)12