1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.objects;
  27 
  28 import jdk.internal.dynalink.CallSiteDescriptor;
  29 import jdk.internal.dynalink.beans.StaticClass;
  30 import jdk.internal.dynalink.linker.GuardedInvocation;
  31 import jdk.internal.dynalink.linker.LinkRequest;
  32 import jdk.nashorn.internal.objects.annotations.Attribute;
  33 import jdk.nashorn.internal.objects.annotations.Constructor;
  34 import jdk.nashorn.internal.objects.annotations.Function;
  35 import jdk.nashorn.internal.objects.annotations.ScriptClass;
  36 import jdk.nashorn.internal.runtime.Context;
  37 import jdk.nashorn.internal.runtime.NativeJavaPackage;
  38 import jdk.nashorn.internal.runtime.PropertyMap;
  39 import jdk.nashorn.internal.runtime.ScriptObject;
  40 
  41 /**
  42  * This is "JavaImporter" constructor. This constructor allows you to use Java types omitting explicit package names.
  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) {
  90         return new NativeJavaImporter(args);
  91     }
  92 
  93     /**
  94      * "No such property" call placeholder.
  95      *
  96      * This can never be called as we override {@link ScriptObject#noSuchProperty}. We do declare it here as it's a signal
  97      * to {@link jdk.nashorn.internal.runtime.WithObject} that it's worth trying doing a {@code noSuchProperty} on this object.
  98      *
  99      * @param self self reference
 100      * @param name property name
 101      * @return never returns
 102      */
 103     @Function(attributes = Attribute.NOT_ENUMERABLE)
 104     public static Object __noSuchProperty__(final Object self, final Object name) {
 105         throw new AssertionError("__noSuchProperty__ placeholder called");
 106     }
 107 
 108     /**
 109      * "No such method call" placeholder
 110      *
 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)) {
 156                     return obj;
 157                 }
 158             } else if (obj instanceof NativeJavaPackage) {
 159                 final String pkgName  = ((NativeJavaPackage)obj).getName();
 160                 final String fullName = pkgName.isEmpty() ? name : (pkgName + "." + name);
 161                 final Context context = Global.instance().getContext();
 162                 try {
 163                     return StaticClass.forClass(context.findClass(fullName));
 164                 } catch (final ClassNotFoundException e) {
 165                     // IGNORE
 166                 }
 167             }
 168         }
 169         return null;
 170     }
 171 }
--- EOF ---