Search in sources :

Example 11 with IAnnotationBinding

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

the class JdtBasedTypeFactory method createAnnotationValues.

protected void createAnnotationValues(IBinding annotated, JvmAnnotationTarget result) {
    try {
        resolveAnnotations.start();
        IAnnotationBinding[] annotationBindings = annotated.getAnnotations();
        if (annotationBindings.length != 0) {
            InternalEList<JvmAnnotationReference> annotations = (InternalEList<JvmAnnotationReference>) result.getAnnotations();
            for (IAnnotationBinding annotation : annotationBindings) {
                annotations.addUnique(createAnnotationReference(annotation));
            }
        }
    } catch (AbortCompilation aborted) {
        if (aborted.problem.getID() == IProblem.IsClassPathCorrect) {
        // ignore
        } else {
            log.info("Couldn't resolve annotations of " + annotated, aborted);
        }
    } finally {
        resolveAnnotations.stop();
    }
}
Also used : IAnnotationBinding(org.eclipse.jdt.core.dom.IAnnotationBinding) InternalEList(org.eclipse.emf.ecore.util.InternalEList) AbortCompilation(org.eclipse.jdt.internal.compiler.problem.AbortCompilation) JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference)

Example 12 with IAnnotationBinding

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

the class BindingConverter method getPackageElement.

/**
   * JDT package bindings do not include annotations, so add them from the
   * package's AST node.
   */
public static JdtPackageElement getPackageElement(PackageDeclaration pkg) {
    IPackageBinding binding = pkg.resolveBinding();
    JdtPackageElement pkgElement = (JdtPackageElement) getElement(binding);
    if (pkgElement.getAnnotationMirrors().isEmpty() && pkg.annotations().size() > 0) {
        for (Object modifier : pkg.annotations()) {
            IAnnotationBinding annotation = ((org.eclipse.jdt.core.dom.Annotation) modifier).resolveAnnotationBinding();
            pkgElement.addAnnotation(new JdtAnnotationMirror(annotation));
        }
    }
    return pkgElement;
}
Also used : IAnnotationBinding(org.eclipse.jdt.core.dom.IAnnotationBinding) IPackageBinding(org.eclipse.jdt.core.dom.IPackageBinding)

Example 13 with IAnnotationBinding

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

the class ASTNodeFactory method newCreationType.

public static Type newCreationType(AST ast, ITypeBinding typeBinding, ImportRewrite importRewrite, ImportRewriteContext importContext) {
    if (typeBinding.isParameterizedType()) {
        Type baseType = newCreationType(ast, typeBinding.getTypeDeclaration(), importRewrite, importContext);
        ParameterizedType parameterizedType = ast.newParameterizedType(baseType);
        for (ITypeBinding typeArgument : typeBinding.getTypeArguments()) {
            parameterizedType.typeArguments().add(newCreationType(ast, typeArgument, importRewrite, importContext));
        }
        return parameterizedType;
    } else if (typeBinding.isParameterizedType()) {
        Type elementType = newCreationType(ast, typeBinding.getElementType(), importRewrite, importContext);
        ArrayType arrayType = ast.newArrayType(elementType, 0);
        while (typeBinding.isArray()) {
            Dimension dimension = ast.newDimension();
            IAnnotationBinding[] typeAnnotations = typeBinding.getTypeAnnotations();
            for (IAnnotationBinding typeAnnotation : typeAnnotations) {
                dimension.annotations().add(importRewrite.addAnnotation(typeAnnotation, ast, importContext));
            }
            arrayType.dimensions().add(dimension);
            typeBinding = typeBinding.getComponentType();
        }
        return arrayType;
    } else if (typeBinding.isWildcardType()) {
        ITypeBinding bound = typeBinding.getBound();
        typeBinding = (bound != null) ? bound : typeBinding.getErasure();
        return newCreationType(ast, typeBinding, importRewrite, importContext);
    } else {
        return importRewrite.addImport(typeBinding, ast, importContext);
    }
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) 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) Dimension(org.eclipse.jdt.core.dom.Dimension)

Example 14 with IAnnotationBinding

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

the class StubUtility2 method createParameters.

