Search in sources :

Example 26 with StructuralPropertyDescriptor

use of org.eclipse.jdt.core.dom.StructuralPropertyDescriptor in project flux by eclipse.

the class Bindings method getBindingOfParentTypeContext.

/**
	 * Returns the type binding of the node's type context or null if the node is inside
	 * an annotation, type parameter, super type declaration, or Javadoc of a top level type.
	 * The result of this method is equal to the result of {@link #getBindingOfParentType(ASTNode)} for nodes in the type's body.
	 * 
	 * @param node an AST node
	 * @return the type binding of the node's parent type context, or <code>null</code>
	 */
public static ITypeBinding getBindingOfParentTypeContext(ASTNode node) {
    StructuralPropertyDescriptor lastLocation = null;
    while (node != null) {
        if (node instanceof AbstractTypeDeclaration) {
            AbstractTypeDeclaration decl = (AbstractTypeDeclaration) node;
            if (lastLocation == decl.getBodyDeclarationsProperty() || lastLocation == decl.getJavadocProperty()) {
                return decl.resolveBinding();
            } else if (decl instanceof EnumDeclaration && lastLocation == EnumDeclaration.ENUM_CONSTANTS_PROPERTY) {
                return decl.resolveBinding();
            }
        } else if (node instanceof AnonymousClassDeclaration) {
            return ((AnonymousClassDeclaration) node).resolveBinding();
        }
        lastLocation = node.getLocationInParent();
        node = node.getParent();
    }
    return null;
}
Also used : AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) EnumDeclaration(org.eclipse.jdt.core.dom.EnumDeclaration)

Example 27 with StructuralPropertyDescriptor

use of org.eclipse.jdt.core.dom.StructuralPropertyDescriptor in project processing by processing.

the class CompletionGenerator method findClosestParentNode.

protected static ASTNode findClosestParentNode(int lineNumber, ASTNode node) {
    // Base.loge("Props of " + node.getClass().getName());
    for (StructuralPropertyDescriptor prop : (Iterable<StructuralPropertyDescriptor>) node.structuralPropertiesForType()) {
        if (prop.isChildProperty() || prop.isSimpleProperty()) {
            if (node.getStructuralProperty(prop) != null) {
                //              .println(node.getStructuralProperty(prop) + " -> " + (prop));
                if (node.getStructuralProperty(prop) instanceof ASTNode) {
                    ASTNode cnode = (ASTNode) node.getStructuralProperty(prop);
                    //            log("Looking at " + getNodeAsString(cnode)+ " for line num " + lineNumber);
                    int cLineNum = ((CompilationUnit) cnode.getRoot()).getLineNumber(cnode.getStartPosition() + cnode.getLength());
                    if (getLineNumber(cnode) <= lineNumber && lineNumber <= cLineNum) {
                        return findClosestParentNode(lineNumber, cnode);
                    }
                }
            }
        } else if (prop.isChildListProperty()) {
            List<ASTNode> nodelist = (List<ASTNode>) node.getStructuralProperty(prop);
            for (ASTNode cnode : nodelist) {
                int cLineNum = ((CompilationUnit) cnode.getRoot()).getLineNumber(cnode.getStartPosition() + cnode.getLength());
                //          log("Looking at " + getNodeAsString(cnode)+ " for line num " + lineNumber);
                if (getLineNumber(cnode) <= lineNumber && lineNumber <= cLineNum) {
                    return findClosestParentNode(lineNumber, cnode);
                }
            }
        }
    }
    return node;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) List(java.util.List) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Example 28 with StructuralPropertyDescriptor

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

the class UseDiamondOperatorRefactoring method parentAllowsDiamondOperator.

private boolean parentAllowsDiamondOperator(ClassInstanceCreation node) {
    final ASTNode parentInfo = getFirstParentOfType(node, ParenthesizedExpression.class);
    final StructuralPropertyDescriptor locationInParent = parentInfo.getLocationInParent();
    switch(parentInfo.getParent().getNodeType()) {
        case ASSIGNMENT:
            return Assignment.RIGHT_HAND_SIDE_PROPERTY.equals(locationInParent);
        case METHOD_INVOCATION:
            // FIXME some of them can be refactored
            return false;
        case RETURN_STATEMENT:
            return ReturnStatement.EXPRESSION_PROPERTY.equals(locationInParent);
        case VARIABLE_DECLARATION_FRAGMENT:
            return VariableDeclarationFragment.INITIALIZER_PROPERTY.equals(locationInParent);
        default:
            return false;
    }
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Aggregations

StructuralPropertyDescriptor (org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)28 ASTNode (org.eclipse.jdt.core.dom.ASTNode)21 Expression (org.eclipse.jdt.core.dom.Expression)10 AST (org.eclipse.jdt.core.dom.AST)7 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)7 SimpleName (org.eclipse.jdt.core.dom.SimpleName)7 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)7 Block (org.eclipse.jdt.core.dom.Block)6 CastExpression (org.eclipse.jdt.core.dom.CastExpression)6 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)6 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)6 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)6 List (java.util.List)5 Assignment (org.eclipse.jdt.core.dom.Assignment)5 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)5 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)5 ForStatement (org.eclipse.jdt.core.dom.ForStatement)5 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)5 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)5 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)5