Search in sources :

Example 16 with IAnnotationBinding

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

the class JavadocFinder method getAnnotations.

private String getAnnotations(IJavaElement element, ITypeRoot editorInputElement, IRegion hoverRegion) throws URISyntaxException, JavaModelException {
    if (!(element instanceof IPackageFragment)) {
        if (!(element instanceof IAnnotatable))
            return null;
        if (((IAnnotatable) element).getAnnotations().length == 0)
            return null;
    }
    IBinding binding = null;
    //TODO
    //getHoveredASTNode(editorInputElement, hoverRegion);
    ASTNode node = null;
    if (node == null) {
    //todo use ast ported parser,that uses our java model
    //            ASTParser p = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
    //            p.setProject(element.getJavaProject());
    //            p.setBindingsRecovery(true);
    //            try {
    //                binding = p.createBindings(new IJavaElement[]{element}, null)[0];
    //            } catch (OperationCanceledException e) {
    //                return null;
    //            }
    } else {
        binding = resolveBinding(node);
    }
    if (binding == null)
        return null;
    IAnnotationBinding[] annotations = binding.getAnnotations();
    if (annotations.length == 0)
        return null;
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < annotations.length; i++) {
        //TODO: skip annotations that don't have an @Documented annotation?
        addAnnotation(buf, element, annotations[i]);
        //$NON-NLS-1$
        buf.append("<br>");
    }
    return buf.toString();
}
Also used : IAnnotatable(org.eclipse.jdt.core.IAnnotatable) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IAnnotationBinding(org.eclipse.jdt.core.dom.IAnnotationBinding) IBinding(org.eclipse.jdt.core.dom.IBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 17 with IAnnotationBinding

use of org.eclipse.jdt.core.dom.IAnnotationBinding in project bndtools by bndtools.

the class MemberValuePairLocationRetriever method visit.

/**
 * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.SingleMemberAnnotation)
 */
@Override
public boolean visit(SingleMemberAnnotation node) {
    final IAnnotationBinding annotationBinding = node.resolveAnnotationBinding();
    if (annotationBinding != null) {
        final String nodeName = annotationBinding.getAnnotationType().getQualifiedName();
        boolean match;
        try {
            match = this.annotationNameMatch.test(nodeName);
        } catch (Exception e) {
            match = false;
        }
        if (match) {
            this.locatedSourceRange = new SourceRange(node.getValue().getStartPosition(), node.getValue().getLength());
        }
    }
    return false;
}
Also used : IAnnotationBinding(org.eclipse.jdt.core.dom.IAnnotationBinding) ISourceRange(org.eclipse.jdt.core.ISourceRange) SourceRange(org.eclipse.jdt.core.SourceRange)

Example 18 with IAnnotationBinding

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

the class StubUtility2 method findNullnessDefault.

// TODO Remove this by addressing https://bugs.eclipse.org/bugs/show_bug.cgi?id=531511
private static IAnnotationBinding findNullnessDefault(IBinding contextBinding, IJavaProject javaProject) {
    if (JavaCore.ENABLED.equals(javaProject.getOption(JavaCore.COMPILER_ANNOTATION_NULL_ANALYSIS, true))) {
        String annotationName = javaProject.getOption(JavaCore.COMPILER_NONNULL_BY_DEFAULT_ANNOTATION_NAME, true);
        while (contextBinding != null) {
            for (IAnnotationBinding annotation : contextBinding.getAnnotations()) {
                ITypeBinding annotationType = annotation.getAnnotationType();
                if (annotationType != null && annotationType.getQualifiedName().equals(annotationName)) {
                    return annotation;
                }
            }
            // travel out:
            switch(contextBinding.getKind()) {
                case IBinding.METHOD:
                    IMethodBinding methodBinding = (IMethodBinding) contextBinding;
                    contextBinding = methodBinding.getDeclaringMember();
                    if (contextBinding == null) {
                        contextBinding = methodBinding.getDeclaringClass();
                    }
                    break;
                case IBinding.VARIABLE:
                    IVariableBinding variableBinding = (IVariableBinding) contextBinding;
                    contextBinding = variableBinding.getDeclaringMethod();
                    if (contextBinding == null) {
                        contextBinding = variableBinding.getDeclaringClass();
                    }
                    break;
                case IBinding.TYPE:
                    ITypeBinding currentClass = (ITypeBinding) contextBinding;
                    contextBinding = currentClass.getDeclaringMember();
                    if (contextBinding == null) {
                        contextBinding = currentClass.getDeclaringMethod();
                        if (contextBinding == null) {
                            contextBinding = currentClass.getDeclaringClass();
                            if (contextBinding == null) {
                                contextBinding = currentClass.getPackage();
                            }
                        }
                    }
                    break;
                default:
                    contextBinding = null;
                    break;
            }
        }
    }
    return null;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) IAnnotationBinding(org.eclipse.jdt.core.dom.IAnnotationBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 19 with IAnnotationBinding

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

the class StubUtility2 method getImplementationModifiers.

// public static DelegateEntry[] getDelegatableMethods(ITypeBinding binding) {
// final List<DelegateEntry> tuples= new ArrayList<>();
// final List<IMethodBinding> declared= new ArrayList<>();
// IMethodBinding[] typeMethods= binding.getDeclaredMethods();
// for (int index= 0; index < typeMethods.length; index++) {
// declared.add(typeMethods[index]);
// }
// IVariableBinding[] typeFields= binding.getDeclaredFields();
// for (int index= 0; index < typeFields.length; index++) {
// IVariableBinding fieldBinding= typeFields[index];
// if (fieldBinding.isField() && !fieldBinding.isEnumConstant() && !fieldBinding.isSynthetic()) {
// getDelegatableMethods(new ArrayList<>(declared), fieldBinding, fieldBinding.getType(), binding, tuples);
// }
// }
// // list of tuple<IVariableBinding, IMethodBinding>
// return tuples.toArray(new DelegateEntry[tuples.size()]);
// }
// private static void getDelegatableMethods(List<IMethodBinding> methods, IVariableBinding fieldBinding, ITypeBinding typeBinding, ITypeBinding binding, List<DelegateEntry> result) {
// boolean match= false;
// if (typeBinding.isTypeVariable()) {
// ITypeBinding[] typeBounds= typeBinding.getTypeBounds();
// if (typeBounds.length > 0) {
// for (int i= 0; i < typeBounds.length; i++) {
// getDelegatableMethods(methods, fieldBinding, typeBounds[i], binding, result);
// }
// } else {
// ITypeBinding objectBinding= Bindings.findTypeInHierarchy(binding, "java.lang.Object"); //$NON-NLS-1$
// if (objectBinding != null) {
// getDelegatableMethods(methods, fieldBinding, objectBinding, binding, result);
// }
// }
// } else {
// IMethodBinding[] candidates= getDelegateCandidates(typeBinding, binding);
// for (int index= 0; index < candidates.length; index++) {
// match= false;
// final IMethodBinding methodBinding= candidates[index];
// for (int offset= 0; offset < methods.size() && !match; offset++) {
// if (Bindings.areOverriddenMethods(methods.get(offset), methodBinding)) {
// match= true;
// }
// }
// if (!match) {
// result.add(new DelegateEntry(methodBinding, fieldBinding));
// methods.add(methodBinding);
// }
// }
// final ITypeBinding superclass= typeBinding.getSuperclass();
// if (superclass != null) {
// getDelegatableMethods(methods, fieldBinding, superclass, binding, result);
// }
// ITypeBinding[] superInterfaces= typeBinding.getInterfaces();
// for (int offset= 0; offset < superInterfaces.length; offset++) {
// getDelegatableMethods(methods, fieldBinding, superInterfaces[offset], binding, result);
// }
// }
// }
// private static IMethodBinding[] getDelegateCandidates(ITypeBinding binding, ITypeBinding hierarchy) {
// List<IMethodBinding> allMethods= new ArrayList<>();
// boolean isInterface= binding.isInterface();
// IMethodBinding[] typeMethods= binding.getDeclaredMethods();
// for (int index= 0; index < typeMethods.length; index++) {
// final int modifiers= typeMethods[index].getModifiers();
// if (!typeMethods[index].isConstructor() && !Modifier.isStatic(modifiers) && (isInterface || Modifier.isPublic(modifiers))) {
// IMethodBinding result= Bindings.findOverriddenMethodInHierarchy(hierarchy, typeMethods[index]);
// if (result != null && Flags.isFinal(result.getModifiers())) {
// continue;
// }
// ITypeBinding[] parameterBindings= typeMethods[index].getParameterTypes();
// boolean upper= false;
// for (int offset= 0; offset < parameterBindings.length; offset++) {
// if (parameterBindings[offset].isWildcardType() && parameterBindings[offset].isUpperbound()) {
// upper= true;
// }
// }
// if (!upper) {
// allMethods.add(typeMethods[index]);
// }
// }
// }
// return allMethods.toArray(new IMethodBinding[allMethods.size()]);
// }
private static List<IExtendedModifier> getImplementationModifiers(AST ast, IMethodBinding method, boolean inInterface, ImportRewrite importRewrite, ImportRewriteContext context, IAnnotationBinding defaultNullness) throws JavaModelException {
    IJavaProject javaProject = importRewrite.getCompilationUnit().getJavaProject();
    int modifiers = method.getModifiers();
    if (inInterface) {
        modifiers = modifiers & ~Modifier.PROTECTED & ~Modifier.PUBLIC;
        if (Modifier.isAbstract(modifiers) && JavaModelUtil.is18OrHigher(javaProject)) {
            modifiers = modifiers | Modifier.DEFAULT;
        }
    } else {
        modifiers = modifiers & ~Modifier.DEFAULT;
    }
    modifiers = modifiers & ~Modifier.ABSTRACT & ~Modifier.NATIVE & ~Modifier.PRIVATE;
    IAnnotationBinding[] annotations = method.getAnnotations();
    if (modifiers != Modifier.NONE && annotations.length > 0) {
        // need an AST of the source method to preserve order of modifiers
        IMethod iMethod = (IMethod) method.getJavaElement();
        if (iMethod != null && isSourceAvailable(iMethod)) {
            ASTParser parser = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL);
            parser.setSource(iMethod.getTypeRoot());
            parser.setIgnoreMethodBodies(true);
            CompilationUnit otherCU = (CompilationUnit) parser.createAST(null);
            ASTNode otherMethod = NodeFinder.perform(otherCU, iMethod.getSourceRange());
            if (otherMethod instanceof MethodDeclaration) {
                MethodDeclaration otherMD = (MethodDeclaration) otherMethod;
                ArrayList<IExtendedModifier> result = new ArrayList<>();
                List<IExtendedModifier> otherModifiers = otherMD.modifiers();
                for (IExtendedModifier otherModifier : otherModifiers) {
                    if (otherModifier instanceof Modifier) {
                        int otherFlag = ((Modifier) otherModifier).getKeyword().toFlagValue();
                        if ((otherFlag & modifiers) != 0) {
                            modifiers = ~otherFlag & modifiers;
                            result.addAll(ast.newModifiers(otherFlag));
                        }
                    } else {
                        Annotation otherAnnotation = (Annotation) otherModifier;
                        String n = otherAnnotation.getTypeName().getFullyQualifiedName();
                        for (IAnnotationBinding annotation : annotations) {
                            ITypeBinding otherAnnotationType = annotation.getAnnotationType();
                            String qn = otherAnnotationType.getQualifiedName();
                            if (qn.endsWith(n) && (qn.length() == n.length() || qn.charAt(qn.length() - n.length() - 1) == '.')) {
                                if (StubUtility2.isCopyOnInheritAnnotation(otherAnnotationType, javaProject, defaultNullness)) {
                                    result.add(importRewrite.addAnnotation(annotation, ast, context));
                                }
                                break;
                            }
                        }
                    }
                }
                result.addAll(ASTNodeFactory.newModifiers(ast, modifiers));
                return result;
            }
        }
    }
    ArrayList<IExtendedModifier> result = new ArrayList<>();
    for (IAnnotationBinding annotation : annotations) {
        if (StubUtility2.isCopyOnInheritAnnotation(annotation.getAnnotationType(), javaProject, defaultNullness)) {
            result.add(importRewrite.addAnnotation(annotation, ast, context));
        }
    }
    result.addAll(ASTNodeFactory.newModifiers(ast, modifiers));
    return result;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ArrayList(java.util.ArrayList) Annotation(org.eclipse.jdt.core.dom.Annotation) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) IJavaProject(org.eclipse.jdt.core.IJavaProject) IAnnotationBinding(org.eclipse.jdt.core.dom.IAnnotationBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IMethod(org.eclipse.jdt.core.IMethod) ASTParser(org.eclipse.jdt.core.dom.ASTParser) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier)

Example 20 with IAnnotationBinding

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

the class ASTNodeFactory method newCreationType.

/**
 * Create a Type suitable as the creationType in a ClassInstanceCreation expression.
 * @param ast The AST to create the nodes for.
 * @param typeBinding binding representing the given class type
 * @param importRewrite the import rewrite to use
 * @param importContext the import context used to determine which (null) annotations to consider
 * @return a Type suitable as the creationType in a ClassInstanceCreation expression.
 */
public static Type newCreationType(AST ast, ITypeBinding typeBinding, ImportRewrite importRewrite, ImportRewriteContext importContext) {
    if (typeBinding.isParameterizedType()) {
        Type baseType = newCreationType(ast, typeBinding.getTypeDeclaration(), importRewrite, importContext);
        IAnnotationBinding[] typeAnnotations = importContext.removeRedundantTypeAnnotations(typeBinding.getTypeAnnotations(), TypeLocation.NEW, typeBinding);
        for (IAnnotationBinding typeAnnotation : typeAnnotations) {
            ((AnnotatableType) baseType).annotations().add(importRewrite.addAnnotation(typeAnnotation, ast, importContext));
        }
        ParameterizedType parameterizedType = ast.newParameterizedType(baseType);
        for (ITypeBinding typeArgument : typeBinding.getTypeArguments()) {
            typeArgument = StubUtility2.replaceWildcardsAndCaptures(typeArgument);
            parameterizedType.typeArguments().add(importRewrite.addImport(typeArgument, ast, importContext, TypeLocation.TYPE_ARGUMENT));
        }
        return parameterizedType;
    } else {
        return importRewrite.addImport(typeBinding, ast, importContext, TypeLocation.NEW);
    }
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) Type(org.eclipse.jdt.core.dom.Type) AnnotatableType(org.eclipse.jdt.core.dom.AnnotatableType) UnionType(org.eclipse.jdt.core.dom.UnionType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) IAnnotationBinding(org.eclipse.jdt.core.dom.IAnnotationBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Aggregations

IAnnotationBinding (org.eclipse.jdt.core.dom.IAnnotationBinding)21 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)14 ArrayType (org.eclipse.jdt.core.dom.ArrayType)6 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)6 Type (org.eclipse.jdt.core.dom.Type)6 Annotation (org.eclipse.jdt.core.dom.Annotation)5 ArrayList (java.util.ArrayList)4 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 Dimension (org.eclipse.jdt.core.dom.Dimension)4 InternalEList (org.eclipse.emf.ecore.util.InternalEList)3 IJavaProject (org.eclipse.jdt.core.IJavaProject)3 IExtendedModifier (org.eclipse.jdt.core.dom.IExtendedModifier)3 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)3 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)3 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)3 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)3 UnionType (org.eclipse.jdt.core.dom.UnionType)3 List (java.util.List)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 IJavaElement (org.eclipse.jdt.core.IJavaElement)2