public static boolean isPackageAccessible(Class<?> clazz) { return haveAccessibility() || !PACKAGE_ACCESS.matcher(clazz.getName()).matches(); }
public static void main( String args[] ) { if ( !Capabilities.classExists( "bsh.util.Util" ) ) System.out.println("Can't find the BeanShell utilities..."); if ( Capabilities.haveSwing() ) { //bsh.util.Util.startSplashScreen(); try { new Interpreter().eval("desktop()"); } catch ( EvalError e ) { System.err.println("Couldn't start desktop: "+e); } } else { System.err.println( "Can't find javax.swing package: " +" An AWT based Console is available but not built by default."); //AWTConsole.main( args ); } } }
/** Create a new instance of the class manager. Class manager instnaces are now associated with the interpreter. @see bsh.Interpreter.getClassManager() @see bsh.Interpreter.setClassLoader( ClassLoader ) */ public static BshClassManager createClassManager( Interpreter interpreter ) { BshClassManager manager; // Do we have the optional package? if ( Capabilities.classExists("bsh.classpath.ClassManagerImpl") ) try { // Try to load the module // don't refer to it directly here or we're dependent upon it Class<?> clazz = Capabilities.getExisting("bsh.classpath.ClassManagerImpl"); manager = (BshClassManager) clazz.getConstructor().newInstance(); } catch ( IllegalArgumentException | ReflectiveOperationException | SecurityException e) { throw new InterpreterError("Error loading classmanager", e); } else manager = new BshClassManager(); manager.declaringInterpreter = interpreter; return manager; }
if ( !Capabilities.haveAccessibility() ) Capabilities.setAccessibility( true );
public static boolean haveSwing() { // classExists caches info for us return classExists( "javax.swing.JButton" ); }
Capabilities.setAccessibility(true); } catch (Capabilities.Unavailable e) { throw new EvalError("Defining classes currently requires reflective Accessibility.",
/** Use direct Class.forName() to test for the existence of a class. We should not use BshClassManager here because: a) the systems using these tests would probably not load the classes through it anyway. b) bshclassmanager is heavy and touches other class files. this capabilities code must be light enough to be used by any system **including the remote applet**. */ public static boolean classExists( String name ) { if ( !classes.containsKey(name) ) try { /* Note: do *not* change this to BshClassManager plainClassForName() or equivalent. This class must not touch any other bsh classes. */ classes.put(name, Class.forName( name )); } catch ( ClassNotFoundException e ) { classes.put(name, null); } return getExisting( name ) != null; }
public static boolean haveSwing() { // classExists caches info for us return classExists( "javax.swing.JButton" ); }
/** Package private abstract invocable constructor. * Collects an accessible member entity as the common invocable apparatus. * If running privileged we enforce accessibility for all types here. * @param member an accessible member entity */ <M extends AccessibleObject & Member> Invocable(M member) { flags = member.getModifiers(); declaringClass = member.getDeclaringClass(); name = member.getName(); toString = member.toString(); lastParameterIndex = 0; isStatic = Reflect.isStatic(member); isSynthetic = member.isSynthetic(); if (Capabilities.haveAccessibility() && member.getDeclaringClass() != Class.class) member.setAccessible(true); }
public static void main( String args[] ) { if ( !Capabilities.classExists( "bsh.util.Util" ) ) System.out.println("Can't find the BeanShell utilities..."); if ( Capabilities.haveSwing() ) { bsh.util.Util.startSplashScreen(); try (Interpreter interpreter = new Interpreter()) { interpreter.eval("desktop()"); } catch ( IOException | EvalError e ) { System.err.println("Couldn't start desktop: "+e); } } else { System.err.println( "Can't find javax.swing package: " +" An AWT based Console is available but not built by default."); //AWTConsole.main( args ); } } }
/** Create a new instance of the class manager. Class manager instnaces are now associated with the interpreter. @see bsh.Interpreter.getClassManager() @see bsh.Interpreter.setClassLoader( ClassLoader ) */ public static BshClassManager createClassManager( Interpreter interpreter ) { BshClassManager manager; // Do we have the optional package? if ( Capabilities.classExists("bsh.classpath.ClassManagerImpl") ) try { // Try to load the module // don't refer to it directly here or we're dependent upon it Class clazz = Class.forName( "bsh.classpath.ClassManagerImpl" ); manager = (BshClassManager) clazz.newInstance(); } catch ( Exception e ) { throw new InterpreterError("Error loading classmanager", e); } else manager = new BshClassManager(); if ( interpreter == null ) interpreter = new Interpreter(); manager.declaringInterpreter = interpreter; classManagers.put(manager,null); return manager; }
/** * @throws ReflectError if the field is not found. */ /* Note: this should really just throw NoSuchFieldException... need to change related signatures and code. */ protected static Field resolveExpectedJavaField(Class clas, String fieldName, boolean staticOnly) throws UtilEvalError, ReflectError { Field field; try { if (Capabilities.haveAccessibility()) { field = findAccessibleField(clas, fieldName); } else // Class getField() finds only public (and in interfaces, etc.) { field = clas.getField(fieldName); } } catch (NoSuchFieldException e) { throw new ReflectError("No such field: " + fieldName, e); } catch (SecurityException e) { throw new UtilTargetError("Security Exception while searching fields of: " + clas, e); } if (staticOnly && !Modifier.isStatic(field.getModifiers())) { throw new UtilEvalError("Can't reach instance field: " + fieldName + " from static context: " + clas.getName()); } return field; }
/** Recursive processing of interfaces. * Methods are stored by reference. * @param interfaces for whom members are collected */ private void processInterfaces(Class<?>[] interfaces) { for (Class<?> intr : interfaces) { if (isPackageAccessible(intr)) { memberCache.init(intr); for (Field f : intr.getDeclaredFields()) cacheMember(Invocable.get(f)); for (Method m: intr.getDeclaredMethods()) if (isPublic(m) || haveAccessibility()) cacheMember(memberCache.get(intr) .findMethod(m.getName(), m.getParameterTypes())); } processInterfaces(intr.getInterfaces()); } }
if (isPackageAccessible(type) && ((isPackageScope(type) && !isPrivate(type)) || isPublic(type) || haveAccessibility())) { for (Field f : type.getDeclaredFields()) if (isPublic(f) || haveAccessibility()) cacheMember(Invocable.get(f)); for (Method m : type.getDeclaredMethods()) if (isPublic(m) || haveAccessibility()) if (clazz == type) cacheMember(Invocable.get(m)); else cacheMember(memberCache.get(type)
Object val = Primitive.VOID; if ( null != var && (!var.hasModifier("private") || haveAccessibility()) ) val = ns.unwrapVariable(var); if (Primitive.VOID != val) Object val = Primitive.VOID; if ( null != var && (!var.hasModifier("private") || haveAccessibility()) ) val = ns.unwrapVariable(var); if (Primitive.VOID != val)
Constructor[] constructors = Capabilities.haveAccessibility() ? clas.getDeclaredConstructors() : clas.getConstructors();
/** Get an LHS reference to an object field. This method also deals with the field style property access. In the field does not exist we check for a property setter. */ static LHS getLHSObjectField( Object object, String fieldName ) throws UtilEvalError, ReflectError { if ( object instanceof This ) return new LHS( ((This)object).namespace, fieldName, false ); try { Invocable f = resolveExpectedJavaField( object.getClass(), fieldName, false/*staticOnly*/ ); return new LHS(object, f); } catch ( ReflectError e ) { NameSpace ns = getThisNS(object); if (isGeneratedClass(object.getClass()) && null != ns && ns.isClass) { Variable var = ns.getVariableImpl(fieldName, true); if ( null != var && (!var.hasModifier("private") || haveAccessibility()) ) return new LHS(ns, fieldName); } // not a field, try property access if ( hasObjectPropertySetter( object.getClass(), fieldName ) ) return new LHS( object, fieldName ); else throw e; } }
boolean publicOnly = !Capabilities.haveAccessibility();
static LHS getLHSStaticField(Class<?> clas, String fieldName) throws UtilEvalError, ReflectError { try { Invocable f = resolveExpectedJavaField( clas, fieldName, true/*onlystatic*/); return new LHS(f); } catch ( ReflectError e ) { NameSpace ns = getThisNS(clas); if (isGeneratedClass(clas) && null != ns && ns.isClass) { Variable var = ns.getVariableImpl(fieldName, true); if ( null != var && (!var.hasModifier("private") || haveAccessibility()) ) return new LHS(ns, fieldName); } // not a field, try property access if ( hasObjectPropertySetter( clas, fieldName ) ) return new LHS( clas, fieldName ); else throw e; } }