PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/bsh/ClassNameSpace.java

#
Java | 310 lines | 160 code | 36 blank | 114 comment | 38 complexity | 245a6bf69e61f92934ae109a4de3b1e0 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*****************************************************************************
  2. * *
  3. * This file is part of the BeanShell Java Scripting distribution. *
  4. * Documentation and updates may be found at http://www.beanshell.org/ *
  5. * *
  6. * Sun Public License Notice: *
  7. * *
  8. * The contents of this file are subject to the Sun Public License Version *
  9. * 1.0 (the "License"); you may not use this file except in compliance with *
  10. * the License. A copy of the License is available at http://www.sun.com *
  11. * *
  12. * The Original Code is BeanShell. The Initial Developer of the Original *
  13. * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
  14. * (C) 2000. All Rights Reserved. *
  15. * *
  16. * GNU Public License Notice: *
  17. * *
  18. * Alternatively, the contents of this file may be used under the terms of *
  19. * the GNU Lesser General Public License (the "LGPL"), in which case the *
  20. * provisions of LGPL are applicable instead of those above. If you wish to *
  21. * allow use of your version of this file only under the terms of the LGPL *
  22. * and not to allow others to use your version of this file under the SPL, *
  23. * indicate your decision by deleting the provisions above and replace *
  24. * them with the notice and other provisions required by the LGPL. If you *
  25. * do not delete the provisions above, a recipient may use your version of *
  26. * this file under either the SPL or the LGPL. *
  27. * *
  28. * Patrick Niemeyer (pat@pat.net) *
  29. * Author of Learning Java, O'Reilly & Associates *
  30. * http://www.pat.net/~pat/ *
  31. * *
  32. *****************************************************************************/
  33. package bsh;
  34. import java.util.Vector;
  35. /**
  36. A ClassNameSpace represents the body a scripted class definition or
  37. scripted class instance. In the case of the class definition it serves as
  38. the Class object and holds the class initializer method (called before the
  39. constructor).
  40. <p/>
  41. When serving as a class definition, only static members are visible and
  42. attempting to access an instance member will cause a "can't reach instance
  43. from static context" exception.
  44. <p/>
  45. When serving as a class instance static members are dissallowed (except
  46. by the initializer) and are ignored, allowing them to be seen in the class
  47. definition.
  48. <p/>
  49. Class instances also serve as the top scope of instance variables declared
  50. within the class (by the initializer).
  51. */
  52. /*
  53. This would obviously be cleaner if we made a second subclass of NameSpace:
  54. ClassInstanceNameSpace, however with code generation I think we'll only
  55. need one entity (the instance namespace) so let's leave both in here
  56. for now.
  57. */
  58. class ClassNameSpace extends NameSpace
  59. {
  60. public static final int CLASS=1, INSTANCE=2;
  61. int type;
  62. //Class [] interfaces;
  63. public ClassNameSpace(
  64. NameSpace parent, String name, int type )
  65. throws EvalError
  66. {
  67. super( parent, name );
  68. this.type = type;
  69. }
  70. /**
  71. Set the interfaces. This is used by constructClassInstance() on
  72. CLASS types.
  73. public setInterfaces( Class [] interfaces ) {
  74. this.interfaces = interfaces;
  75. }
  76. */
  77. /**
  78. @return may return null
  79. Class [] getInterfaces()
  80. {
  81. if ( getParent() instanceof ClassNameSpace
  82. && ((ClassNameSpace)getParent()).isClass()
  83. )
  84. {
  85. // collect all interfaces from super chain
  86. ClassNameSpace cns = (ClassNameSpace)getParent();
  87. Vector v = new Vector();
  88. }
  89. else
  90. return interfaces;
  91. }
  92. */
  93. public void setVariable( String name, Object value, boolean strictJava )
  94. throws UtilEvalError
  95. {
  96. // class instance is top scope for loose vars
  97. if ( isClassInstance() ) {
  98. boolean recurse = false;
  99. super.setVariable( name, value, strictJava, recurse );
  100. }
  101. super.setVariable( name, value, strictJava );
  102. }
  103. /**
  104. This is a hook to allow ClassNameSpace to add functionality in
  105. getVariableImpl()
  106. */
  107. protected boolean isVisible( Variable var )
  108. throws UtilEvalError
  109. {
  110. // If we are a class declaration space being asked for an instance
  111. // variable throw an exception.
  112. if ( isClass() && var != null && !var.hasModifier("static") )
  113. throw new UtilEvalError( "Can't reach instance var: "+var.name
  114. +" from static context: "+this);
  115. // If we are a class instance ignore any static variables and allow
  116. // them to be found in the static class space.
  117. if ( isClassInstance() && var != null && var.hasModifier("static") )
  118. return false;
  119. return true;
  120. }
  121. /**
  122. This is a hook to allow ClassNameSpace to add functionality to
  123. getMethod()
  124. */
  125. protected boolean isVisible( BshMethod method )
  126. throws UtilEvalError
  127. {
  128. String name = null;
  129. if ( method != null )
  130. name = method.getName();
  131. // If we are a class def space being asked for an instance
  132. // method throw an exception.
  133. if ( isClass() && method != null && !method.hasModifier("static")
  134. // Bit of a hack, allow us to see default constructor
  135. && !name.equals( BSHClassDeclaration.CLASSINITNAME )
  136. // See regular constructors
  137. && !nsName.equals( name )
  138. )
  139. throw new UtilEvalError(
  140. "Can't reach instance method: "+name
  141. +" from static context: "+this);
  142. // If we are a class instance ignore any static methods and allow
  143. // them to be found in the static class space.
  144. if ( isClassInstance() && method != null
  145. && method.hasModifier("static")
  146. )
  147. return false;
  148. return true;
  149. }
  150. public boolean isClassInstance() { return type == INSTANCE ; }
  151. public boolean isClass() { return type == CLASS; }
  152. public This constructClassInstance( Object[] args,
  153. Interpreter interpreter, CallStack callstack, SimpleNode callerInfo )
  154. throws EvalError
  155. {
  156. NameSpace classNameSpace = this;
  157. String className = classNameSpace.getName();
  158. // Get the default constructor
  159. BshMethod classInitializer = null;
  160. try {
  161. classInitializer =
  162. classNameSpace.getMethod(
  163. BSHClassDeclaration.CLASSINITNAME, new Class[0] );
  164. } catch ( UtilEvalError e ) { // shouldn't happen
  165. throw e.toEvalError(
  166. "Error getting default constructor", callerInfo, callstack );
  167. }
  168. if ( classInitializer == null )
  169. throw new EvalError("Unable to find initializer for class.",
  170. callerInfo, callstack);
  171. // Get the (non-initializer) constructor, if any
  172. Class [] sig = Reflect.getTypes( args );
  173. BshMethod constructor = null;
  174. try {
  175. constructor =
  176. classNameSpace.getMethod( className, sig );
  177. } catch ( UtilEvalError e ) { // shouldn't happen
  178. throw e.toEvalError(
  179. "Error getting constructor", callerInfo, callstack );
  180. }
  181. // No constructor found
  182. if ( constructor == null )
  183. {
  184. // args constructor not found
  185. if ( args.length > 0 )
  186. throw new EvalError("Constructor not found: "+
  187. StringUtil.methodString( className, sig ),
  188. callerInfo, callstack );
  189. else
  190. // no args constructor missing. Ok if no other constructors exist
  191. {
  192. BshMethod [] methods = classNameSpace.getMethods();
  193. for(int i=0; i<methods.length; i++)
  194. if ( methods[i].getName().equals( className ) )
  195. throw new EvalError(
  196. "Default (no args) constructor not found: "+
  197. StringUtil.methodString( className, sig ),
  198. callerInfo, callstack );
  199. }
  200. }
  201. // Invoke the constructors
  202. // Recurse to handle superclasses
  203. NameSpace superNameSpace = null;
  204. if ( classNameSpace.getParent() instanceof ClassNameSpace
  205. && ((ClassNameSpace)classNameSpace.getParent()).isClass()
  206. )
  207. {
  208. // Call the superClass's default constructor
  209. // Note: this isn't always right -
  210. // We need to allow super() in the constructor()
  211. This superInstance =
  212. ((ClassNameSpace)classNameSpace.getParent())
  213. .constructClassInstance(
  214. new Object[0], interpreter, callstack, callerInfo );
  215. superNameSpace = superInstance.getNameSpace();
  216. }
  217. // Chain the instance namespaces
  218. NameSpace instanceNameSpace;
  219. if ( superNameSpace != null )
  220. instanceNameSpace = new ClassNameSpace(
  221. superNameSpace, className, INSTANCE );
  222. else
  223. instanceNameSpace = new ClassNameSpace(
  224. classNameSpace, className, INSTANCE );
  225. callstack.push( instanceNameSpace );
  226. // Invoke the class initializer method
  227. try {
  228. classInitializer.invoke(
  229. new Object[0], interpreter, callstack, callerInfo,
  230. true/*overrideNameSpace*/ );
  231. } catch ( EvalError e ) {
  232. e.reThrow("Exception in default constructor: "+e);
  233. }
  234. // Call the specific constructor if any
  235. if ( constructor != null )
  236. {
  237. try {
  238. constructor.invoke(
  239. args, interpreter, callstack, callerInfo,
  240. true/*overrideNameSpace*/ );
  241. } catch ( EvalError e ) {
  242. e.reThrow("Exception in constructor: "+e);
  243. }
  244. }
  245. callstack.pop();
  246. // return the initialized object
  247. This instance = instanceNameSpace.getThis( interpreter );
  248. return instance;
  249. }
  250. protected void checkVariableModifiers( String name, Modifiers modifiers )
  251. throws UtilEvalError
  252. {
  253. // allow all valid inside class
  254. }
  255. protected void checkMethodModifiers( BshMethod method )
  256. throws UtilEvalError
  257. {
  258. // allow all valid inside class
  259. }
  260. public String toString()
  261. {
  262. return
  263. "Scripted Class "
  264. +(isClassInstance() ? "Instance " : "")
  265. +" : " +super.toString();
  266. }
  267. public static boolean isScriptedClass( Object obj )
  268. {
  269. return
  270. obj instanceof This
  271. && ((This)obj).getNameSpace() instanceof ClassNameSpace
  272. && ((ClassNameSpace)((This)obj).getNameSpace()).isClass();
  273. }
  274. }