or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

advanced-features.mdclass-object-access.mdclasspath-management.mdexception-handling.mdindex.mdinterface-implementation.mdjvm-management.mdtype-system.md

class-object-access.mddocs/

0

# Java Class and Object Access

1

2

Access and manipulation of Java classes, objects, and packages through Python interfaces. This module provides structured access to Java APIs and seamless object interoperability between Python and Java.

3

4

## Capabilities

5

6

### Package Access

7

8

Gateway classes for structured access to Java packages and automatic class importation.

9

10

```python { .api }

11

class JPackage:

12

"""Gateway for automatic importation of Java classes.

13

14

Provides structured access to Java packages and classes.

15

Sub-packages are created on demand.

16

"""

17

18

def __init__(self, name: str, strict: bool = False):

19

"""Create a package gateway.

20

21

Args:

22

name: Package name (e.g., "java.lang", "com.example")

23

strict: If True, raises error for missing packages

24

"""

25

26

# Global package instances

27

java: JPackage # Gateway to java.* packages

28

javax: JPackage # Gateway to javax.* packages

29

```

30

31

### Class Management

32

33

Meta classes and factories for working with Java classes and creating instances.

34

35

```python { .api }

36

class JClass:

37

"""Meta class for Java classes with type factory functionality.

38

39

Provides access to Java class metadata, constructors, and static methods.

40

"""

41

42

def __init__(self, name: str):

43

"""Load a Java class by name.

44

45

Args:

46

name: Fully qualified class name (e.g., "java.lang.String")

47

48

Raises:

49

TypeError: If class cannot be found or loaded

50

"""

51

52

def __call__(self, *args, **kwargs):

53

"""Create new instance of the Java class.

54

55

Args:

56

*args: Constructor arguments

57

**kwargs: Additional options

58

59

Returns:

60

JObject: New instance of the Java class

61

"""

62

63

class JInterface:

64

"""Base class for Java interfaces.

65

66

Used to represent Java interface types and enable

67

interface implementation from Python.

68

"""

69

70

def JOverride(func):

71

"""Decorator to mark method as overriding Java method.

72

73

Args:

74

func: Python method that overrides Java method

75

76

Returns:

77

func: The decorated method

78

"""

79

```

80

81

### Object Manipulation

82

83

Base classes for working with Java objects and accessing their methods and fields.

84

85

```python { .api }

86

class JObject:

87

"""Base class for all Java objects with casting functionality.

88

89

Provides common methods for Java objects and type conversion.

90

"""

91

92

def getClass(self):

93

"""Get the Java Class object for this instance.

94

95

Returns:

96

JClass: The Java Class object

97

"""

98

99

def toString(self) -> str:

100

"""Get string representation via Java toString().

101

102

Returns:

103

str: String representation of the object

104

"""

105

106

def equals(self, other) -> bool:

107

"""Check equality via Java equals() method.

108

109

Args:

110

other: Object to compare with

111

112

Returns:

113

bool: True if objects are equal

114

"""

115

116

def hashCode(self) -> int:

117

"""Get hash code via Java hashCode() method.

118

119

Returns:

120

int: Hash code of the object

121

"""

122

123

# Direct access to internal classes

124

JMethod: type # Direct access to _jpype._JMethod

125

JField: type # Direct access to _jpype._JField

126

```

127

128

## Usage Examples

129

130

### Package Access

131

132

```python

133

import jpype

134

135

jpype.startJVM()

136

137

# Using global package objects

138

String = jpype.java.lang.String

139

System = jpype.java.lang.System

140

ArrayList = jpype.java.util.ArrayList

141

142

# Create custom package gateway

143

mypackage = jpype.JPackage("com.example")

144

MyClass = mypackage.MyClass

145

146

jpype.shutdownJVM()

147

```

148

149

### Class Loading and Instantiation

150

151

```python

152

import jpype

153

154

jpype.startJVM()

155

156

# Load class explicitly

157

StringClass = jpype.JClass("java.lang.String")

158

ArrayListClass = jpype.JClass("java.util.ArrayList")

159

160

# Create instances

161

hello = StringClass("Hello World")

162

list_obj = ArrayListClass()

163

164

# Alternative: direct package access

165

String = jpype.java.lang.String

166

ArrayList = jpype.java.util.ArrayList

167

168

hello2 = String("Hello Again")

169

list_obj2 = ArrayList()

170

171

jpype.shutdownJVM()

172

```

173

174

### Object Method Access

175

176

```python

177

import jpype

178

179

jpype.startJVM()

180

181

# Create Java objects

182

string_obj = jpype.java.lang.String("Hello Java")

183

list_obj = jpype.java.util.ArrayList()

184

185

# Call Java methods

186

print(string_obj.length()) # 10

187

print(string_obj.toUpperCase()) # "HELLO JAVA"

188

print(string_obj.charAt(0)) # 'H'

189

190

# Collection operations

191

list_obj.add("item1")

192

list_obj.add("item2")

193

print(list_obj.size()) # 2

194

print(list_obj.get(0)) # "item1"

195

196

# Object comparison

197

string2 = jpype.java.lang.String("Hello Java")

198

print(string_obj.equals(string2)) # True

199

print(string_obj.hashCode())

200

201

jpype.shutdownJVM()

202

```

203

204

### Static Method and Field Access

205

206

```python

207

import jpype

208

209

jpype.startJVM()

210

211

# Access static methods

212

Math = jpype.java.lang.Math

213

result = Math.sqrt(16.0) # 4.0

214

pi = Math.PI # 3.141592653589793

215

216

# Access system properties

217

System = jpype.java.lang.System

218

java_version = System.getProperty("java.version")

219

System.out.println("Hello from Python!")

220

221

# Set system properties

222

System.setProperty("myapp.setting", "value")

223

224

jpype.shutdownJVM()

225

```

226

227

### Class Metadata

228

229

```python

230

import jpype

231

232

jpype.startJVM()

233

234

# Get class information

235

String = jpype.java.lang.String

236

string_obj = String("test")

237

238

# Access class object

239

clazz = string_obj.getClass()

240

print(clazz.getName()) # "java.lang.String"

241

print(clazz.getSuperclass().getName()) # "java.lang.Object"

242

243

# Check instance types

244

print(isinstance(string_obj, jpype.JObject)) # True

245

246

jpype.shutdownJVM()

247

```

248

249

### Working with Nested Classes

250

251

```python

252

import jpype

253

254

jpype.startJVM()

255

256

# Access nested classes using $ notation

257

Entry = jpype.JClass("java.util.Map$Entry")

258

259

# Or through package access

260

Map = jpype.java.util.Map

261

Entry = Map.Entry

262

263

jpype.shutdownJVM()

264

```