Skip to content

Commit

Permalink
Component validation
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoPangxie732 committed Sep 25, 2024
1 parent fedfa7a commit 17c29c8
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ public final void removeComponent(@NotNull Class<? extends Component> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ public class Documented implements Component {
/**
* The contents
*/
public final ObjectArrayList<String> contents = new ObjectArrayList<>();
public final ObjectArrayList<@NotNull String> contents = new ObjectArrayList<>();

/**
* Gets the contents
* @return The contents
*/
public List<String> getContents() {
public List<@NotNull String> getContents() {
return contents;
}

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ public String toString() {
}

public static class Paired extends LocalVariableTable<PairedMapping> 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);
}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public void setOwner(ClassMapping<T> 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;
Expand Down

0 comments on commit 17c29c8

Please sign in to comment.