Search in sources :

Example 1 with IPackageBinding

use of org.eclipse.jdt.core.dom.IPackageBinding in project j2objc by google.

the class BindingConverter method getElement.

public static Element getElement(IBinding binding) {
    if (binding == null) {
        return null;
    }
    JdtElement element = elementCache.get(binding);
    if (element != null) {
        return element;
    }
    if (binding instanceof GeneratedMethodBinding) {
        return ((GeneratedMethodBinding) binding).asElement();
    } else if (binding instanceof GeneratedVariableBinding) {
        return ((GeneratedVariableBinding) binding).asElement();
    } else if (binding instanceof IMethodBinding) {
        element = new JdtExecutableElement((IMethodBinding) binding);
    } else if (binding instanceof IPackageBinding) {
        element = new JdtPackageElement((IPackageBinding) binding);
    } else if (binding instanceof ITypeBinding) {
        ITypeBinding typeBinding = (ITypeBinding) binding;
        element = typeBinding.isTypeVariable() || typeBinding.isCapture() ? new JdtTypeParameterElement(typeBinding) : new JdtTypeElement(typeBinding);
    } else if (binding instanceof IVariableBinding) {
        element = JdtVariableElement.create((IVariableBinding) binding);
    } else {
        throw new AssertionError("unknown element binding: " + binding.getClass().getSimpleName());
    }
    elementCache.put(binding, element);
    return element;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) IPackageBinding(org.eclipse.jdt.core.dom.IPackageBinding)

Example 2 with IPackageBinding

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

the class SuperCallRatherThanUselessOverridingRefactoring method isMethodUsedInItsPackage.

/**
 * This method is extremely expensive.
 */
private boolean isMethodUsedInItsPackage(IMethodBinding methodBinding, MethodDeclaration node) {
    final IPackageBinding methodPackage = methodBinding.getDeclaringClass().getPackage();
    final AtomicBoolean methodIsUsedInPackage = new AtomicBoolean(false);
    final SearchRequestor requestor = new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) {
            methodIsUsedInPackage.set(true);
        }
    };
    try {
        final SearchEngine searchEngine = new SearchEngine();
        searchEngine.search(createPattern(methodBinding.getJavaElement(), REFERENCES, R_EXACT_MATCH), new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, SearchEngine.createJavaSearchScope(new IJavaElement[] { methodPackage.getJavaElement() }), requestor, ctx.getProgressMonitor());
        return methodIsUsedInPackage.get();
    } catch (CoreException e) {
        throw new UnhandledException(node, e);
    }
}
Also used : SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) UnhandledException(org.autorefactor.util.UnhandledException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IJavaElement(org.eclipse.jdt.core.IJavaElement) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) IPackageBinding(org.eclipse.jdt.core.dom.IPackageBinding)

Example 3 with IPackageBinding

use of org.eclipse.jdt.core.dom.IPackageBinding in project eclipse.jdt.ls by eclipse.

the class RedundantNullnessTypeAnnotationsFilter method getNNBDAnnotation.

