/interpreter/tags/at2dist041108/src/edu/vub/at/objects/symbiosis/JavaPackage.java

http://ambienttalk.googlecode.com/ · Java · 206 lines · 112 code · 14 blank · 80 comment · 2 complexity · 4135bed12bdede81207e761e39ff1463 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * JavaPackage.java created on 19-nov-2006 at 12:31:39
  4. * (c) Programming Technology Lab, 2006 - 2007
  5. * Authors: Tom Van Cutsem & Stijn Mostinckx
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. package edu.vub.at.objects.symbiosis;
  29. import edu.vub.at.exceptions.InterpreterException;
  30. import edu.vub.at.exceptions.XClassNotFound;
  31. import edu.vub.at.objects.ATClosure;
  32. import edu.vub.at.objects.ATContext;
  33. import edu.vub.at.objects.ATObject;
  34. import edu.vub.at.objects.ATTypeTag;
  35. import edu.vub.at.objects.ATTable;
  36. import edu.vub.at.objects.coercion.NativeTypeTags;
  37. import edu.vub.at.objects.grammar.ATSymbol;
  38. import edu.vub.at.objects.mirrors.NativeClosure;
  39. import edu.vub.at.objects.mirrors.PrimitiveMethod;
  40. import edu.vub.at.objects.mirrors.Reflection;
  41. import edu.vub.at.objects.natives.FieldMap;
  42. import edu.vub.at.objects.natives.MethodDictionary;
  43. import edu.vub.at.objects.natives.NATNumber;
  44. import edu.vub.at.objects.natives.NATObject;
  45. import edu.vub.at.objects.natives.NATTable;
  46. import edu.vub.at.objects.natives.NATText;
  47. import edu.vub.at.objects.natives.grammar.AGSymbol;
  48. import java.util.LinkedList;
  49. import java.util.Vector;
  50. /**
  51. * A JavaPackage represents (part of) a Java package name and serves the same purpose
  52. * as AmbientTalk Namespace objects, but for loading Java classes rather than AT objects.
  53. *
  54. * The behaviour of a JavaPackage object relies on Java naming conventions for automatic
  55. * loading of classes. If some Java code does not follow the naming conventions, then explicit
  56. * loading of packages or classes must be done via a JavaPackage's provided base-level methods.
  57. *
  58. * Selecting a field f from a JavaPackage encapsulating the path p has the following semantics:
  59. * - if f starts with an uppercase symbol, the field access is interpreted as a class reference:
  60. * The JavaPackage tries to load the class p.f.
  61. * If the class does not exist, an XSelectorNotFound exception is thrown.
  62. * - if f starts with a lowercase symbol, the field access is interpreted as a subpackage reference:
  63. * The JavaPackage creates a new field referring to a JavaPackage whose path equals 'p.f.'
  64. *
  65. * JavaPackage instances are isolates, hence, they are pass-by-copy.
  66. *
  67. * @author tvcutsem
  68. */
  69. public final class JavaPackage extends NATObject {
  70. private static final String _PKG_SEP_ = ".";
  71. /** def class(name) { nil } */
  72. private static final PrimitiveMethod _PRIM_CLS_ = new PrimitiveMethod(
  73. AGSymbol.jAlloc("class"), NATTable.atValue(new ATObject[] { AGSymbol.jAlloc("name")})) {
  74. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  75. return ((JavaPackage)ctx.base_lexicalScope()).base_class(arguments.base_at(NATNumber.ONE).asSymbol());
  76. }
  77. };
  78. /** def package(name) { nil } */
  79. private static final PrimitiveMethod _PRIM_PKG_ = new PrimitiveMethod(
  80. AGSymbol.jAlloc("package"), NATTable.atValue(new ATObject[] { AGSymbol.jAlloc("name")})) {
  81. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  82. return ((JavaPackage)ctx.base_lexicalScope()).base_package(arguments.base_at(NATNumber.ONE).asSymbol());
  83. }
  84. };
  85. private final String path_;
  86. /**
  87. * A JavaPackage object encapsulates a package path.
  88. * A package path is a '.'-separated string, always ending with a '.'
  89. * The jlobby root package has an empty package path
  90. *
  91. * A JavaPackage is initialized as an AT/2 isolate object.
  92. *
  93. * @param path the pathname of this JavaPackage, e.g. 'java.' or 'java.lang.'
  94. */
  95. public JavaPackage(String path) {
  96. super(new ATTypeTag[] { NativeTypeTags._ISOLATE_ });
  97. path_ = path;
  98. try {
  99. super.meta_addMethod(_PRIM_CLS_);
  100. super.meta_addMethod(_PRIM_PKG_);
  101. } catch (InterpreterException e) {
  102. throw new RuntimeException("Failed to initialize a JavaPackage: " + e.getMessage());
  103. }
  104. }
  105. /**
  106. * Private constructor used only for cloning
  107. */
  108. private JavaPackage(FieldMap map,
  109. Vector state,
  110. LinkedList customFields,
  111. MethodDictionary methodDict,
  112. ATObject dynamicParent,
  113. ATObject lexicalParent,
  114. byte flags,
  115. ATTypeTag[] types,
  116. String path) throws InterpreterException {
  117. super(map, state, customFields, methodDict, dynamicParent, lexicalParent, flags, types);
  118. path_ = path;
  119. }
  120. /**
  121. * For a JavaPackage object, doesNotUnderstand triggers the querying of the Java classpath
  122. * to load classes corresponding to the missing selector. Depending on the case of the
  123. * selector's first letter, the access is interpreted as a class or a package reference.
  124. */
  125. public ATClosure meta_doesNotUnderstand(final ATSymbol selector) throws InterpreterException {
  126. // first, convert the AmbientTalk name to a Java selector.
  127. String s = selector.base_text().asNativeText().javaValue;
  128. if (Character.isUpperCase(s.charAt(0))) {
  129. // the field access is interpreted as a class reference
  130. return new NativeClosure.Accessor(selector, this) {
  131. protected ATObject access() throws InterpreterException {
  132. return base_class(selector);
  133. }
  134. };
  135. } else {
  136. // the field access is interpreted as a package reference
  137. return new NativeClosure.Accessor(selector, this) {
  138. protected ATObject access() throws InterpreterException {
  139. return base_package(selector);
  140. }
  141. };
  142. }
  143. }
  144. public NATText meta_print() throws InterpreterException {
  145. return NATText.atValue("<jpackage:"+path_+">");
  146. }
  147. protected NATObject createClone(FieldMap map,
  148. Vector state,
  149. LinkedList customFields,
  150. MethodDictionary methodDict,
  151. ATObject dynamicParent,
  152. ATObject lexicalParent,
  153. byte flags, ATTypeTag[] types) throws InterpreterException {
  154. return new JavaPackage(map,
  155. state,
  156. customFields,
  157. methodDict,
  158. dynamicParent,
  159. lexicalParent,
  160. flags,
  161. types,
  162. path_);
  163. }
  164. /**
  165. * Allows the AT programmer to explicitly load a class. This might be necessary if the
  166. * class starts with a lowercase letter.
  167. */
  168. public ATObject base_class(ATSymbol selector) throws InterpreterException {
  169. // try to see if a class corresponding to the selector prefixed with
  170. // this package's pathname exists
  171. String qualifiedClassname = path_ + Reflection.upSelector(selector);
  172. try {
  173. Class c = Class.forName(qualifiedClassname);
  174. JavaClass jc = JavaClass.wrapperFor(c);
  175. // bind the new class to the selector within this JavaPackage
  176. this.meta_defineField(selector, jc);
  177. return jc;
  178. } catch (ClassNotFoundException e) {
  179. throw new XClassNotFound(qualifiedClassname, e);
  180. }
  181. }
  182. /**
  183. * Allows the AT programmer to explicitly load a package. This might be necessary if the
  184. * package starts with an uppercase letter.
  185. */
  186. public ATObject base_package(ATSymbol selector) throws InterpreterException {
  187. // define a new Java package with a trailing '.'
  188. JavaPackage jpkg = new JavaPackage(path_ + Reflection.upSelector(selector) + _PKG_SEP_);
  189. this.meta_defineField(selector, jpkg);
  190. return jpkg;
  191. }
  192. }