-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJavaSystem.java
More file actions
228 lines (192 loc) · 7.05 KB
/
JavaSystem.java
File metadata and controls
228 lines (192 loc) · 7.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package org.perlonjava.runtime.perlmodule;
import org.perlonjava.runtime.runtimetypes.*;
import java.util.Map;
import java.util.Properties;
import static org.perlonjava.runtime.runtimetypes.RuntimeScalarCache.scalarUndef;
/**
* Java::System - Perl module for accessing Java system properties and environment
* <p>
* This module provides a Perl interface to Java's System class, allowing
* Perl code to access system properties, environment variables, and other
* JVM information.
*/
public class JavaSystem extends PerlModuleBase {
public JavaSystem() {
super("Java::System", true);
}
public static void initialize() {
JavaSystem javaSystem = new JavaSystem();
javaSystem.initializeExporter();
// Export commonly used functions by default
javaSystem.defineExport("EXPORT",
"getProperty",
"getProperties",
"getenv",
"getEnvMap",
"currentTimeMillis",
"nanoTime",
"gc",
"exit"
);
// Additional functions available on request
javaSystem.defineExport("EXPORT_OK",
"getSecurityManager",
"identityHashCode",
"lineSeparator",
"arraycopy"
);
try {
// Register all methods
javaSystem.registerMethod("getProperty", null);
javaSystem.registerMethod("getProperties", null);
javaSystem.registerMethod("getenv", null);
javaSystem.registerMethod("getEnvMap", null);
javaSystem.registerMethod("currentTimeMillis", null);
javaSystem.registerMethod("nanoTime", null);
javaSystem.registerMethod("gc", null);
javaSystem.registerMethod("exit", null);
javaSystem.registerMethod("identityHashCode", null);
javaSystem.registerMethod("lineSeparator", null);
javaSystem.registerMethod("arraycopy", null);
} catch (NoSuchMethodException e) {
System.err.println("Warning: Missing JavaSystem method: " + e.getMessage());
}
}
/**
* Get a system property by name
* Usage: my $value = getProperty('java.version');
* my $value = getProperty('os.name', 'default');
*/
public static RuntimeList getProperty(RuntimeArray args, int ctx) {
if (args.size() < 1) {
throw new IllegalArgumentException("getProperty requires at least 1 argument");
}
String key = args.get(0).toString();
String defaultValue = args.size() > 1 ? args.get(1).toString() : null;
String value = System.getProperty(key, defaultValue);
if (value == null) {
return scalarUndef.getList();
}
return new RuntimeScalar(value).getList();
}
/**
* Get all system properties as a hash
* Usage: my %props = getProperties();
*/
public static RuntimeList getProperties(RuntimeArray args, int ctx) {
Properties props = System.getProperties();
RuntimeHash hash = new RuntimeHash();
for (String key : props.stringPropertyNames()) {
hash.put(key, new RuntimeScalar(props.getProperty(key)));
}
if (ctx == RuntimeContextType.LIST) {
return hash.getList();
} else {
return hash.createReference().getList();
}
}
/**
* Get an environment variable by name
* Usage: my $value = getenv('PATH');
*/
public static RuntimeList getenv(RuntimeArray args, int ctx) {
if (args.size() < 1) {
throw new IllegalArgumentException("getenv requires 1 argument");
}
String key = args.get(0).toString();
String value = System.getenv(key);
if (value == null) {
return scalarUndef.getList();
}
return new RuntimeScalar(value).getList();
}
/**
* Get all environment variables as a hash
* Usage: my %env = getEnvMap();
*/
public static RuntimeList getEnvMap(RuntimeArray args, int ctx) {
Map<String, String> env = System.getenv();
RuntimeHash hash = new RuntimeHash();
for (Map.Entry<String, String> entry : env.entrySet()) {
hash.put(entry.getKey(), new RuntimeScalar(entry.getValue()));
}
if (ctx == RuntimeContextType.LIST) {
return hash.getList();
} else {
return hash.createReference().getList();
}
}
/**
* Get current time in milliseconds
* Usage: my $time = currentTimeMillis();
*/
public static RuntimeList currentTimeMillis(RuntimeArray args, int ctx) {
return new RuntimeScalar(System.currentTimeMillis()).getList();
}
/**
* Get high-resolution time in nanoseconds
* Usage: my $time = nanoTime();
*/
public static RuntimeList nanoTime(RuntimeArray args, int ctx) {
return new RuntimeScalar(System.nanoTime()).getList();
}
/**
* Request garbage collection
* Usage: gc();
*/
public static RuntimeList gc(RuntimeArray args, int ctx) {
System.gc();
return scalarUndef.getList();
}
/**
* Exit the JVM by throwing PerlExitException.
* <p>
* This allows embedded/library use where the calling Java application
* can catch the exception and continue execution.
* Usage: exit(0);
*
* @throws PerlExitException always thrown with the exit code
*/
public static RuntimeList exit(RuntimeArray args, int ctx) {
int status = args.size() > 0 ? (int) args.get(0).getLong() : 0;
throw new PerlExitException(status);
}
/**
* Get identity hash code of an object
* Usage: my $hash = identityHashCode($ref);
*/
public static RuntimeList identityHashCode(RuntimeArray args, int ctx) {
if (args.isEmpty()) {
throw new IllegalArgumentException("identityHashCode requires 1 argument");
}
Object obj = args.get(0);
int hashCode = System.identityHashCode(obj);
return new RuntimeScalar(hashCode).getList();
}
/**
* Get the system line separator
* Usage: my $sep = lineSeparator();
*/
public static RuntimeList lineSeparator(RuntimeArray args, int ctx) {
return new RuntimeScalar(System.lineSeparator()).getList();
}
/**
* Copy array elements (simplified version)
* Usage: arraycopy(\@src, $srcPos, \@dest, $destPos, $length);
*/
public static RuntimeList arraycopy(RuntimeArray args, int ctx) {
if (args.size() < 5) {
throw new IllegalArgumentException("arraycopy requires 5 arguments");
}
RuntimeArray src = args.get(0).arrayDeref();
int srcPos = (int) args.get(1).getLong();
RuntimeArray dest = args.get(2).arrayDeref();
int destPos = (int) args.get(3).getLong();
int length = (int) args.get(4).getLong();
// Perform the copy
for (int i = 0; i < length; i++) {
dest.get(destPos + i).set(src.get(srcPos + i));
}
return scalarUndef.getList();
}
}