Example usage for org.eclipse.jdt.core.dom Name isSimpleName

List of usage examples for org.eclipse.jdt.core.dom Name isSimpleName

Introduction

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

Prototype

public final boolean isSimpleName() 

Source Link

Document

Returns whether this name is a simple name (SimpleName).

Usage

From source file:ch.acanda.eclipse.pmd.java.resolution.SuppressWarningsQuickFix.java

License:Open Source License

private Annotation findExistingSuppressWarningsAnnotation(final List<IExtendedModifier> modifiers) {
    for (final IExtendedModifier modifier : modifiers) {
        if (modifier.isAnnotation()) {
            final Annotation annotation = (Annotation) modifier;
            final Name typeName = annotation.getTypeName();
            if (typeName.isSimpleName() && "SuppressWarnings".equals(typeName.getFullyQualifiedName())
                    || typeName.isQualifiedName()
                            && "java.lang.SuppressWarnings".equals(typeName.getFullyQualifiedName())) {
                return annotation;
            }/*from w  w  w .  ja  v  a 2 s.  c  om*/
        }
    }
    return null;
}

From source file:com.google.devtools.j2objc.jdt.JdtJ2ObjCIncompatibleStripper.java

License:Apache License

private String getFirstComponent(Name name) {
    if (name.isSimpleName()) {
        return ((SimpleName) name).getIdentifier();
    } else {/*from  ww  w . j a  va 2s .c  om*/
        return getFirstComponent(((QualifiedName) name).getQualifier());
    }
}

From source file:com.google.devtools.j2objc.jdt.JdtJ2ObjCIncompatibleStripper.java

License:Apache License

private String getLastComponent(Name name) {
    if (name.isSimpleName()) {
        return ((SimpleName) name).getIdentifier();
    } else {//w  w  w  .  j  a v  a 2s. co m
        return ((QualifiedName) name).getName().getIdentifier();
    }
}

From source file:com.google.gdt.eclipse.core.JavaASTUtils.java

License:Open Source License

@SuppressWarnings("unchecked")
public static boolean hasSuppressWarnings(BodyDeclaration decl, String warningType) {
    List<IExtendedModifier> modifiers = decl.modifiers();
    for (IExtendedModifier modifier : modifiers) {
        if (modifier.isAnnotation()) {
            assert (modifier instanceof Annotation);
            Annotation annotation = (Annotation) modifier;

            // Get the (simple) type name of the annotation (we can't resolve the
            // annotation's type if binding resolution is disabled on our AST).
            Name annotationType = annotation.getTypeName();
            if (annotationType.isQualifiedName()) {
                annotationType = ((QualifiedName) annotationType).getName();
            }/*from  w w w  .j  a  va  2s .c  o  m*/
            assert (annotationType.isSimpleName());
            String annotationTypeName = ((SimpleName) annotationType).getIdentifier();

            // Look for @SuppressWarnings annotations
            if (annotationTypeName.equals(SuppressWarnings.class.getSimpleName())) {
                // Now extract the parameter representing the set of warnings that
                // should be suppressed
                if (annotation instanceof SingleMemberAnnotation) {
                    SingleMemberAnnotation suppressWarnings = (SingleMemberAnnotation) annotation;
                    Expression annotationValue = suppressWarnings.getValue();
                    return containsAnnotationValue(annotationValue, warningType);
                } else if (annotation instanceof NormalAnnotation) {
                    NormalAnnotation suppressWarnings = (NormalAnnotation) annotation;
                    List<MemberValuePair> annotationValues = suppressWarnings.values();
                    for (MemberValuePair annotationValue : annotationValues) {
                        SimpleName annotationValueName = annotationValue.getName();
                        if (annotationValueName.getIdentifier().equals("value")) {
                            return containsAnnotationValue(annotationValue.getValue(), warningType);
                        }
                    }
                }

                return false;
            }
        }
    }

    return false;
}

From source file:com.google.googlejavaformat.java.JavaInputAstVisitor.java

License:Apache License

/** Helper method for {@link ImportDeclaration}s, {@link Name}s, and {@link QualifiedName}s. */
private void visitName(Name node, BreakOrNot breaks) {
    sync(node);//from   w ww  .  jav a2 s.  c o  m
    if (node.isSimpleName()) {
        visit((SimpleName) node);
    } else {
        visitQualifiedName((QualifiedName) node, breaks);
    }
}

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;/*w  w w. jav a2  s .c  o m*/
    }

    // 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:com.intel.ide.eclipse.mpt.ast.UnresolvedElementsSubProcessor.java

