Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ keywords =
project_urls =
Documentation = https://docs.oracle.com/en/java/javase/17/docs/api/index.html
Funding = https://github.com/sponsors/cesarcoatl
Source = https://github.com/ignition-devs/java-api/tree/main
Source = https://github.com/ignition-devs/java-api/tree/17
Tracker = https://github.com/ignition-devs/java-api/issues

[options]
Expand Down
153 changes: 152 additions & 1 deletion src/java/lang/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,25 @@
"String",
"StringBuffer",
"StringBuilder",
"System",
"Thread",
"Throwable",
"UnsupportedOperationException",
]

import __builtin__ as builtins
import array
import os
import time
from typing import TYPE_CHECKING, Any, List, Optional, Union

from java.lang.reflect import Type

if TYPE_CHECKING:
from java.io import InputStream, PrintStream
from java.nio import CharBuffer
from java.util import Iterator, Spliterator
from java.nio.channels import Channel
from java.util import Iterator, Properties, Spliterator
from java.util.function import Consumer
from java.util.stream import IntStream, Stream

Expand Down Expand Up @@ -683,6 +687,153 @@ def trimToSize(self):
pass


class System(Object):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚪ Low · System can be instantiated

When a caller invokes System(), System inherits the instantiable Object constructor and defines no guard, so construction succeeds despite the class contract that it cannot be instantiated.

Add a constructor guard that prevents callers from instantiating System.

Prompt for AI agents
In `src/java/lang/__init__.py` at line 690:

**System can be instantiated**

When a caller invokes `System()`, `System` inherits the instantiable `Object` constructor and defines no guard, so construction succeeds despite the class contract that it cannot be instantiated.

Add a constructor guard that prevents callers from instantiating `System`.

"""The System class contains several useful class fields and
methods.

It cannot be instantiated. Among the facilities provided by the
System class are standard input, standard output, and error output
streams; access to externally defined properties and environment
variables; a means of loading files and libraries; and a utility
method for quickly copying a portion of an array.
"""

err = None # type: PrintStream
in_ = None # type: InputStream
out = None # type: PrintStream
Comment on lines +701 to +703

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High · Standard streams are unavailable

When a caller accesses any standard stream, System.err, System.in_, and System.out are initialized to None, so accessing a stream and calling its Java I/O methods fails with an attribute error instead of using the process’s standard streams.

Initialize the fields to usable wrappers for the process’s standard error, input, and output streams.

Prompt for AI agents
In `src/java/lang/__init__.py` at lines 701-703:

**Standard streams are unavailable**

When a caller accesses any standard stream, `System.err`, `System.in_`, and `System.out` are initialized to `None`, so accessing a stream and calling its Java I/O methods fails with an attribute error instead of using the process’s standard streams.

Initialize the fields to usable wrappers for the process’s standard error, input, and output streams.


@staticmethod
def arraycopy(src, srcPos, dest, destPos, length):
# type: (Any, int, Any, int, int) -> None
pass

@staticmethod
def clearProperty(key):
# type: (Union[str, unicode]) -> Union[str, unicode]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Nullable returns are typed as non-null

When a property is absent or has no previous value, or no inherited channel is available, the return annotations for clearProperty, getProperty, setProperty, and inheritedChannel exclude None even though these calls can return null, so type checkers allow callers to use missing values without handling them.

Annotate these return values as optional strings or an optional channel, as appropriate.

Also at src/java/lang/__init__.py:755-756, src/java/lang/__init__.py:770, src/java/lang/__init__.py:828-829, stubs/stubs/java/lang/__init__.pyi:211, stubs/stubs/java/lang/__init__.pyi:229-231, stubs/stubs/java/lang/__init__.pyi:235, stubs/stubs/java/lang/__init__.pyi:257-260.

Prompt for AI agents
In `src/java/lang/__init__.py` at line 712:

**Nullable returns are typed as non-null**

When a property is absent or has no previous value, or no inherited channel is available, the return annotations for `clearProperty`, `getProperty`, `setProperty`, and `inheritedChannel` exclude `None` even though these calls can return null, so type checkers allow callers to use missing values without handling them.

