# GenericRemoteMessage

Artifact: [androidx.test.espresso:espresso-remote](https://developer.android.com/jetpack/androidx/releases/test-espresso) [View Source](https://cs.android.com/search?q=file:androidx/test/espresso/remote/GenericRemoteMessage.java+class:androidx.test.espresso.remote.GenericRemoteMessage)

*** ** * ** ***


```
class GenericRemoteMessage : EspressoRemoteMessage.To<MessageLite!>
```

<br />

*** ** * ** ***

Generic implementation of the `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/EspressoRemoteMessage` interface, which uses reflection for proto message serialization and deserialization.

Every Espresso matcher, view action or view assertion needs to support proto serialization to participate in any kind of cross process UI interaction using Espresso Remote.

Each serializable type T needs to provide two things. First a proto message declaration of type T. Second an `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/EspressoRemoteMessage` class which implements the `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/EspressoRemoteMessage.To` and `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/EspressoRemoteMessage.From` interfaces to serialize/deserialization any state into their proto message equivalent.

This `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/GenericRemoteMessage` class is special type of `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/EspressoRemoteMessage`, which implements the the `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/EspressoRemoteMessage.To` and `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/EspressoRemoteMessage.From` interfaces and uses reflection to perform serialisation of an object of type T into its proto message representation.

Espresso Remote uses a global `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/RemoteDescriptorRegistry` for `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/RemoteDescriptor` lookups. Where a `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/RemoteDescriptor` is a descriptor object which contains all the necessary meta information for proto serialization.

Usage:

For a type `Foo` with fields `bar` and `baz`:

```kotlin
public class Foo {
  private final String bar;
  private final int baz;

  protected Foo(String bar, int baz) { // set fields }
}
```

Note: A protected constructor with serializable fields in declared order is required for proto deserialization.

Create a corresponding proto definition, `FooProto.proto` for `Foo`:

```kotlin
message FooProto {
  bytes bar = 1; // Name needs to match Foo#bar
  bytes baz = 2; // Name needs to match Foo#baz
}
```

The proto type definitions used with `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/GenericRemoteMessage` need to be of type `
byte` and the type names must match the variable name of the serializable fields in `
Foo.java`.

The last step is to create a `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/RemoteDescriptor` using a `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/RemoteDescriptor.Builder`, to configure all the meta data required for generic serialization. Finally register the descriptor with the `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/RemoteDescriptorRegistry`. A typical descriptor builder looks like this:

```kotlin
new RemoteDescriptor.Builder()
  .setInstanceType(Foo.class)
  .setInstanceFieldDescriptors(
    FieldDescriptor.of(String.class, "bar")),
    FieldDescriptor.of(int.class, 32))
  .setRemoteType(GenericRemoteMessage.class)
  .setProtoType(FooProto.class)
  .build();
```

First set the instance type, `Foo.class`. Then specify the serializable fields using a `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/FieldDescriptor`. Where a `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/FieldDescriptor` represents the name and the type of a reflective field of `Foo`. Any fields described by the field properties and passed to `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/RemoteDescriptor.Builder#setInstanceFieldDescriptors(androidx.test.espresso.remote.FieldDescriptor...)` will be serialised into `FooProto.proto`. Next specify that type `Foo` will use for it's serialization entry point, in this case `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/GenericRemoteMessage` and lastly set the proto message class.

Note: The declared field properties order, must match the protected constructor of `
Foo`, which takes the serializable fields in declared order!

| Throws |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/RemoteProtocolException` | if a `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/FieldDescriptor`s field name does not match any field in the declared type or proto message. |
| `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/RemoteProtocolException` | if type cannot be serialised or dematerialised |

## Summary

| ### Constants |
|---|---|
| `const https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/EspressoRemoteMessage.From<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html!, https://protobuf.dev/reference/java/api-docs/com/google/protobuf/MessageLite.html!>!` | `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/GenericRemoteMessage#FROM()` This is used to create and deserialize a proto message into an instance type |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/GenericRemoteMessage#GenericRemoteMessage(java.lang.Object)(instance: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)` Creates a new `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/GenericRemoteMessage`. |

| ### Public functions |
|---|---|
| `https://protobuf.dev/reference/java/api-docs/com/google/protobuf/MessageLite.html!` | `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/GenericRemoteMessage#toProto()()` |

## Constants

### FROM

```
const val FROM: EspressoRemoteMessage.From<Any!, MessageLite!>!
```

This is used to create and deserialize a proto message into an instance type

## Public constructors

### GenericRemoteMessage

```
GenericRemoteMessage(instance: Any)
```

Creates a new `https://developer.android.com/reference/kotlin/androidx/test/espresso/remote/GenericRemoteMessage`.

This constructor is called reflectively and should not be used.

| Parameters |
|---|---|
| `instance: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html` | the object that needs to be serialized into a proto |

## Public functions

### toProto

```
fun toProto(): MessageLite!
```