Example usage for org.eclipse.jdt.internal.compiler.lookup FieldBinding getAnnotations

List of usage examples for org.eclipse.jdt.internal.compiler.lookup FieldBinding getAnnotations

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.lookup FieldBinding getAnnotations.

Prototype

@Override
    public AnnotationBinding[] getAnnotations() 

Source Link

Usage

From source file:com.codenvy.ide.ext.java.server.TypeBindingConvector.java

License:Open Source License

private static JsonElement toJsonField(FieldBinding field) {
    JsonObject object = new JsonObject();
    object.addProperty("modifiers", field.modifiers);
    object.add("constant", BinaryTypeConvector.toJsonConstant(field.constant()));
    object.add("genericSignature", field.genericSignature() == null ? JsonNull.INSTANCE
            : new JsonPrimitive(new String(field.genericSignature())));
    object.add("name", field.name == null ? JsonNull.INSTANCE : new JsonPrimitive(new String(field.name)));
    object.addProperty("tagBits", String.valueOf(field.tagBits));
    object.add("typeName",
            field.type == null ? JsonNull.INSTANCE : new JsonPrimitive(new String(field.type.signature())));
    object.add("annotations", toJsonAnnotations(field.getAnnotations()));
    return object;
}

From source file:info.archinnov.achilles.internals.parser.AnnotationTree.java

License:Apache License

public static AnnotationTree buildFrom(AptUtils aptUtils, GlobalParsingContext parsingContext,
        VariableElement varElm) {
    final String fieldName = varElm.getSimpleName().toString();
    final Name className = enclosingClass(varElm).getQualifiedName();
    final TypeMirror currentType = varElm.asType();

    final Frozen frozen = varElm.getAnnotation(Frozen.class);
    final JSON json = varElm.getAnnotation(JSON.class);
    final Enumerated enumerated = varElm.getAnnotation(Enumerated.class);
    final Codec codec = varElm.getAnnotation(Codec.class);
    final RuntimeCodec runtimeCodec = varElm.getAnnotation(RuntimeCodec.class);
    final Computed computed = varElm.getAnnotation(Computed.class);
    final Counter counter = varElm.getAnnotation(Counter.class);
    final TimeUUID timeUUID = varElm.getAnnotation(TimeUUID.class);
    final ASCII ascii = varElm.getAnnotation(ASCII.class);

    parsingContext.fieldValidator().validateCompatibleCodecAnnotationsOnField(aptUtils, fieldName, className,
            frozen, json, enumerated, codec, runtimeCodec, computed, counter, timeUUID, ascii);

    final List<? extends TypeMirror> nestedTypes = currentType.getKind() == TypeKind.DECLARED
            ? MoreTypes.asDeclared(currentType).getTypeArguments()
            : Arrays.asList();//from w w w .  j av a2 s  .c  o  m

    if (isEclipseCompiler(varElm)) {
        final FieldBinding binding = (FieldBinding) ((VariableElementImpl) varElm)._binding;

        final List<AnnotationBinding> annotationBindings = Arrays.asList(binding.getAnnotations());

        final Map<Class<? extends Annotation>, TypedMap> annotationInfo = annotationBindings.stream()
                .filter(annotBinding -> {
                    final String annotationName = annotBinding.getAnnotationType().debugName();
                    return JSON.class.getCanonicalName().equals(annotationName)
                            || EmptyCollectionIfNull.class.getCanonicalName().equals(annotationName)
                            || Enumerated.class.getCanonicalName().equals(annotationName)
                            || Frozen.class.getCanonicalName().equals(annotationName)
                            || Computed.class.getCanonicalName().equals(annotationName)
                            || Counter.class.getCanonicalName().equals(annotationName)
                            || TimeUUID.class.getCanonicalName().equals(annotationName)
                            || ASCII.class.getCanonicalName().equals(annotationName)
                            || Codec.class.getCanonicalName().equals(annotationName)
                            || RuntimeCodec.class.getCanonicalName().equals(annotationName)
                            || Index.class.getCanonicalName().equals(annotationName)
                            || SASI.class.getCanonicalName().equals(annotationName)
                            || DSE_Search.class.getCanonicalName().equals(annotationName)
                            || PartitionKey.class.getCanonicalName().equals(annotationName)
                            || ClusteringColumn.class.getCanonicalName().equals(annotationName);
                }).map(x -> inspectSupportedAnnotation_Ecj(aptUtils, currentType, x))
                .collect(Collectors.toMap(Tuple2::_1, Tuple2::_2));

        final AnnotationTree annotationTree = new AnnotationTree(currentType, annotationInfo, 1);
        List<TypeBinding> typeBindings = new ArrayList<>();
        if (binding.type instanceof ParameterizedTypeBinding) {
            typeBindings = Arrays.asList(((ParameterizedTypeBinding) binding.type).typeArguments());
        }

        buildTree_Ecj(aptUtils, annotationTree, 1, nestedTypes, typeBindings);
        return annotationTree;
    } else if (isJavaCompiler(varElm)) {
        final Map<Class<? extends Annotation>, TypedMap> annotationInfo = varElm.getAnnotationMirrors().stream()
                .filter(x -> areSameByClass(x, JSON.class) || areSameByClass(x, EmptyCollectionIfNull.class)
                        || areSameByClass(x, Enumerated.class) || areSameByClass(x, Frozen.class)
                        || areSameByClass(x, Computed.class) || areSameByClass(x, Counter.class)
                        || areSameByClass(x, TimeUUID.class) || areSameByClass(x, ASCII.class)
                        || areSameByClass(x, Codec.class) || areSameByClass(x, RuntimeCodec.class)
                        || areSameByClass(x, Index.class) || areSameByClass(x, SASI.class)
                        || areSameByClass(x, DSE_Search.class) || areSameByClass(x, PartitionKey.class)
                        || areSameByClass(x, ClusteringColumn.class))
                .map(x -> (AnnotationMirror) x).collect(Collectors.toMap(x -> toAnnotation_Javac(aptUtils, x),
                        x -> inspectSupportedAnnotation_Javac(aptUtils, currentType, x)));

        final AnnotationTree annotationTree = new AnnotationTree(currentType, annotationInfo, 1);

        final SymbolMetadata metadata = ((Symbol.VarSymbol) varElm).getMetadata();

        final List<Attribute.TypeCompound> typeAttributes = metadata == null ? Arrays.asList()
                : metadata.getTypeAttributes();
        buildTree_Javac(aptUtils, annotationTree, 1, nestedTypes, typeAttributes);
        return annotationTree;
    } else {
        aptUtils.printError(
                "Unknown compiler, only standard Java compiler and Eclipse ECJ compiler are supported");
        return null;
    }

}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.util.AstConverter.java

