Example usage for org.eclipse.jdt.core.dom FieldAccess NAME_PROPERTY

List of usage examples for org.eclipse.jdt.core.dom FieldAccess NAME_PROPERTY

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom FieldAccess NAME_PROPERTY.

Prototype

ChildPropertyDescriptor NAME_PROPERTY

To view the source code for org.eclipse.jdt.core.dom FieldAccess NAME_PROPERTY.

Click Source Link

Document

The "name" structural property of this node type (child type: SimpleName ).

Usage

From source file:com.intel.ide.eclipse.mpt.ast.UnresolvedElementsSubProcessor.java

License:Open Source License

public static void getVariableProposals(IInvocationContext context, IProblemLocation problem,
        IVariableBinding resolvedField, Map<String, Map> proposals) throws CoreException {

    ICompilationUnit cu = context.getCompilationUnit();

    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveredNode(astRoot);
    if (selectedNode == null) {
        return;//from  ww  w .  j a va 2s  .  c om
    }

    // type that defines the variable
    ITypeBinding binding = null;
    ITypeBinding declaringTypeBinding = Bindings.getBindingOfParentTypeContext(selectedNode);
    if (declaringTypeBinding == null) {
        return;
    }

    // possible type kind of the node
    boolean suggestVariableProposals = true;
    while (selectedNode instanceof ParenthesizedExpression) {
        selectedNode = ((ParenthesizedExpression) selectedNode).getExpression();
    }

    Name node = null;

    switch (selectedNode.getNodeType()) {
    case ASTNode.SIMPLE_NAME:
        node = (SimpleName) selectedNode;

        if (!isVariableProposalSafe(node))
            return;
        ASTNode parent = node.getParent();
        StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
        if (locationInParent == MethodInvocation.EXPRESSION_PROPERTY) {
        } else if (locationInParent == FieldAccess.NAME_PROPERTY) {
            Expression expression = ((FieldAccess) parent).getExpression();
            if (expression != null) {
                binding = expression.resolveTypeBinding();
                if (binding == null) {
                    node = null;
                }
            }
        } else if (parent instanceof SimpleType) {
            suggestVariableProposals = false;
        } else if (parent instanceof QualifiedName) {
            Name qualifier = ((QualifiedName) parent).getQualifier();
            if (qualifier != node) {
                binding = qualifier.resolveTypeBinding();
            } else {
            }
            ASTNode outerParent = parent.getParent();
            while (outerParent instanceof QualifiedName) {
                outerParent = outerParent.getParent();
            }
            if (outerParent instanceof SimpleType) {
                suggestVariableProposals = false;
            }
        } else if (locationInParent == SwitchCase.EXPRESSION_PROPERTY) {
            ITypeBinding switchExp = ((SwitchStatement) node.getParent().getParent()).getExpression()
                    .resolveTypeBinding();
            if (switchExp != null && switchExp.isEnum()) {
                binding = switchExp;
            }
        } else if (locationInParent == SuperFieldAccess.NAME_PROPERTY) {
            binding = declaringTypeBinding.getSuperclass();
        }
        break;
    case ASTNode.QUALIFIED_NAME:
        QualifiedName qualifierName = (QualifiedName) selectedNode;
        ITypeBinding qualifierBinding = qualifierName.getQualifier().resolveTypeBinding();
        if (qualifierBinding != null) {
            node = qualifierName.getName();
            binding = qualifierBinding;
        } else {
            node = qualifierName.getQualifier();
            suggestVariableProposals = node.isSimpleName();
        }
        if (selectedNode.getParent() instanceof SimpleType) {
            suggestVariableProposals = false;
        }
        break;
    case ASTNode.FIELD_ACCESS:
        FieldAccess access = (FieldAccess) selectedNode;
        Expression expression = access.getExpression();
        if (expression != null) {
            binding = expression.resolveTypeBinding();
            if (binding != null) {
                node = access.getName();
            }
        }
        break;
    case ASTNode.SUPER_FIELD_ACCESS:
        binding = declaringTypeBinding.getSuperclass();
        node = ((SuperFieldAccess) selectedNode).getName();
        break;
    default:
    }

    if (node == null) {
        return;
    }

    if (!suggestVariableProposals) {
        return;
    }

    SimpleName simpleName = node.isSimpleName() ? (SimpleName) node : ((QualifiedName) node).getName();
    boolean isWriteAccess = ASTResolving.isWriteAccess(node);

    if (resolvedField == null || binding == null
            || resolvedField.getDeclaringClass() != binding.getTypeDeclaration()
                    && Modifier.isPrivate(resolvedField.getModifiers())) {

        // new fields
        addNewFieldProposals(cu, astRoot, binding, declaringTypeBinding, simpleName, isWriteAccess, proposals);
    }
}

