static Object constructObject( Class<?> clas, Object object, Object[] args ) throws ReflectError, InvocationTargetException { if ( null == clas ) return Primitive.NULL; if ( clas.isInterface() ) throw new ReflectError( "Can't create instance of an interface: "+clas); Class<?>[] types = Types.getTypes(args); if (clas.isMemberClass() && !isStatic(clas) && null != object) types = Stream.concat(Stream.of(object.getClass()), Stream.of(types)).toArray(Class[]::new); Interpreter.debug("Looking for most specific constructor: ", clas); Invocable con = BshClassManager.memberCache.get(clas) .findMethod(clas.getName(), types); if ( con == null || (args.length != con.getParameterCount() && !con.isVarArgs() && !con.isInnerClass())) throw cantFindConstructor( clas, types ); try { return con.invoke( object, args ); } catch(InvocationTargetException e) { if (e.getCause().getCause() instanceof IllegalAccessException) throw new ReflectError( "We don't have permission to create an instance. " + e.getCause().getCause().getMessage() + " Use setAccessibility(true) to enable access.", e.getCause().getCause()); throw e; } }