Skip to content
This repository was archived by the owner on May 12, 2024. It is now read-only.

Commit ff53cd6

Browse files
committed
long RandomGenerator.nextLong(long)
1 parent 49201b2 commit ff53cd6

4 files changed

Lines changed: 98 additions & 1 deletion

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* This file is part of JavaDowngrader - https://github.com/RaphiMC/JavaDowngrader
3+
* Copyright (C) 2023 RK_01/RaphiMC and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package net.raphimc.javadowngrader.runtime.jdk.internal.util.random;
19+
20+
import java.util.Random;
21+
22+
public class RandomSupport {
23+
public static final String BAD_BOUND = "bound must be positive";
24+
25+
public static void checkBound(long bound) {
26+
if (bound <= 0) {
27+
throw new IllegalArgumentException(BAD_BOUND);
28+
}
29+
}
30+
31+
public static long boundedNextLong(Random rng, long bound) {
32+
final long m = bound - 1;
33+
long r = rng.nextLong();
34+
if ((bound & m) == 0L) {
35+
r &= m;
36+
} else {
37+
//noinspection StatementWithEmptyBody
38+
for (long u = r >>> 1;
39+
u + m - (r = u % bound) < 0L;
40+
u = rng.nextLong() >>> 1)
41+
;
42+
}
43+
return r;
44+
}
45+
}

src/main/java/net/raphimc/javadowngrader/transformer/j16/Java17ToJava16.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,23 @@
1818
package net.raphimc.javadowngrader.transformer.j16;
1919

2020
import net.raphimc.javadowngrader.transformer.DowngradingTransformer;
21+
import net.raphimc.javadowngrader.transformer.j16.methodcallreplacer.RandomGeneratorNextLongMCR;
2122
import org.objectweb.asm.Opcodes;
2223

2324
public class Java17ToJava16 extends DowngradingTransformer {
25+
public static final String RANDOM_SUPPORT = "net/raphimc/javadowngrader/runtime/jdk/internal/util/random/RandomSupport";
2426

2527
public Java17ToJava16() {
2628
super(Opcodes.V17, Opcodes.V16);
29+
30+
final String[] randomClasses = {
31+
"java/util/Random",
32+
"java/security/SecureRandom",
33+
"java/util/concurrent/ThreadLocalRandom"
34+
};
35+
for (final String clazz : randomClasses) {
36+
addMethodCallReplacer(Opcodes.INVOKEVIRTUAL, clazz, "nextLong", "(J)J", new RandomGeneratorNextLongMCR());
37+
}
2738
}
2839

2940
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* This file is part of JavaDowngrader - https://github.com/RaphiMC/JavaDowngrader
3+
* Copyright (C) 2023 RK_01/RaphiMC and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package net.raphimc.javadowngrader.transformer.j16.methodcallreplacer;
19+
20+
import net.raphimc.javadowngrader.transformer.MethodCallReplacer;
21+
import org.objectweb.asm.Opcodes;
22+
import org.objectweb.asm.tree.*;
23+
24+
import static net.raphimc.javadowngrader.transformer.j16.Java17ToJava16.RANDOM_SUPPORT;
25+
26+
public class RandomGeneratorNextLongMCR implements MethodCallReplacer {
27+
@Override
28+
public InsnList getReplacement(ClassNode classNode, MethodNode method, String originalName, String originalDesc) {
29+
final InsnList replacement = new InsnList();
30+
31+
// Random long1 long2
32+
replacement.add(new InsnNode(Opcodes.DUP2));
33+
// Random long1 long2 long1 long2
34+
replacement.add(new MethodInsnNode(Opcodes.INVOKESTATIC, RANDOM_SUPPORT, "checkBound", "(J)V"));
35+
// Random long1 long2
36+
replacement.add(new MethodInsnNode(Opcodes.INVOKESTATIC, RANDOM_SUPPORT, "boundedNextLong", "(Ljava/util/Random;J)J"));
37+
// long1 long2
38+
39+
return replacement;
40+
}
41+
}

src/main/java/net/raphimc/javadowngrader/transformer/j8/methodcallreplacer/BufferMCR.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public InsnList getReplacement(ClassNode classNode, MethodNode methodNode, Strin
3535
final String newDesc = Type.getMethodDescriptor(Type.getObjectType("java/nio/Buffer"), Type.getArgumentTypes(originalDesc));
3636

3737
final InsnList replacement = new InsnList();
38-
replacement.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/nio/Buffer", originalName, newDesc));
38+
replacement.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, bufferClass, originalName, newDesc));
3939
replacement.add(new TypeInsnNode(Opcodes.CHECKCAST, this.bufferClass));
4040
return replacement;
4141
}

0 commit comments

Comments
 (0)