Skip to content

Commit b39a9db

Browse files
committed
Call clone() before calling extension methods if auto clone is enabled.
Otherwise when our extension methods mutate the RequestOptions we pass them, the mutations create a new RequestOptions object that’s dropped at the end of the extension method, which causes any options applied in the extension method to be ignored. Cloning first matches the behavior of other RequestOptions methods and ensures that the RequestOptions object passed into the extension method is mutable.
1 parent 48754e6 commit b39a9db

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

annotation/compiler/src/main/java/com/bumptech/glide/annotation/compiler/RequestOptionsGenerator.java

+20-13
Original file line numberDiff line numberDiff line change
@@ -231,21 +231,28 @@ private List<MethodAndStaticVar> generateMethodsForRequestOptionsExtension(
231231
List<? extends VariableElement> parameters =
232232
element.getParameters().subList(1, element.getParameters().size());
233233

234-
// Add the correct super() call.
235-
if (overrideType == OVERRIDE_EXTEND) {
236-
String callSuper = "super.$L(";
237-
List<Object> args = new ArrayList<>();
238-
args.add(element.getSimpleName().toString());
239-
if (!parameters.isEmpty()) {
240-
for (VariableElement variable : parameters) {
241-
callSuper += "$L, ";
242-
args.add(variable.getSimpleName().toString());
243-
}
244-
callSuper = callSuper.substring(0, callSuper.length() - 2);
234+
// Generates the String and list of arguments to pass in when calling this method or super.
235+
// IE centerCrop(context) creates methodLiterals="%L" and methodArgs=[centerCrop, context].
236+
List<Object> methodArgs = new ArrayList<>();
237+
methodArgs.add(element.getSimpleName().toString());
238+
String methodLiterals = "";
239+
if (!parameters.isEmpty()) {
240+
for (VariableElement variable : parameters) {
241+
methodLiterals += "$L, ";
242+
methodArgs.add(variable.getSimpleName().toString());
245243
}
246-
callSuper += ")";
244+
methodLiterals = methodLiterals.substring(0, methodLiterals.length() - 2);
245+
}
246+
247+
builder.beginControlFlow("if (isAutoCloneEnabled())")
248+
.addStatement(
249+
"return clone().$N(" + methodLiterals + ")", methodArgs.toArray(new Object[0]))
250+
.endControlFlow();
247251

248-
builder.addStatement(callSuper, args.toArray(new Object[0]))
252+
// Add the correct super() call.
253+
if (overrideType == OVERRIDE_EXTEND) {
254+
String callSuper = "super.$L(" + methodLiterals + ")";
255+
builder.addStatement(callSuper, methodArgs.toArray(new Object[0]))
249256
.addJavadoc(processorUtil.generateSeeMethodJavadoc(
250257
requestOptionsName, methodName, parameters))
251258
.addAnnotation(Override.class);

library/src/main/java/com/bumptech/glide/request/RequestOptions.java

+4
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,10 @@ private RequestOptions selfOrThrowIfLocked() {
12881288
return this;
12891289
}
12901290

1291+
protected boolean isAutoCloneEnabled() {
1292+
return isAutoCloneEnabled;
1293+
}
1294+
12911295
@NonNull
12921296
public final Map<Class<?>, Transformation<?>> getTransformations() {
12931297
return transformations;

0 commit comments

Comments
 (0)