src/jdk/nashorn/internal/objects/NativeJavaImporter.java

Print this page
rev 746 : 8031715: Indexed access to java package not working
Reviewed-by: lagergren, hannesw
rev 755 : 8035948: Redesign property listeners for shared classes
Reviewed-by: sundar, lagergren
rev 760 : 8037400: Remove getInitialMap getters and GlobalObject interface
Reviewed-by: lagergren, jlaskey, attila


  43  * Objects of this constructor are used along with {@code "with"} statements and as such are not usable in ECMAScript
  44  * strict mode. Example:
  45  * <pre>
  46  *     var imports = new JavaImporter(java.util, java.io);
  47  *     with (imports) {
  48  *         var m = new HashMap(); // java.util.HashMap
  49  *         var f = new File("."); // java.io.File
  50  *         ...
  51  *     }
  52  * </pre>
  53  * Note however that the preferred way for accessing Java types in Nashorn is through the use of
  54  * {@link NativeJava#type(Object, Object) Java.type()} method.
  55  */
  56 @ScriptClass("JavaImporter")
  57 public final class NativeJavaImporter extends ScriptObject {
  58     private final Object[] args;
  59 
  60     // initialized by nasgen
  61     private static PropertyMap $nasgenmap$;
  62 
  63     static PropertyMap getInitialMap() {
  64         return $nasgenmap$;
  65     }
  66 
  67     private NativeJavaImporter(final Object[] args, final ScriptObject proto, final PropertyMap map) {
  68         super(proto, map);
  69         this.args = args;
  70     }
  71 
  72     private NativeJavaImporter(final Object[] args, final Global global) {
  73         this(args, global.getJavaImporterPrototype(), global.getJavaImporterMap());
  74     }
  75 
  76     private NativeJavaImporter(final Object[] args) {
  77         this(args, Global.instance());
  78     }
  79 
  80     @Override
  81     public String getClassName() {
  82         return "JavaImporter";
  83     }
  84 
  85     /**
  86      * Constructor
  87      * @param isNew is the new operator used for instantiating this NativeJavaImporter
  88      * @param self self reference
  89      * @param args arguments
  90      * @return NativeJavaImporter instance
  91      */
  92     @Constructor(arity = 1)
  93     public static Object constructor(final boolean isNew, final Object self, final Object... args) {


 115      * This can never be called as we override {@link ScriptObject#noSuchMethod}. We do declare it here as it's a signal
 116      * to {@link jdk.nashorn.internal.runtime.WithObject} that it's worth trying doing a noSuchProperty on this object.
 117      *
 118      * @param self self reference
 119      * @param args arguments to method
 120      * @return never returns
 121      */
 122     @Function(attributes = Attribute.NOT_ENUMERABLE)
 123     public static Object __noSuchMethod__(final Object self, final Object... args) {
 124         throw new AssertionError("__noSuchMethod__ placeholder called");
 125     }
 126 
 127     @Override
 128     public GuardedInvocation noSuchProperty(final CallSiteDescriptor desc, final LinkRequest request) {
 129         return createAndSetProperty(desc) ? super.lookup(desc, request) : super.noSuchProperty(desc, request);
 130     }
 131 
 132     @Override
 133     public GuardedInvocation noSuchMethod(final CallSiteDescriptor desc, final LinkRequest request) {
 134         return createAndSetProperty(desc) ? super.lookup(desc, request) : super.noSuchMethod(desc, request);





 135     }
 136 
 137     private boolean createAndSetProperty(final CallSiteDescriptor desc) {
 138         final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
 139         final Object value = createProperty(name);
 140         if(value != null) {
 141             set(name, value, false);
 142             return true;
 143         }
 144         return false;
 145     }
 146 
 147     private Object createProperty(final String name) {
 148         final int len = args.length;
 149 
 150         for (int i = len - 1; i > -1; i--) {
 151             final Object obj = args[i];
 152 
 153             if (obj instanceof StaticClass) {
 154                 if (((StaticClass)obj).getRepresentedClass().getSimpleName().equals(name)) {


  43  * Objects of this constructor are used along with {@code "with"} statements and as such are not usable in ECMAScript
  44  * strict mode. Example:
  45  * <pre>
  46  *     var imports = new JavaImporter(java.util, java.io);
  47  *     with (imports) {
  48  *         var m = new HashMap(); // java.util.HashMap
  49  *         var f = new File("."); // java.io.File
  50  *         ...
  51  *     }
  52  * </pre>
  53  * Note however that the preferred way for accessing Java types in Nashorn is through the use of
  54  * {@link NativeJava#type(Object, Object) Java.type()} method.
  55  */
  56 @ScriptClass("JavaImporter")
  57 public final class NativeJavaImporter extends ScriptObject {
  58     private final Object[] args;
  59 
  60     // initialized by nasgen
  61     private static PropertyMap $nasgenmap$;
  62 




  63     private NativeJavaImporter(final Object[] args, final ScriptObject proto, final PropertyMap map) {
  64         super(proto, map);
  65         this.args = args;
  66     }
  67 
  68     private NativeJavaImporter(final Object[] args, final Global global) {
  69         this(args, global.getJavaImporterPrototype(), $nasgenmap$);
  70     }
  71 
  72     private NativeJavaImporter(final Object[] args) {
  73         this(args, Global.instance());
  74     }
  75 
  76     @Override
  77     public String getClassName() {
  78         return "JavaImporter";
  79     }
  80 
  81     /**
  82      * Constructor
  83      * @param isNew is the new operator used for instantiating this NativeJavaImporter
  84      * @param self self reference
  85      * @param args arguments
  86      * @return NativeJavaImporter instance
  87      */
  88     @Constructor(arity = 1)
  89     public static Object constructor(final boolean isNew, final Object self, final Object... args) {


 111      * This can never be called as we override {@link ScriptObject#noSuchMethod}. We do declare it here as it's a signal
 112      * to {@link jdk.nashorn.internal.runtime.WithObject} that it's worth trying doing a noSuchProperty on this object.
 113      *
 114      * @param self self reference
 115      * @param args arguments to method
 116      * @return never returns
 117      */
 118     @Function(attributes = Attribute.NOT_ENUMERABLE)
 119     public static Object __noSuchMethod__(final Object self, final Object... args) {
 120         throw new AssertionError("__noSuchMethod__ placeholder called");
 121     }
 122 
 123     @Override
 124     public GuardedInvocation noSuchProperty(final CallSiteDescriptor desc, final LinkRequest request) {
 125         return createAndSetProperty(desc) ? super.lookup(desc, request) : super.noSuchProperty(desc, request);
 126     }
 127 
 128     @Override
 129     public GuardedInvocation noSuchMethod(final CallSiteDescriptor desc, final LinkRequest request) {
 130         return createAndSetProperty(desc) ? super.lookup(desc, request) : super.noSuchMethod(desc, request);
 131     }
 132 
 133     @Override
 134     protected Object invokeNoSuchProperty(final String name) {
 135         return createProperty(name);
 136     }
 137 
 138     private boolean createAndSetProperty(final CallSiteDescriptor desc) {
 139         final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
 140         final Object value = createProperty(name);
 141         if(value != null) {
 142             set(name, value, false);
 143             return true;
 144         }
 145         return false;
 146     }
 147 
 148     private Object createProperty(final String name) {
 149         final int len = args.length;
 150 
 151         for (int i = len - 1; i > -1; i--) {
 152             final Object obj = args[i];
 153 
 154             if (obj instanceof StaticClass) {
 155                 if (((StaticClass)obj).getRepresentedClass().getSimpleName().equals(name)) {