License:Open Source License

public static void addNewTypeProposals(ICompilationUnit cu, Name refNode, int kind, int relevance,
        Map<String, LinkedCorrectionProposal> proposals) throws CoreException {
    Name node = refNode;
    //      do {/*from   ww w.java2s.c o  m*/
    String typeName = ASTNodes.getSimpleNameIdentifier(node);
    Name qualifier = null;
    // only propose to create types for qualifiers when the name starts with upper case
    boolean isPossibleName = isLikelyTypeName(typeName) || node == refNode;
    if (isPossibleName) {
        IPackageFragment enclosingPackage = null;
        IType enclosingType = null;
        if (node.isSimpleName()) {
            enclosingPackage = (IPackageFragment) cu.getParent();
            return;
            // don't suggest member type, user can select it in wizard
        } else {
            Name qualifierName = ((QualifiedName) node).getQualifier();
            IBinding binding = qualifierName.resolveBinding();
            if (binding != null && binding.isRecovered()) {
                binding = null;
            }
            if (binding instanceof ITypeBinding) {
                enclosingType = (IType) binding.getJavaElement();
            } else if (binding instanceof IPackageBinding) {
                qualifier = qualifierName;
                enclosingPackage = (IPackageFragment) binding.getJavaElement();
            } else {
                IJavaElement[] res = cu.codeSelect(qualifierName.getStartPosition(), qualifierName.getLength());
                if (res != null && res.length > 0 && res[0] instanceof IType) {
                    enclosingType = (IType) res[0];
                } else {
                    qualifier = qualifierName;
                    enclosingPackage = JavaModelUtil.getPackageFragmentRoot(cu)
                            .getPackageFragment(ASTResolving.getFullName(qualifierName));
                }
            }
        }
        int rel = relevance;
        if (enclosingPackage != null && isLikelyPackageName(enclosingPackage.getElementName())) {
            rel += 3;
        }

        if (enclosingPackage != null
                && !enclosingPackage.getCompilationUnit(typeName + JavaModelUtil.DEFAULT_CU_SUFFIX).exists()
                || enclosingType != null && !enclosingType.isReadOnly()
                        && !enclosingType.getType(typeName).exists()) { // new member type
            IJavaElement enclosing = enclosingPackage != null ? (IJavaElement) enclosingPackage : enclosingType;

            String name = node.getFullyQualifiedName();

            if ((kind & SimilarElementsRequestor.CLASSES) != 0) {
                proposals.put(name, new NewCUProposal(cu, node, NewCUProposal.K_CLASS, enclosing, rel + 3));
            } else if ((kind & SimilarElementsRequestor.INTERFACES) != 0) {
                proposals.put(name, new NewCUProposal(cu, node, NewCUProposal.K_INTERFACE, enclosing, rel + 2));
            } else if ((kind & SimilarElementsRequestor.ENUMS) != 0) {
                proposals.put(name, new NewCUProposal(cu, node, NewCUProposal.K_ENUM, enclosing, rel));
            }
        }
    }
    node = qualifier;
    //      } while (node != null);
}

From source file:edu.buffalo.cse.green.relationships.RelationshipGenerator.java

License:Open Source License

/**
 * Convenience method for removing the last segment of a name.
 * //from  w  w w  .java  2s .com
 * @param name - The name.
 * @return The name's qualifier (the part other than the simple name).
 */
public static Name getQualifier(Name name) {
    if (name.isSimpleName()) {
        return null;
    }
    QualifiedName qualName = (QualifiedName) name;
    return qualName.getQualifier();
}

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: {//  w w w . ja v  a  2  s.  co  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:io.spring.boot.development.eclipse.visitors.AstUtils.java

License:Open Source License

private static String findQualifiedTypeName(Annotation annotation) {
    Name name = annotation.getTypeName();
    if (name.isSimpleName()) {
        ITypeBinding typeBinding = annotation.resolveTypeBinding();
        if (typeBinding == null) {
            return null;
        }// w w  w . j  av  a 2s . c  o  m
        return typeBinding.getQualifiedName();
    }
    return name.getFullyQualifiedName();
}