// based on org.eclipse.jdt.apt.core.internal.declaration.ASTBasedDeclarationImpl.getAnnotationInstancesFromAST()
private static /* @Nullable */
IAnnotationBinding getNNBDAnnotation(ASTNode astNode, String nonNullByDefaultName) {
    List<IExtendedModifier> extendsMods = null;
    switch(astNode.getNodeType()) {
        case ASTNode.COMPILATION_UNIT:
            {
                // special case: when reaching the root of the ast, check the package annotations.
                PackageDeclaration packageDeclaration = ((CompilationUnit) astNode).getPackage();
                if (packageDeclaration != null) {
                    IPackageBinding packageBinding = packageDeclaration.resolveBinding();
                    if (packageBinding != null) {
                        for (IAnnotationBinding annotationBinding : packageBinding.getAnnotations()) {
                            ITypeBinding annotationType = annotationBinding.getAnnotationType();
                            if (annotationType != null && annotationType.getQualifiedName().equals(nonNullByDefaultName)) {
                                return annotationBinding;
                            }
                        }
                    }
                }
                return null;
            }
        case ASTNode.TYPE_DECLARATION:
        case ASTNode.ANNOTATION_TYPE_DECLARATION:
        case ASTNode.ENUM_DECLARATION:
        case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
        case ASTNode.METHOD_DECLARATION:
        case ASTNode.FIELD_DECLARATION:
        case ASTNode.ENUM_CONSTANT_DECLARATION:
            extendsMods = ((BodyDeclaration) astNode).modifiers();
            break;
        case ASTNode.VARIABLE_DECLARATION_STATEMENT:
            extendsMods = ((VariableDeclarationStatement) astNode).modifiers();
            break;
        case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
            extendsMods = ((VariableDeclarationExpression) astNode).modifiers();
            break;
        case ASTNode.SINGLE_VARIABLE_DECLARATION:
            extendsMods = ((SingleVariableDeclaration) astNode).modifiers();
            break;
        case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
            final ASTNode parent = ((VariableDeclarationFragment) astNode).getParent();
            if (parent instanceof BodyDeclaration) {
                extendsMods = ((BodyDeclaration) parent).modifiers();
            }
            break;
        default:
            return null;
    }
    if (extendsMods != null) {
        for (IExtendedModifier extMod : extendsMods) {
            if (extMod.isAnnotation()) {
                Annotation annotation = (Annotation) extMod;
                IAnnotationBinding annotationBinding = annotation.resolveAnnotationBinding();
                if (annotationBinding != null) {
                    ITypeBinding annotationType = annotationBinding.getAnnotationType();
                    if (annotationType != null && annotationType.getQualifiedName().equals(nonNullByDefaultName)) {
                        return annotationBinding;
                    }
                }
            }
        }
    }
    return null;
}
Also used : IAnnotationBinding(org.eclipse.jdt.core.dom.IAnnotationBinding) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) PackageDeclaration(org.eclipse.jdt.core.dom.PackageDeclaration) Annotation(org.eclipse.jdt.core.dom.Annotation) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) IPackageBinding(org.eclipse.jdt.core.dom.IPackageBinding)

Example 4 with IPackageBinding

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

the class BindingLabelProvider method getTypeLabel.

