Search in sources :

Example 56 with FieldDeclaration

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

the class ExtractMethodRefactoring method initializeDestinations.

private void initializeDestinations() {
    List<ASTNode> result = new ArrayList<ASTNode>();
    BodyDeclaration decl = fAnalyzer.getEnclosingBodyDeclaration();
    ASTNode current = ASTResolving.findParentType(decl.getParent());
    if (fAnalyzer.isValidDestination(current)) {
        result.add(current);
    }
    if (current != null && (decl instanceof MethodDeclaration || decl instanceof Initializer || decl instanceof FieldDeclaration)) {
        ITypeBinding binding = ASTNodes.getEnclosingType(current);
        ASTNode next = ASTResolving.findParentType(current.getParent());
        while (next != null && binding != null && binding.isNested()) {
            if (fAnalyzer.isValidDestination(next)) {
                result.add(next);
            }
            current = next;
            binding = ASTNodes.getEnclosingType(current);
            next = ASTResolving.findParentType(next.getParent());
        }
    }
    fDestinations = result.toArray(new ASTNode[result.size()]);
    fDestination = fDestinations[fDestinationIndex];
}
Also used : Initializer(org.eclipse.jdt.core.dom.Initializer) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 57 with FieldDeclaration

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

the class ExtractConstantRefactoring method depends.

/* bd is a static field declaration or static initializer */
private static boolean depends(IExpressionFragment selected, BodyDeclaration bd) {
    if (bd instanceof FieldDeclaration) {
        FieldDeclaration fieldDecl = (FieldDeclaration) bd;
        for (Iterator<VariableDeclarationFragment> fragments = fieldDecl.fragments().iterator(); fragments.hasNext(); ) {
            VariableDeclarationFragment fragment = fragments.next();
            SimpleName staticFieldName = fragment.getName();
            if (selected.getSubFragmentsMatching(ASTFragmentFactory.createFragmentForFullSubtree(staticFieldName)).length != 0)
                return true;
        }
    }
    return false;
}
Also used : VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) SimpleName(org.eclipse.jdt.core.dom.SimpleName) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 58 with FieldDeclaration

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

the class ExtractConstantRefactoring method computeConstantDeclarationLocation.

private void computeConstantDeclarationLocation() throws JavaModelException {
    if (isDeclarationLocationComputed())
        return;
    BodyDeclaration lastStaticDependency = null;
    Iterator<BodyDeclaration> decls = getContainingTypeDeclarationNode().bodyDeclarations().iterator();
    while (decls.hasNext()) {
        BodyDeclaration decl = decls.next();
        int modifiers;
        if (decl instanceof FieldDeclaration)
            modifiers = ((FieldDeclaration) decl).getModifiers();
        else if (decl instanceof Initializer)
            modifiers = ((Initializer) decl).getModifiers();
        else {
            continue;
        /* this declaration is not a field declaration
				              or initializer, so the placement of the constant
				              declaration relative to it does not matter */
        }
        if (Modifier.isStatic(modifiers) && depends(getSelectedExpression(), decl))
            lastStaticDependency = decl;
    }
    if (lastStaticDependency == null)
        fInsertFirst = true;
    else
        fToInsertAfter = lastStaticDependency;
}
Also used : Initializer(org.eclipse.jdt.core.dom.Initializer) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 59 with FieldDeclaration

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

the class CallInliner method initializeRewriteState.

private void initializeRewriteState() {
    fFieldInitializer = false;
    ASTNode parent = fInvocation.getParent();
    do {
        if (parent instanceof FieldDeclaration) {
            fFieldInitializer = true;
            return;
        } else if (parent instanceof Block) {
            return;
        }
        parent = parent.getParent();
    } while (parent != null);
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 60 with FieldDeclaration

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

the class ConvertAnonymousToNestedRefactoring method mustInnerClassBeStatic.

public boolean mustInnerClassBeStatic() {
    ITypeBinding typeBinding = ((AbstractTypeDeclaration) ASTNodes.getParent(fAnonymousInnerClassNode, AbstractTypeDeclaration.class)).resolveBinding();
    ASTNode current = fAnonymousInnerClassNode.getParent();
    boolean ans = false;
    while (current != null) {
        switch(current.getNodeType()) {
            case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
            case ASTNode.CONSTRUCTOR_INVOCATION:
                return true;
            case ASTNode.ANONYMOUS_CLASS_DECLARATION:
                {
                    AnonymousClassDeclaration enclosingAnonymousClassDeclaration = (AnonymousClassDeclaration) current;
                    ITypeBinding binding = enclosingAnonymousClassDeclaration.resolveBinding();
                    if (binding != null && Bindings.isSuperType(typeBinding, binding.getSuperclass())) {
                        return false;
                    }
                    break;
                }
            case ASTNode.FIELD_DECLARATION:
                {
                    FieldDeclaration enclosingFieldDeclaration = (FieldDeclaration) current;
                    if (Modifier.isStatic(enclosingFieldDeclaration.getModifiers())) {
                        ans = true;
                    }
                    break;
                }
            case ASTNode.METHOD_DECLARATION:
                {
                    MethodDeclaration enclosingMethodDeclaration = (MethodDeclaration) current;
                    if (Modifier.isStatic(enclosingMethodDeclaration.getModifiers())) {
                        ans = true;
                    }
                    break;
                }
            case ASTNode.TYPE_DECLARATION:
                {
                    return ans;
                }
        }
        current = current.getParent();
    }
    return ans;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)103 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)53 ASTNode (org.eclipse.jdt.core.dom.ASTNode)51 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)46 AST (org.eclipse.jdt.core.dom.AST)26 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)26 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)26 Type (org.eclipse.jdt.core.dom.Type)24 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)22 Block (org.eclipse.jdt.core.dom.Block)17 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)16 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)16 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)16 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)16 ArrayList (java.util.ArrayList)15 SimpleName (org.eclipse.jdt.core.dom.SimpleName)15 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)14 Expression (org.eclipse.jdt.core.dom.Expression)14 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)14 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)13