Search in sources :

Example 6 with QualifiedName

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

the class CodeStyleFix method createToStaticAccessOperations.

private static ToStaticAccessOperation[] createToStaticAccessOperations(CompilationUnit astRoot, HashMap<ASTNode, Block> createdBlocks, IProblemLocation problem, boolean conservative) {
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    if (selectedNode == null) {
        return null;
    }
    Expression qualifier = null;
    IBinding accessBinding = null;
    if (selectedNode instanceof SimpleName) {
        selectedNode = selectedNode.getParent();
    }
    if (selectedNode instanceof QualifiedName) {
        QualifiedName name = (QualifiedName) selectedNode;
        qualifier = name.getQualifier();
        accessBinding = name.resolveBinding();
    } else if (selectedNode instanceof MethodInvocation) {
        MethodInvocation methodInvocation = (MethodInvocation) selectedNode;
        qualifier = methodInvocation.getExpression();
        accessBinding = methodInvocation.getName().resolveBinding();
    } else if (selectedNode instanceof FieldAccess) {
        FieldAccess fieldAccess = (FieldAccess) selectedNode;
        qualifier = fieldAccess.getExpression();
        accessBinding = fieldAccess.getName().resolveBinding();
    }
    if (accessBinding != null && qualifier != null) {
        if (conservative && ASTResolving.findParentStatement(qualifier) == null)
            return null;
        ToStaticAccessOperation declaring = null;
        ITypeBinding declaringTypeBinding = getDeclaringTypeBinding(accessBinding);
        if (declaringTypeBinding != null) {
            // use generic to avoid any type arguments
            declaringTypeBinding = declaringTypeBinding.getTypeDeclaration();
            declaring = new ToStaticAccessOperation(declaringTypeBinding, qualifier, createdBlocks);
        }
        ToStaticAccessOperation instance = null;
        ITypeBinding instanceTypeBinding = Bindings.normalizeTypeBinding(qualifier.resolveTypeBinding());
        if (instanceTypeBinding != null) {
            // use generic to avoid any type arguments
            instanceTypeBinding = instanceTypeBinding.getTypeDeclaration();
            if (instanceTypeBinding.getTypeDeclaration() != declaringTypeBinding) {
                instance = new ToStaticAccessOperation(instanceTypeBinding, qualifier, createdBlocks);
            }
        }
        if (declaring != null && instance != null) {
            return new ToStaticAccessOperation[] { declaring, instance };
        } else {
            return new ToStaticAccessOperation[] { declaring };
        }
    }
    return null;
}
Also used : ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) IBinding(org.eclipse.jdt.core.dom.IBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess)

Example 7 with QualifiedName

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

the class ConvertForLoopOperation method validateLengthQuery.

/*
	 * Must be one of:
	 * <ul>
	 * <li>[result].length</li>
	 * </ul>
	 */
private boolean validateLengthQuery(Expression lengthQuery) {
    if (lengthQuery instanceof QualifiedName) {
        QualifiedName qualifiedName = (QualifiedName) lengthQuery;
        SimpleName name = qualifiedName.getName();
        if (!LENGTH_QUERY.equals(name.getIdentifier()))
            return false;
        Name arrayAccess = qualifiedName.getQualifier();
        ITypeBinding accessType = arrayAccess.resolveTypeBinding();
        if (accessType == null)
            return false;
        if (!accessType.isArray())
            return false;
        IBinding arrayBinding = arrayAccess.resolveBinding();
        if (arrayBinding == null)
            return false;
        fArrayBinding = arrayBinding;
        fArrayAccess = arrayAccess;
        return true;
    } else if (lengthQuery instanceof FieldAccess) {
        FieldAccess fieldAccess = (FieldAccess) lengthQuery;
        SimpleName name = fieldAccess.getName();
        if (!LENGTH_QUERY.equals(name.getIdentifier()))
            return false;
        Expression arrayAccess = fieldAccess.getExpression();
        ITypeBinding accessType = arrayAccess.resolveTypeBinding();
        if (accessType == null)
            return false;
        if (!accessType.isArray())
            return false;
        IBinding arrayBinding = getBinding(arrayAccess);
        if (arrayBinding == null)
            return false;
        fArrayBinding = arrayBinding;
        fArrayAccess = arrayAccess;
        return true;
    }
    return false;
}
Also used : PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IBinding(org.eclipse.jdt.core.dom.IBinding) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName)

Example 8 with QualifiedName

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

the class UnusedCodeFix method hasSideEffect.