private static void getTypeLabel(ITypeBinding binding, long flags, StringBuffer buffer) {
    if ((flags & JavaElementLabels.T_FULLY_QUALIFIED) != 0) {
        final IPackageBinding pack = binding.getPackage();
        if (pack != null && !pack.isUnnamed()) {
            buffer.append(pack.getName());
            buffer.append('.');
        }
    }
    if ((flags & (JavaElementLabels.T_FULLY_QUALIFIED | JavaElementLabels.T_CONTAINER_QUALIFIED)) != 0) {
        final ITypeBinding declaring = binding.getDeclaringClass();
        if (declaring != null) {
            getTypeLabel(declaring, JavaElementLabels.T_CONTAINER_QUALIFIED | (flags & JavaElementLabels.P_COMPRESSED), buffer);
            buffer.append('.');
        }
        final IMethodBinding declaringMethod = binding.getDeclaringMethod();
        if (declaringMethod != null) {
            getMethodLabel(declaringMethod, 0, buffer);
            buffer.append('.');
        }
    }
    if (binding.isCapture()) {
        getTypeLabel(binding.getWildcard(), flags & JavaElementLabels.T_TYPE_PARAMETERS, buffer);
    } else if (binding.isWildcardType()) {
        buffer.append('?');
        ITypeBinding bound = binding.getBound();
        if (bound != null) {
            if (binding.isUpperbound()) {
                // $NON-NLS-1$
                buffer.append(" extends ");
            } else {
                // $NON-NLS-1$
                buffer.append(" super ");
            }
            getTypeLabel(bound, flags & JavaElementLabels.T_TYPE_PARAMETERS, buffer);
        }
    } else if (binding.isArray()) {
        getTypeLabel(binding.getElementType(), flags & JavaElementLabels.T_TYPE_PARAMETERS, buffer);
        appendDimensions(binding.getDimensions(), buffer);
    } else {
        // type variables, primitive, reftype
        String name = binding.getTypeDeclaration().getName();
        if (name.length() == 0) {
            if (binding.isEnum()) {
                buffer.append('{' + JavaElementLabels.ELLIPSIS_STRING + '}');
            } else if (binding.isAnonymous()) {
                ITypeBinding[] superInterfaces = binding.getInterfaces();
                ITypeBinding baseType;
                if (superInterfaces.length > 0) {
                    baseType = superInterfaces[0];
                } else {
                    baseType = binding.getSuperclass();
                }
                if (baseType != null) {
                    StringBuffer anonymBaseType = new StringBuffer();
                    getTypeLabel(baseType, flags & JavaElementLabels.T_TYPE_PARAMETERS, anonymBaseType);
                    buffer.append(Messages.format(JavaUIMessages.JavaElementLabels_anonym_type, anonymBaseType.toString()));
                } else {
                    buffer.append(JavaUIMessages.JavaElementLabels_anonym);
                }
            } else {
                // $NON-NLS-1$
                buffer.append("UNKNOWN");
            }
        } else {
            buffer.append(name);
        }
        if ((flags & JavaElementLabels.T_TYPE_PARAMETERS) != 0) {
            if (binding.isGenericType()) {
                getTypeParametersLabel(binding.getTypeParameters(), buffer);
            } else if (binding.isParameterizedType()) {
                getTypeArgumentsLabel(binding.getTypeArguments(), flags, buffer);
            }
        }
    }
    if ((flags & JavaElementLabels.T_POST_QUALIFIED) != 0) {
        final IMethodBinding declaringMethod = binding.getDeclaringMethod();
        final ITypeBinding declaringType = binding.getDeclaringClass();
        if (declaringMethod != null) {
            buffer.append(JavaElementLabels.CONCAT_STRING);
            getMethodLabel(declaringMethod, JavaElementLabels.T_FULLY_QUALIFIED | (flags & JavaElementLabels.P_COMPRESSED), buffer);
        } else if (declaringType != null) {
            buffer.append(JavaElementLabels.CONCAT_STRING);
            getTypeLabel(declaringType, JavaElementLabels.T_FULLY_QUALIFIED | (flags & JavaElementLabels.P_COMPRESSED), buffer);
        } else {
            final IPackageBinding pack = binding.getPackage();
            if (pack != null && !pack.isUnnamed()) {
                buffer.append(JavaElementLabels.CONCAT_STRING);
                buffer.append(pack.getName());
            }
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IPackageBinding(org.eclipse.jdt.core.dom.IPackageBinding)

Example 5 with IPackageBinding

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

the class SuperCallRatherThanUselessOverridingCleanUp method isMethodUsedInItsPackage.

/**
 * This method is extremely expensive.
 */
private boolean isMethodUsedInItsPackage(final IMethodBinding methodBinding, final MethodDeclaration visited) {
    IPackageBinding methodPackage = methodBinding.getDeclaringClass().getPackage();
    final AtomicBoolean methodIsUsedInPackage = new AtomicBoolean(false);
    SearchRequestor requestor = new SearchRequestor() {

        @Override
        public void acceptSearchMatch(final SearchMatch match) {
            methodIsUsedInPackage.lazySet(true);
        }
    };
    try {
        SearchEngine searchEngine = new SearchEngine();
        searchEngine.search(SearchPattern.createPattern(methodBinding.getJavaElement(), IJavaSearchConstants.REFERENCES, SearchPattern.R_EXACT_MATCH), new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, SearchEngine.createJavaSearchScope(new IJavaElement[] { methodPackage.getJavaElement() }), requestor, cuRewrite.getProgressMonitor());
        return methodIsUsedInPackage.get();
    } catch (CoreException e) {
        throw new UnhandledException(visited, e);
    }
}
Also used : SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) UnhandledException(org.autorefactor.util.UnhandledException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IJavaElement(org.eclipse.jdt.core.IJavaElement) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) IPackageBinding(org.eclipse.jdt.core.dom.IPackageBinding)

Aggregations

IPackageBinding (org.eclipse.jdt.core.dom.IPackageBinding)11 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)7 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)4 IJavaElement (org.eclipse.jdt.core.IJavaElement)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 UnhandledException (org.autorefactor.util.UnhandledException)2 CoreException (org.eclipse.core.runtime.CoreException)2 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)2 IAnnotationBinding (org.eclipse.jdt.core.dom.IAnnotationBinding)2 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)2 SearchEngine (org.eclipse.jdt.core.search.SearchEngine)2 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)2 SearchRequestor (org.eclipse.jdt.core.search.SearchRequestor)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1 IType (org.eclipse.jdt.core.IType)1 AST (org.eclipse.jdt.core.dom.AST)1 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 Annotation (org.eclipse.jdt.core.dom.Annotation)1 Block (org.eclipse.jdt.core.dom.Block)1