Last Comment Bug 245882 - JavaImporter constructor
: JavaImporter constructor
Status: RESOLVED FIXED
:
Product: Rhino
Classification: Components
Component: Core (show other bugs)
: other
: All All
: -- enhancement (vote)
: 1.6R1
Assigned To: Igor Bukanov
: Phil Schwartau
:
Mentors:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2004-06-08 08:16 PDT by Igor Bukanov
Modified: 2004-08-17 00:12 PDT (History)
1 user (show)
See Also:
Crash Signature:
(edit)
QA Whiteboard:
Iteration: ---
Points: ---


Attachments
Implementing JavaImporter (15.49 KB, patch)
2004-06-08 15:59 PDT, Igor Bukanov
no flags Details | Diff | Splinter Review
Additional fix: no hiding Object.prototype.constructor (2.27 KB, patch)
2004-06-09 10:06 PDT, Igor Bukanov
no flags Details | Diff | Splinter Review
Regression fix (1.20 KB, patch)
2004-08-16 09:16 PDT, Igor Bukanov
no flags Details | Diff | Splinter Review

Description Igor Bukanov 2004-06-08 08:16:28 PDT
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.
Comment 1 Igor Bukanov 2004-06-08 15:59:42 PDT
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.
Comment 2 Igor Bukanov 2004-06-09 10:06:33 PDT
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.
Comment 3 Igor Bukanov 2004-06-09 10:11:09 PDT
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();
    ...
}

Comment 4 Igor Bukanov 2004-06-09 10:11:27 PDT
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();
    ...
}
Comment 5 Igor Bukanov 2004-08-03 04:36:57 PDT
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();
...
}
Comment 6 Igor Bukanov 2004-08-12 00:25:33 PDT
Proper target milestone
Comment 7 Igor Bukanov 2004-08-16 09:16:25 PDT
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 Merten Schumann 2004-08-17 00:12:31 PDT
JavaImporter is an absolutely useful extension to Rhino IMHO, nice feature!
   Merten

Note You need to log in before you can comment on or make changes to this bug.