changeset 53407:20cad94ca972 intrinsics-project

Pattern matching
author jlaskey
date Tue, 13 Nov 2018 13:46:19 -0400
parents d1bdbb71f3a0
children 2c83f64aa372
files src/java.base/share/classes/java/lang/invoke/IntrinsicFactory.java src/java.base/share/classes/java/lang/invoke/StringBootstraps.java src/jdk.compiler/share/classes/com/sun/tools/javac/intrinsics/StringProcessorFactory.java
diffstat 3 files changed, 113 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/lang/invoke/IntrinsicFactory.java	Tue Nov 13 15:00:34 2018 +0100
+++ b/src/java.base/share/classes/java/lang/invoke/IntrinsicFactory.java	Tue Nov 13 13:46:19 2018 -0400
@@ -321,4 +321,23 @@
         return ObjectsBootstraps.hashBootstrap(lookup, name, methodType);
     }
 
+    /**
+     * stringMatchesBootstrap bootstrap.
+     * @param lookup      MethodHandles lookup
+     * @param name        Name of method
+     * @param methodType  Method signature
+     * @param pattern     Pattern string
+     * @throws NoSuchMethodException no such method
+     * @throws IllegalAccessException illegal access
+     * @throws StringConcatException string concat error
+     * @return Callsite for intrinsic method
+     */
+    public static CallSite stringMatchesBootstrap(MethodHandles.Lookup lookup,
+                                                  String name,
+                                                  MethodType methodType,
+                                                  String pattern)
+            throws NoSuchMethodException, IllegalAccessException {
+        return StringBootstraps.patternMatchBootstrap(lookup, name, methodType, pattern);
+    }
+
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/java.base/share/classes/java/lang/invoke/StringBootstraps.java	Tue Nov 13 13:46:19 2018 -0400
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package java.lang.invoke;
+
+import java.lang.invoke.ConstantCallSite;
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Bootstrapping support for String intrinsics.
+ */
+public final class StringBootstraps {
+    /**
+     * Bootstrap for String intrinsics.
+     * @param lookup         MethodHandles lookup
+     * @param name           Name of method
+     * @param methodType     Method signature
+     * @param pattern        Pattern string
+     * @return Callsite for intrinsic method
+     * @throws IllegalAccessException illegal access
+     * @throws NoSuchMethodException no such method
+     */
+    public static CallSite patternMatchBootstrap(MethodHandles.Lookup lookup,
+                                                 String name,
+                                                 MethodType methodType,
+                                                 String pattern)
+        throws NoSuchMethodException, IllegalAccessException {
+        assert methodType.returnType() == boolean.class;
+
+        MethodType matcherMethodType = MethodType.methodType(Matcher.class, CharSequence.class);
+        MethodType matcherMethodTypeCast = MethodType.methodType(Matcher.class, Pattern.class, String.class);
+        MethodType matchesMethodType = MethodType.methodType(boolean.class);
+
+        MethodHandle matcher = lookup.findVirtual(Pattern.class, "matcher", matcherMethodType).
+                asType(matcherMethodTypeCast);
+        MethodHandle matches = lookup.findVirtual(Matcher.class, "matches", matchesMethodType);
+
+        return new ConstantCallSite(
+                MethodHandles.filterArguments(matches, 0, matcher.bindTo(Pattern.compile(pattern))));
+    }
+}
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/intrinsics/StringProcessorFactory.java	Tue Nov 13 15:00:34 2018 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/intrinsics/StringProcessorFactory.java	Tue Nov 13 13:46:19 2018 -0400
@@ -27,6 +27,8 @@
 
 import java.lang.constant.ClassDesc;
 import java.lang.constant.ConstantDesc;
+import java.lang.constant.ConstantDescs;
+import java.lang.constant.DynamicCallSiteDesc;
 import java.lang.constant.MethodTypeDesc;
 
 /**
@@ -119,7 +121,31 @@
                             }
                         }
                 }
-             }
+
+                switch (methodName) {
+                    case "matches":
+                        ConstantDesc constantPattern = constantArgs[1];
+
+                        if (constantPattern == null) {
+                            return new Result.None();
+                        }
+
+                        MethodTypeDesc methodTypeLessPattern = methodType.dropParameterTypes(0, 1);
+
+                        return new Result.Indy(
+                                DynamicCallSiteDesc.of(
+                                        ConstantDescs.ofCallsiteBootstrap(
+                                                ClassDesc.of("java.lang.invoke.IntrinsicFactory"),
+                                                "stringMatchesBootstrap",
+                                                ClassDesc.of("java.lang.invoke.CallSite")
+                                        ),
+                                        methodName,
+                                        methodTypeLessPattern,
+                                        new ConstantDesc[]{ constantPattern }),
+                                Intrinsics.dropArg(argClassDescs.length, 1)
+                        );
+                }
+            }
 
             return new Result.None();
         }