private static boolean hasSideEffect(SimpleName reference) {
    ASTNode parent = reference.getParent();
    while (parent instanceof QualifiedName) {
        parent = parent.getParent();
    }
    if (parent instanceof FieldAccess) {
        parent = parent.getParent();
    }
    ASTNode node = null;
    int nameParentType = parent.getNodeType();
    if (nameParentType == ASTNode.ASSIGNMENT) {
        Assignment assignment = (Assignment) parent;
        node = assignment.getRightHandSide();
    } else if (nameParentType == ASTNode.SINGLE_VARIABLE_DECLARATION) {
        SingleVariableDeclaration decl = (SingleVariableDeclaration) parent;
        node = decl.getInitializer();
        if (node == null)
            return false;
    } else if (nameParentType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
        node = parent;
    } else {
        return false;
    }
    ArrayList<Expression> sideEffects = new ArrayList<Expression>();
    node.accept(new SideEffectFinder(sideEffects));
    return sideEffects.size() > 0;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Example 9 with QualifiedName

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

the class ConvertAnonymousToNestedRefactoring method getAllAccessedFields.

private List<IBinding> getAllAccessedFields() {
    final List<IBinding> accessedFields = new ArrayList<IBinding>();
    ASTVisitor visitor = new ASTVisitor() {

        @Override
        public boolean visit(FieldAccess node) {
            final IVariableBinding binding = node.resolveFieldBinding();
            if (binding != null && !binding.isEnumConstant())
                accessedFields.add(binding);
            return super.visit(node);
        }

        @Override
        public boolean visit(QualifiedName node) {
            final IBinding binding = node.resolveBinding();
            if (binding != null && binding instanceof IVariableBinding) {
                IVariableBinding variable = (IVariableBinding) binding;
                if (!variable.isEnumConstant() && variable.isField())
                    accessedFields.add(binding);
            }
            return super.visit(node);
        }

        @Override
        public boolean visit(SimpleName node) {
            final IBinding binding = node.resolveBinding();
            if (binding != null && binding instanceof IVariableBinding) {
                IVariableBinding variable = (IVariableBinding) binding;
                if (!variable.isEnumConstant() && variable.isField())
                    accessedFields.add(binding);
            }
            return super.visit(node);
        }

        @Override
        public boolean visit(SuperFieldAccess node) {
            final IVariableBinding binding = node.resolveFieldBinding();
            if (binding != null && !binding.isEnumConstant())
                accessedFields.add(binding);
            return super.visit(node);
        }
    };
    fAnonymousInnerClassNode.accept(visitor);
    return accessedFields;
}
Also used : IBinding(org.eclipse.jdt.core.dom.IBinding) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 10 with QualifiedName

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

the class ExtractConstantRefactoring method checkExpression.

private RefactoringStatus checkExpression() throws JavaModelException {
    RefactoringStatus result = new RefactoringStatus();
    result.merge(checkExpressionBinding());
    if (result.hasFatalError())
        return result;
    checkAllStaticFinal();
    IExpressionFragment selectedExpression = getSelectedExpression();
    Expression associatedExpression = selectedExpression.getAssociatedExpression();
    if (associatedExpression instanceof NullLiteral)
        result.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_null_literals));
    else if (!ConstantChecks.isLoadTimeConstant(selectedExpression))
        result.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_not_load_time_constant));
    else if (associatedExpression instanceof SimpleName) {
        if (associatedExpression.getParent() instanceof QualifiedName && associatedExpression.getLocationInParent() == QualifiedName.NAME_PROPERTY || associatedExpression.getParent() instanceof FieldAccess && associatedExpression.getLocationInParent() == FieldAccess.NAME_PROPERTY)
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_select_expression);
    }
    return result;
}
Also used : IExpressionFragment(org.eclipse.jdt.internal.corext.dom.fragments.IExpressionFragment) Expression(org.eclipse.jdt.core.dom.Expression) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) NullLiteral(org.eclipse.jdt.core.dom.NullLiteral)

Aggregations

QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)30 SimpleName (org.eclipse.jdt.core.dom.SimpleName)22 ASTNode (org.eclipse.jdt.core.dom.ASTNode)18 Name (org.eclipse.jdt.core.dom.Name)13 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)12 Expression (org.eclipse.jdt.core.dom.Expression)10 IBinding (org.eclipse.jdt.core.dom.IBinding)9 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)9 SimpleType (org.eclipse.jdt.core.dom.SimpleType)9 ArrayList (java.util.ArrayList)7 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)7 Type (org.eclipse.jdt.core.dom.Type)6 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)5 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)5 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)5 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)5 List (java.util.List)4 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)4 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)4