Пример #1
0
  private Task createTaskObject(
      Project project, Class<? extends Task> type, String name, boolean generateGetters) {
    if (!Task.class.isAssignableFrom(type)) {
      throw new GradleException(
          String.format(
              "Cannot create task of type '%s' as it does not implement the Task interface.",
              type.getSimpleName()));
    }

    Class<? extends Task> generatedType;
    if (generateGetters) {
      generatedType = generator.generate(type);
    } else {
      generatedType = type;
    }

    Constructor<? extends Task> constructor = null;
    Object[] params = null;
    try {
      constructor = generatedType.getDeclaredConstructor();
      params = new Object[0];
    } catch (NoSuchMethodException e) {
      // Ignore
    }
    try {
      constructor = generatedType.getDeclaredConstructor(Project.class, String.class);
      params = new Object[] {project, name};
    } catch (NoSuchMethodException e) {
      // Ignore
    }
    if (constructor == null) {
      throw new GradleException(
          String.format(
              "Cannot create task of type '%s' as it does not have an appropriate public constructor.",
              type.getSimpleName()));
    }

    AbstractTask.injectIntoNextInstance(project, name);
    try {
      return constructor.newInstance(params);
    } catch (InvocationTargetException e) {
      throw new GradleException(
          String.format("Could not create task of type '%s'.", type.getSimpleName()), e.getCause());
    } catch (Exception e) {
      throw new GradleException(
          String.format("Could not create task of type '%s'.", type.getSimpleName()), e);
    } finally {
      AbstractTask.injectIntoNextInstance(null, null);
    }
  }