Annotate these return values as optional strings or an optional channel, as appropriate.

Also at `src/java/lang/__init__.py:755-756`, `src/java/lang/__init__.py:770`, `src/java/lang/__init__.py:828-829`, `stubs/stubs/java/lang/__init__.pyi:211`, `stubs/stubs/java/lang/__init__.pyi:229-231`, `stubs/stubs/java/lang/__init__.pyi:235`, `stubs/stubs/java/lang/__init__.pyi:257-260`.

pass

@staticmethod
def console():
# type: () -> Any
pass

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Java accessors lack usable types

When a caller uses these accessors and expects completion or type checking for their Java return values, console(), getenv(), and getLogger() return Any in the declarations and stubs, so IDEs and type checkers cannot validate the Console, environment map or optional string, and System.Logger APIs.

Give the accessors their Java return types in the declarations and stubs, including distinct return types for the two getenv() forms.

Also at stubs/stubs/java/lang/__init__.pyi:213, stubs/stubs/java/lang/__init__.pyi:221, stubs/stubs/java/lang/__init__.pyi:223, src/java/lang/__init__.py:737, src/java/lang/__init__.py:743.

Prompt for AI agents
In `src/java/lang/__init__.py` at line 718:

**Java accessors lack usable types**

When a caller uses these accessors and expects completion or type checking for their Java return values, `console()`, `getenv()`, and `getLogger()` return `Any` in the declarations and stubs, so IDEs and type checkers cannot validate the `Console`, environment map or optional string, and `System.Logger` APIs.

Give the accessors their Java return types in the declarations and stubs, including distinct return types for the two `getenv()` forms.

Also at `stubs/stubs/java/lang/__init__.pyi:213`, `stubs/stubs/java/lang/__init__.pyi:221`, `stubs/stubs/java/lang/__init__.pyi:223`, `src/java/lang/__init__.py:737`, `src/java/lang/__init__.py:743`.


@staticmethod
def currentTimeMillis():
# type: () -> long
return long(time.time() * 1000)

@staticmethod
def exit(status):
# type: (int) -> None
pass

@staticmethod
def gc():
# type: () -> None
pass

@staticmethod
def getenv(name=None):
# type: (Optional[Union[str, unicode]]) -> Any
return dict(os.environ) if name is None else os.environ.get(str(name))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Null names expose the environment

When a caller explicitly passes None as the environment-variable name, getenv() takes the no-argument branch and returns the entire environment instead of raising the null-argument error required by Java's named overload, exposing variables to the caller.

Distinguish an omitted argument from an explicitly passed None and raise the required null-argument error for the latter.

Also at stubs/stubs/java/lang/__init__.pyi:221.

Prompt for AI agents
In `src/java/lang/__init__.py` at line 738:

**Null names expose the environment**

When a caller explicitly passes `None` as the environment-variable name, `getenv()` takes the no-argument branch and returns the entire environment instead of raising the null-argument error required by Java's named overload, exposing variables to the caller.

Distinguish an omitted argument from an explicitly passed `None` and raise the required null-argument error for the latter.

Also at `stubs/stubs/java/lang/__init__.pyi:221`.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Unicode names fail lookup

When the caller supplies a non-ASCII Unicode environment-variable name, getenv() converts the name with str(name), which attempts ASCII encoding under Python 2 and raises UnicodeEncodeError before the lookup, so callers cannot access the variable.

Convert Unicode names without ASCII encoding before looking them up in os.environ.

Prompt for AI agents
In `src/java/lang/__init__.py` at line 738:

**Unicode names fail lookup**

When the caller supplies a non-ASCII Unicode environment-variable name, `getenv()` converts the name with `str(name)`, which attempts ASCII encoding under Python 2 and raises `UnicodeEncodeError` before the lookup, so callers cannot access the variable.

Convert Unicode names without ASCII encoding before looking them up in `os.environ`.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Environment snapshot can fail during updates

When one thread calls getenv() without a name while another removes an entry from os.environ, dict(os.environ) copies the shared mapping without synchronization, so a concurrent thread that removes an environment entry can make the copy fail instead of returning a map.

Synchronize environment-map copying with updates, or handle copy failures and retry.

Prompt for AI agents
In `src/java/lang/__init__.py` at line 738:

**Environment snapshot can fail during updates**

When one thread calls `getenv()` without a name while another removes an entry from `os.environ`, `dict(os.environ)` copies the shared mapping without synchronization, so a concurrent thread that removes an environment entry can make the copy fail instead of returning a map.

Synchronize environment-map copying with updates, or handle copy failures and retry.


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Environment changes succeed

When a caller mutates the map returned by no-argument getenv(), getenv() returns a mutable dict, so the mutation succeeds instead of raising the unsupported-operation error required by Java's unmodifiable environment map.

Return an unmodifiable mapping from the no-argument form of getenv().

Also at src/java/lang/__init__.py:736-738.

Prompt for AI agents
In `src/java/lang/__init__.py` at line 739:

**Environment changes succeed**

When a caller mutates the map returned by no-argument `getenv()`, `getenv()` returns a mutable `dict`, so the mutation succeeds instead of raising the unsupported-operation error required by Java's unmodifiable environment map.

Return an unmodifiable mapping from the no-argument form of `getenv()`.

Also at `src/java/lang/__init__.py:736-738`.

@staticmethod
def getLogger(name, bundle=None):
# type: (Union[str, unicode], Optional[Any]) -> Any
pass

@staticmethod
def getProperties():
# type: () -> Properties
pass

@staticmethod
def getProperty(
key, # type: Union[str, unicode]
def_=None, # type: Optional[Union[str, unicode]]
):
# type: (...) -> Union[str, unicode]
pass

@staticmethod
def getSecurityManager():
# type: () -> Any
pass

@staticmethod
def identityHashCode(x):
# type: (Object) -> int
return id(x)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Null identity hashes are wrong

When the caller passes None, identityHashCode() returns id(None) rather than Java's required 0, and its Object annotation rejects null at type-check time, so callers get the wrong hash or cannot pass null.

Accept an optional object and return 0 when the argument is None.

Also at stubs/stubs/java/lang/__init__.pyi:233.

Prompt for AI agents
In `src/java/lang/__init__.py` at line 766:

**Null identity hashes are wrong**

When the caller passes `None`, `identityHashCode()` returns `id(None)` rather than Java's required `0`, and its `Object` annotation rejects null at type-check time, so callers get the wrong hash or cannot pass null.

Accept an optional object and return `0` when the argument is `None`.

Also at `stubs/stubs/java/lang/__init__.pyi:233`.


@staticmethod
def inheritedChannel():
# type: () -> Channel
pass

@staticmethod
def lineSeparator():
# type: () -> Union[str, unicode]
return os.linesep

@staticmethod
def load(filename):
# type: (Union[str, unicode]) -> None
pass

@staticmethod
def loadLibrary(libname):
# type: (Union[str, unicode]) -> None
pass

@staticmethod
def mapLibraryName(libname):
# type: (Union[str, unicode]) -> Union[str, unicode]
pass

@staticmethod
def nanoTime():
# type: () -> long
return long(time.time() * 1000000000)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High · Elapsed-time readings jump

When the system clock is adjusted while callers measure elapsed time, System.nanoTime() uses time.time(), so clock adjustments make successive readings jump or go backward and produce incorrect elapsed-time results.

Implement nanoTime() using a monotonic clock source.

Also at src/java/lang/__init__.py:797.

Prompt for AI agents
In `src/java/lang/__init__.py` at line 796:

**Elapsed-time readings jump**

When the system clock is adjusted while callers measure elapsed time, `System.nanoTime()` uses `time.time()`, so clock adjustments make successive readings jump or go backward and produce incorrect elapsed-time results.

Implement `nanoTime()` using a monotonic clock source.

Also at `src/java/lang/__init__.py:797`.


@staticmethod
def runFinalization():
# type: () -> None
pass

@staticmethod
def setErr(err):
# type: (PrintStream) -> None
pass

@staticmethod
def setIn(in_):
# type: (InputStream) -> None
pass

@staticmethod
def setOut(out):
# type: (PrintStream) -> None
pass

