Search in sources :

Example 26 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 27 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 28 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 29 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 30 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)62 ASTNode (org.eclipse.jdt.core.dom.ASTNode)37 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)25 AnonymousClassDeclaration (org.eclipse.jdt.core.dom.AnonymousClassDeclaration)18 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)18 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)17 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)17 AST (org.eclipse.jdt.core.dom.AST)15 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)12 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)12 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)11 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)11 Type (org.eclipse.jdt.core.dom.Type)10 ArrayList (java.util.ArrayList)9 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)9 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)9 IType (org.eclipse.jdt.core.IType)8 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)8 Expression (org.eclipse.jdt.core.dom.Expression)7 Javadoc (org.eclipse.jdt.core.dom.Javadoc)7