DownloadInfo.url can be null in forge 1.13.2 version json

This commit is contained in:
huanghongxun 2019-02-06 13:35:16 +08:00
parent 68a02966d0
commit 38725abc48
6 changed files with 28 additions and 24 deletions

View File

@ -22,6 +22,7 @@ import com.google.gson.annotations.SerializedName;
import org.jackhuang.hmcl.util.Immutable; import org.jackhuang.hmcl.util.Immutable;
import org.jackhuang.hmcl.util.StringUtils; import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.ToStringBuilder; import org.jackhuang.hmcl.util.ToStringBuilder;
import org.jackhuang.hmcl.util.gson.TolerableValidationException;
import org.jackhuang.hmcl.util.gson.Validation; import org.jackhuang.hmcl.util.gson.Validation;
/** /**
@ -74,8 +75,8 @@ public class DownloadInfo implements Validation {
} }
@Override @Override
public void validate() throws JsonParseException { public void validate() throws JsonParseException, TolerableValidationException {
if (StringUtils.isBlank(url)) if (StringUtils.isBlank(url))
throw new JsonParseException("DownloadInfo url can not be null"); throw new TolerableValidationException();
} }
} }

View File

@ -21,6 +21,7 @@ import com.google.gson.JsonParseException;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import org.jackhuang.hmcl.util.Immutable; import org.jackhuang.hmcl.util.Immutable;
import org.jackhuang.hmcl.util.StringUtils; import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.gson.TolerableValidationException;
/** /**
* *
@ -54,7 +55,7 @@ public class IdDownloadInfo extends DownloadInfo {
} }
@Override @Override
public void validate() throws JsonParseException { public void validate() throws JsonParseException, TolerableValidationException {
super.validate(); super.validate();
if (StringUtils.isBlank(id)) if (StringUtils.isBlank(id))

View File

@ -20,6 +20,7 @@ package org.jackhuang.hmcl.game;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import org.jackhuang.hmcl.util.StringUtils; import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.gson.TolerableValidationException;
import org.jackhuang.hmcl.util.gson.Validation; import org.jackhuang.hmcl.util.gson.Validation;
/** /**
@ -66,7 +67,7 @@ public final class LoggingInfo implements Validation {
} }
@Override @Override
public void validate() throws JsonParseException { public void validate() throws JsonParseException, TolerableValidationException {
file.validate(); file.validate();
if (StringUtils.isBlank(argument)) if (StringUtils.isBlank(argument))
throw new JsonParseException("LoggingInfo.argument is empty."); throw new JsonParseException("LoggingInfo.argument is empty.");

View File

@ -15,24 +15,15 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package org.jackhuang.hmcl.game; package org.jackhuang.hmcl.util.gson;
/** /**
* What's circle dependency? * This exception gets thrown by implementations of {@link Validation#validate()} if you want to replace
* When C inherits from B, and B inherits from something else, and finally inherits from C again. * the nullable JSON-parsed object which does not satisfy the constraint with null value.
* * @see Validation
* @author huangyuhui
*/ */
public final class CircleDependencyException extends GameException { public final class TolerableValidationException extends Exception {
public CircleDependencyException() { public TolerableValidationException() {
}
public CircleDependencyException(String message) {
super(message);
}
public CircleDependencyException(String message, Throwable cause) {
super(message, cause);
} }
} }

View File

@ -36,5 +36,5 @@ public interface Validation {
* *
* @throws JsonParseException if fields are filled in wrong format or wrong type. * @throws JsonParseException if fields are filled in wrong format or wrong type.
*/ */
void validate() throws JsonParseException; void validate() throws JsonParseException, TolerableValidationException;
} }

View File

@ -40,8 +40,13 @@ public final class ValidationTypeAdapterFactory implements TypeAdapterFactory {
return new TypeAdapter<T>() { return new TypeAdapter<T>() {
@Override @Override
public void write(JsonWriter writer, T t) throws IOException { public void write(JsonWriter writer, T t) throws IOException {
if (t instanceof Validation) if (t instanceof Validation) {
((Validation) t).validate(); try {
((Validation) t).validate();
} catch (TolerableValidationException e) {
delegate.write(writer, null);
}
}
delegate.write(writer, t); delegate.write(writer, t);
} }
@ -49,8 +54,13 @@ public final class ValidationTypeAdapterFactory implements TypeAdapterFactory {
@Override @Override
public T read(JsonReader reader) throws IOException { public T read(JsonReader reader) throws IOException {
T t = delegate.read(reader); T t = delegate.read(reader);
if (t instanceof Validation) if (t instanceof Validation) {
((Validation) t).validate(); try {
((Validation) t).validate();
} catch (TolerableValidationException e) {
return null;
}
}
return t; return t;
} }
}; };