PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/bsh/Capabilities.java

#
Java | 143 lines | 52 code | 17 blank | 74 comment | 9 complexity | 0cdebd0cf1fb5c94da3e0a13afb53396 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 org.gjt.sp.jedit.bsh;
  34. import java.util.Hashtable;
  35. /**
  36. The map of extended features supported by the runtime in which we live.
  37. <p>
  38. This class should be independent of all other bsh classes!
  39. <p>
  40. Note that tests for class existence here do *not* use the
  41. BshClassManager, as it may require other optional class files to be
  42. loaded.
  43. */
  44. public class Capabilities
  45. {
  46. private static boolean accessibility = false;
  47. public static boolean haveSwing() {
  48. // classExists caches info for us
  49. return classExists( "javax.swing.JButton" );
  50. }
  51. public static boolean canGenerateInterfaces() {
  52. // classExists caches info for us
  53. return classExists( "java.lang.reflect.Proxy" );
  54. }
  55. /**
  56. If accessibility is enabled
  57. determine if the accessibility mechanism exists and if we have
  58. the optional bsh package to use it.
  59. Note that even if both are true it does not necessarily mean that we
  60. have runtime permission to access the fields... Java security has
  61. a say in it.
  62. @see org.gjt.sp.jedit.bsh.ReflectManager
  63. */
  64. public static boolean haveAccessibility()
  65. {
  66. return accessibility;
  67. }
  68. public static void setAccessibility( boolean b )
  69. throws Unavailable
  70. {
  71. if ( b == false )
  72. {
  73. accessibility = false;
  74. return;
  75. }
  76. if ( !classExists( "java.lang.reflect.AccessibleObject" )
  77. || !classExists("org.gjt.sp.jedit.bsh.reflect.ReflectManagerImpl")
  78. )
  79. throw new Unavailable( "Accessibility unavailable" );
  80. // test basic access
  81. try {
  82. String.class.getDeclaredMethods();
  83. } catch ( SecurityException e ) {
  84. throw new Unavailable("Accessibility unavailable: "+e);
  85. }
  86. accessibility = true;
  87. }
  88. private static Hashtable classes = new Hashtable();
  89. /**
  90. Use direct Class.forName() to test for the existence of a class.
  91. We should not use BshClassManager here because:
  92. a) the systems using these tests would probably not load the
  93. classes through it anyway.
  94. b) bshclassmanager is heavy and touches other class files.
  95. this capabilities code must be light enough to be used by any
  96. system **including the remote applet**.
  97. */
  98. public static boolean classExists( String name )
  99. {
  100. Object c = classes.get( name );
  101. if ( c == null ) {
  102. try {
  103. /*
  104. Note: do *not* change this to
  105. BshClassManager plainClassForName() or equivalent.
  106. This class must not touch any other bsh classes.
  107. */
  108. c = Class.forName( name );
  109. } catch ( ClassNotFoundException e ) { }
  110. if ( c != null )
  111. classes.put(c,"unused");
  112. }
  113. return c != null;
  114. }
  115. /**
  116. An attempt was made to use an unavailable capability supported by
  117. an optional package. The normal operation is to test before attempting
  118. to use these packages... so this is runtime exception.
  119. */
  120. public static class Unavailable extends UtilEvalError
  121. {
  122. public Unavailable(String s ){ super(s); }
  123. }
  124. }