From source file:de.ovgu.cide.export.physical.ahead.MethodObjectHelper.java

License:Open Source License

/**
 * replaces all implicit access to "this" with explicit access (i.e. writes
 * a this. before every method invocation or field access where possible)
 * /*from w w w. j a  v a 2s  .c  o  m*/
 * 
 * @param targetMethod
 * @param colorManager
 */
public static void makeThisAccessExplicit(final MethodDeclaration targetMethod) {
    final AST ast = targetMethod.getAST();
    final TypeDeclaration thisClass = RefactoringUtils.getContainingType(targetMethod);
    targetMethod.accept(new ASTVisitor() {
        @Override
        public boolean visit(MethodInvocation node) {
            if (node.getExpression() == null)
                node.setExpression(ast.newThisExpression());
            return super.visit(node);
        }

        @Override
        public boolean visit(FieldAccess node) {
            if (node.getExpression() == null)
                node.setExpression(ast.newThisExpression());
            return super.visit(node);
        }

        @Override
        public void endVisit(SimpleName node) {
            if (node.getLocationInParent() == FieldAccess.NAME_PROPERTY)
                return;
            if (node.getLocationInParent() == QualifiedName.NAME_PROPERTY)
                return;

            IBinding binding = ((Name) node).resolveBinding();
            if (binding instanceof IVariableBinding) {
                IVariableBinding vBinding = (IVariableBinding) binding;
                if (vBinding.isField()) {
                    ITypeBinding bc = vBinding.getDeclaringClass();
                    if (thisClass.resolveBinding() == bc) {
                        /*
                         * workaround if a field access is represented by a
                         * qualified name (this qualified name must be first
                         * replaced by a field access
                         */
                        if (node.getLocationInParent() == QualifiedName.QUALIFIER_PROPERTY) {
                            QualifiedName parent = (QualifiedName) node.getParent();
                            parent.setQualifier(ast.newSimpleName("notUsed"));
                            FieldAccess fieldAccess = ast.newFieldAccess();
                            RefactoringUtils.replaceASTNode(parent, fieldAccess);
                            fieldAccess.setExpression(node);
                            fieldAccess.setName(ast.newSimpleName(parent.getName().getIdentifier()));
                        }

                        FieldAccess fieldAccess = ast.newFieldAccess();
                        RefactoringUtils.replaceASTNode(node, fieldAccess);
                        fieldAccess.setExpression(ast.newThisExpression());
                        fieldAccess.setName(node);
                    }

                }
            }

        }
    });
}

From source file:edu.illinois.jflow.core.transformations.code.ExtractClosureAnalyzer.java

License:Open Source License

@Override
public void endVisit(CompilationUnit node) {
    RefactoringStatus status = getStatus();
    superCall: {/*from  ww w  .  jav a 2 s. c o m*/
        if (status.hasFatalError())
            break superCall;
        if (!hasSelectedNodes()) {
            ASTNode coveringNode = getLastCoveringNode();
            if (coveringNode instanceof Block && coveringNode.getParent() instanceof MethodDeclaration) {
                MethodDeclaration methodDecl = (MethodDeclaration) coveringNode.getParent();
                Message[] messages = ASTNodes.getMessages(methodDecl, ASTNodes.NODE_ONLY);
                if (messages.length > 0) {
                    status.addFatalError(
                            Messages.format(JFlowRefactoringCoreMessages.ExtractClosureAnalyzer_compile_errors,
                                    BasicElementLabels
                                            .getJavaElementName(methodDecl.getName().getIdentifier())),
                            JavaStatusContext.create(fCUnit, methodDecl));
                    break superCall;
                }
            }
            status.addFatalError(JFlowRefactoringCoreMessages.ExtractClosureAnalyzer_only_method_body);
            break superCall;
        }
        fEnclosingBodyDeclaration = (BodyDeclaration) ASTNodes.getParent(getFirstSelectedNode(),
                BodyDeclaration.class);
        if (fEnclosingBodyDeclaration == null
                || (fEnclosingBodyDeclaration.getNodeType() != ASTNode.METHOD_DECLARATION
                        && fEnclosingBodyDeclaration.getNodeType() != ASTNode.FIELD_DECLARATION
                        && fEnclosingBodyDeclaration.getNodeType() != ASTNode.INITIALIZER)) {
            status.addFatalError(JFlowRefactoringCoreMessages.ExtractClosureAnalyzer_only_method_body);
            break superCall;
        } else if (ASTNodes.getEnclosingType(fEnclosingBodyDeclaration) == null) {
            status.addFatalError(
                    JFlowRefactoringCoreMessages.ExtractClosureAnalyzer_compile_errors_no_parent_binding);
            break superCall;
        } else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
            fEnclosingMethodBinding = ((MethodDeclaration) fEnclosingBodyDeclaration).resolveBinding();
        }
        if (!isSingleExpressionOrStatementSet()) {
            status.addFatalError(JFlowRefactoringCoreMessages.ExtractClosureAnalyzer_single_expression_or_set);
            break superCall;
        }
        if (isExpressionSelected()) {
            ASTNode expression = getFirstSelectedNode();
            if (expression instanceof Name) {
                Name name = (Name) expression;
                if (name.resolveBinding() instanceof ITypeBinding) {
                    status.addFatalError(
                            JFlowRefactoringCoreMessages.ExtractClosureAnalyzer_cannot_extract_type_reference);
                    break superCall;
                }
                if (name.resolveBinding() instanceof IMethodBinding) {
                    status.addFatalError(
                            JFlowRefactoringCoreMessages.ExtractClosureAnalyzer_cannot_extract_method_name_reference);
                    break superCall;
                }
                if (name.resolveBinding() instanceof IVariableBinding) {
                    StructuralPropertyDescriptor locationInParent = name.getLocationInParent();
                    if (locationInParent == QualifiedName.NAME_PROPERTY
                            || (locationInParent == FieldAccess.NAME_PROPERTY
                                    && !(((FieldAccess) name.getParent())
                                            .getExpression() instanceof ThisExpression))) {
                        status.addFatalError(
                                JFlowRefactoringCoreMessages.ExtractClosureAnalyzer_cannot_extract_part_of_qualified_name);
                        break superCall;
                    }
                }
                if (name.isSimpleName() && ((SimpleName) name).isDeclaration()) {
                    status.addFatalError(
                            JFlowRefactoringCoreMessages.ExtractClosureAnalyzer_cannot_extract_name_in_declaration);
                    break superCall;
                }
            }
            fForceStatic = ASTNodes.getParent(expression, ASTNode.SUPER_CONSTRUCTOR_INVOCATION) != null
                    || ASTNodes.getParent(expression, ASTNode.CONSTRUCTOR_INVOCATION) != null;
        }
        status.merge(LocalTypeAnalyzer.perform(fEnclosingBodyDeclaration, getSelection()));
        computeLastStatementSelected();
    }
    super.endVisit(node);
}

From source file:org.eclipse.ajdt.internal.ui.editor.quickfix.UnresolvedElementsSubProcessor.java

License:Open Source License

