|
@@ -47,9 +47,15 @@ public class EvaluatorImplementer {
|
|
|
private final ProcessFunction processFunction;
|
|
|
private final ClassName implementation;
|
|
|
|
|
|
- public EvaluatorImplementer(Elements elements, ExecutableElement processFunction, String extraName, List<TypeMirror> warnExceptions) {
|
|
|
+ public EvaluatorImplementer(
|
|
|
+ Elements elements,
|
|
|
+ javax.lang.model.util.Types types,
|
|
|
+ ExecutableElement processFunction,
|
|
|
+ String extraName,
|
|
|
+ List<TypeMirror> warnExceptions
|
|
|
+ ) {
|
|
|
this.declarationType = (TypeElement) processFunction.getEnclosingElement();
|
|
|
- this.processFunction = new ProcessFunction(processFunction, warnExceptions);
|
|
|
+ this.processFunction = new ProcessFunction(elements, types, processFunction, warnExceptions);
|
|
|
|
|
|
this.implementation = ClassName.get(
|
|
|
elements.getPackageOf(declarationType).toString(),
|
|
@@ -87,6 +93,7 @@ public class EvaluatorImplementer {
|
|
|
}
|
|
|
builder.addMethod(realEval(false));
|
|
|
builder.addMethod(toStringMethod());
|
|
|
+ builder.addMethod(close());
|
|
|
return builder.build();
|
|
|
}
|
|
|
|
|
@@ -219,6 +226,20 @@ public class EvaluatorImplementer {
|
|
|
return builder.build();
|
|
|
}
|
|
|
|
|
|
+ private MethodSpec close() {
|
|
|
+ MethodSpec.Builder builder = MethodSpec.methodBuilder("close").addAnnotation(Override.class);
|
|
|
+ builder.addModifiers(Modifier.PUBLIC);
|
|
|
+
|
|
|
+ List<String> invocations = processFunction.args.stream().map(ProcessFunctionArg::closeInvocation).filter(s -> s != null).toList();
|
|
|
+ if (invocations.isEmpty() == false) {
|
|
|
+ builder.addStatement(
|
|
|
+ "$T.closeExpectNoException(" + invocations.stream().collect(Collectors.joining(", ")) + ")",
|
|
|
+ Types.RELEASABLES
|
|
|
+ );
|
|
|
+ }
|
|
|
+ return builder.build();
|
|
|
+ }
|
|
|
+
|
|
|
private interface ProcessFunctionArg {
|
|
|
/**
|
|
|
* Type containing the actual data for a page of values for this field. Usually a
|
|
@@ -276,7 +297,15 @@ public class EvaluatorImplementer {
|
|
|
*/
|
|
|
void buildInvocation(StringBuilder pattern, List<Object> args, boolean blockStyle);
|
|
|
|
|
|
+ /**
|
|
|
+ * Accumulate invocation pattern and arguments to implement {@link Object#toString()}.
|
|
|
+ */
|
|
|
void buildToStringInvocation(StringBuilder pattern, List<Object> args, String prefix);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The string to close this argument or {@code null}.
|
|
|
+ */
|
|
|
+ String closeInvocation();
|
|
|
}
|
|
|
|
|
|
private record StandardProcessFunctionArg(TypeName type, String name) implements ProcessFunctionArg {
|
|
@@ -368,6 +397,11 @@ public class EvaluatorImplementer {
|
|
|
args.add(prefix + name + "=");
|
|
|
args.add(name);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String closeInvocation() {
|
|
|
+ return name;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private record ArrayProcessFunctionArg(TypeName componentType, String name) implements ProcessFunctionArg {
|
|
@@ -470,9 +504,16 @@ public class EvaluatorImplementer {
|
|
|
args.add(Arrays.class);
|
|
|
args.add(name);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String closeInvocation() {
|
|
|
+ return "() -> Releasables.close(" + name + ")";
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private record FixedProcessFunctionArg(TypeName type, String name, boolean includeInToString) implements ProcessFunctionArg {
|
|
|
+ private record FixedProcessFunctionArg(TypeName type, String name, boolean includeInToString, boolean releasable)
|
|
|
+ implements
|
|
|
+ ProcessFunctionArg {
|
|
|
@Override
|
|
|
public TypeName dataType(boolean blockStyle) {
|
|
|
return type;
|
|
@@ -534,6 +575,11 @@ public class EvaluatorImplementer {
|
|
|
args.add(name);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String closeInvocation() {
|
|
|
+ return releasable ? name : null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private record BuilderProcessFunctionArg(ClassName type, String name) implements ProcessFunctionArg {
|
|
@@ -593,6 +639,11 @@ public class EvaluatorImplementer {
|
|
|
public void buildToStringInvocation(StringBuilder pattern, List<Object> args, String prefix) {
|
|
|
// Don't want to include
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String closeInvocation() {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private static class ProcessFunction {
|
|
@@ -601,7 +652,12 @@ public class EvaluatorImplementer {
|
|
|
private final BuilderProcessFunctionArg builderArg;
|
|
|
private final List<TypeMirror> warnExceptions;
|
|
|
|
|
|
- private ProcessFunction(ExecutableElement function, List<TypeMirror> warnExceptions) {
|
|
|
+ private ProcessFunction(
|
|
|
+ Elements elements,
|
|
|
+ javax.lang.model.util.Types types,
|
|
|
+ ExecutableElement function,
|
|
|
+ List<TypeMirror> warnExceptions
|
|
|
+ ) {
|
|
|
this.function = function;
|
|
|
args = new ArrayList<>();
|
|
|
BuilderProcessFunctionArg builderArg = null;
|
|
@@ -610,7 +666,14 @@ public class EvaluatorImplementer {
|
|
|
String name = v.getSimpleName().toString();
|
|
|
Fixed fixed = v.getAnnotation(Fixed.class);
|
|
|
if (fixed != null) {
|
|
|
- args.add(new FixedProcessFunctionArg(type, name, fixed.includeInToString()));
|
|
|
+ args.add(
|
|
|
+ new FixedProcessFunctionArg(
|
|
|
+ type,
|
|
|
+ name,
|
|
|
+ fixed.includeInToString(),
|
|
|
+ Types.extendsSuper(types, v.asType(), "org.elasticsearch.core.Releasable")
|
|
|
+ )
|
|
|
+ );
|
|
|
continue;
|
|
|
}
|
|
|
if (type instanceof ClassName c
|