Closed Bug 245882 Opened 21 years ago Closed 21 years ago

JavaImporter constructor

Categories

(Rhino Graveyard :: Core, enhancement)

enhancement
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: igor, Assigned: igor)

Details

Attachments

(3 files)

Currently in Rhino importPackage and importCass are available when ImporterTopLevel is a scope object. I suggest to add JavaImporter constructor to Rhino that would construct explicit ImporterTopLevel objects. The constructor should be initialized during Context.initStandardObjects call to allow in all scripts a usage like: var Swing = new JavaImporter(); Swing.importPackage(Packages.java.awt.event); Swing.importPackage(Packages.javax.swing); Swing.importClass(Packages.java.awt.EventQueue); and later: var btn = new Swing.JButton() etc. In this way it would be possible to create ready-to-use aliases to package groups in application without danger of namespace pollution. The constructor should also allow to pass a list of packages/classes to import to simplify usage: var Swing = new JavaImporter(Packages.java.awt.event, Packages.javax.swing, Packages.java.awt.EventQueue); The extension should be orthogonal to the current usage of ImporterTopLevel as top scope object.
Simultaneous usage of ImporterTopLevel as top scope and prototype object makes the patch bigger then necessary. For example, it alters IdScriptable to avoid setting parent scope to self causing infinite loops.
The previous patch made JavaImporter a full-fetured host object constructor that defines among others JavaImporter.prototype.constructor property. This is problematic for ImporterTopLevel usage as top scope object as it makes top-level constructor property to point to JavaImporter.prototype.constructor and not to Object.prototype.constructor. This additional fix resolves the regression.
Fixed: I committed both patches. With the patch applied and "with" operator one can almost achive affect of global importPackage without adding package classes to the scope permanently: with (new JavaImporter(Packages.java.awt.event)) { var btn = new JButton(); ... }
Status: NEW → RESOLVED
Closed: 21 years ago
Resolution: --- → FIXED
Fixed: I committed both patches. With the patch applied and "with" operator one can almost achive effect of global importPackage without adding package classes to the scope permanently: with (new JavaImporter(Packages.java.awt.event)) { var btn = new JButton(); ... }
The example in comment 4 is wrong. A better way to demonstrate usability of JavaImporter is this code: var SwingGui = JavaImporter(Packages.javax.swing, Packages.javax.swing.event, Packages.javax.swing.border, java.awt.event, java.awt.Point, java.awt.Rectangle, java.awt.Dimension); ... with (SwingGui) { var mybutton = new JButton(test); var mypoint = new Point(10, 10); var myframe = new JFrame(); ... }
Proper target milestone
Target Milestone: --- → 1.5R6
Attached patch Regression fixSplinter Review
The patch (which I already committed) fixes regression reported by Merten Schumman: The new implementation of importPackage/importClass assumed that thisObj should always be ImporterTopLevel. But this is wrong when ImporterTopLevel is used as top scope object to provide global import* functions since such scope can be a part of prototype chain.
JavaImporter is an absolutely useful extension to Rhino IMHO, nice feature! Merten
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: