Ver Fonte

Seal Translog.Operation class (#95109)

We assume (i.e. require) that we know all the implementations of
`Translog$Operation`. This commit enforces this at compile time by
making it a `sealed` class.
David Turner há 2 anos atrás
pai
commit
9c95fca70a

+ 4 - 4
server/src/main/java/org/elasticsearch/index/translog/Translog.java

@@ -1058,7 +1058,7 @@ public class Translog extends AbstractIndexShardComponent implements IndexShardC
      * A generic interface representing an operation performed on the transaction log.
      * Each is associated with a type.
      */
-    public abstract static class Operation implements Writeable {
+    public abstract static sealed class Operation implements Writeable permits Delete, Index, NoOp {
         public enum Type {
             @Deprecated
             CREATE((byte) 1),
@@ -1130,7 +1130,7 @@ public class Translog extends AbstractIndexShardComponent implements IndexShardC
         protected abstract void writeBody(StreamOutput out) throws IOException;
     }
 
-    public static class Index extends Operation {
+    public static final class Index extends Operation {
 
         public static final int FORMAT_NO_PARENT = 9; // since 7.0
         public static final int FORMAT_NO_VERSION_TYPE = FORMAT_NO_PARENT + 1;
@@ -1296,7 +1296,7 @@ public class Translog extends AbstractIndexShardComponent implements IndexShardC
 
     }
 
-    public static class Delete extends Operation {
+    public static final class Delete extends Operation {
 
         private static final int FORMAT_6_0 = 4; // 6.0 - *
         public static final int FORMAT_NO_PARENT = FORMAT_6_0 + 1; // since 7.0
@@ -1410,7 +1410,7 @@ public class Translog extends AbstractIndexShardComponent implements IndexShardC
         }
     }
 
-    public static class NoOp extends Operation {
+    public static final class NoOp extends Operation {
         private final String reason;
 
         public String reason() {