Java Code Examples for java.lang.annotation.ElementType#FIELD

The following examples show how to use java.lang.annotation.ElementType#FIELD . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NonFieldTargetDetector.java    From aircon with MIT License 6 votes vote down vote up
@Override
protected void visitConfigTypeAnnotation(final UAnnotation node, final UClass owner) {
	final List<UAnnotation> annotations = ((UAnnotated) owner).getAnnotations();
	for (UAnnotation annotation : annotations) {
		if (!isTargetAnnotation(annotation)) {
			continue;
		}

		final ElementType[] annotationTargets = getTargetValue(annotation);
		if (annotationTargets.length == 1 && annotationTargets[0] == ElementType.FIELD) {
			return;
		}
	}

	reportPsi(owner.getNameIdentifier());
}
 
Example 2
Source File: AnnotationId.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Construct an instance. It initially contains no elements.
 *
 * @param declaringType    the type declaring the program element.
 * @param type             the annotation type.
 * @param annotatedElement the program element type to be annotated.
 * @return an annotation {@code AnnotationId<D,V>} instance.
 */
public static <D, V> AnnotationId<D, V> get(TypeId<D> declaringType, TypeId<V> type,
                                            ElementType annotatedElement) {
    if (annotatedElement != ElementType.TYPE &&
            annotatedElement != ElementType.METHOD &&
            annotatedElement != ElementType.FIELD &&
            annotatedElement != ElementType.PARAMETER) {
        throw new IllegalArgumentException("element type is not supported to annotate yet.");
    }

    return new AnnotationId<>(declaringType, type, annotatedElement);
}
 
Example 3
Source File: AnnotationId.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void addToField(DexMaker dexMaker, FieldId<?, ?> field,List<AnnotationId<?,?>> ids) {
    ClassDefItem classDefItem = dexMaker.getTypeDeclaration(field.declaringType).toClassDefItem();
    if (classDefItem == null) {
        throw new NullPointerException("No class defined item is found");
    }
    Annotations annotations = new Annotations();
    for (AnnotationId<?,?> id:ids){
        if (id.annotatedElement != ElementType.FIELD) {
            throw new IllegalStateException("This annotation is not for method");
        }

        if (field.declaringType != id.declaringType) {
            throw new IllegalArgumentException("Field" + field + "'s declaring type is inconsistent with" + id);
        }

        // Generate CstType
        CstType cstType = CstType.intern(id.type.ropType);

        // Generate Annotation
        Annotation annotation = new Annotation(cstType, AnnotationVisibility.RUNTIME);

        // Add generated annotation
        for (NameValuePair nvp : id.elements.values()) {
            annotation.add(nvp);
        }
        annotations.add(annotation);
    }

    CstFieldRef cstFieldRef = field.constant;
    classDefItem.addFieldAnnotations(cstFieldRef, annotations, dexMaker.getDexFile());
}
 
Example 4
Source File: ActionTestControllerTest.java    From dolphin-platform with Apache License 2.0 5 votes vote down vote up
/** Start ElementType Type Related Action Test */
@Test
public void callPublicMethodWithElementTypeParam() {
    Assert.assertNull(controller.getModel().getEnumValue());
    final ElementType value = ElementType.FIELD;
    controller.invoke(PUBLIC_WITH_ELEMENT_TYPE_PARAM_ACTION, new Param(PARAM_NAME, value));
    Assert.assertEquals(controller.getModel().getEnumValue(), value);
}
 
Example 5
Source File: AnnotationId.java    From dexmaker with Apache License 2.0 5 votes vote down vote up
/**
 *  Construct an instance. It initially contains no elements.
 *
 * @param declaringType the type declaring the program element.
 * @param type the annotation type.
 * @param annotatedElement the program element type to be annotated.
 * @return an annotation {@code AnnotationId<D,V>} instance.
 */
public static <D, V> AnnotationId<D, V> get(TypeId<D> declaringType, TypeId<V> type,
                                            ElementType annotatedElement) {
    if (annotatedElement != ElementType.TYPE &&
            annotatedElement != ElementType.METHOD &&
            annotatedElement != ElementType.FIELD &&
            annotatedElement != ElementType.PARAMETER) {
        throw new IllegalArgumentException("element type is not supported to annotate yet.");
    }

    return new AnnotationId<>(declaringType, type, annotatedElement);
}
 
Example 6
Source File: FieldInfo.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public ElementType getElementType() {
    return ElementType.FIELD;
}
 
Example 7
Source File: UnresolvedXField.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public ElementType getElementType() {
    return ElementType.FIELD;
}
 
Example 8
Source File: PreferenceBinderProcessor.java    From preferencebinder with Apache License 2.0 4 votes vote down vote up
private void parseBindPreference(Element annotatedElement) {
    if (bindPreferenceAnnotationHasError(annotatedElement)) {
        return;
    }

    // Assemble information on the binding point.
    final TypeElement enclosingElement = (TypeElement) annotatedElement.getEnclosingElement();
    final BindPref annotation = annotatedElement.getAnnotation(BindPref.class);
    final String[] preferenceKeys = annotation.value();
    final String name = annotatedElement.getSimpleName().toString();

    final boolean isField = annotatedElement.getKind().isField();
    final ElementType elementType = isField?ElementType.FIELD:ElementType.METHOD;
    String type;

    if(!annotation.init() && !annotation.listen()) {
        error(annotatedElement, "@BindPref binding has no effect (it should either initialize or listen)", enclosingElement.getQualifiedName(), name);
        return;
    } else if(preferenceKeys.length == 0) {
        error(annotatedElement, "Missing preference key(s) for @BindPref annotation", enclosingElement.getQualifiedName(), name);
        return;
    } else if(isField){
        if(preferenceKeys.length > 1) {
            error(annotatedElement, "Multiple preference keys are only allowed for @BindPref method annotations (not fields)", enclosingElement.getQualifiedName(), name);
            return;
        }

        if(annotation.bindTo().prefType == null) {
            type = annotatedElement.asType().toString();
        } else {
            type = annotation.bindTo().prefType.getFieldTypeDef();
        }
    }else {
        // Assemble information on the binding point.
        ExecutableElement executableElement = (ExecutableElement) annotatedElement;
        List<? extends VariableElement> params = executableElement.getParameters();

        if(annotation.bindTo() != WidgetBindingType.ASSIGN) {
            error(annotatedElement, "@BindPref method annotations should not use the \"bindTo\" property", enclosingElement.getQualifiedName(), name);
            return;
        } else if(preferenceKeys.length > 1) {
            if(params.size() > 0) {
                error(annotatedElement, "@BindPref method annotations with multiple preference keys can not have method parameters", enclosingElement.getQualifiedName(), name);
                return;
            }
            type = null;
        }else if(params.size() != 1) {
            error(annotatedElement,
                    "Methods annotated with @BindPref must have a single parameter. (%s.%s)",
                    enclosingElement.getQualifiedName(),
                    name);
            return;
        }else {
            type = params.get(0).asType().toString();
        }
    }

    BinderClassFactory binder = getOrCreateTargetClass(enclosingElement);
    Binding binding = new Binding(name, type, elementType, annotation.bindTo());

    for(String preferenceKey : preferenceKeys) {
        if(annotation.init()) {
            binder.addInitBinding(preferenceKey, binding);
        }
        if (annotation.listen()) {
            binder.addListenerBinding(preferenceKey, binding);
        }
    }

    // Add the type-erased version to the valid binding targets set.
    targetClassNames.add(enclosingElement.toString());
}