harden compareTo() implementations

This commit is contained in:
Adrian Siekierka authored and asie committed 2019-09-16 19:20:41 +02:00
1 parent ec491e3b5d
commit a42e86b62e
6 files changed
+60 -12

No files matched your search

@@ -49,6 +49,7 @@ import org.spongepowered.asm.service.IMixinService;
import org.spongepowered.asm.service.ITransformer;
import org.spongepowered.asm.service.MixinService;
import org.spongepowered.asm.service.MixinServiceAbstract;
import org.spongepowered.asm.util.CompareUtil;
import org.spongepowered.asm.util.Constants;
import org.spongepowered.asm.util.IConsumer;
import org.spongepowered.asm.util.ITokenProvider;
@@ -812,13 +813,11 @@ public final class MixinEnvironment implements ITokenProvider {
@Override
public int compareTo(TokenProviderWrapper other) {
if (other == null) {
return 0;
}
if (other.priority == this.priority) {
return other.order - this.order;
return CompareUtil.compare(other.order, this.order);
} else {
return (other.priority < this.priority) ? -1 : 1;
}
return (other.priority - this.priority);
}
public IEnvironmentTokenProvider getProvider() {
@@ -30,6 +30,7 @@ import java.util.Map;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.spongepowered.asm.util.Bytecode;
import org.spongepowered.asm.util.CompareUtil;
/**
* Used to keep track of instruction nodes in a {@link Target} method which are
@@ -197,7 +198,7 @@ public class InjectionNodes extends ArrayList<InjectionNodes.InjectionNode> {
*/
@Override
public int compareTo(InjectionNode other) {
return other == null ? Integer.MAX_VALUE : this.hashCode() - other.hashCode();
return other == null ? Integer.MAX_VALUE : CompareUtil.compare(this.hashCode(), other.hashCode());
}
/* (non-Javadoc)
@@ -52,6 +52,7 @@ import org.spongepowered.asm.mixin.refmap.RemappingReferenceMapper;
import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException;
import org.spongepowered.asm.service.IMixinService;
import org.spongepowered.asm.service.MixinService;
import org.spongepowered.asm.util.CompareUtil;
import org.spongepowered.asm.util.VersionNumber;
import com.google.common.base.Strings;
@@ -1000,9 +1001,10 @@ final class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
return 0;
}
if (other.priority == this.priority) {
return this.order - other.order;
return CompareUtil.compare(this.order, other.order);
} else {
return (this.priority < other.priority) ? -1 : 1;
}
return (this.priority - other.priority);
}
/**
@@ -67,6 +67,7 @@ import org.spongepowered.asm.service.IMixinService;
import org.spongepowered.asm.service.MixinService;
import org.spongepowered.asm.util.Annotations;
import org.spongepowered.asm.util.Bytecode;
import org.spongepowered.asm.util.CompareUtil;
import org.spongepowered.asm.util.perf.Profiler;
import org.spongepowered.asm.util.perf.Profiler.Section;
@@ -1200,9 +1201,10 @@ class MixinInfo implements Comparable<MixinInfo>, IMixinInfo {
return 0;
}
if (other.priority == this.priority) {
return this.order - other.order;
return CompareUtil.compare(this.order, other.order);
} else {
return (this.priority < other.priority) ? -1 : 1;
}
return (this.priority - other.priority);
}
/**
@@ -0,0 +1,45 @@
/*
* This file is part of Mixin, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.asm.util;
/**
* Comparison helpers.
*/
public final class CompareUtil {
private CompareUtil() {
}
/**
* Officially added in Java 7.
*/
public static int compare(int a, int b) {
return ((a < b) ? -1 : ((a == b) ? 0 : 1));
}
public static int compare(long a, long b) {
return ((a < b) ? -1 : ((a == b) ? 0 : 1));
}
}
@@ -106,8 +106,7 @@ public final class VersionNumber implements Comparable<VersionNumber>, Serializa
if (other == null) {
return 1;
}
long delta = this.value - other.value;
return delta > 0 ? 1 : delta < 0 ? -1 : 0;
return CompareUtil.compare(this.value, other.value);
}
/* (non-Javadoc)