From a5824f7325b1881cfad61dc2df19eabfca8c9d23 Mon Sep 17 00:00:00 2001 From: olly Date: Wed, 13 May 2020 11:58:56 +0100 Subject: [PATCH] Restrict Decoder generic exception type to extend DecoderException This is useful for merging the FFmpeg video support pull request, since it allows a Decoder base class implementation to catch DecoderException rather than forcing it to catch Exception (i.e., everything). Note that unfortunately, Java doesn't allow catching of a generic type (i.e., you cannot "catch (E e)") due to type erasure. PiperOrigin-RevId: 311300719 --- .../android/exoplayer2/decoder/Decoder.java | 2 +- .../exoplayer2/decoder/DecoderException.java | 26 +++++++++++++++---- .../exoplayer2/decoder/SimpleDecoder.java | 2 +- .../text/SubtitleDecoderException.java | 18 ++++++------- 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/decoder/Decoder.java b/library/core/src/main/java/com/google/android/exoplayer2/decoder/Decoder.java index 4552d190c3..c94eb2c38a 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/decoder/Decoder.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/decoder/Decoder.java @@ -24,7 +24,7 @@ import androidx.annotation.Nullable; * @param The type of buffer output from the decoder. * @param The type of exception thrown from the decoder. */ -public interface Decoder { +public interface Decoder { /** * Returns the name of the decoder. diff --git a/library/core/src/main/java/com/google/android/exoplayer2/decoder/DecoderException.java b/library/core/src/main/java/com/google/android/exoplayer2/decoder/DecoderException.java index c07e646f09..0af3313ea3 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/decoder/DecoderException.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/decoder/DecoderException.java @@ -15,20 +15,36 @@ */ package com.google.android.exoplayer2.decoder; +import androidx.annotation.Nullable; + /** Thrown when a {@link Decoder} error occurs. */ public class DecoderException extends Exception { - /** @param message The detail message for this exception. */ + /** + * Creates an instance. + * + * @param message The detail message for this exception. + */ public DecoderException(String message) { super(message); } /** - * @param message The detail message for this exception. - * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method). - * A null value is permitted, and indicates that the cause is nonexistent or unknown. + * Creates an instance. + * + * @param cause The cause of this exception, or {@code null}. */ - public DecoderException(String message, Throwable cause) { + public DecoderException(@Nullable Throwable cause) { + super(cause); + } + + /** + * Creates an instance. + * + * @param message The detail message for this exception. + * @param cause The cause of this exception, or {@code null}. + */ + public DecoderException(String message, @Nullable Throwable cause) { super(message, cause); } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/decoder/SimpleDecoder.java b/library/core/src/main/java/com/google/android/exoplayer2/decoder/SimpleDecoder.java index 8f660c4c24..beaea6cf37 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/decoder/SimpleDecoder.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/decoder/SimpleDecoder.java @@ -27,7 +27,7 @@ import java.util.ArrayDeque; */ @SuppressWarnings("UngroupedOverloads") public abstract class SimpleDecoder< - I extends DecoderInputBuffer, O extends OutputBuffer, E extends Exception> + I extends DecoderInputBuffer, O extends OutputBuffer, E extends DecoderException> implements Decoder { private final Thread decodeThread; diff --git a/library/core/src/main/java/com/google/android/exoplayer2/text/SubtitleDecoderException.java b/library/core/src/main/java/com/google/android/exoplayer2/text/SubtitleDecoderException.java index b235706370..7de577f18c 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/text/SubtitleDecoderException.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/text/SubtitleDecoderException.java @@ -15,10 +15,11 @@ */ package com.google.android.exoplayer2.text; -/** - * Thrown when an error occurs decoding subtitle data. - */ -public class SubtitleDecoderException extends Exception { +import androidx.annotation.Nullable; +import com.google.android.exoplayer2.decoder.DecoderException; + +/** Thrown when an error occurs decoding subtitle data. */ +public class SubtitleDecoderException extends DecoderException { /** * @param message The detail message for this exception. @@ -27,17 +28,16 @@ public class SubtitleDecoderException extends Exception { super(message); } - /** @param cause The cause of this exception. */ - public SubtitleDecoderException(Exception cause) { + /** @param cause The cause of this exception, or {@code null}. */ + public SubtitleDecoderException(@Nullable Throwable cause) { super(cause); } /** * @param message The detail message for this exception. - * @param cause The cause of this exception. + * @param cause The cause of this exception, or {@code null}. */ - public SubtitleDecoderException(String message, Throwable cause) { + public SubtitleDecoderException(String message, @Nullable Throwable cause) { super(message, cause); } - }