ByteCodeEventHandlerInfo(ComponentSystem handler, Method method, int priority, String activity, Collection<Class<? extends Component>> filterComponents, Collection<Class<? extends Component>> componentParams) { this.handler = handler; this.activity = activity; this.methodAccess = MethodAccess.get(handler.getClass()); methodIndex = methodAccess.getIndex(method.getName(), method.getParameterTypes()); this.filterComponents = ImmutableList.copyOf(filterComponents); this.componentParams = ImmutableList.copyOf(componentParams); this.priority = priority; }
static private void recursiveAddInterfaceMethodsToList (Class interfaceType, ArrayList<Method> methods) { addDeclaredMethodsToList(interfaceType, methods); for (Class nextInterface : interfaceType.getInterfaces()) recursiveAddInterfaceMethodsToList(nextInterface, methods); } }
static <T> T invokeObject( final Object instance, final String name, final Object... args) { return Fn.getNull(() -> { final MethodAccess access = MethodAccess.get(instance.getClass()); // Direct invoke, multi overwrite for unbox/box issue still existing. // TODO: Unbox/Box type issue Object result; try { result = access.invoke(instance, name, args); } catch (final Throwable ex) { ex.printStackTrace(); // Could not call, re-find the method by index // Search method by argument index because could not call directly final int index; final List<Class<?>> types = new ArrayList<>(); for (final Object arg : args) { types.add(Ut.toPrimary(arg.getClass())); } index = access.getIndex(name, types.toArray(new Class<?>[]{})); result = access.invoke(instance, index, args); } final Object ret = result; return Fn.getNull(() -> (T) ret, ret); }, instance, name); }
public Object invoke (Object target, Object[] args) throws IllegalAccessException, InvocationTargetException { try { return methodAccess.invoke(target, methodAccessIndex, args); } catch (Exception ex) { throw new InvocationTargetException(ex); } } }
MethodAccess methodAccess = MethodAccess.get(obj.getClass()); logResult = methodAccess.invoke(obj, logCache.getIndex()); formatMethod(recursivePath, methodAccess.getMethodNames()[logCache.getIndex()]));
/** * 构造器 * @param target */ protected ClassBeanWrapper(T target) { this.target = target; this._fields = new HashMap<String, Field>(); for (Field _field : getFields(target.getClass(), true)/*target.getClass().getDeclaredFields()*/) { if (Modifier.isStatic(_field.getModifiers())) { continue; } this._fields.put(_field.getName(), _field); } // this.methodAccess = __methodCache.get(target.getClass()); if (this.methodAccess == null) { this.methodAccess = MethodAccess.get(target.getClass()); __methodCache.put(target.getClass(), this.methodAccess); } }
/** * Builds the method sorted cache. Sorts the methods alphabetically by name. */ private static void buildMethodCache(Class<?> clazz, MethodAccess methodAccess) { if(!_methodSortCache.containsKey(clazz)) { LogCache[] sortedLogCache = new LogCache[methodAccess.getMethodNames().length]; String[] sortedMethodNames = Arrays.copyOf(methodAccess.getMethodNames(), sortedLogCache.length); Arrays.sort(sortedMethodNames); for(int i = 0; i < sortedLogCache.length; i++) { sortedLogCache[i] = new LogCache(methodAccess.getIndex(sortedMethodNames[i]), getMethodLogScope(clazz, sortedMethodNames[i])); } _methodSortCache.put(clazz, sortedLogCache); } }
public ParamsCommandDispatcher(MethodAccess access, CommandListener container, Method method, String plugin, Command command) { this.access = access; this.container = container; this.methodIndex = access.getIndex(method.getName(), method.getParameterTypes()); this.plugin = plugin; this.command = command;
static <T> T invokeObject( final Object instance, final String name, final Object... args) { return Fn.getNull(() -> { final MethodAccess access = MethodAccess.get(instance.getClass()); // Direct invoke, multi overwrite for unbox/box issue still existing. // TODO: Unbox/Box type issue Object result; try { result = access.invoke(instance, name, args); } catch (final Throwable ex) { ex.printStackTrace(); // Could not call, re-find the method by index // Search method by argument index because could not call directly final int index; final List<Class<?>> types = new ArrayList<>(); for (final Object arg : args) { types.add(Ut.toPrimary(arg.getClass())); } index = access.getIndex(name, types.toArray(new Class<?>[]{})); result = access.invoke(instance, index, args); } final Object ret = result; return Fn.getNull(() -> (T) ret, ret); }, instance, name); }
@Override @SuppressWarnings("unchecked") public U getValue(T target) { if (getterIndex != NO_METHOD) { return (U) methodAccess.invoke(target, getterIndex); } else { return (U) fieldAccess.get(target, fieldIndex); } }
MethodAccess access = MethodAccess.get(cls); Method[] methods = cls.getDeclaredMethods();
ByteCodeEventHandlerInfo(ComponentSystem handler, Method method, int priority, String activity, Collection<Class<? extends Component>> filterComponents, Collection<Class<? extends Component>> componentParams) { this.handler = handler; this.activity = activity; this.methodAccess = MethodAccess.get(handler.getClass()); methodIndex = methodAccess.getIndex(method.getName(), method.getParameterTypes()); this.filterComponents = ImmutableList.copyOf(filterComponents); this.componentParams = ImmutableList.copyOf(componentParams); this.priority = priority; }
@Override public void setValue(T target, U value) { if (setterIndex != NO_METHOD) { methodAccess.invoke(target, setterIndex, value); } else { fieldAccess.set(target, fieldIndex, value); } } }
/** * 初始化providerInvoker */ private Invoker<Object> initProviderInvoker(String key, String className, String methodName, String[] parameterTypeNames, URL url) { Invoker<Object> wrapperInvoker; Invoker<Object> invoker = new RpcProviderInvoker<>(getBeanFunc); Class clazz = ClassUtils.getClassByName(className); invoker.setInterface(clazz); invoker.setUrl(url); //类级别的注解 Provider typeProvider = invoker.getInterface().getAnnotation(Provider.class); setTypeParameters(typeProvider, url.getParameters()); Set<String> excludeFilters = Arrays.stream(typeProvider.excludeFilters()).collect(Collectors.toSet()); //兼容性更好些 Method method = ClassUtils.getMethod(className, methodName, parameterTypeNames); invoker.setMethod(method); //reflectasm更快些(实际上是把反射生成了直接调用的代码) MethodAccess methodAccess = MethodAccess.get(ClassUtils.getClassByName(className)); invoker.setMethodAccess(methodAccess); //方法级别的注解 Provider methodProvider = method.getAnnotation(Provider.class); //方法级别的会覆盖type级别的 if (null != methodProvider) { setMethodParameters(methodProvider, url.getParameters()); } wrapperInvoker = FilterWrapper.ins().buildInvokerChain(invoker, "", Constants.PROVIDER, excludeFilters); invokerMap.putIfAbsent(key, wrapperInvoker); return wrapperInvoker; }
private static void recursiveAddInterfaceMethodsToList (Class interfaceType, ArrayList<Method> methods) { addDeclaredMethodsToList(interfaceType, methods); for (Class nextInterface : interfaceType.getInterfaces()) { recursiveAddInterfaceMethodsToList(nextInterface, methods); } } }
ReflectASMFieldAccessor(Class<T> ownerType, Field field, Class<U> fieldType) throws InaccessibleFieldException { methodAccess = MethodAccess.get(ownerType); Method getter = ReflectionUtil.findGetter(field); if (getter != null) { getterIndex = methodAccess.getIndex(getter.getName()); } Method setter = ReflectionUtil.findSetter(field); if (setter != null) { setterIndex = methodAccess.getIndex(setter.getName()); } if (getterIndex == NO_METHOD || setterIndex == NO_METHOD) { fieldAccess = FieldAccess.get(ownerType); try { fieldIndex = fieldAccess.getIndex(field.getName()); } catch (IllegalArgumentException e) { throw new InaccessibleFieldException("Failed to create accessor for field '" + field.getName() + "' of type '" + ownerType.getName() + "'", e); } } }