/**
   * Calls the callback with every parameter of the method, skipping out the offset parameter
   * introduced by F2J for array arguments.
   *
   * @param method
   * @param callback
   */
  protected void iterateRelevantParameters(
      Method method, boolean offsets, ParameterCallback callback) {
    if (method.getParameterTypes().length == 0) return;

    String[] names = new String[0];
    try {
      names = paranamer.lookupParameterNames(method, true);
    } catch (ParameterNamesNotFoundException e) {
      getLog().warn(e);
    }

    for (int i = 0; i < method.getParameterTypes().length; i++) {
      Class<?> param = method.getParameterTypes()[i];
      if (i > 0
          && !offsets
          && param == Integer.TYPE
          && method.getParameterTypes()[i - 1].isArray()) {
        continue;
      }
      String name;
      if (names.length > 0) name = names[i];
      else name = "arg" + i;

      String offsetName = null;
      if (i < method.getParameterTypes().length - 1
          && param.isArray()
          && method.getParameterTypes()[i + 1] == Integer.TYPE) offsetName = names[i + 1];

      callback.process(i, param, name, offsetName);
    }
  }
 public boolean hasOffsets(Method method) {
   Class<?> last = null;
   for (int i = 0; i < method.getParameterTypes().length; i++) {
     Class<?> param = method.getParameterTypes()[i];
     if (last != null && last.isArray() && param.equals(Integer.TYPE)) return true;
     last = param;
   }
   return false;
 }
Пример #3
0
  private static void printParameterChecks(
      PrintWriter writer,
      ExecutableElement method,
      Map<VariableElement, TypeInfo> typeinfos,
      Mode mode,
      final boolean generate_error_checks) {
    if (mode == Mode.NORMAL) {
      final GenerateAutos gen_autos_annotation = method.getAnnotation(GenerateAutos.class);
      if (gen_autos_annotation != null && gen_autos_annotation.sizeVariables().length > 0) {
        // For the auto-generated parameters, declare and init a size variable (that can be reused
        // by @Code)
        for (final VariableElement param : method.getParameters()) {
          if (Arrays.binarySearch(
                  gen_autos_annotation.sizeVariables(), param.getSimpleName().toString())
              >= 0) {
            final int shifting = getBufferElementSizeExponent(typeinfos.get(param).getType());
            final Check check_annotation = param.getAnnotation(Check.class);

            writer.print("\t\tlong " + param.getSimpleName() + "_size = ");
            if (check_annotation == null || !check_annotation.canBeNull()) {
              writer.println(param.getSimpleName() + ".remaining() << " + shifting + ";");
            } else {
              writer.println(
                  param.getSimpleName()
                      + " == null ? 0 : "
                      + param.getSimpleName()
                      + ".remaining() << "
                      + shifting
                      + ";");
            }
          }
        }
      }
    }

    for (VariableElement param : method.getParameters()) {
      Class java_type = Utils.getJavaType(param.asType());
      if (java_type.isArray()
          || (Utils.isAddressableType(java_type)
              && (mode != Mode.BUFFEROBJECT || param.getAnnotation(BufferObject.class) == null)
              && (mode != Mode.AUTOS || getAutoTypeParameter(method, param) == null)
              && param.getAnnotation(Result.class) == null
              && !Utils.isReturnParameter(method, param))) {
        String check_value = null;
        boolean can_be_null = false;
        Check check_annotation = param.getAnnotation(Check.class);
        if (check_annotation != null) {
          check_value = check_annotation.value();
          can_be_null = check_annotation.canBeNull();
        }
        if ((Buffer.class.isAssignableFrom(java_type)
                || PointerBuffer.class.isAssignableFrom(java_type))
            && param.getAnnotation(Constant.class) == null) {
          TypeInfo typeinfo = typeinfos.get(param);
          printParameterCheck(
              writer,
              method,
              param.getSimpleName().toString(),
              typeinfo.getType().getSimpleName(),
              check_value,
              can_be_null,
              param.getAnnotation(NullTerminated.class),
              generate_error_checks);
        } else if (String.class.equals(java_type)) {
          if (!can_be_null) {
            writer.println("\t\tBufferChecks.checkNotNull(" + param.getSimpleName() + ");");
          }
        } else if (java_type.isArray()) {
          printArrayParameterCheck(
              writer, param.getSimpleName().toString(), check_value, can_be_null);
        }
      }
    }
    if (method.getAnnotation(CachedResult.class) != null) {
      printParameterCheck(
          writer, method, Utils.CACHED_BUFFER_NAME, null, null, true, null, generate_error_checks);
    }
  }