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

Commit 0c4e508

Browse files
committed
Improved code
1 parent 8957214 commit 0c4e508

3 files changed

Lines changed: 41 additions & 11 deletions

File tree

bootstrap/src/main/java/net/raphimc/javadowngrader/bootstrap/AgentMain.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import net.raphimc.javadowngrader.impl.classtransform.util.ClassNameUtil;
2424
import net.raphimc.javadowngrader.runtime.RuntimeRoot;
2525
import net.raphimc.javadowngrader.util.Constants;
26-
import net.raphimc.javadowngrader.util.JavaVersion;
2726

2827
import java.io.IOException;
2928
import java.io.UncheckedIOException;
@@ -67,16 +66,7 @@ public static void agentmain(String args, Instrumentation instrumentation) throw
6766
transformerManager.addBytecodeTransformer(new JavaDowngraderTransformer(transformerManager));
6867
transformerManager.hookInstrumentation(instrumentation);
6968

70-
if (System.getProperty("spoofJavaVersion") != null) {
71-
final JavaVersion spoofedJavaVersion = JavaVersion.getByName(System.getProperty("spoofJavaVersion"));
72-
if (spoofedJavaVersion == null) {
73-
System.err.println("Unable to find version '" + System.getProperty("spoofJavaVersion") + "'");
74-
System.exit(-1);
75-
}
76-
System.setProperty("java.version", spoofedJavaVersion.getFakeJavaVersionName());
77-
System.setProperty("java.class.version", String.valueOf(spoofedJavaVersion.getVersion()));
78-
System.setProperty("java.specification.version", spoofedJavaVersion.getFakeSpecificationVersionName());
79-
}
69+
JavaVersionSpoofer.modifyProperties();
8070
}
8171

8272
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 3 of the License, or (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.bootstrap;
19+
20+
import net.raphimc.javadowngrader.util.JavaVersion;
21+
22+
public class JavaVersionSpoofer {
23+
24+
public static void modifyProperties() {
25+
if (System.getProperty("spoofJavaVersion") != null) {
26+
final JavaVersion spoofedJavaVersion = JavaVersion.getByName(System.getProperty("spoofJavaVersion"));
27+
if (spoofedJavaVersion == null) {
28+
System.err.println("Unable to find version '" + System.getProperty("spoofJavaVersion") + "'");
29+
System.exit(-1);
30+
}
31+
System.setProperty("java.version", spoofedJavaVersion.getFakeJavaVersionName());
32+
System.setProperty("java.class.version", String.valueOf(spoofedJavaVersion.getVersion()));
33+
System.setProperty("java.specification.version", spoofedJavaVersion.getFakeSpecificationVersionName());
34+
}
35+
}
36+
37+
}

bootstrap/src/main/java/net/raphimc/javadowngrader/bootstrap/Main.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public static void main(String[] args) throws ClassNotFoundException {
3838
final String jarPath = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath();
3939
final String jarName = jarPath.substring(jarPath.lastIndexOf('/') + 1);
4040
System.out.println("Usage: java -jar " + jarName + " <jar> [args]");
41+
System.out.println("Usage as java agent: java -javaagent:" + jarName + " -jar ...");
4142
return;
4243
}
4344

@@ -70,6 +71,8 @@ public static void main(String[] args) throws ClassNotFoundException {
7071
final InjectionClassLoader injectionClassLoader = new InjectionClassLoader(transformerManager, urls);
7172
Thread.currentThread().setContextClassLoader(injectionClassLoader);
7273
Methods.invoke(null, Methods.getDeclaredMethod(injectionClassLoader.loadClass(mainClass), "main", String[].class), (Object) args);
74+
75+
JavaVersionSpoofer.modifyProperties();
7376
}
7477

7578
}

0 commit comments

Comments
 (0)