Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Java Annotation Processor? #601

Closed
ScottPierce opened this issue Jan 5, 2016 · 3 comments
Closed

Java Annotation Processor? #601

ScottPierce opened this issue Jan 5, 2016 · 3 comments

Comments

@ScottPierce
Copy link

Various searches have uncovered references to an undocumented api, adding annotation_processors list and annotation_processor_deps to java_library (and android_library?) rules. I also found posts (from over a year ago) where there was talk of changing the API to something more like Bazel's Java Plugin.

The most recent post I've found on this is here: #85 A link is provided to buck's JavaLibraryDescription, which I'm assuming is the change to a bazel-like api?

I have 2 questions:

  1. How should I be using Annotation Processors now? I don't have a good grasp on the buck source code, so I'm not sure how to use the JavaLibraryDescription. A sample with it's various fields would be great.
  2. What is the current state of annotation processors in buck now? They seem to have had a non-solidified api for several years now. Are there any plans?
@sdwilsh
Copy link
Contributor

sdwilsh commented Jan 5, 2016

  1. That API is correct, and there's some ad-hoc docs here: http://stackoverflow.com/questions/32915721/documentation-for-annotation-processors-buck
  2. We'd like to move to Bazel's API of declaring a java_plugin for these, and it wouldn't be hard to do; it's just a matter of doing the work for it and documenting it. It hasn't been the highest priority.

For what it's worth, the current API is used quite extensively at Facebook, so even if it were to change it, we'd support the old API for a while I suspect (and at the very least, we'd announce it in our release notes if we did change it).

@davido
Copy link
Contributor

davido commented Jan 7, 2016

We are using monkey patching to achieve what you want (https://github.com/gerrit-review/gerrit/blob/master/tools/default.defs):

# Rule definitions loaded by default into every BUCK file.

include_defs('//lib/auto/auto_value.defs')
include_defs('//tools/gwt-constants.defs')
include_defs('//tools/java_doc.defs')
include_defs('//tools/java_sources.defs')
include_defs('//tools/git.defs')
import copy
import traceback
import os
from multiprocessing import cpu_count

# Set defaults on java rules:
#  - Add AutoValue annotation processing support.
#  - Treat source files as UTF-8.

_buck_java_library = java_library
def java_library(*args, **kwargs):
  _munge_args(kwargs)
  _buck_java_library(*args, **kwargs)

_buck_java_test = java_test
def java_test(*args, **kwargs):
  _munge_args(kwargs)
  _buck_java_test(*args, **kwargs)


# Munge kwargs to set Gerrit-specific defaults.
def _munge_args(kwargs):
  _set_auto_value(kwargs)
  _set_extra_arguments(kwargs)

def _set_extra_arguments(kwargs):
  ext = 'extra_arguments'
  if ext not in kwargs:
    kwargs[ext] = []
  extra_args = kwargs[ext]

  for arg in extra_args:
    if arg.startswith('-encoding'):
      return

  extra_args.extend(['-encoding', 'UTF-8'])

def _set_auto_value(kwargs):
  apk = 'annotation_processors'
  if apk not in kwargs:
    kwargs[apk] = []
  aps = kwargs.get(apk, [])

  apdk = 'annotation_processor_deps'
  if apdk not in kwargs:
    kwargs[apdk] = []
  apds = kwargs.get(apdk, [])

  all_deps = kwargs.get('deps', []) + kwargs.get('exported_deps', [])
  if AUTO_VALUE_DEP in all_deps:
    aps.extend(AUTO_VALUE_PROCESSORS)
    apds.extend(AUTO_VALUE_PROCESSOR_DEPS)

[...]

And the defines are here (https://github.com/gerrit-review/gerrit/blob/master/lib/auto/auto_value.defs):

# NOTE: Do not use this file in your build rules; automatically supported by
# our implementation of java_library.

AUTO_VALUE_DEP = '//lib/auto:auto-value'

# Annotation processor classpath requires transitive dependencies.
# TODO: Clean this up when buck issue is closed and there is a
# better supported interface:
# https://github.com/facebook/buck/issues/85
AUTO_VALUE_PROCESSOR_DEPS = [
  '//lib:velocity',
  '//lib/auto:auto-value',
  '//lib/commons:collections',
  '//lib/commons:lang',
  '//lib/commons:oro',
]

AUTO_VALUE_PROCESSORS = [
  'com.google.auto.value.processor.AutoAnnotationProcessor',
  'com.google.auto.value.processor.AutoValueProcessor',
]

@sdwilsh
Copy link
Contributor

sdwilsh commented Feb 4, 2016

I think I already answered this for you, so I'm going to close it. If you disagree, please reopen and clarify what you feel is unresolved.

@sdwilsh sdwilsh closed this as completed Feb 4, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants