Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/gh/worker-api/array-type' CONTRI…
Browse files Browse the repository at this point in the history
…BUTING.md LICENSE README.md build build.gradle.kts buildSrc config gradle gradle.png gradle.properties gradlew gradlew.bat intTestHomeDir released-versions.json settings.gradle.kts subprojects version.txt origin/gh/worker-api/array-type: Capture the array type when isolating an array
  • Loading branch information
big-guy committed Jul 10, 2019
2 parents caeed2e + d233efb commit 8bf1647
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ private <T> T processValue(@Nullable Object value, ValueVisitor<T> visitor) {
if (value.getClass().isArray()) {
int length = Array.getLength(value);
if (length == 0) {
return visitor.emptyArray();
return visitor.emptyArray(value.getClass().getComponentType());
}
ImmutableList.Builder<T> builder = ImmutableList.builderWithExpectedSize(length);
for (int i = 0; i < length; i++) {
Object element = Array.get(value, i);
builder.add(processValue(element, visitor));
}
return visitor.array(builder.build());
return visitor.array(builder.build(), value.getClass().getComponentType());
}
if (value instanceof Attribute) {
return visitor.attributeValue((Attribute<?>) value);
Expand Down Expand Up @@ -199,9 +199,9 @@ private interface ValueVisitor<T> {

T fromIsolatable(Isolatable<?> value);

T emptyArray();
T emptyArray(Class<?> arrayType);

T array(ImmutableList<T> elements);
T array(ImmutableList<T> elements, Class<?> arrayType);

T emptyList();

Expand Down Expand Up @@ -292,12 +292,12 @@ public ValueSnapshot serialized(Object value, byte[] serializedValue) {
}

@Override
public ValueSnapshot emptyArray() {
public ValueSnapshot emptyArray(Class<?> arrayType) {
return ArrayValueSnapshot.EMPTY;
}

@Override
public ValueSnapshot array(ImmutableList<ValueSnapshot> elements) {
public ValueSnapshot array(ImmutableList<ValueSnapshot> elements, Class<?> arrayType) {
return new ArrayValueSnapshot(elements);
}

Expand Down Expand Up @@ -402,13 +402,13 @@ public Isolatable<?> serialized(Object value, byte[] serializedValue) {
}

@Override
public Isolatable<?> emptyArray() {
return IsolatedArray.EMPTY;
public Isolatable<?> emptyArray(Class<?> arrayType) {
return IsolatedArray.empty(arrayType);
}

@Override
public Isolatable<?> array(ImmutableList<Isolatable<?>> elements) {
return new IsolatedArray(elements);
public Isolatable<?> array(ImmutableList<Isolatable<?>> elements, Class<?> arrayType) {
return new IsolatedArray(elements, arrayType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
import org.gradle.internal.snapshot.ValueSnapshot;

import javax.annotation.Nullable;
import java.lang.reflect.Array;

public class IsolatedArray extends AbstractArraySnapshot<Isolatable<?>> implements Isolatable<Object[]> {
public static final IsolatedArray EMPTY = new IsolatedArray(ImmutableList.of());
public static final IsolatedArray EMPTY = empty(Object.class);
private final Class<?> arrayType;

public IsolatedArray(ImmutableList<Isolatable<?>> elements) {
public IsolatedArray(ImmutableList<Isolatable<?>> elements, Class<?> arrayType) {
super(elements);
this.arrayType = arrayType;
}

@Override
Expand All @@ -43,7 +46,7 @@ public ValueSnapshot asSnapshot() {

@Override
public Object[] isolate() {
Object[] toReturn = new Object[elements.size()];
Object[] toReturn = (Object[]) Array.newInstance(arrayType, elements.size());
for (int i = 0; i < elements.size(); i++) {
Isolatable<?> element = elements.get(i);
toReturn[i] = element.isolate();
Expand All @@ -56,4 +59,12 @@ public Object[] isolate() {
public <S> S coerce(Class<S> type) {
return null;
}

public Class<?> getArrayType() {
return arrayType;
}

public static IsolatedArray empty(Class<?> arrayType) {
return new IsolatedArray(ImmutableList.of(), arrayType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,28 @@ class WorkerExecutorParametersIntegrationTest extends AbstractIntegrationSpec {
isolationMode << ISOLATION_MODES
}

def "can provide array parameters with isolation mode #isolationMode"() {
buildFile << """
ext.testObject = ["foo", "bar"] as String[]
${parameterRunnableWithType('String[]', 'println param.join(",")') }
task runWork(type: ParameterTask) {
isolationMode = ${isolationMode}
params = [testObject]
}
"""

when:
succeeds("runWork")

then:
outputContains("foo,bar")

where:
isolationMode << ISOLATION_MODES
}

def "can provide list property parameters with isolation mode #isolationMode"() {
buildFile << """
ext.testObject = objects.listProperty(String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,14 +475,16 @@ private class IsolatedArraySerializer implements IsolatableSerializer<IsolatedAr
@Override
public void write(Encoder encoder, IsolatedArray value) throws Exception {
encoder.writeByte(ISOLATED_ARRAY);
encoder.writeString(value.getArrayType().getName());
writeIsolatableSequence(encoder, value.getElements());
}

@Override
public IsolatedArray read(Decoder decoder) throws Exception {
ImmutableList.Builder<Isolatable<?>> builder = ImmutableList.builder();
Class<?> arrayType = fromClassName(decoder.readString());
readIsolatableSequence(decoder, builder);
return new IsolatedArray(builder.build());
return new IsolatedArray(builder.build(), arrayType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ class IsolatableSerializerRegistryTest extends Specification {

then:
newIsolatables[0].isolate() == array

and:
newIsolatables[0].isolate().class == String[].class
}

def "can serialize/deserialize isolated List"() {
Expand Down

0 comments on commit 8bf1647

Please sign in to comment.