public static void getVariableProposals(IInvocationContext context, IProblemLocation problem,
        IVariableBinding resolvedField, Collection proposals) throws CoreException {

    ICompilationUnit cu = context.getCompilationUnit();

    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveredNode(astRoot);
    if (selectedNode == null) {
        return;/*  w  w  w.  ja  v a  2  s  . com*/
    }

    // type that defines the variable
    ITypeBinding binding = null;
    ITypeBinding declaringTypeBinding = Bindings.getBindingOfParentTypeContext(selectedNode);
    if (declaringTypeBinding == null) {
        return;
    }

    // possible type kind of the node
    boolean suggestVariableProposals = true;
    int typeKind = 0;

    while (selectedNode instanceof ParenthesizedExpression) {
        selectedNode = ((ParenthesizedExpression) selectedNode).getExpression();
    }

    Name node = null;

    switch (selectedNode.getNodeType()) {
    case ASTNode.SIMPLE_NAME:
        node = (SimpleName) selectedNode;
        ASTNode parent = node.getParent();
        StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
        if (locationInParent == MethodInvocation.EXPRESSION_PROPERTY) {
            typeKind = SimilarElementsRequestor.CLASSES;
        } else if (locationInParent == FieldAccess.NAME_PROPERTY) {
            Expression expression = ((FieldAccess) parent).getExpression();
            if (expression != null) {
                binding = expression.resolveTypeBinding();
                if (binding == null) {
                    node = null;
                }
            }
        } else if (parent instanceof SimpleType) {
            suggestVariableProposals = false;
            typeKind = SimilarElementsRequestor.REF_TYPES;
        } else if (parent instanceof QualifiedName) {
            Name qualifier = ((QualifiedName) parent).getQualifier();
            if (qualifier != node) {
                binding = qualifier.resolveTypeBinding();
            } else {
                typeKind = SimilarElementsRequestor.REF_TYPES & ~SimilarElementsRequestor.VARIABLES;
            }
            ASTNode outerParent = parent.getParent();
            while (outerParent instanceof QualifiedName) {
                outerParent = outerParent.getParent();
            }
            if (outerParent instanceof SimpleType) {
                typeKind = SimilarElementsRequestor.REF_TYPES & ~SimilarElementsRequestor.VARIABLES;
                suggestVariableProposals = false;
            }
        } else if (locationInParent == SwitchCase.EXPRESSION_PROPERTY) {
            ITypeBinding switchExp = ((SwitchStatement) node.getParent().getParent()).getExpression()
                    .resolveTypeBinding();
            if (switchExp != null && switchExp.isEnum()) {
                binding = switchExp;
            }
        } else if (locationInParent == SuperFieldAccess.NAME_PROPERTY) {
            binding = declaringTypeBinding.getSuperclass();
        }
        break;
    case ASTNode.QUALIFIED_NAME:
        QualifiedName qualifierName = (QualifiedName) selectedNode;
        ITypeBinding qualifierBinding = qualifierName.getQualifier().resolveTypeBinding();
        if (qualifierBinding != null) {
            node = qualifierName.getName();
            binding = qualifierBinding;
        } else {
            node = qualifierName.getQualifier();
            typeKind = SimilarElementsRequestor.REF_TYPES & ~SimilarElementsRequestor.VARIABLES;
            suggestVariableProposals = node.isSimpleName();
        }
        if (selectedNode.getParent() instanceof SimpleType) {
            typeKind = SimilarElementsRequestor.REF_TYPES & ~SimilarElementsRequestor.VARIABLES;
            suggestVariableProposals = false;
        }
        break;
    case ASTNode.FIELD_ACCESS:
        FieldAccess access = (FieldAccess) selectedNode;
        Expression expression = access.getExpression();
        if (expression != null) {
            binding = expression.resolveTypeBinding();
            if (binding != null) {
                node = access.getName();
            }
        }
        break;
    case ASTNode.SUPER_FIELD_ACCESS:
        binding = declaringTypeBinding.getSuperclass();
        node = ((SuperFieldAccess) selectedNode).getName();
        break;
    default:
    }

    if (node == null) {
        return;
    }

    // add type proposals
    if (typeKind != 0) {
        if (!JavaModelUtil.is50OrHigher(cu.getJavaProject())) {
            typeKind = typeKind & ~(SimilarElementsRequestor.ANNOTATIONS | SimilarElementsRequestor.ENUMS
                    | SimilarElementsRequestor.VARIABLES);
        }

        int relevance = Character.isUpperCase(ASTNodes.getSimpleNameIdentifier(node).charAt(0)) ? 5 : -2;
        addSimilarTypeProposals(typeKind, cu, node, relevance + 1, proposals);
        addNewTypeProposals(cu, node, typeKind, relevance, proposals);
    }

    if (!suggestVariableProposals) {
        return;
    }

    SimpleName simpleName = node.isSimpleName() ? (SimpleName) node : ((QualifiedName) node).getName();
    boolean isWriteAccess = ASTResolving.isWriteAccess(node);

    // similar variables
    addSimilarVariableProposals(cu, astRoot, binding, simpleName, isWriteAccess, proposals);

    if (resolvedField == null || binding == null
            || resolvedField.getDeclaringClass() != binding.getTypeDeclaration()
                    && Modifier.isPrivate(resolvedField.getModifiers())) {

        // new fields
        addNewFieldProposals(cu, astRoot, binding, declaringTypeBinding, simpleName, isWriteAccess, proposals);

        // new parameters and local variables
        if (binding == null) {
            addNewVariableProposals(cu, node, simpleName, proposals);
        }
    }
}

From source file:org.eclipse.wb.internal.core.utils.ast.AstNodeUtils.java

License:Open Source License

/**
 * @return <code>true</code> if given {@link ASTNode} is single variable {@link SimpleName} or
 *         {@link FieldAccess} like "this.fieldName".
 *///from ww  w.  j  av a 2  s. c o m
public static boolean isVariable(ASTNode variable) {
    // FieldAccess
    if (variable instanceof FieldAccess) {
        FieldAccess fieldAccess = (FieldAccess) variable;
        return fieldAccess.getExpression() instanceof ThisExpression;
    }
    // SimpleName
    if (variable instanceof SimpleName) {
        StructuralPropertyDescriptor locationInParent = variable.getLocationInParent();
        if (locationInParent == MethodInvocation.NAME_PROPERTY || locationInParent == SimpleType.NAME_PROPERTY
                || locationInParent == FieldAccess.NAME_PROPERTY
                || locationInParent == QualifiedName.NAME_PROPERTY
                || locationInParent == MethodDeclaration.NAME_PROPERTY
                || locationInParent == TypeDeclaration.NAME_PROPERTY) {
            return false;
        }
        // variable has binding
        return getVariableBinding(variable) != null;
    }
    // unknown ASTNode
    return false;
}