Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a custom annotation to Android Saripaar

I just started using android saripaar library for a client's app. I wanted to add a custom validation for a field. However, there doesn't seem to be a way to create a custom annotation. I have to manually put in rule in the validator.

How do I create a custom annotation for the same?

like image 992
Amit Avatar asked Jan 28 '14 12:01

Amit


People also ask

Can we create custom annotation in spring boot?

In this way, we can create different custom annotations for validation purposes. You can find the full source code here. It is easy to create and use custom annotations in Java. Java developers will be relieved of redundant code by using custom annotations.

How do I create a custom annotation in Java?

The first step toward creating a custom annotation is to declare it using the @interface keyword:. public @interface JsonSerializable { } The next step is to add meta-annotations to specify the scope and the target of our custom annotation: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.Type) public @interface JsonSerializable { }

How do I create custom annotations in Swift?

The first step toward creating a custom annotation is to declare it using the @interface keyword: The next step is to add meta-annotations to specify the scope and the target of our custom annotation: As we can see, our first annotation has runtime visibility, and we can apply it to types (classes).

What are Java annotations in Android development?

Java Annotations are introduced in Java 1.5 and now they are heavily used in Java frameworks including Android. Java Annotations are metadata about the program embedded in the program itself but annotations have no direct effect on the operation of the code they annotate (i.e. it does not affect the execution of the program).

How to add Kotlin annotations to Android apps?

The first step is to build a new module that will house your annotations. Go to Android Studio and then click on File -> New ->New Module then choose Kotlin and after that, you need to add a name to the module like you usually do with your android projects. Image 2. Selecting the Module


1 Answers

(Disclosure: I'm the author)

Saripaar v2 allows you to define custom annotations.

Here's how you do it.

Step 1 Define your custom annotation as follows. Make sure you have a RUNTIME retention policy and your annotation must be targeted towards FIELD element types. The message and messageResId attributes are mandatory, so watch the names and the types.

@ValidateUsing(HaggleRule.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Haggle {
    public int messageResId()   default -1;                     // Mandatory attribute
    public String message()     default "Oops... too pricey";   // Mandatory attribute
    public int sequence()       default -1;                     // Mandatory attribute

    public double maximumAskingPrice();                         // Your attributes
}

Step 2 Define your rule by extending the AnnotationRule class.

public class HaggleRule extends AnnotationRule<Haggle, Double> {

    protected HaggleRule(Haggle haggle) {
        super(haggle);
    }

    @Override
    public boolean isValid(Double data) {
        boolean isValid = false;
        double maximumAskingPrice = mRuleAnnotation.maximumAskingPrice();

        // Do some clever validation....

        return isValid;
    }
}

Step 3 Register your rule.

Validator.registerAnnotation(Haggle.class); // Your annotation class instance

Simple as that. Take a look at the source code if you want to. Saripaar v2 is now available on Maven Central.

like image 98
Ragunath Jawahar Avatar answered Sep 28 '22 11:09

Ragunath Jawahar