1.1 --- a/rt/emul/mini/src/main/java/java/lang/String.java Thu Oct 10 09:33:40 2013 +0200
1.2 +++ b/rt/emul/mini/src/main/java/java/lang/String.java Mon Oct 21 15:16:04 2013 +0200
1.3 @@ -26,6 +26,8 @@
1.4 package java.lang;
1.5
1.6 import java.io.UnsupportedEncodingException;
1.7 +import java.lang.reflect.InvocationTargetException;
1.8 +import java.lang.reflect.Method;
1.9 import java.util.Comparator;
1.10 import java.util.Locale;
1.11 import org.apidesign.bck2brwsr.core.ExtraJavaScript;
1.12 @@ -2098,13 +2100,31 @@
1.13 * @since 1.4
1.14 * @spec JSR-51
1.15 */
1.16 + public boolean matches(String regex) {
1.17 + try {
1.18 + return matchesViaJS(regex);
1.19 + } catch (Throwable t) {
1.20 + // fallback to classical behavior
1.21 + try {
1.22 + Method m = Class.forName("java.util.regex.Pattern").getMethod("matches", String.class, CharSequence.class);
1.23 + return (Boolean)m.invoke(null, regex, this);
1.24 + } catch (InvocationTargetException ex) {
1.25 + if (ex.getTargetException() instanceof RuntimeException) {
1.26 + throw (RuntimeException)ex.getTargetException();
1.27 + }
1.28 + } catch (Throwable another) {
1.29 + // will report the old one
1.30 + }
1.31 + throw new RuntimeException(t);
1.32 + }
1.33 + }
1.34 @JavaScriptBody(args = { "regex" }, body =
1.35 "var self = this.toString();\n"
1.36 + "var re = new RegExp(regex.toString());\n"
1.37 + "var r = re.exec(self);\n"
1.38 + "return r != null && r.length > 0 && self.length == r[0].length;"
1.39 )
1.40 - public boolean matches(String regex) {
1.41 + private boolean matchesViaJS(String regex) {
1.42 throw new UnsupportedOperationException();
1.43 }
1.44