Skip to content

Conversation

graalvmbot
Copy link
Collaborator

This test now works. Seems much nicer UX

import java
java.add_to_classpath(".")
MiscUtils = java.type("MiscUtils")

class Misc(MiscUtils, old_style=False):
    def __init__(self):
        print(f"Misc.__init__ runs to initialize the object {self=}")
        self.misc_value = 42

    def numIntsNeededToStoreBytes(self, byteCount):
        print(f"== Misc(MiscUtils).numIntsNeededToStoreBytes({byteCount=}) overrides a Java method, so calls to the Java object go here")
        return self.getClass().static.numIntsNeededToStoreBytes(byteCount)

    def py_native_test_bit(self, val, bit):
        return val & (1 << bit)

    def py_call_numIntsNeededToStoreBytes(self, byteCount):
        print(f"  -->   inside py_call_numIntsNeededToStoreBytes, which is not callable from Java at all")
        _ = self.misc_value
        y = self.numIntsNeededToStoreBytes(10)
        x = self.py_native_test_bit(y if y else 7, 1)
        return y if x else None

misc = Misc()

print(f"{misc.mask(10)=}")
print(f"{misc.py_call_numIntsNeededToStoreBytes(10)=}")
print(f"{misc.py_native_test_bit(7, 1)=}")
print(f"{misc.extractBits(7, 1, 2)=}")
print(f"{misc.misc_value=}")
print(f"{misc.someInstanceMethod()=}")
print(f"{Misc=}")
print(f"{misc=}")
print(f"{type(misc)=}")
print(f"{type(misc).mro()=}")
print(f"{issubclass(Misc, MiscUtils)=}")
print(f"{issubclass(Misc, Misc)=}")
print(f"{isinstance(Misc(), MiscUtils)=}")
print(f"{isinstance(Misc(), Misc)=}")
public abstract class MiscUtils {

    public String someInstanceMethod() {
        return "an instance method on the Java side";
    }

    public static int mask(int v) {
        return v;
    }

    public static int numIntsNeededToStoreBytes(int byteCount) {
        return (byteCount + 3) / 4;
    }

    public static long extractBits(long from, int lsb, int width) {
        return (from >>> lsb) & mask(width);
    }
}

@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Sep 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OCA Verified All contributors have signed the Oracle Contributor Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants