Repository navigation
Skeleton code for annotation processor. - #12828
Conversation
|
Can one of the admins verify this patch? |
|
@kbsriram, thanks for your PR! By analyzing the history of the files in this pull request, we identified @asimshankar, @karllessard and @jart to be potential reviewers. |
|
@asimshankar what do you think? Jenkins, test this please. |
|
@asimshankar ping |
72015b2 to
da2e5bf
Compare
asimshankar
left a comment
There was a problem hiding this comment.
Thanks for doing this and apologies for the delay. Some comments.
| import javax.lang.model.element.TypeElement; | ||
| import javax.tools.Diagnostic.Kind; | ||
|
|
||
| public final class OperatorProcessor extends AbstractProcessor { |
There was a problem hiding this comment.
A docstring explaining what this is and how it's doing it would be nice :)
|
|
||
| # Helper function to copy a srcjar after moving any source files | ||
| # directly under the root to the "maven-style" src/main/java layout | ||
| # Arguments: |
There was a problem hiding this comment.
I'm a bit lost on why we end up in this situation (that the src_jar contains anything outside the "maven-style" hierarchy. Perhaps that will dawn on my as I see the rest of the CL, just wondering out aloud for now.
There was a problem hiding this comment.
The problem is that the annotation processor doesn't control where its generated sources are written. The processor only provides a package name - the rest of it is determined by the environment.
The environment is first determined by the way bazel calls javac, and then by how bazel packages such generated files into its -srcs.jar. bazel generates files into a bazel-out directory, and then jars them directly in the root of its -srcs.jar file. Is there an option I missed to control all this? I tried adding things like compiler options, but this ends up just confusing bazel.
There was a problem hiding this comment.
Maybe mention this in a comment?
There was a problem hiding this comment.
Thanks for the review Asim!
Have added a comment here.
|
|
||
| /** Basic tests for {@link org.tensorflow.processor.operator.OperatorProcessor}. */ | ||
| @RunWith(JUnit4.class) | ||
| public final class BasicProcessorTest { |
There was a problem hiding this comment.
Should this be OperatorProcessorTest?
There was a problem hiding this comment.
Done - if the tests cases gets large large, I'll split it into BasicOperatorProcessorTest if needed.
|
|
||
| java_library( | ||
| name = "operator_plugin_library", | ||
| srcs = [":operator_plugin_sources"], |
There was a problem hiding this comment.
Why bother with the filegroup here instead of say srcs = glob(["src/gen/java/org/tensorflow/processor/**/*.java"])?
| name = "operator_plugin_library", | ||
| srcs = [":operator_plugin_sources"], | ||
| javacopts = JAVACOPTS, | ||
| resources = [":operator_plugin_config"], |
There was a problem hiding this comment.
Same here - inline the resource instead of indirecting through a filegroup?
|
|
||
| // Collect all classes tagged with our annotation. | ||
| Set<TypeElement> opClasses = new HashSet<TypeElement>(); | ||
| for (TypeElement annotation : annotations) { |
There was a problem hiding this comment.
Shouldn't there be precisely one annotations? If so, perhaps we can assert that annotations.size() is 1 and then call collectOpClasses once?
There was a problem hiding this comment.
It can be zero or 1 depending on which phase this runs in. I'll combine this with the rest of the constraint reformulation you suggested. Ptal, and if overall it reads better to you, sgtm.
| // API class which cannot be modified once generated. If we find an annotation after we've | ||
| // generated our code, flag the location of each such class. | ||
| if (hasRun) { | ||
| error(e, "Cannot handle generated @Operators"); |
There was a problem hiding this comment.
Perhaps elaborate on the error message - explaining that we already processed Operators and wrote out a file and cannot process additional ones?
|
|
||
| // @Operator can only apply to types, so e must be a TypeElement. | ||
| if (!(e instanceof TypeElement)) { | ||
| error(e, "@Operator can only be applied to classes"); |
There was a problem hiding this comment.
Can we add more information that will help those that may encounter this error in debugging? For example, e.getKind() and e.getSimpleName() seem like they have something useful in them?
There was a problem hiding this comment.
Given this shows up as a compiler error message with file/line/character pointing to the incorrectly targeted element, do you think that might be sufficient to locate the problem element itself? I've added e.getKind() to the error message as well.
| message(Kind.ERROR, e, message, args); | ||
| } | ||
|
|
||
| private void message(Kind kind, Element e, String msg, Object... args) { |
There was a problem hiding this comment.
Optional: If message is used only for error, perhaps inline it in error?
| // This processor has to aggregate all op classes in one round, as it generates a single Ops | ||
| // API class which cannot be modified once generated. If we find an annotation after we've | ||
| // generated our code, flag the location of each such class. | ||
| if (hasRun) { |
There was a problem hiding this comment.
Instead of doing this in the loop, what about the following:
- Make this method:
private boolean collectOpClasses(Set<? extends Element> elements, Set<TypeElement> opClasses) {
for (Element e : elements) {
...
}
}- Make the caller (
process) do something like:
assert annotations.size() == 0;
Set<? extends Element> annotated = roundEnv.getElementsAnnotatedWith(annotation);
if (annotated.size() == 0) {
return true;
}
if (annotated.size() > 0 && hasRun) {
// print error
return false;
}
if (!collectOpClasses(annotated, opClasses)) {
return false;
}
...It's essentially the same, but I think it makes the expected constraints clearer to follow. Though, it's possible you disagree :)
There was a problem hiding this comment.
Done, combined with the previous suggestion.
7dc76be to
952d5ec
Compare
asimshankar
left a comment
There was a problem hiding this comment.
Looks great, thanks. One minor comment
| deps = [":operator_plugin_library"], | ||
| ) | ||
|
|
||
| java_library( |
There was a problem hiding this comment.
Optional: Call this annotation_processors or just processors?
There was a problem hiding this comment.
Each java_plugin defines one processor, so I've renamed it to "processor" and "processor_library", wdyt? If we add another processor, we can disambiguate this name later as needed.
|
|
||
| # Helper function to copy a srcjar after moving any source files | ||
| # directly under the root to the "maven-style" src/main/java layout | ||
| # Arguments: |
There was a problem hiding this comment.
Maybe mention this in a comment?
Add build changes and skeleton code (with test harness) for the Operator annotation processor. This change focuses on build and test-related changes, and generates an empty Ops class. Please see tensorflow#7149 for the master tracking issue.
952d5ec to
5200945
Compare
|
@tensorflow-jenkins test this please |
|
Jenkins, test this please |
|
Could a kindly member look at/restart the testing? Looks like the "Ubuntu CC" test didn't run, or stalled. Thanks! |
|
Jenkins, test this please. |
Add build changes and skeleton code (with test harness) for
the Operator annotation processor. This change focuses on build
and test-related changes, and generates an empty Ops class.
Please see #7149 for the master tracking issue.