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

The following examples show how to use java.lang.annotation.ElementType#METHOD . 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: AnnotatedClass.java    From classpy with MIT License 6 votes vote down vote up
public void testParameterAnnotations(
    @MyRuntimeAnnotation(
        intValue = 456,
        strValue = "test",
        enumValue = ElementType.METHOD,
        classValue = String.class,
        annotationValue = @Target({ElementType.METHOD}),
        arrayValue = {"X", "Y", "Z"}
    )
    @MyClassAnnotation(
        intValue = 456,
        strValue = "test",
        enumValue = ElementType.METHOD,
        classValue = String.class,
        annotationValue = @Target({}),
        arrayValue = {"X", "Y", "Z"}
    )   
    int param1
) {
    // ...
}
 
Example 2
Source File: AnnotatedClass.java    From classpy with MIT License 6 votes vote down vote up
@MyRuntimeAnnotation(
    intValue = 456,
    strValue = "test",
    enumValue = ElementType.METHOD,
    classValue = String.class,
    annotationValue = @Target({ElementType.METHOD}),
    arrayValue = {"X", "Y", "Z"}
)
@MyClassAnnotation(
    intValue = 456,
    strValue = "test",
    enumValue = ElementType.METHOD,
    classValue = String.class,
    annotationValue = @Target({}),
    arrayValue = {"X", "Y", "Z"}
)
public void testMethodAnnotations() {
    
}
 
Example 3
Source File: BinderClassFactory.java    From preferencebinder with Apache License 2.0 5 votes vote down vote up
private void emitEmptyValueListenerBindings(StringBuilder builder, Collection<Binding> bindings) {
    for (Binding binding : bindings) {
        if(binding.getBindingType() == ElementType.METHOD && binding.getType()==null) {
            builder.append(INDENT_3);
            emitMethodCall(builder, null, binding);
        }
    }
}
 
Example 4
Source File: AnnotationId.java    From dexmaker with Apache License 2.0 5 votes vote down vote up
/**
 * Add this annotation to a method.
 *
 * @param dexMaker DexMaker instance.
 * @param method Method to be added to.
 */
public void addToMethod(DexMaker dexMaker, MethodId<?, ?> method) {
    if (annotatedElement != ElementType.METHOD) {
        throw new IllegalStateException("This annotation is not for method");
    }

    if (!method.declaringType.equals(declaringType)) {
        throw new IllegalArgumentException("Method" + method + "'s declaring type is inconsistent with" + this);
    }

    ClassDefItem classDefItem = dexMaker.getTypeDeclaration(declaringType).toClassDefItem();

    if (classDefItem == null) {
        throw new NullPointerException("No class defined item is found");
    } else {
        CstMethodRef cstMethodRef = method.constant;

        if (cstMethodRef == null) {
            throw new NullPointerException("Method reference is NULL");
        } else {
            // Generate CstType
            CstType cstType = CstType.intern(type.ropType);

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

            // Add generated annotation
            Annotations annotations = new Annotations();
            for (NameValuePair nvp : elements.values()) {
                annotation.add(nvp);
            }
            annotations.add(annotation);
            classDefItem.addMethodAnnotations(cstMethodRef, annotations, dexMaker.getDexFile());
        }
    }
}
 
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: AnnotationPredicates.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Override
public boolean apply(InfoPb info) {
  if (ElementType.METHOD == checkLocation) {
    return apply(info.getMethodAnnotationList());
  } else {
    return apply(info.getClassAnnotationList());
  }
}
 
Example 7
Source File: ActionTestControllerTest.java    From dolphin-platform with Apache License 2.0 5 votes vote down vote up
@Test
public void callPrivateMethodWithElementTypeParam() {
    Assert.assertNull(controller.getModel().getEnumValue());
    final ElementType value = ElementType.METHOD;
    controller.invoke(PUBLIC_WITH_ELEMENT_TYPE_PARAM_ACTION, new Param(PARAM_NAME, value));
    Assert.assertEquals(controller.getModel().getEnumValue().compareTo(value), 0);
}
 
Example 8
Source File: GenericConfig.java    From smallrye-fault-tolerance with Apache License 2.0 5 votes vote down vote up
GenericConfig(Class<X> annotationType, Class<?> beanClass, Method method) {
    this(beanClass, method, null, annotationType,
            method.isAnnotationPresent(annotationType)
                    ? method.getAnnotation(annotationType)
                    : getAnnotationFromClass(annotationType, beanClass),
            method.isAnnotationPresent(annotationType) ? ElementType.METHOD : ElementType.TYPE);
}
 
Example 9
Source File: BinderClassFactory.java    From preferencebinder with Apache License 2.0 5 votes vote down vote up
private boolean hasNonEmptyBinding(Collection<Binding> bindings) {
    for(Binding binding : bindings) {
        if(binding.getBindingType() != ElementType.METHOD || binding.getType() != null) {
            return true;
        }
    }
    return false;
}
 
Example 10
Source File: UnresolvedXMethod.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public ElementType getElementType() {
    if (Const.CONSTRUCTOR_NAME.equals(getName())) {
        return ElementType.CONSTRUCTOR;
    }
    return ElementType.METHOD;
}
 
Example 11
Source File: MethodInfo.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public ElementType getElementType() {
    if (Const.CONSTRUCTOR_NAME.equals(getName())) {
        return ElementType.CONSTRUCTOR;
    }
    return ElementType.METHOD;
}
 
Example 12
Source File: AnnotationId.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Add this annotation to a method.
 *
 * @param dexMaker DexMaker instance.
 * @param method   Method to be added to.
 */
public static void addToMethod(DexMaker dexMaker, MethodId<?, ?> method,List<AnnotationId<?,?>> ids) {
    ClassDefItem classDefItem = dexMaker.getTypeDeclaration(method.declaringType).toClassDefItem();

    if (classDefItem == null) {
        throw new NullPointerException("No class defined item is found");
    }
    CstMethodRef cstMethodRef = method.constant;
    if (cstMethodRef == null) {
        throw new NullPointerException("Method reference is NULL");
    }
    Annotations annotations = new Annotations();
    for (AnnotationId<?, ?> id :
            ids) {
        if (id.annotatedElement != ElementType.METHOD) {
            throw new IllegalStateException("This annotation is not for method");
        }

        if (method.declaringType != id.declaringType) {
            throw new IllegalArgumentException("Method" + method + "'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);
    }
    classDefItem.addMethodAnnotations(cstMethodRef, annotations, dexMaker.getDexFile());


}
 
Example 13
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 14
Source File: GenericConfig.java    From smallrye-fault-tolerance with Apache License 2.0 5 votes vote down vote up
GenericConfig(Class<X> annotationType, AnnotatedMethod<?> annotatedMethod) {
    this(annotatedMethod.getDeclaringType()
            .getJavaClass(), annotatedMethod.getJavaMember(), annotatedMethod, annotationType,
            annotatedMethod.isAnnotationPresent(annotationType) ? annotatedMethod.getAnnotation(annotationType)
                    : annotatedMethod.getDeclaringType()
                            .getAnnotation(annotationType),
            annotatedMethod.isAnnotationPresent(annotationType) ? ElementType.METHOD : ElementType.TYPE);
}
 
Example 15
Source File: ActionControllerTest.java    From dolphin-platform with Apache License 2.0 4 votes vote down vote up
@Test(dataProvider = ENDPOINTS_DATAPROVIDER, description = "Tests if an private action method with ElementType param can be called")
public void testCallPrivateMethodWithElementTypeParams(String containerType, String endpoint) {
    final ElementType value = ElementType.METHOD;
    performActionForElementType(containerType, endpoint, PRIVATE_WITH_ELEMENT_TYPE_PARAM_ACTION, value, new Param(PARAM_NAME, value));
}
 
Example 16
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());
}
 
Example 17
Source File: AnnotationPredicates.java    From android-test with Apache License 2.0 4 votes vote down vote up
static Predicate<InfoPb> newMethodAnnotationPresentPredicate(
    String annotationClassName, Predicate<List<AnnotationValuePb>> valuePredicate) {
  return new AnnotationPresent(ElementType.METHOD, annotationClassName, valuePredicate);
}