Search in sources :

Example 81 with AbstractTypeDeclaration

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

the class ExtractConstantRefactoring method getReplacementScope.

/*
	 * Elements returned by next() are BodyDeclaration or Annotation instances.
	 */
private Iterator<ASTNode> getReplacementScope() throws JavaModelException {
    boolean declPredecessorReached = false;
    Collection<ASTNode> scope = new ArrayList<ASTNode>();
    AbstractTypeDeclaration containingType = getContainingTypeDeclarationNode();
    if (containingType instanceof EnumDeclaration) {
        // replace in all enum constants bodies
        EnumDeclaration enumDeclaration = (EnumDeclaration) containingType;
        scope.addAll(enumDeclaration.enumConstants());
    }
    for (Iterator<IExtendedModifier> iter = containingType.modifiers().iterator(); iter.hasNext(); ) {
        IExtendedModifier modifier = iter.next();
        if (modifier instanceof Annotation) {
            scope.add((ASTNode) modifier);
        }
    }
    for (Iterator<BodyDeclaration> bodyDeclarations = containingType.bodyDeclarations().iterator(); bodyDeclarations.hasNext(); ) {
        BodyDeclaration bodyDeclaration = bodyDeclarations.next();
        if (bodyDeclaration == getNodeToInsertConstantDeclarationAfter())
            declPredecessorReached = true;
        if (insertFirst() || declPredecessorReached || !isStaticFieldOrStaticInitializer(bodyDeclaration))
            scope.add(bodyDeclaration);
    }
    return scope.iterator();
}
Also used : ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) Annotation(org.eclipse.jdt.core.dom.Annotation) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) EnumDeclaration(org.eclipse.jdt.core.dom.EnumDeclaration) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 82 with AbstractTypeDeclaration

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

the class PromoteTempToFieldRefactoring method getNamesOfFieldsInDeclaringType.

private String[] getNamesOfFieldsInDeclaringType() {
    final AbstractTypeDeclaration type = getEnclosingType();
    if (type instanceof TypeDeclaration) {
        FieldDeclaration[] fields = ((TypeDeclaration) type).getFields();
        List<String> result = new ArrayList<String>(fields.length);
        for (int i = 0; i < fields.length; i++) {
            for (Iterator<VariableDeclarationFragment> iter = fields[i].fragments().iterator(); iter.hasNext(); ) {
                VariableDeclarationFragment field = iter.next();
                result.add(field.getName().getIdentifier());
            }
        }
        return result.toArray(new String[result.size()]);
    }
    return new String[] {};
}
Also used : VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ArrayList(java.util.ArrayList) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 83 with AbstractTypeDeclaration

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

the class JavaTextSelection method resolveInClassInitializer.

public boolean resolveInClassInitializer() {
    if (fInClassInitializerRequested)
        return fInClassInitializer;
    fInClassInitializerRequested = true;
    resolveSelectedNodes();
    ASTNode node = getStartNode();
    if (node == null) {
        fInClassInitializer = true;
    } else {
        while (node != null) {
            int nodeType = node.getNodeType();
            if (node instanceof AbstractTypeDeclaration) {
                fInClassInitializer = false;
                break;
            } else if (nodeType == ASTNode.ANONYMOUS_CLASS_DECLARATION) {
                fInClassInitializer = false;
                break;
            } else if (nodeType == ASTNode.INITIALIZER) {
                fInClassInitializer = true;
                break;
            }
            node = node.getParent();
        }
    }
    return fInClassInitializer;
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 84 with AbstractTypeDeclaration

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

the class JavaTextSelection method resolveInVariableInitializer.

public boolean resolveInVariableInitializer() {
    if (fInVariableInitializerRequested)
        return fInVariableInitializer;
    fInVariableInitializerRequested = true;
    resolveSelectedNodes();
    ASTNode node = getStartNode();
    ASTNode last = null;
    while (node != null) {
        int nodeType = node.getNodeType();
        if (node instanceof AbstractTypeDeclaration) {
            fInVariableInitializer = false;
            break;
        } else if (nodeType == ASTNode.ANONYMOUS_CLASS_DECLARATION) {
            fInVariableInitializer = false;
            break;
        } else if (nodeType == ASTNode.VARIABLE_DECLARATION_FRAGMENT && ((VariableDeclarationFragment) node).getInitializer() == last) {
            fInVariableInitializer = true;
            break;
        } else if (nodeType == ASTNode.SINGLE_VARIABLE_DECLARATION && ((SingleVariableDeclaration) node).getInitializer() == last) {
            fInVariableInitializer = true;
            break;
        } else if (nodeType == ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION && ((AnnotationTypeMemberDeclaration) node).getDefault() == last) {
            fInVariableInitializer = true;
            break;
        }
        last = node;
        node = node.getParent();
    }
    return fInVariableInitializer;
}
Also used : AnnotationTypeMemberDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 85 with AbstractTypeDeclaration

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

the class RemoveUnneededThisExpressionRefactoring method thisExpressionRefersToEnclosingType.

private static boolean thisExpressionRefersToEnclosingType(Name thisQualifierName, ASTNode node) {
    if (thisQualifierName == null) {
        return true;
    }
    final ASTNode enclosingType = getEnclosingType(node);
    if (enclosingType instanceof AnonymousClassDeclaration) {
        return false;
    }
    final AbstractTypeDeclaration ancestor = (AbstractTypeDeclaration) enclosingType;
    if (thisQualifierName instanceof SimpleName) {
        return isEqual((SimpleName) thisQualifierName, ancestor.getName());
    } else if (thisQualifierName instanceof QualifiedName) {
        final QualifiedName qn = (QualifiedName) thisQualifierName;
        return isEqual(qn.getName(), ancestor.getName()) && thisExpressionRefersToEnclosingType(qn.getQualifier(), ancestor);
    }
    throw new NotImplementedException(thisQualifierName);
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) NotImplementedException(org.autorefactor.util.NotImplementedException) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)233 ASTNode (org.eclipse.jdt.core.dom.ASTNode)116 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)88 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)85 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)81 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)72 AnonymousClassDeclaration (org.eclipse.jdt.core.dom.AnonymousClassDeclaration)52 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)49 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)49 AST (org.eclipse.jdt.core.dom.AST)44 ArrayList (java.util.ArrayList)43 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)41 IType (org.eclipse.jdt.core.IType)40 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)38 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)38 Type (org.eclipse.jdt.core.dom.Type)37 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)36 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)34 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)25 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)24