Last active
June 16, 2021 10:44
-
-
Save mykeul/2d69f72bcb73ff7a72f167a86329d0a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TODO try to remove javassist dependency with this class | |
public class Re2jHack | |
{ | |
private static final AtomicBoolean PATCHED = new AtomicBoolean(); | |
private static void addMethodUnicodeIsLetter(final ClassPool cp) throws CannotCompileException, NotFoundException | |
{ | |
final CtClass cc = cp.get("com.google.re2j.Unicode"); | |
cc.addMethod( | |
CtMethod.make("static boolean isLetter(int r) { return is(com.google.re2j.UnicodeTables.L, r); }", cc)); | |
cc.toClass(); | |
} | |
public static void patch() | |
{ | |
patch(Arrays.asList()); | |
} | |
/** | |
* Override of the patch() method working with classloaders of Tomcat/war | |
* | |
* @param classes | |
* some Class instances loaded by eventual other classloaders | |
*/ | |
public static void patch(final Iterable<Class<? extends Object>> classes) | |
{ | |
if (PATCHED.compareAndSet(false, true)) | |
{ | |
try | |
{ | |
final ClassPool cp = ClassPool.getDefault(); | |
for (final Class<? extends Object> clazz : classes) | |
cp.insertClassPath(new ClassClassPath(clazz)); | |
patchPR100(cp); | |
} | |
catch (final CannotCompileException | NotFoundException e) | |
{ | |
throw new RuntimeException(e); | |
} | |
} | |
} | |
private static void patchPR100(final ClassPool cp) throws CannotCompileException, NotFoundException | |
{ | |
// @see https://github.com/google/re2j/pull/100 | |
addMethodUnicodeIsLetter(cp); | |
patchUtilsIsWordRune(cp); | |
} | |
private static void patchUtilsIsWordRune(final ClassPool cp) throws CannotCompileException, NotFoundException | |
{ | |
final CtClass cc = cp.get("com.google.re2j.Utils"); | |
cc.getDeclaredMethod("isWordRune") | |
.setBody("return (com.google.re2j.Unicode.isLetter($1) || ('0' <= $1 && $1 <= '9') || $1 == '_');"); | |
cc.toClass(); | |
} | |
private Re2jHack() | |
{ | |
// avoid instances | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment