Skip to content

Commit

Permalink
Warn extension authors if build step and recorder are in the same pac…
Browse files Browse the repository at this point in the history
…kage

This is meant to guard against cases where a call to a
recorder method cannot be proxied (due to packaging rules)
leading to a real call to the recorder method being performed
in the build step method.
For public recorder methods, the method call is always proxyable, thus by warning about same package use,
we nudge extension authors towards using public methods
in their recorder classes.

Note that we cannot simply reject package private
methods in recorder since these methods could very well
be used in code that is not called at build time.

Relates to: #33957
  • Loading branch information
geoand committed Jun 13, 2023
1 parent 0f4c893 commit bfc0904
Showing 1 changed file with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.Name;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
Expand Down Expand Up @@ -375,6 +376,11 @@ private void validateRecordBuildSteps(TypeElement clazz) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
"Class '" + parameterTypeElement.getQualifiedName()
+ "' is annotated with @Recorder and therefore cannot be made as a final class.");
} else if (getPackageName(clazz).equals(getPackageName(parameterTypeElement))) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,
"Build step class '" + clazz.getQualifiedName()
+ "' and recorder '" + parameterTypeElement
+ "' share the same package. This is highly discouraged as it can lead to unexpected results.");
}
hasRecorder = true;
break;
Expand All @@ -390,6 +396,10 @@ private void validateRecordBuildSteps(TypeElement clazz) {
}
}

private Name getPackageName(TypeElement clazz) {
return processingEnv.getElementUtils().getPackageOf(clazz).getQualifiedName();
}

private StringBuilder getRelativeBinaryName(TypeElement te, StringBuilder b) {
final Element enclosing = te.getEnclosingElement();
if (enclosing instanceof TypeElement) {
Expand Down

0 comments on commit bfc0904

Please sign in to comment.