JavaImporter constructor

RESOLVED FIXED in 1.6R1

Status

Rhino
Core
--
enhancement
RESOLVED FIXED
13 years ago
13 years ago

People

(Reporter: Igor Bukanov, Assigned: Igor Bukanov)

Tracking

Details

Attachments

(3 attachments)

(Assignee)

Description

13 years ago
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.
(Assignee)

Comment 1

13 years ago
Created attachment 150309 [details] [diff] [review]
Implementing JavaImporter

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.
(Assignee)

Comment 2

13 years ago
Created attachment 150367 [details] [diff] [review]
Additional fix: no hiding Object.prototype.constructor

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.
(Assignee)

Comment 3

13 years ago
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
Last Resolved: 13 years ago
Resolution: --- → FIXED
(Assignee)

Comment 4

13 years ago
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();
    ...
}
(Assignee)

Comment 5

13 years ago
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();
...
}
(Assignee)

Comment 6

13 years ago
Proper target milestone
Target Milestone: --- → 1.5R6
(Assignee)

Comment 7

13 years ago
Created attachment 156272 [details] [diff] [review]
Regression fix

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.

Comment 8

13 years ago
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.