groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jwagenleit...@apache.org
Subject [6/6] groovy git commit: GROOVY-7611: java.util.Optional should evaluate to false if empty (closes #545)
Date Sat, 27 May 2017 21:07:17 GMT
GROOVY-7611: java.util.Optional should evaluate to false if empty (closes #545)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/0b6e8dcb
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/0b6e8dcb
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/0b6e8dcb

Branch: refs/heads/master
Commit: 0b6e8dcbbc78fb7514b0db96a09c46fb1808e3e7
Parents: 7744159
Author: John Wagenleitner <jwagenleitner@apache.org>
Authored: Sun May 21 10:16:06 2017 -0700
Committer: John Wagenleitner <jwagenleitner@apache.org>
Committed: Sat May 27 13:39:08 2017 -0700

----------------------------------------------------------------------
 build.gradle                                    |  9 ++++
 gradle/docs.gradle                              |  5 +-
 .../stc/StaticTypeCheckingSupport.java          |  4 ++
 .../groovy/vmplugin/VMPluginFactory.java        | 25 ++++-----
 .../org/codehaus/groovy/vmplugin/vm8/Java8.java | 54 ++++++++++++++++++++
 .../vm8/PluginDefaultGroovyMethods.java         | 45 ++++++++++++++++
 .../vm8/PluginDefaultGroovyMethodsSCTest.groovy | 26 ++++++++++
 .../vm8/PluginDefaultGroovyMethodsTest.groovy   | 45 ++++++++++++++++
 8 files changed, 194 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/0b6e8dcb/build.gradle
----------------------------------------------------------------------
diff --git a/build.gradle b/build.gradle
index 47e0d19..e59b82e 100644
--- a/build.gradle
+++ b/build.gradle
@@ -268,12 +268,18 @@ sourceSets {
                     'src/main',
                     "$generatedDirectory/src/main"
             ]
+            if (!JavaVersion.current().isJava8Compatible()) {
+                exclude '**/vm8/*'
+            }
         }
         groovy {
             srcDirs = [
                     'src/main',
                     "$generatedDirectory/src/main"
             ]
+            if (!JavaVersion.current().isJava8Compatible()) {
+                exclude '**/vm8/*'
+            }
         }
         resources {
             srcDirs = ['src/main', 'src/tools', 'src/resources']
@@ -292,6 +298,9 @@ sourceSets {
     test {
         groovy {
             srcDirs = ['src/test']
+            if (!JavaVersion.current().isJava8Compatible()) {
+                exclude '**/vm8/*'
+            }
         }
         resources {
             srcDirs = ['src/test-resources']

http://git-wip-us.apache.org/repos/asf/groovy/blob/0b6e8dcb/gradle/docs.gradle
----------------------------------------------------------------------
diff --git a/gradle/docs.gradle b/gradle/docs.gradle
index d7843ab..936da47 100644
--- a/gradle/docs.gradle
+++ b/gradle/docs.gradle
@@ -143,7 +143,7 @@ task docGDK {
                     arg(value: '-link')
                     arg(value: 'groovy,org.codehaus.groovy=http://docs.groovy-lang.org/latest/html/gapi/')
                     arg(value: '-link')
-                    arg(value: 'java,org.xml,javax,org.w3c=http://docs.oracle.com/javase/7/docs/api/')
+                    arg(value: 'java,org.xml,javax,org.w3c=http://docs.oracle.com/javase/8/docs/api/')
                     // either package name if in core or fully qualified path otherwise
                     arg(value: 'org.codehaus.groovy.runtime.DefaultGroovyMethods')
                     arg(value: 'org.codehaus.groovy.runtime.DefaultGroovyStaticMethods')
@@ -155,8 +155,7 @@ task docGDK {
                     arg(value: 'org.codehaus.groovy.runtime.SocketGroovyMethods')
                     arg(value: 'org.codehaus.groovy.runtime.StringGroovyMethods')
                     arg(value: 'org.codehaus.groovy.vmplugin.v5.PluginDefaultGroovyMethods')
-                    arg(value: 'org.codehaus.groovy.vmplugin.v6.PluginDefaultGroovyMethods')
-                    arg(value: 'org.codehaus.groovy.vmplugin.v6.PluginStaticGroovyMethods')
+                    arg(value: 'org.codehaus.groovy.vmplugin.vm8.PluginDefaultGroovyMethods')
                     // TODO don't hard-code these
                     arg(value: 'subprojects/groovy-sql/src/main/java/org/codehaus/groovy/runtime/SqlGroovyMethods.java')
                     arg(value: 'subprojects/groovy-swing/src/main/java/org/codehaus/groovy/runtime/SwingGroovyMethods.java')

http://git-wip-us.apache.org/repos/asf/groovy/blob/0b6e8dcb/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java b/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
index d9a477d..1aebf93 100644
--- a/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -37,6 +37,7 @@ import org.codehaus.groovy.runtime.m12n.MetaInfExtensionModule;
 import org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl;
 import org.codehaus.groovy.tools.GroovyClass;
 import org.codehaus.groovy.transform.trait.Traits;
+import org.codehaus.groovy.vmplugin.VMPluginFactory;
 import org.objectweb.asm.Opcodes;
 
 import java.lang.ref.WeakReference;
@@ -2124,6 +2125,9 @@ public abstract class StaticTypeCheckingSupport {
             instanceExtClasses.add(FloatArrayStaticTypesHelper.class);
             instanceExtClasses.add(DoubleArrayStaticTypesHelper.class);
 
+            Collections.addAll(instanceExtClasses, VMPluginFactory.getPlugin().getPluginDefaultGroovyMethods());
+            Collections.addAll(staticExtClasses, VMPluginFactory.getPlugin().getPluginStaticGroovyMethods());
+
             scanClassesForDGMMethods(methods, staticExtClasses, true);
             scanClassesForDGMMethods(methods, instanceExtClasses, false);
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/0b6e8dcb/src/main/org/codehaus/groovy/vmplugin/VMPluginFactory.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/vmplugin/VMPluginFactory.java b/src/main/org/codehaus/groovy/vmplugin/VMPluginFactory.java
index 127f65e..fe625fb 100644
--- a/src/main/org/codehaus/groovy/vmplugin/VMPluginFactory.java
+++ b/src/main/org/codehaus/groovy/vmplugin/VMPluginFactory.java
@@ -18,6 +18,8 @@
  */
 package org.codehaus.groovy.vmplugin;
 
+import org.codehaus.groovy.vmplugin.v7.Java7;
+
 /**
  * Factory class to get functionality based on the VM version.
  * The usage of this class is not for public use, only for the
@@ -26,26 +28,17 @@ package org.codehaus.groovy.vmplugin;
  */
 public class VMPluginFactory {
 
-    private static final String JDK5_CLASSNAME_CHECK = "java.lang.annotation.Annotation";
-    private static final String JDK6_CLASSNAME_CHECK = "javax.script.ScriptEngine";
-    private static final String JDK7_CLASSNAME_CHECK = "java.util.Objects";
-
-    private static final String JDK5_PLUGIN_NAME = "org.codehaus.groovy.vmplugin.v5.Java5";
-    private static final String JDK6_PLUGIN_NAME = "org.codehaus.groovy.vmplugin.v6.Java6";
-    private static final String JDK7_PLUGIN_NAME = "org.codehaus.groovy.vmplugin.v7.Java7";
+    private static final String JDK8_CLASSNAME_CHECK = "java.util.Optional";
+    private static final String JDK8_PLUGIN_NAME = "org.codehaus.groovy.vmplugin.vm8.Java8";
 
-    private static VMPlugin plugin;
+    private static final VMPlugin plugin;
 
     static {
-        plugin = createPlugin(JDK7_CLASSNAME_CHECK, JDK7_PLUGIN_NAME);
-        if (plugin == null) {
-            // v6 plugin is the same as v5 but with some scripting stuff
-            // so check below is good enough for now (can be true for JVM 5)
-            plugin = createPlugin(JDK6_CLASSNAME_CHECK, JDK6_PLUGIN_NAME);
-        }
-        if (plugin == null) {
-            plugin = createPlugin(JDK5_CLASSNAME_CHECK, JDK5_PLUGIN_NAME);
+        VMPlugin target = createPlugin(JDK8_CLASSNAME_CHECK, JDK8_PLUGIN_NAME);
+        if (target == null) {
+            target = new Java7();
         }
+        plugin = target;
     }
 
     public static VMPlugin getPlugin() {

http://git-wip-us.apache.org/repos/asf/groovy/blob/0b6e8dcb/src/main/org/codehaus/groovy/vmplugin/vm8/Java8.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/vmplugin/vm8/Java8.java b/src/main/org/codehaus/groovy/vmplugin/vm8/Java8.java
new file mode 100644
index 0000000..905af38
--- /dev/null
+++ b/src/main/org/codehaus/groovy/vmplugin/vm8/Java8.java
@@ -0,0 +1,54 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.codehaus.groovy.vmplugin.vm8;
+
+import org.codehaus.groovy.vmplugin.v7.Java7;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Java 8 based functions.
+ *
+ * @since 2.5.0
+ */
+public class Java8 extends Java7 {
+
+    private final Class<?>[] PLUGIN_DGM;
+
+    public Java8() {
+        super();
+        List<Class<?>> dgmClasses = new ArrayList<>();
+        Collections.addAll(dgmClasses, (Class<?>[]) super.getPluginDefaultGroovyMethods());
+        dgmClasses.add(PluginDefaultGroovyMethods.class);
+        PLUGIN_DGM = dgmClasses.toArray(new Class<?>[0]);
+    }
+
+    @Override
+    public Class<?>[] getPluginDefaultGroovyMethods() {
+        return PLUGIN_DGM;
+    }
+
+    @Override
+    public int getVersion() {
+        return 8;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/groovy/blob/0b6e8dcb/src/main/org/codehaus/groovy/vmplugin/vm8/PluginDefaultGroovyMethods.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/vmplugin/vm8/PluginDefaultGroovyMethods.java b/src/main/org/codehaus/groovy/vmplugin/vm8/PluginDefaultGroovyMethods.java
new file mode 100644
index 0000000..7ae5321
--- /dev/null
+++ b/src/main/org/codehaus/groovy/vmplugin/vm8/PluginDefaultGroovyMethods.java
@@ -0,0 +1,45 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.codehaus.groovy.vmplugin.vm8;
+
+import java.util.Optional;
+
+/**
+ * Defines new Groovy methods which appear on normal JDK 8
+ * classes inside the Groovy environment.
+ *
+ * @since 2.5.0
+ */
+public class PluginDefaultGroovyMethods {
+
+    // No instances, static methods only
+    private PluginDefaultGroovyMethods() {
+    }
+
+    /**
+     * Coerce an Optional instance to a boolean value.
+     *
+     * @param optional the Optional
+     * @return {@code true} if a value is present, otherwise {@code false}
+     */
+    public static boolean asBoolean(Optional<?> optional) {
+        return optional.isPresent();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/groovy/blob/0b6e8dcb/src/test/org/codehaus/groovy/vmplugin/vm8/PluginDefaultGroovyMethodsSCTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/vmplugin/vm8/PluginDefaultGroovyMethodsSCTest.groovy
b/src/test/org/codehaus/groovy/vmplugin/vm8/PluginDefaultGroovyMethodsSCTest.groovy
new file mode 100644
index 0000000..373403c
--- /dev/null
+++ b/src/test/org/codehaus/groovy/vmplugin/vm8/PluginDefaultGroovyMethodsSCTest.groovy
@@ -0,0 +1,26 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.codehaus.groovy.vmplugin.vm8
+
+import org.codehaus.groovy.classgen.asm.sc.StaticCompilationTestSupport
+
+class PluginDefaultGroovyMethodsSCTest
+        extends PluginDefaultGroovyMethodsTest
+        implements StaticCompilationTestSupport {
+}

http://git-wip-us.apache.org/repos/asf/groovy/blob/0b6e8dcb/src/test/org/codehaus/groovy/vmplugin/vm8/PluginDefaultGroovyMethodsTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/vmplugin/vm8/PluginDefaultGroovyMethodsTest.groovy
b/src/test/org/codehaus/groovy/vmplugin/vm8/PluginDefaultGroovyMethodsTest.groovy
new file mode 100644
index 0000000..5afe05e
--- /dev/null
+++ b/src/test/org/codehaus/groovy/vmplugin/vm8/PluginDefaultGroovyMethodsTest.groovy
@@ -0,0 +1,45 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.codehaus.groovy.vmplugin.vm8
+
+import groovy.transform.stc.StaticTypeCheckingTestCase
+
+class PluginDefaultGroovyMethodsTest extends StaticTypeCheckingTestCase {
+
+    // GROOVY-7611
+    void testOptionalAsBoolean() {
+        assertScript '''
+            boolean m() {
+                assert Optional.of('foo')
+                assert !Optional.empty()
+                assert !Optional.ofNullable(null)
+
+                def x = Optional.empty() ? 1 : -1
+                assert x == -1
+
+                x = Optional.ofNullable(null) ? 1 : -1
+                assert x == -1
+                
+                Optional.empty()
+            }            
+            assert !m()
+        '''
+    }
+
+}


Mime
View raw message