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

Commit 1b13afe

Browse files
authored
Merge pull request #13 from ayunami2000/main
Add Java 11 String strip methods
2 parents 9def59c + 1805b30 commit 1b13afe

4 files changed

Lines changed: 133 additions & 0 deletions

File tree

src/main/java/net/raphimc/javadowngrader/transformer/j10/Java11ToJava10.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public Java11ToJava10() {
3232

3333
this.addMethodCallReplacer(Opcodes.INVOKEVIRTUAL, "java/lang/String", "isBlank", "()Z", new StringIsBlankMCR());
3434

35+
this.addMethodCallReplacer(Opcodes.INVOKEVIRTUAL, "java/lang/String", "strip", "()Ljava/lang/String;", new StringStripMCR());
36+
this.addMethodCallReplacer(Opcodes.INVOKEVIRTUAL, "java/lang/String", "stripLeading", "()Ljava/lang/String;", new StringStripLeadingMCR());
37+
this.addMethodCallReplacer(Opcodes.INVOKEVIRTUAL, "java/lang/String", "stripTrailing", "()Ljava/lang/String;", new StringStripTrailingMCR());
38+
3539
this.addMethodCallReplacer(Opcodes.INVOKESTATIC, "java/nio/file/Files", "readString", new FilesReadStringMCR());
3640

3741
this.addMethodCallReplacer(Opcodes.INVOKESTATIC, "java/nio/file/Path", "of", new PathOfMCR());
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.transformer.j10.methodcallreplacer;
19+
20+
import net.raphimc.javadowngrader.RuntimeDepCollector;
21+
import net.raphimc.javadowngrader.transformer.DowngradeResult;
22+
import net.raphimc.javadowngrader.transformer.MethodCallReplacer;
23+
import org.objectweb.asm.Opcodes;
24+
import org.objectweb.asm.tree.*;
25+
26+
public class StringStripLeadingMCR implements MethodCallReplacer {
27+
28+
@Override
29+
public InsnList getReplacement(ClassNode classNode, MethodNode methodNode, String originalName, String originalDesc, RuntimeDepCollector depCollector, DowngradeResult result) {
30+
final InsnList replacement = new InsnList();
31+
32+
// String
33+
replacement.add(new LdcInsnNode("^\\s++"));
34+
// String String
35+
replacement.add(new LdcInsnNode(""));
36+
// String String String
37+
replacement.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/String", "replaceFirst", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"));
38+
// String
39+
40+
return replacement;
41+
}
42+
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.transformer.j10.methodcallreplacer;
19+
20+
import net.raphimc.javadowngrader.RuntimeDepCollector;
21+
import net.raphimc.javadowngrader.transformer.DowngradeResult;
22+
import net.raphimc.javadowngrader.transformer.MethodCallReplacer;
23+
import org.objectweb.asm.Opcodes;
24+
import org.objectweb.asm.tree.*;
25+
26+
public class StringStripMCR implements MethodCallReplacer {
27+
28+
@Override
29+
public InsnList getReplacement(ClassNode classNode, MethodNode methodNode, String originalName, String originalDesc, RuntimeDepCollector depCollector, DowngradeResult result) {
30+
final InsnList replacement = new InsnList();
31+
32+
// String
33+
replacement.add(new LdcInsnNode("^\\s++|\\s++$"));
34+
// String String
35+
replacement.add(new LdcInsnNode(""));
36+
// String String String
37+
replacement.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/String", "replaceAll", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"));
38+
// String
39+
40+
return replacement;
41+
}
42+
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.transformer.j10.methodcallreplacer;
19+
20+
import net.raphimc.javadowngrader.RuntimeDepCollector;
21+
import net.raphimc.javadowngrader.transformer.DowngradeResult;
22+
import net.raphimc.javadowngrader.transformer.MethodCallReplacer;
23+
import org.objectweb.asm.Opcodes;
24+
import org.objectweb.asm.tree.*;
25+
26+
public class StringStripTrailingMCR implements MethodCallReplacer {
27+
28+
@Override
29+
public InsnList getReplacement(ClassNode classNode, MethodNode methodNode, String originalName, String originalDesc, RuntimeDepCollector depCollector, DowngradeResult result) {
30+
final InsnList replacement = new InsnList();
31+
32+
// String
33+
replacement.add(new LdcInsnNode("\\s++$"));
34+
// String String
35+
replacement.add(new LdcInsnNode(""));
36+
// String String String
37+
replacement.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/String", "replaceFirst", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"));
38+
// String
39+
40+
return replacement;
41+
}
42+
43+
}

0 commit comments

Comments
 (0)