Skip to content

Commit

Permalink
Add shortcut to directly return one if two sides are identical in red…
Browse files Browse the repository at this point in the history
…uceField

Implement equals and hashCode for mapping components
  • Loading branch information
XiaoPangxie732 committed Jan 22, 2024
1 parent 67754fd commit cfb8678
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,14 @@ private Optional<PairedMapping> processSuperField(String owner, String name) {
}

private PairedMapping reduceField(PairedMapping left, PairedMapping right) {
if (left == right) return left;
int leftAcc = extraClassesInformation.getAccessFlags(left.getOwned().owner.mapping.unmappedName,
left.unmappedName) & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED | Opcodes.ACC_PRIVATE);
int rightAcc = extraClassesInformation.getAccessFlags(right.getOwned().owner.mapping.unmappedName,
right.unmappedName) & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED | Opcodes.ACC_PRIVATE);
if((leftAcc & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) != 0) {
if((rightAcc & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) != 0) throw new IllegalArgumentException("This can't happen!");
if((rightAcc & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) != 0)
throw new IllegalArgumentException("This can't happen!");
return left;
} else if((rightAcc & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) != 0) return right;
else if(Modifier.isPrivate(leftAcc) || Modifier.isPrivate(rightAcc))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,7 @@ public static void setMappedNamespace(ClassMapping<? extends NameGetter.Namespac
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ClassMapping)) return false;
ClassMapping<?> that = (ClassMapping<?>) o;
if (!(o instanceof ClassMapping<?> that)) return false;
return mapping.equals(that.mapping) && methods.equals(that.methods) && fields.equals(that.fields);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.intellij.lang.annotations.Subst;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

/**
* Descriptor component for paired mappings
*/
Expand All @@ -30,7 +32,7 @@ public class Descriptor implements Component {
public @NotNull String unmappedDescriptor;

public Descriptor(@NotNull String unmappedDescriptor) {
this.unmappedDescriptor = unmappedDescriptor;
this.unmappedDescriptor = Objects.requireNonNull(unmappedDescriptor);
}

public void reverseUnmapped(ClassifiedMappingRemapper remapper) {
Expand All @@ -42,7 +44,7 @@ public void reverseUnmapped(ClassifiedMappingRemapper remapper) {
}

public void setUnmappedDescriptor(@NotNull String unmappedDescriptor) {
this.unmappedDescriptor = unmappedDescriptor;
this.unmappedDescriptor = Objects.requireNonNull(unmappedDescriptor);
}

@Override
Expand All @@ -65,7 +67,7 @@ public static class Mapped implements Component {
public @NotNull String mappedDescriptor;

public Mapped(@NotNull String mappedDescriptor) {
this.mappedDescriptor = mappedDescriptor;
this.mappedDescriptor = Objects.requireNonNull(mappedDescriptor);
}

public void reverseMapped(ClassifiedMappingRemapper remapper) {
Expand All @@ -77,7 +79,7 @@ public void reverseMapped(ClassifiedMappingRemapper remapper) {
}

public void setMappedDescriptor(@NotNull String mappedDescriptor) {
this.mappedDescriptor = mappedDescriptor;
this.mappedDescriptor = Objects.requireNonNull(mappedDescriptor);
}

@Override
Expand All @@ -102,15 +104,15 @@ public static class Namespaced extends Descriptor {

public Namespaced(@NotNull String unmappedDescriptor, @NotNull String descriptorNamespace) {
super(unmappedDescriptor);
this.descriptorNamespace = descriptorNamespace;
this.descriptorNamespace = Objects.requireNonNull(descriptorNamespace);
}

public @NotNull String getDescriptorNamespace() {
return descriptorNamespace;
}

public void setDescriptorNamespace(@NotNull String descriptorNamespace) {
this.descriptorNamespace = descriptorNamespace;
this.descriptorNamespace = Objects.requireNonNull(descriptorNamespace);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,16 @@ public String getDoc() {
public void setDoc(String doc) {
this.doc = Objects.requireNonNull(doc, "null documentation isn't meaningful huh?");
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Documented that)) return false;
return Objects.equals(doc, that.doc);
}

@Override
public int hashCode() {
return Objects.hashCode(doc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,16 @@ public int getEndLineNumber() {
public void setEndLineNumber(int endLineNumber) {
this.endLineNumber = endLineNumber;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof LineNumber that)) return false;
return startLineNumber == that.startLineNumber && endLineNumber == that.endLineNumber;
}

@Override
public int hashCode() {
return 31 * startLineNumber + endLineNumber;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ public void reverseAll(ClassifiedMappingRemapper remapper) {
values.forEach(v -> v.reverse(remapper));
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof LocalVariableTable that)) return false;
return Objects.equals(lvt, that.lvt);
}

@Override
public int hashCode() {
return Objects.hashCode(lvt);
}

public static class Namespaced implements Component, NameGetter.Namespaced {
private String unmappedNamespace;
private String mappedNamespace;
Expand Down Expand Up @@ -96,5 +108,17 @@ public void setMappedNamespace(@NotNull String namespace) {
this.mappedNamespace = Objects.requireNonNull(namespace);
values.forEach(v -> v.setMappedNamespace(mappedNamespace));
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Namespaced that)) return false;
return Objects.equals(unmappedNamespace, that.unmappedNamespace) && Objects.equals(mappedNamespace, that.mappedNamespace) && Objects.equals(lvt, that.lvt);
}

@Override
public int hashCode() {
return Objects.hash(unmappedNamespace, mappedNamespace, lvt);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import cn.maxpixel.mcdecompiler.mapping.Mapping;
import cn.maxpixel.mcdecompiler.mapping.collection.ClassMapping;

import java.util.Objects;

public class Owned<T extends Mapping> implements Component {
public ClassMapping<T> owner;

Expand All @@ -31,4 +33,16 @@ public ClassMapping<T> getOwner() {
public void setOwner(ClassMapping<T> owner) {
this.owner = owner;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Owned<?> owned)) return false;
return Objects.equals(owner.mapping, owned.owner.mapping);
}

@Override
public int hashCode() {
return Objects.hashCode(owner.mapping);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,16 @@ public boolean isStatic() {
public void setStatic(boolean isStatic) {
this.isStatic = isStatic;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof StaticIdentifiable that)) return false;
return isStatic == that.isStatic;
}

@Override
public int hashCode() {
return Boolean.hashCode(isStatic);
}
}

0 comments on commit cfb8678

Please sign in to comment.