PageRenderTime 16ms CodeModel.GetById 11ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/bsh/Capabilities.java

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