Search in sources :

Example 6 with FieldDeclaration

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

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

the class VariableDeclarationRewrite method rewriteModifiers.

public static void rewriteModifiers(final FieldDeclaration declarationNode, final VariableDeclarationFragment[] toChange, final int includedModifiers, final int excludedModifiers, final ASTRewrite rewrite, final TextEditGroup group) {
    final List<VariableDeclarationFragment> fragmentsToChange = Arrays.asList(toChange);
    AST ast = declarationNode.getAST();
    /*
 * Problem: Same declarationNode can be the subject of multiple calls to this method.
 * For the 2nd++ calls, the original declarationNode has already been rewritten, and this has to be taken into account.
 * 
 * Assumption:
 * - Modifiers for each VariableDeclarationFragment are modified at most once.
 * 
 * Solution:
 * - Maintain a map from original VariableDeclarationFragments to their new FieldDeclaration.
 * - Original modifiers in declarationNode belong to the first fragment.
 * - When a later fragment needs different modifiers, we create a new FieldDeclaration and move all successive fragments into that declaration
 * - When a fragment has been moved to a new declaration, make sure we don't create a new move target again, but instead use the already created one 
 */
    List<VariableDeclarationFragment> fragments = declarationNode.fragments();
    Iterator<VariableDeclarationFragment> iter = fragments.iterator();
    ListRewrite blockRewrite;
    if (declarationNode.getParent() instanceof AbstractTypeDeclaration) {
        blockRewrite = rewrite.getListRewrite(declarationNode.getParent(), ((AbstractTypeDeclaration) declarationNode.getParent()).getBodyDeclarationsProperty());
    } else {
        blockRewrite = rewrite.getListRewrite(declarationNode.getParent(), AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
    }
    VariableDeclarationFragment lastFragment = iter.next();
    ASTNode lastStatement = declarationNode;
    if (fragmentsToChange.contains(lastFragment)) {
        ModifierRewrite modifierRewrite = ModifierRewrite.create(rewrite, declarationNode);
        modifierRewrite.setModifiers(includedModifiers, excludedModifiers, group);
    }
    ListRewrite fragmentsRewrite = null;
    while (iter.hasNext()) {
        VariableDeclarationFragment currentFragment = iter.next();
        @SuppressWarnings("unchecked") Map<VariableDeclarationFragment, MovedFragment> lookup = (Map<VariableDeclarationFragment, MovedFragment>) rewrite.getProperty(MovedFragment.class.getName());
        if (lookup == null) {
            lookup = new HashMap<VariableDeclarationFragment, MovedFragment>();
            rewrite.setProperty(MovedFragment.class.getName(), lookup);
        }
        MovedFragment currentMovedFragment = lookup.get(currentFragment);
        boolean changeLast = fragmentsToChange.contains(lastFragment);
        boolean changeCurrent = fragmentsToChange.contains(currentFragment);
        if (changeLast != changeCurrent || lookup.containsKey(lastFragment)) {
            ModifierRewrite modifierRewrite = null;
            if (currentMovedFragment != null) {
                if (currentMovedFragment.fUsesOriginalModifiers) {
                    // Need to put in the right modifiers (removing any existing ones).
                    modifierRewrite = ModifierRewrite.create(rewrite, currentMovedFragment.fDeclaration);
                    ListRewrite listRewrite = rewrite.getListRewrite(currentMovedFragment.fDeclaration, FieldDeclaration.MODIFIERS2_PROPERTY);
                    List<IExtendedModifier> extendedList = listRewrite.getRewrittenList();
                    for (int i = 0; i < extendedList.size(); i++) {
                        ASTNode curr = (ASTNode) extendedList.get(i);
                        if (curr instanceof Modifier)
                            rewrite.remove(curr, group);
                    }
                }
            // otherwise, don't need to touch the modifiers, so leave modifierRewrite null
            } else {
                // need to split an existing field declaration
                VariableDeclarationFragment moveTarget;
                moveTarget = (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment);
                FieldDeclaration newStatement = (FieldDeclaration) ast.createInstance(FieldDeclaration.class);
                rewrite.getListRewrite(newStatement, FieldDeclaration.FRAGMENTS_PROPERTY).insertLast(moveTarget, group);
                lookup.put(currentFragment, new MovedFragment(moveTarget, newStatement, !changeCurrent));
                rewrite.set(newStatement, FieldDeclaration.TYPE_PROPERTY, rewrite.createCopyTarget(declarationNode.getType()), group);
                modifierRewrite = ModifierRewrite.create(rewrite, newStatement);
                modifierRewrite.copyAllAnnotations(declarationNode, group);
                blockRewrite.insertAfter(newStatement, lastStatement, group);
                fragmentsRewrite = rewrite.getListRewrite(newStatement, FieldDeclaration.FRAGMENTS_PROPERTY);
                lastStatement = newStatement;
            }
            if (modifierRewrite != null) {
                if (changeCurrent) {
                    int newModifiers = (declarationNode.getModifiers() & ~excludedModifiers) | includedModifiers;
                    modifierRewrite.setModifiers(newModifiers, excludedModifiers, group);
                } else {
                    int newModifiers = declarationNode.getModifiers();
                    modifierRewrite.setModifiers(newModifiers, Modifier.NONE, group);
                }
            }
        } else if (fragmentsRewrite != null) {
            VariableDeclarationFragment fragment0;
            boolean usesOriginalModifiers = true;
            if (currentMovedFragment != null) {
                fragment0 = currentMovedFragment.fMoveTarget;
                usesOriginalModifiers = currentMovedFragment.fUsesOriginalModifiers;
                rewrite.getListRewrite(currentMovedFragment.fDeclaration, FieldDeclaration.FRAGMENTS_PROPERTY).remove(fragment0, group);
            } else {
                fragment0 = (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment);
            }
            lookup.put(currentFragment, new MovedFragment(fragment0, lastStatement, usesOriginalModifiers));
            fragmentsRewrite.insertLast(fragment0, group);
        }
        lastFragment = currentFragment;
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) HashMap(java.util.HashMap) Map(java.util.Map) Modifier(org.eclipse.jdt.core.dom.Modifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 8 with FieldDeclaration

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

the class SelfEncapsulateFieldRefactoring method addGetterSetterChanges.

private List<TextEditGroup> addGetterSetterChanges(CompilationUnit root, ASTRewrite rewriter, String lineDelimiter, boolean usingLocalSetter, boolean usingLocalGetter) throws CoreException {
    List<TextEditGroup> result = new ArrayList<TextEditGroup>(2);
    AST ast = root.getAST();
    FieldDeclaration decl = (FieldDeclaration) ASTNodes.getParent(fFieldDeclaration, ASTNode.FIELD_DECLARATION);
    int position = 0;
    int numberOfMethods = 0;
    List<BodyDeclaration> members = ASTNodes.getBodyDeclarations(decl.getParent());
    for (Iterator<BodyDeclaration> iter = members.iterator(); iter.hasNext(); ) {
        BodyDeclaration element = iter.next();
        if (element.getNodeType() == ASTNode.METHOD_DECLARATION) {
            if (fInsertionIndex == -1) {
                break;
            } else if (fInsertionIndex == numberOfMethods) {
                position++;
                break;
            }
            numberOfMethods++;
        }
        position++;
    }
    TextEditGroup description;
    ListRewrite rewrite = fRewriter.getListRewrite(decl.getParent(), getBodyDeclarationsProperty(decl.getParent()));
    if (!usingLocalGetter) {
        description = new TextEditGroup(RefactoringCoreMessages.SelfEncapsulateField_add_getter);
        result.add(description);
        rewrite.insertAt(createGetterMethod(ast, rewriter, lineDelimiter), position++, description);
    }
    if (!JdtFlags.isFinal(fField) && !usingLocalSetter) {
        description = new TextEditGroup(RefactoringCoreMessages.SelfEncapsulateField_add_setter);
        result.add(description);
        rewrite.insertAt(createSetterMethod(ast, rewriter, lineDelimiter), position, description);
    }
    if (!JdtFlags.isPrivate(fField))
        result.add(makeDeclarationPrivate(rewriter, decl));
    return result;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ArrayList(java.util.ArrayList) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 9 with FieldDeclaration

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

the class SelfEncapsulateFieldRefactoring method createGetterMethod.

private MethodDeclaration createGetterMethod(AST ast, ASTRewrite rewriter, String lineDelimiter) throws CoreException {
    FieldDeclaration field = (FieldDeclaration) ASTNodes.getParent(fFieldDeclaration, FieldDeclaration.class);
    Type type = field.getType();
    MethodDeclaration result = ast.newMethodDeclaration();
    result.setName(ast.newSimpleName(fGetterName));
    result.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiers()));
    Type returnType = DimensionRewrite.copyTypeAndAddDimensions(type, fFieldDeclaration.extraDimensions(), rewriter);
    result.setReturnType2(returnType);
    Block block = ast.newBlock();
    result.setBody(block);
    String body = CodeGeneration.getGetterMethodBodyContent(fField.getCompilationUnit(), getTypeName(field.getParent()), fGetterName, fField.getElementName(), lineDelimiter);
    if (body != null) {
        ASTNode getterNode = rewriter.createStringPlaceholder(body, ASTNode.BLOCK);
        block.statements().add(getterNode);
    } else {
        ReturnStatement rs = ast.newReturnStatement();
        rs.setExpression(ast.newSimpleName(fField.getElementName()));
        block.statements().add(rs);
    }
    if (fGenerateJavadoc) {
        String string = CodeGeneration.getGetterComment(fField.getCompilationUnit(), getTypeName(field.getParent()), fGetterName, fField.getElementName(), ASTNodes.asString(type), StubUtility.getBaseName(fField), lineDelimiter);
        if (string != null) {
            Javadoc javadoc = (Javadoc) fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC);
            result.setJavadoc(javadoc);
        }
    }
    return result;
}
Also used : IType(org.eclipse.jdt.core.IType) Type(org.eclipse.jdt.core.dom.Type) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) Javadoc(org.eclipse.jdt.core.dom.Javadoc) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 10 with FieldDeclaration

use of org.eclipse.jdt.core.dom.FieldDeclaration 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)

Aggregations

FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)41 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)24 ASTNode (org.eclipse.jdt.core.dom.ASTNode)23 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)17 AST (org.eclipse.jdt.core.dom.AST)15 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)13 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)12 Type (org.eclipse.jdt.core.dom.Type)12 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)10 Expression (org.eclipse.jdt.core.dom.Expression)8 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)8 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)8 Block (org.eclipse.jdt.core.dom.Block)7 Initializer (org.eclipse.jdt.core.dom.Initializer)7 SimpleName (org.eclipse.jdt.core.dom.SimpleName)7 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)7 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)7 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)7 ArrayList (java.util.ArrayList)6 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)6