public int getParameterCount() { if (null == this.javaMethod) return paramCount; return this.javaMethod.getParameterCount(); } /**
/** {@inheritDoc} */ @Override public int hashCode() { return getClass().hashCode() ^ getName().hashCode() ^ getDeclaringClass().hashCode() ^ getParameterCount() ^ getReturnType().hashCode() ^ getModifiers() ^ Stream.of(getParameterTypes()) .map(t -> null == t ? 39 : t.hashCode()) .reduce(75, (a, b) -> a ^ b).intValue(); } }
/** {@inheritDoc} */ @Override public boolean equals(Object o) { if (o == null) return false; if (this == o) return true; if (this.getClass() != o.getClass()) return false; Invocable member = (Invocable) o; if (!getName().equals(member.getName()) || getDeclaringClass() != member.getDeclaringClass() || getParameterCount() != member.getParameterCount() || getReturnType() != member.getReturnType() || getModifiers() != member.getModifiers()) return false; for (int i = 0; i < getParameterCount(); i++) if (getParameterTypes()[i] != member.getParameterTypes()[i]) return false; return true; }
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; } }
/** Wrap super constructor as a BshMethod. * @param name constructor name * @param con the super constructor * @param declaringNameSpace the name space */ DelayedEvalBshMethod(String name, Invocable con, NameSpace declaringNameSpace) { this(name, con.getReturnTypeDescriptor(), null, new String[con.getParameterCount()], con.getParamTypeDescriptors(), null, new BSHBlock(0), declaringNameSpace, null, null, null); this.constructor = con; this.getModifiers().addModifier("public"); this.getParameterModifiers(); declaringNameSpace.setMethod(this); this.constructorArgs = This.CONTEXT_ARGS.get().remove(name); }
/** All purpose MethodHandle invoke implementation, with or without args. * @param base represents the base object instance. * @param pars parameter arguments * @return invocation result * @throws Throwable combined exceptions */ private synchronized Object invokeTarget(Object base, Object[] pars) throws Throwable { Reflect.logInvokeMethod("Invoking method (entry): ", this, pars); List<Object> params = collectParamaters(base, pars); Reflect.logInvokeMethod("Invoking method (after): ", this, params); if (getParameterCount() > 0) return getMethodHandle().invokeWithArguments(params); if (isStatic() || this instanceof ConstructorInvocable) return getMethodHandle().invoke(); return getMethodHandle().invoke(params.get(0)); }