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());
      }
    }
  }
Add this annotation to a method.

@param dexMaker DexMaker instance.
@param method Method to be added to.
android/android_framework/dex-maker/src/main/java/com/android/dx/AnnotationId.java