License:Open Source License

public static FieldDeclaration createField(FieldBinding fieldBinding, TypeDeclaration roleDeclaration,
        AstGenerator gen) {/*  ww  w .j a va 2  s .  c  o  m*/

    if (fieldBinding == null)
        return null;

    FieldDeclaration fieldDeclaration = new FieldDeclaration();
    fieldDeclaration.type = gen.typeReference(fieldBinding.type);

    fieldDeclaration.modifiers = (fieldBinding.modifiers & ~ExtraCompilerModifiers.AccBlankFinal); // this modifier is not used on fieldDecl (AST), overlaps with AccReadOnly
    fieldDeclaration.name = fieldBinding.name;
    if (fieldBinding.copyInheritanceSrc != null)
        fieldDeclaration.copyInheritanceSrc = fieldBinding.copyInheritanceSrc;
    else
        fieldDeclaration.copyInheritanceSrc = fieldBinding;

    AnnotationBinding[] annotBindings = fieldBinding.getAnnotations();
    if (annotBindings != Binding.NO_ANNOTATIONS) {
        ProblemReporter pr = fieldBinding.isStatic() ? roleDeclaration.staticInitializerScope.problemReporter()
                : roleDeclaration.initializerScope.problemReporter();
        Annotation[] annotDecls = new Annotation[annotBindings.length];
        boolean hasAnnotationError = false;
        for (int i = 0; i < annotBindings.length; i++) {
            AnnotationBinding binding = annotBindings[i];
            ElementValuePair[] elementValuePairs = binding.getElementValuePairs();
            char[][] annotTypeName = binding.getAnnotationType().compoundName;
            if (elementValuePairs == Binding.NO_ELEMENT_VALUE_PAIRS) {
                annotDecls[i] = gen.markerAnnotation(annotTypeName);
            } else {
                int numPairs = elementValuePairs.length;
                char[][] names = new char[numPairs][];
                Expression[] values = new Expression[numPairs];
                for (int j = 0; j < names.length; j++) {
                    names[j] = elementValuePairs[j].getName();
                    Object elementValue = elementValuePairs[j].getValue();
                    values[j] = annotationValues(elementValue, gen, pr);
                }
                if (values.length == 0 || values[0] == null) {
                    pr.unexpectedAnnotationStructure(annotTypeName, fieldBinding.name, gen.sourceStart,
                            gen.sourceEnd);
                    hasAnnotationError = true;
                } else if (numPairs == 1 && CharOperation.equals(names[0], TypeConstants.VALUE)) {
                    annotDecls[i] = gen.singleMemberAnnotation(annotTypeName, values[0]);
                } else {
                    annotDecls[i] = gen.normalAnnotation(annotTypeName, names, values);
                }
            }
        }
        if (!hasAnnotationError)
            fieldDeclaration.annotations = annotDecls;
    }

    //field initializations are copied using a RoleInitializationMethod
    return fieldDeclaration;
}