41 import jdk.nashorn.internal.runtime.NativeJavaPackage; |
41 import jdk.nashorn.internal.runtime.NativeJavaPackage; |
42 import jdk.nashorn.internal.runtime.PropertyMap; |
42 import jdk.nashorn.internal.runtime.PropertyMap; |
43 import jdk.nashorn.internal.runtime.ScriptObject; |
43 import jdk.nashorn.internal.runtime.ScriptObject; |
44 import jdk.nashorn.internal.runtime.ScriptRuntime; |
44 import jdk.nashorn.internal.runtime.ScriptRuntime; |
45 import jdk.nashorn.internal.runtime.UnwarrantedOptimismException; |
45 import jdk.nashorn.internal.runtime.UnwarrantedOptimismException; |
|
46 import jdk.nashorn.internal.runtime.WithObject; |
46 import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor; |
47 import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor; |
47 |
48 |
48 /** |
49 /** |
49 * This is "JavaImporter" constructor. This constructor allows you to use Java types omitting explicit package names. |
50 * This is "JavaImporter" constructor. This constructor allows you to use Java types omitting explicit package names. |
50 * Objects of this constructor are used along with {@code "with"} statements and as such are not usable in ECMAScript |
51 * Objects of this constructor are used along with {@code "with"} statements and as such are not usable in ECMAScript |
104 * @param name property name |
105 * @param name property name |
105 * @return value of the missing property |
106 * @return value of the missing property |
106 */ |
107 */ |
107 @Function(attributes = Attribute.NOT_ENUMERABLE) |
108 @Function(attributes = Attribute.NOT_ENUMERABLE) |
108 public static Object __noSuchProperty__(final Object self, final Object name) { |
109 public static Object __noSuchProperty__(final Object self, final Object name) { |
109 if (! (self instanceof NativeJavaImporter)) { |
110 final NativeJavaImporter javaImporter = getJavaImporter(self); |
110 throw typeError("not.a.java.importer", ScriptRuntime.safeToString(self)); |
111 if (javaImporter != null) { |
111 } |
112 return javaImporter.createProperty(JSType.toString(name)); |
112 return ((NativeJavaImporter)self).createProperty(JSType.toString(name)); |
113 } |
|
114 throw typeError("not.a.java.importer", ScriptRuntime.safeToString(self)); |
|
115 } |
|
116 |
|
117 private static NativeJavaImporter getJavaImporter(Object self) { |
|
118 final NativeJavaImporter expression; |
|
119 if (self instanceof NativeJavaImporter) { |
|
120 expression = (NativeJavaImporter)self; |
|
121 } else if (self instanceof ScriptObject) { |
|
122 expression = getJavaImporterInScope((ScriptObject)self); |
|
123 } else { |
|
124 expression = null; |
|
125 } |
|
126 return expression; |
|
127 } |
|
128 |
|
129 private static NativeJavaImporter getJavaImporterInScope(ScriptObject self) { |
|
130 for (ScriptObject obj = self; obj != null; obj = obj.getProto()) { |
|
131 if (obj instanceof WithObject) { |
|
132 final ScriptObject expression = ((WithObject)obj).getExpression(); |
|
133 if (expression instanceof NativeJavaImporter) { |
|
134 return (NativeJavaImporter)expression; |
|
135 } |
|
136 } |
|
137 } |
|
138 return null; |
113 } |
139 } |
114 |
140 |
115 /** |
141 /** |
116 * "No such method call" handler |
142 * "No such method call" handler |
117 * |
143 * |