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     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(), getInitialMap());
  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) {
  94         return new NativeJavaImporter(args);
  95     }
  96 
  97     /**
  98      * "No such property" call placeholder.
  99      *
 100      * This can never be called as we override {@link ScriptObject#noSuchProperty}. We do declare it here as it's a signal
 101      * to {@link jdk.nashorn.internal.runtime.WithObject} that it's worth trying doing a {@code noSuchProperty} on this object.
 102      *
 103      * @param self self reference
 104      * @param name property name
 105      * @return never returns
 106      */
 107     @Function(attributes = Attribute.NOT_ENUMERABLE)
 108     public static Object __noSuchProperty__(final Object self, final Object name) {
 109         throw new AssertionError("__noSuchProperty__ placeholder called");
 110     }
 111 
 112     /**
 113      * "No such method call" placeholder
 114      *
 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     @Override
 138     protected Object invokeNoSuchProperty(final String name) {
 139         return createProperty(name);
 140     }
 141 
 142     private boolean createAndSetProperty(final CallSiteDescriptor desc) {
 143         final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
 144         final Object value = createProperty(name);
 145         if(value != null) {
 146             set(name, value, false);
 147             return true;
 148         }
 149         return false;
 150     }
 151 
 152     private Object createProperty(final String name) {
 153         final int len = args.length;
 154 
 155         for (int i = len - 1; i > -1; i--) {
 156             final Object obj = args[i];
 157 
 158             if (obj instanceof StaticClass) {
 159                 if (((StaticClass)obj).getRepresentedClass().getSimpleName().equals(name)) {
 160                     return obj;
 161                 }
 162             } else if (obj instanceof NativeJavaPackage) {
 163                 final String pkgName  = ((NativeJavaPackage)obj).getName();
 164                 final String fullName = pkgName.isEmpty() ? name : (pkgName + "." + name);
 165                 final Context context = Global.instance().getContext();
 166                 try {
 167                     return StaticClass.forClass(context.findClass(fullName));
 168                 } catch (final ClassNotFoundException e) {
 169                     // IGNORE
 170                 }
 171             }
 172         }
 173         return null;
 174     }
 175 }
--- EOF ---