private static List<SingleVariableDeclaration> createParameters(IJavaProject project, ImportRewrite imports, ImportRewriteContext context, AST ast, IMethodBinding binding, String[] paramNames, MethodDeclaration decl) {
    boolean is50OrHigher = JavaModelUtil.is50OrHigher(project);
    List<SingleVariableDeclaration> parameters = decl.parameters();
    ITypeBinding[] params = binding.getParameterTypes();
    if (paramNames == null || paramNames.length < params.length) {
        paramNames = StubUtility.suggestArgumentNames(project, binding);
    }
    for (int i = 0; i < params.length; i++) {
        SingleVariableDeclaration var = ast.newSingleVariableDeclaration();
        ITypeBinding type = params[i];
        if (type.isWildcardType()) {
            ITypeBinding bound = type.getBound();
            type = (bound != null) ? bound : type.getErasure();
        }
        if (!is50OrHigher) {
            type = type.getErasure();
            var.setType(imports.addImport(type, ast, context));
        } else if (binding.isVarargs() && type.isArray() && i == params.length - 1) {
            var.setVarargs(true);
            /*
				 * Varargs annotations are special.
				 * Example:
				 *     foo(@O Object @A [] @B ... arg)
				 * => @B is not an annotation on the array dimension that constitutes the vararg.
				 * It's the type annotation of the *innermost* array dimension.
				 */
            int dimensions = type.getDimensions();
            @SuppressWarnings("unchecked") List<Annotation>[] dimensionAnnotations = (List<Annotation>[]) new List<?>[dimensions];
            for (int dim = 0; dim < dimensions; dim++) {
                dimensionAnnotations[dim] = new ArrayList<Annotation>();
                for (IAnnotationBinding annotation : type.getTypeAnnotations()) {
                    dimensionAnnotations[dim].add(imports.addAnnotation(annotation, ast, context));
                }
                type = type.getComponentType();
            }
            Type elementType = imports.addImport(type, ast, context);
            if (dimensions == 1) {
                var.setType(elementType);
            } else {
                ArrayType arrayType = ast.newArrayType(elementType, dimensions - 1);
                List<Dimension> dimensionNodes = arrayType.dimensions();
                for (int dim = 0; dim < dimensions - 1; dim++) {
                    // all except the innermost dimension
                    Dimension dimension = dimensionNodes.get(dim);
                    dimension.annotations().addAll(dimensionAnnotations[dim]);
                }
                var.setType(arrayType);
            }
            List<Annotation> varargTypeAnnotations = dimensionAnnotations[dimensions - 1];
            var.varargsAnnotations().addAll(varargTypeAnnotations);
        } else {
            var.setType(imports.addImport(type, ast, context));
        }
        var.setName(ast.newSimpleName(paramNames[i]));
        IAnnotationBinding[] annotations = binding.getParameterAnnotations(i);
        for (IAnnotationBinding annotation : annotations) {
            if (StubUtility2.isCopyOnInheritAnnotation(annotation.getAnnotationType(), project))
                var.modifiers().add(imports.addAnnotation(annotation, ast, context));
        }
        parameters.add(var);
    }
    return parameters;
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ArrayList(java.util.ArrayList) Dimension(org.eclipse.jdt.core.dom.Dimension) Annotation(org.eclipse.jdt.core.dom.Annotation) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) IAnnotationBinding(org.eclipse.jdt.core.dom.IAnnotationBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) List(java.util.List) ArrayList(java.util.ArrayList)

Example 15 with IAnnotationBinding

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

the class StubUtility2 method getImplementationModifiers.

private static List<IExtendedModifier> getImplementationModifiers(AST ast, IMethodBinding method, boolean inInterface, ImportRewrite importRewrite, ImportRewriteContext context) throws JavaModelException {
    IJavaProject javaProject = importRewrite.getCompilationUnit().getJavaProject();
    int modifiers = method.getModifiers() & ~Modifier.ABSTRACT & ~Modifier.NATIVE & ~Modifier.PRIVATE;
    if (inInterface) {
        modifiers = modifiers & ~Modifier.PROTECTED;
        if (!method.getDeclaringClass().isInterface()) {
            modifiers = modifiers | Modifier.PUBLIC;
        }
    } else {
        modifiers = modifiers & ~Modifier.DEFAULT;
    }
    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 && JavaElementUtil.isSourceAvailable(iMethod)) {
            CheASTParser parser = CheASTParser.newParser(ASTProvider.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<IExtendedModifier>();
                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))
                                    result.add(importRewrite.addAnnotation(annotation, ast, context));
                                break;
                            }
                        }
                    }
                }
                result.addAll(ASTNodeFactory.newModifiers(ast, modifiers));
                return result;
            }
        }
    }
    ArrayList<IExtendedModifier> result = new ArrayList<IExtendedModifier>();
    for (IAnnotationBinding annotation : annotations) {
        if (StubUtility2.isCopyOnInheritAnnotation(annotation.getAnnotationType(), javaProject))
            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) CheASTParser(org.eclipse.jdt.core.dom.CheASTParser) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier)

Aggregations

IAnnotationBinding (org.eclipse.jdt.core.dom.IAnnotationBinding)27 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)14 Annotation (org.eclipse.jdt.core.dom.Annotation)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)6 ArrayType (org.eclipse.jdt.core.dom.ArrayType)6 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)6 Type (org.eclipse.jdt.core.dom.Type)6 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)4 Dimension (org.eclipse.jdt.core.dom.Dimension)4 InternalEList (org.eclipse.emf.ecore.util.InternalEList)3 IJavaProject (org.eclipse.jdt.core.IJavaProject)3 AST (org.eclipse.jdt.core.dom.AST)3 IBinding (org.eclipse.jdt.core.dom.IBinding)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