Welcome! Please see the About page for a little more info on how this works.

+2 votes
ago in Compiler by
retagged ago by

In 1.11.4, if a class has a static field and a static method with the same name, attempting to use the method as a function works as expected.

In 1.12.0, the static field is always returned instead.

A CLI reproduction case:

Clojure 1.12.0
user=> (import '(com.badlogic.gdx.math MathUtils))
com.badlogic.gdx.math.MathUtils
user=> MathUtils/random
#object[com.badlogic.gdx.math.RandomXS128 0x3df978b9 "com.badlogic.gdx.math.RandomXS128@3df978b9"]
user=> (MathUtils/random)
#object[com.badlogic.gdx.math.RandomXS128 0x3df978b9 "com.badlogic.gdx.math.RandomXS128@3df978b9"]
user=> (MathUtils/random 0 10)
#object[com.badlogic.gdx.math.RandomXS128 0x3df978b9 "com.badlogic.gdx.math.RandomXS128@3df978b9"]


Clojure 1.11.4
user=> (import '(com.badlogic.gdx.math MathUtils))
com.badlogic.gdx.math.MathUtils
user=> MathUtils/random
#object[com.badlogic.gdx.math.RandomXS128 0x71c905a3 "com.badlogic.gdx.math.RandomXS128@71c905a3"]
user=> (MathUtils/random)
0.37632704
user=> (MathUtils/random 0 10)
0

A potential test case:

diff --git a/test/clojure/test_clojure/java_interop.clj b/test/clojure/test_clojure/java_interop.clj
index 4a503884..c99370a3 100644
--- a/test/clojure/test_clojure/java_interop.clj
+++ b/test/clojure/test_clojure/java_interop.clj
@@ -20,7 +20,7 @@
             [clojure.test-helper :refer [should-not-reflect]])
   (:import java.util.Base64
            (java.io File FileFilter FilenameFilter)
-           (java.util UUID)
+           (java.util ArrayList Collections$UnmodifiableCollection UUID)
            (java.util.concurrent.atomic AtomicLong AtomicInteger)
            (clojure.test FIConstructor FIStatic FunctionalTester AdapterExerciser)))
 
@@ -783,6 +783,16 @@
   (def fi-static (FIStatic/numbers (fn [i] (< i 0))))
   (is (= [-2 -1] fi-static)))
 
+(deftest static-field-and-method
+  (let [fv FIStatic/numbers
+        mv0 (FIStatic/numbers)
+        mv1 (FIStatic/numbers neg?)]
+    (is (= [-2 -1 0 1 2] fv))
+    (is (instance? Collections$UnmodifiableCollection fv))
+    (is (= [-2 -1 0 1 2] mv0))
+    (is (instance? ArrayList mv0))
+    (is (= [-2 -1] mv1))))
+
 ;; newDirectoryStream is overloaded, takes ^[Path String] or ^[Path DirectoryStream$Filter]
 ;; so this method will reflect
 (defn get-dir-stream [^java.nio.file.Path dir-path glob-pattern]
diff --git a/test/java/clojure/test/FIStatic.java b/test/java/clojure/test/FIStatic.java
index 8273c357..4a8198ae 100644
--- a/test/java/clojure/test/FIStatic.java
+++ b/test/java/clojure/test/FIStatic.java
@@ -1,13 +1,20 @@
 package clojure.test;
 
+import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 import java.util.function.Predicate;
 
 public class FIStatic {
 
+    public static List<Integer> numbers = Collections.unmodifiableList(Arrays.asList(-2, -1, 0, 1, 2));
+
+    public static List<Object> numbers () {
+        return new ArrayList<>(numbers);
+    }
+
     public static List<Object> numbers(Predicate<Integer> pred) {
-        List<Integer> numbers = Arrays.asList(-2, -1, 0, 1, 2);
         Object[] filteredNumbers =  numbers.stream().filter(pred).toArray();
         return Arrays.asList(filteredNumbers);
     }

1 Answer

+1 vote
ago by
selected ago by
 
Best answer

Created CLJ-2899.

...