@staticmethod
def setProperties(props):
# type: (Properties) -> None
pass

@staticmethod
def setProperty(
key, # type: Union[str, unicode]
value, # type: Union[str, unicode]
):
# type: (...) -> Union[str, unicode]
pass

@staticmethod
def setSecurityManager(sm):
# type: (Any) -> None
pass


class Throwable(Object, builtins.Exception):
"""The Throwable class is the superclass of all errors and
exceptions in the Java language.
Expand Down
4 changes: 2 additions & 2 deletions stubs/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ classifiers = [
urls.Documentation = "https://docs.oracle.com/en/java/javase/17/docs/api/index.html"
urls.Funding = "https://github.com/sponsors/cesarcoatl"
urls.Homepage = "https://github.com/ignition-devs/java-api"
urls.Source = "https://github.com/ignition-devs/java-api/tree/main"
urls.Source = "https://github.com/ignition-devs/java-api/tree/17"
urls.Tracker = "https://github.com/ignition-devs/java-api/issues"

[tool.setuptools]
include-package-data = true
package-dir = { "" = "stubs" }
include-package-data = true
package-data."*" = [ "*.pyi", "py.typed" ]
65 changes: 64 additions & 1 deletion stubs/stubs/java/lang/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import __builtin__ as builtins
from typing import Any, List, Optional, Union

from java.io import InputStream, PrintStream
from java.lang.reflect import Type
from java.nio import CharBuffer
from java.util import Iterator, Spliterator
from java.nio.channels import Channel
from java.util import Iterator, Properties, Spliterator
from java.util.function import Consumer
from java.util.stream import IntStream, Stream

Expand Down Expand Up @@ -197,6 +199,67 @@ class StringBuilder(Object, CharSequence):
def substring(self, start: int, end: int = ...) -> Union[str, unicode]: ...
def trimToSize(self) -> None: ...

class System(Object):
err: PrintStream
in_: InputStream
out: PrintStream
@staticmethod
def arraycopy(
src: Any, srcPos: int, dest: Any, destPos: int, length: int
) -> None: ...
@staticmethod
def clearProperty(key: Union[str, unicode]) -> Union[str, unicode]: ...
@staticmethod
def console() -> Any: ...
@staticmethod
def currentTimeMillis() -> long: ...
@staticmethod
def exit(status: int) -> None: ...
@staticmethod
def gc() -> None: ...
@staticmethod
def getenv(name: Optional[Union[str, unicode]] = ...) -> Any: ...
@staticmethod
def getLogger(name: Union[str, unicode], bundle: Optional[Any] = ...) -> Any: ...
@staticmethod
def getProperties() -> Properties: ...
@staticmethod
def getProperty(
key: Union[str, unicode], def_: Optional[Union[str, unicode]] = ...
) -> Union[str, unicode]: ...
@staticmethod
def getSecurityManager() -> Any: ...
@staticmethod
def identityHashCode(x: Object) -> int: ...
@staticmethod
def inheritedChannel() -> Channel: ...
@staticmethod
def lineSeparator() -> Union[str, unicode]: ...
@staticmethod
def load(filename: Union[str, unicode]) -> None: ...
@staticmethod
def loadLibrary(libname: Union[str, unicode]) -> None: ...
@staticmethod
def mapLibraryName(libname: Union[str, unicode]) -> Union[str, unicode]: ...
@staticmethod
def nanoTime() -> long: ...
@staticmethod
def runFinalization() -> None: ...
@staticmethod
def setErr(err: PrintStream) -> None: ...
@staticmethod
def setIn(in_: InputStream) -> None: ...
@staticmethod
def setOut(out: PrintStream) -> None: ...
@staticmethod
def setProperties(props: Properties) -> None: ...
@staticmethod
def setProperty(
key: Union[str, unicode], value: Union[str, unicode]
) -> Union[str, unicode]: ...
@staticmethod
def setSecurityManager(sm: Any) -> None: ...

class Throwable(Object, builtins.Exception):
def __init__(
self, message: Optional[str] = ..., cause: Optional[Throwable] = ...
Expand Down
Loading