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.StringUtils;
import org.jackhuang.hmcl.util.ToStringBuilder;
import org.jackhuang.hmcl.util.gson.TolerableValidationException;
import org.jackhuang.hmcl.util.gson.Validation;
/**
@ -74,8 +75,8 @@ public class DownloadInfo implements Validation {
}
@Override
public void validate() throws JsonParseException {
public void validate() throws JsonParseException, TolerableValidationException {
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 org.jackhuang.hmcl.util.Immutable;
import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.gson.TolerableValidationException;
/**
*
@ -54,7 +55,7 @@ public class IdDownloadInfo extends DownloadInfo {
}
@Override
public void validate() throws JsonParseException {
public void validate() throws JsonParseException, TolerableValidationException {
super.validate();
if (StringUtils.isBlank(id))

View File

@ -20,6 +20,7 @@ package org.jackhuang.hmcl.game;
import com.google.gson.JsonParseException;
import com.google.gson.annotations.SerializedName;
import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.gson.TolerableValidationException;
import org.jackhuang.hmcl.util.gson.Validation;
/**
@ -66,7 +67,7 @@ public final class LoggingInfo implements Validation {
}
@Override
public void validate() throws JsonParseException {
public void validate() throws JsonParseException, TolerableValidationException {
file.validate();
if (StringUtils.isBlank(argument))
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
* 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?
* When C inherits from B, and B inherits from something else, and finally inherits from C again.
*
* @author huangyuhui
* This exception gets thrown by implementations of {@link Validation#validate()} if you want to replace
* the nullable JSON-parsed object which does not satisfy the constraint with null value.
* @see Validation
*/
public final class CircleDependencyException extends GameException {
public final class TolerableValidationException extends Exception {
public CircleDependencyException() {
}
public CircleDependencyException(String message) {
super(message);
}
public CircleDependencyException(String message, Throwable cause) {
super(message, cause);
public TolerableValidationException() {
}
}

View File

@ -36,5 +36,5 @@ public interface Validation {
*
* @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>() {
@Override
public void write(JsonWriter writer, T t) throws IOException {
if (t instanceof Validation)
((Validation) t).validate();
if (t instanceof Validation) {
try {
((Validation) t).validate();
} catch (TolerableValidationException e) {
delegate.write(writer, null);
}
}
delegate.write(writer, t);
}
@ -49,8 +54,13 @@ public final class ValidationTypeAdapterFactory implements TypeAdapterFactory {
@Override
public T read(JsonReader reader) throws IOException {
T t = delegate.read(reader);
if (t instanceof Validation)
((Validation) t).validate();
if (t instanceof Validation) {
try {
((Validation) t).validate();
} catch (TolerableValidationException e) {
return null;
}
}
return t;
}
};