diff --git a/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/Mapping.java b/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/Mapping.java index 4e24d78..70c2f93 100644 --- a/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/Mapping.java +++ b/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/Mapping.java @@ -122,6 +122,17 @@ public final void removeComponent(@NotNull Class component) components.remove(Objects.requireNonNull(component)); } + /** + * Validates all the components of this mapping + * + * @throws IllegalStateException If any of the component fails validation + */ + public final void validateComponents() throws IllegalStateException {// TODO + for (Component value : components.values()) { + value.validate(); + } + } + /* Auto-generated equals, hashCode and toString methods */ @Override public boolean equals(Object o) { diff --git a/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/component/Component.java b/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/component/Component.java index 635829b..0820bc0 100644 --- a/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/component/Component.java +++ b/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/component/Component.java @@ -26,6 +26,6 @@ public interface Component { * Validates this component. * @throws IllegalStateException if the validation fails */ - default void validate() throws IllegalStateException {// TODO: component validation + default void validate() throws IllegalStateException { } } \ No newline at end of file diff --git a/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/component/Documented.java b/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/component/Documented.java index a0bcb0a..1f80243 100644 --- a/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/component/Documented.java +++ b/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/component/Documented.java @@ -35,13 +35,13 @@ public class Documented implements Component { /** * The contents */ - public final ObjectArrayList contents = new ObjectArrayList<>(); + public final ObjectArrayList<@NotNull String> contents = new ObjectArrayList<>(); /** * Gets the contents * @return The contents */ - public List getContents() { + public List<@NotNull String> getContents() { return contents; } @@ -67,11 +67,19 @@ public void setContentString(@NotNull String content) { if (mark < content.length()) contents.add(content.substring(mark)); } + @Override + public void validate() throws IllegalStateException { + for (String content : contents) { + if (content.indexOf('\n') != -1 || content.indexOf('\r') != -1) + throw new IllegalStateException("The document contains invalid characters"); + } + } + @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Documented that)) return false; - return Objects.equals(contents, that.contents); + return contents.equals(that.contents); } @Override diff --git a/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/component/LineNumber.java b/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/component/LineNumber.java index bdafc9f..d4b33db 100644 --- a/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/component/LineNumber.java +++ b/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/component/LineNumber.java @@ -46,6 +46,12 @@ public void setEndLineNumber(int endLineNumber) { this.endLineNumber = endLineNumber; } + @Override + public void validate() throws IllegalStateException { + if (startLineNumber < 0 || endLineNumber < 0 || startLineNumber > endLineNumber) + throw new IllegalStateException("Invalid line number"); + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/component/LocalVariableTable.java b/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/component/LocalVariableTable.java index 6195769..220aa83 100644 --- a/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/component/LocalVariableTable.java +++ b/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/component/LocalVariableTable.java @@ -82,6 +82,14 @@ public String toString() { } public static class Paired extends LocalVariableTable implements Component { + @Override + public void validate() throws IllegalStateException { + lvt.int2ObjectEntrySet().fastForEach(entry -> { + if (entry.getIntKey() < 0 || entry.getIntKey() > 255) throw new IllegalStateException(); + entry.getValue().validateComponents(); + }); + } + public void reverse() { lvt.values().forEach(PairedMapping::reverse); } @@ -133,6 +141,16 @@ public void setFallbackNamespace(@NotNull String namespace) { } } + @Override + public void validate() throws IllegalStateException { + if (unmappedNamespace == null) throw new IllegalStateException(); + lvt.int2ObjectEntrySet().fastForEach(entry -> { + if (entry.getIntKey() < 0 || entry.getIntKey() > 255) + throw new IllegalStateException("Illegal LVT index"); + entry.getValue().validateComponents(); + }); + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/component/Owned.java b/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/component/Owned.java index 9d0174a..7222d60 100644 --- a/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/component/Owned.java +++ b/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/component/Owned.java @@ -40,6 +40,11 @@ public void setOwner(ClassMapping owner) { this.owner = owner; } + @Override + public void validate() throws IllegalStateException { + if (owner == null) throw new IllegalStateException("Owner is null"); + } + @Override public boolean equals(Object o) { if (this == o) return true;