Class PythonJavaBases
The Java class generated for such a Python class extends the Java base and is the only Java
instance of it. At run time the Java base is replaced in the Python class by a Python base class
(created by baseClass(Class)) that records the arguments of super().__init__(...) and
defines a Python method for every accessible instance method of the Java base. Those methods
reach the base implementation through the Java instance bound to the Python object
(bind(Value, ValueCoercible)), which the generated constructors create from the recorded arguments; when the
Java super constructor takes no argument, they create it before __init__ runs, so the
inherited methods __init__ calls reach it. A Python object created in Python code gets
its Java instance the first time an inherited method is called.
- Since:
- 5.2.0
- Author:
- Micronaut Team
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classThe construction of the Java instance of a Python object, see.invalid reference
#constructing(Value) -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic org.graalvm.polyglot.ValueAn argument of thesuper().__init__(...)call recorded on a Python object, for the Java super constructor.static org.graalvm.polyglot.ValueThe Python base class standing in for a Java class, created once per context.static voidbind(org.graalvm.polyglot.Value pythonObject, ValueCoercible javaInstance) Binds the Java instance of a Python object.static <T> @Nullable TThe Java instance already bound to a Python object.static PythonJavaBases.Constructionconstructing(org.graalvm.polyglot.Value pythonObject, Class<?> javaClass) Marks the Python object whose Java instance is being constructed on the current thread.static @Nullable ObjectInvokes a method of the Java base on the Java instance of a Python object; called by the methods of the Python base class.static booleanmatchesVarargs(List<org.graalvm.polyglot.Value> arguments, int from, Class<?> componentType) Whether the trailing Python arguments, from the given index, are accepted by the varargs parameter of a Java base method: each one is an element of the component type, or the single trailing argument is already an array of it.static IllegalArgumentExceptionnoSuchMethod(String baseClass, String name, List<org.graalvm.polyglot.Value> arguments) The error for a call of a Java base method that no accessible overload of the base accepts; thrown by the generatedmicronautInvokeJavaBaseMethod.static @Nullable org.graalvm.polyglot.ValueunderConstruction(Class<?> javaClass) The Python object whose Java instance of the given class is being constructed on the current thread, for a bridge method called by the Java super constructor;nulloutside such a construction.static ObjectThe array a varargs parameter of a Java base method receives for the trailing Python arguments, from the given index: each one converted to the component type, or the single trailing argument when it is already an array of it, as in a Java call.
-
Field Details
-
JAVA_INSTANCE_MEMBER
The attribute of a Python object holding the Java instance of its class.- See Also:
-
SUPER_ARGUMENTS_MEMBER
The attribute of a Python object holding the arguments of itssuper().__init__(...)call.- See Also:
-
-
Method Details
-
baseClass
The Python base class standing in for a Java class, created once per context.- Parameters:
javaClass- The Java class a Python class extends- Returns:
- The Python base class
-
invoke
public static @Nullable Object invoke(org.graalvm.polyglot.Value self, String name, org.graalvm.polyglot.Value arguments) throws Exception Invokes a method of the Java base on the Java instance of a Python object; called by the methods of the Python base class.- Parameters:
self- The Python objectname- The method namearguments- The Python arguments- Returns:
- The result, converted for Python
- Throws:
Exception- The checked exception the base method declares, raised in Python as a host exception
-
constructing
public static PythonJavaBases.Construction constructing(org.graalvm.polyglot.Value pythonObject, Class<?> javaClass) Marks the Python object whose Java instance is being constructed on the current thread. The generated(Value)constructor evaluates this before it runs the Java super constructor, which may call a method the Python class overrides: until the instance holds its Python object, the bridge of that method reaches it throughunderConstruction(Class).- Parameters:
pythonObject- The Python objectjavaClass- The generated Java class being constructed- Returns:
- The construction, finished by the constructor once the super constructor returned
-
underConstruction
The Python object whose Java instance of the given class is being constructed on the current thread, for a bridge method called by the Java super constructor;nulloutside such a construction. Constructions nest when a super constructor creates the Java instance of another Python object, so the asking class selects the innermost construction of its own class.- Parameters:
javaClass- The generated Java class of the asking instance- Returns:
- The Python object, or
null
-
bind
Binds the Java instance of a Python object.- Parameters:
pythonObject- The Python objectjavaInstance- The generated Java instance wrapping it
-
bound
The Java instance already bound to a Python object.- Type Parameters:
T- The generated Java class- Parameters:
pythonObject- The Python objecttype- The generated Java class- Returns:
- The bound instance, or
nullwhen the object has none of that type
-
argument
public static org.graalvm.polyglot.Value argument(org.graalvm.polyglot.Value pythonObject, int index, String constructor) An argument of thesuper().__init__(...)call recorded on a Python object, for the Java super constructor.- Parameters:
pythonObject- The Python objectindex- The argument indexconstructor- A description of the Java constructor, for the error message- Returns:
- The argument
-
matchesVarargs
public static boolean matchesVarargs(List<org.graalvm.polyglot.Value> arguments, int from, Class<?> componentType) Whether the trailing Python arguments, from the given index, are accepted by the varargs parameter of a Java base method: each one is an element of the component type, or the single trailing argument is already an array of it.- Parameters:
arguments- The Python argumentsfrom- The index of the first argument of the varargs parametercomponentType- The component type of the varargs parameter- Returns:
- Whether the arguments are accepted
-
varargs
public static Object varargs(List<org.graalvm.polyglot.Value> arguments, int from, Class<?> componentType) The array a varargs parameter of a Java base method receives for the trailing Python arguments, from the given index: each one converted to the component type, or the single trailing argument when it is already an array of it, as in a Java call.- Parameters:
arguments- The Python argumentsfrom- The index of the first argument of the varargs parametercomponentType- The component type of the varargs parameter- Returns:
- The array, typed with the component type
-
noSuchMethod
public static IllegalArgumentException noSuchMethod(String baseClass, String name, List<org.graalvm.polyglot.Value> arguments) The error for a call of a Java base method that no accessible overload of the base accepts; thrown by the generatedmicronautInvokeJavaBaseMethod.- Parameters:
baseClass- The Java base class namename- The method namearguments- The Python arguments- Returns:
- The error
-