title: How to use Any protobuf type in Java?
body: <p>The code below doesn't work. Is it the best way to use <code>Any</code>? I'm trying to read from/write to <code>Any</code> and identify the type stored by <code>Any</code>.</p>
  <p>My <code>.proto</code>:</p>
  <pre><code>  syntax = &quot;proto3&quot;;
    import &quot;google/protobuf/any.proto&quot;;
    message CommandListPrinters {
    }
    message Commands {
      int32 id = 1;
      repeated google.protobuf.Any command = 2;
    }
  </code></pre>
  <p><code>my.java</code>:</p>
  <pre><code>  CommandListPrinters commandListPrinters = CommandListPrinters.newBuilder().build();
    Any any = Any.newBuilder()
                 .setValue(commandListPrinters.toByteString()).build();
    Commands.Builder commandsBuilder = Commands.newBuilder().setId(0);
    commandsBuilder.addCommand(any);
    Commands commands = commandsBuilder.build();
    //
    byte [] ba = commands.toByteArray();      
    Commands cmds2 = Commands.parseFrom(ba);
    for (Any any2 : cmds2.getCommandList()) {
      Descriptor fe = any2.getDescriptorForType();
      // This IF is FALSE;
      if (fe.equals(CommandListPrinters.getDescriptor()) ) {
        CommandListPrinters cmdLR = CommandListPrinters.parseFrom(any2.getValue());
      }
    }
  </code></pre>