public RpcContext setInvocation(Invocation invocation) { this.invocation = invocation; if (invocation != null) { setMethodName(invocation.getMethodName()); setParameterTypes(invocation.getParameterTypes()); setArguments(invocation.getArguments()); } return this; }
@Mock public static void validatePublicVoidNoArg( Invocation invocation, boolean isStatic, List<Throwable> errors) { FrameworkMethod it = invocation.getInvokedInstance(); if (!isStatic && it.getMethod().getParameterTypes().length > 0) { it.validatePublicVoid(false, errors); return; } invocation.proceed(); }
protected <T> T invoke(CollectionOperation operation) { final NodeEngine nodeEngine = getNodeEngine(); try { Invocation inv = nodeEngine .getOperationService() .createInvocationBuilder(getServiceName(), operation, partitionId) .build(); Future f = inv.invoke(); return nodeEngine.toObject(f.get()); } catch (Throwable throwable) { throw ExceptionUtil.rethrow(throwable); } }
private Object invokeData(CollectionOperation operation, Data dataKey) { final NodeEngine nodeEngine = getNodeEngine(); try { int partitionId = nodeEngine.getPartitionService().getPartitionId(dataKey); Invocation inv = nodeEngine .getOperationService() .createInvocationBuilder(CollectionService.SERVICE_NAME, operation, partitionId) .build(); Future f = inv.invoke(); return nodeEngine.toObject(f.get()); } catch (Throwable throwable) { throw ExceptionUtil.rethrow(throwable); } }
private void pagination(Invocation invocation, StatementHandler target) throws SQLException { final MetaObject metaStatementHandler = getMetaObject(target); final BoundSql boundSql = target.getBoundSql(); Page<?> page = PAGE_THREAD_LOCAL.get(); if (page == null) { page = findPageParameter(boundSql.getParameterObject()); } // 如果传入的参数中有分页对象且sql语句中有select,才做分页处理 String sql = boundSql.getSql().toLowerCase(); if (sql.startsWith("select") && page != null) { // 采用物理分页后,就不需要mybatis的内存分页了,所以重置下面的两个参数 metaStatementHandler.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET); metaStatementHandler.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT); // 设置分页对象里的总记录数和总页数 Connection connection = (Connection) invocation.getArgs()[0]; MappedStatement mappedStatement = (MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement"); if (page.isCountTotal()) { int recordsTotal = getTotalCount(sql, connection, mappedStatement, boundSql); page.setTotalNum(recordsTotal); } // 最后重写sql String pageSql = buildPageSql(sql, page); metaStatementHandler.setValue("delegate.boundSql.sql", pageSql); } }
@Mock public int computeX(Invocation inv, int a, int b) { inputValues.add(a); inputValues.add(b); int x = inv.proceed(); outputValues.add(x); return x; }
private <T> T invoke(Operation operation, Data dataKey) { final NodeEngine nodeEngine = getNodeEngine(); try { int partitionId = nodeEngine.getPartitionService().getPartitionId(dataKey); Invocation invocation = nodeEngine .getOperationService() .createInvocationBuilder(CollectionService.SERVICE_NAME, operation, partitionId) .build(); Future f; Object o; if (config.isStatisticsEnabled()) { long time = System.currentTimeMillis(); f = invocation.invoke(); o = f.get(); if (operation instanceof PutOperation) { getService() .getLocalMultiMapStatsImpl(proxyId) .incrementPuts(System.currentTimeMillis() - time); } else if (operation instanceof RemoveOperation || operation instanceof RemoveAllOperation) { getService() .getLocalMultiMapStatsImpl(proxyId) .incrementRemoves(System.currentTimeMillis() - time); } else if (operation instanceof GetAllOperation) { getService() .getLocalMultiMapStatsImpl(proxyId) .incrementGets(System.currentTimeMillis() - time); } } else { f = invocation.invoke(); o = f.get(); } return (T) nodeEngine.toObject(o); } catch (Throwable throwable) { throw ExceptionUtil.rethrow(throwable); } }
@Mock public int computeX(Invocation inv, int a, int b) { Integer x; try { x = inv.proceed(); return x; } finally { // Statements to be executed on exit would be here. //noinspection UnusedAssignment x = a + b; values.add(x); } }
@SuppressWarnings({"rawtypes", "unchecked"}) @Override public Object intercept(Invocation invocation) throws Throwable { final String name = invocation.getMethod().getName(); final Object target = invocation.getTarget(); if (target instanceof StatementHandler) { pagination(invocation, (StatementHandler) target); } else if (target instanceof Executor) { autoMap(invocation, name); } else if (target instanceof ResultSetHandler) { Object result = invocation.proceed(); if (result instanceof List) { Page page = PAGE_THREAD_LOCAL.get(); if (page != null) { page.addAll((List) result); PAGE_THREAD_LOCAL.remove(); return page; } } return result; } return invocation.proceed(); }
@SuppressWarnings({"rawtypes", "unchecked"}) private void autoMap(Invocation invocation, String name) throws ClassNotFoundException { final Object[] queryArgs = invocation.getArgs(); final MappedStatement ms = (MappedStatement) queryArgs[0]; final Object parameter = queryArgs[1]; String statementId = ms.getId(); MapperMeta meta = getMapperMeta(ms.getConfiguration(), statementId); if (meta.isFillEntity()) { // 将泛型类加入到参数中供CrudTemplate使用 if (parameter != null) { Map map; if (parameter instanceof Map) { map = (HashMap) parameter; map.put(CrudProvider.CLASS_KEY, meta.getEntity()); } else { map = new HashMap(); map.put(CrudProvider.PARA_KEY, parameter); map.put(CrudProvider.CLASS_KEY, meta.getEntity()); } queryArgs[1] = map; } else { queryArgs[1] = meta.getEntity(); } } if (meta.isFillResultMap()) { MetaObject metaMappedStatement = getMetaObject(ms); metaMappedStatement.setValue("resultMaps", meta.getResultMaps()); } if (name.equals("query")) { final RowBounds rowBounds = (RowBounds) queryArgs[2]; if (rowBounds == null || rowBounds == RowBounds.DEFAULT) { Page p = findPageParameter(queryArgs[1]); if (p != null) { queryArgs[2] = new RowBounds(p.getOffset(), p.getLimit()); } } } }
public Object doPage(Invocation invocation) throws Throwable { log.debug("进入Executor"); final Object[] args = invocation.getArgs(); RowBounds rowBounds = (RowBounds) args[2]; PageParam pageParam = null; // PageContextHolder.get(); if (args[1] instanceof MapperMethod.ParamMap<?>) { MapperMethod.ParamMap<?> map = (MapperMethod.ParamMap<?>) args[1]; for (String key : map.keySet()) { if (key.startsWith("param")) { Object obj = map.get(key); if (obj instanceof PageParam) { pageParam = (PageParam) obj; } else if (args[1] instanceof MapperMethod.ParamMap<?>) { args[1] = obj; } } } } // 无分页参数 if (pageParam == null) { log.debug("默认查询"); return invocation.proceed(); } log.debug("分页查询"); PageWrapper page = new PageWrapper(); MappedStatement ms = (MappedStatement) args[0]; args[2] = RowBounds.DEFAULT; // 无需分页 if (pageParam.getPageSize() == 0) { log.debug("全量查询"); Object result = invocation.proceed(); page.addAll((List) result); page.setPageNo(1); page.setPageSize(page.size()); page.setTotalPage(1); page.setTotalCount(page.size()); return page; } SqlSource sqlSource = ((MappedStatement) args[0]).getSqlSource(); dialect = DialectFactory.buildDialect(ms.getConfiguration()); // 需总数 args[0] = getMappedStatement(ms, sqlSource, args[1], Const.CACHE_KEY_COUNT, MappingEnum.INT); Object resultCount = invocation.proceed(); int totalCount = ((Integer) ((List) resultCount).get(0)); log.debug("总条数查询为 {}", totalCount); page.setTotalCount(totalCount); page.setPageNo(pageParam.getPageNo()); page.setPageSize(pageParam.getPageSize()); // 提升效率:count为0不再分页查 if (page.getTotalCount() == 0) { return page; } // 需分页 // 便捷查询:pageSize<=0 仅查询count if (pageParam.getPageSize() > 0) { args[0] = getMappedStatement(ms, sqlSource, args[1], Const.CACHE_KEY_PAGE, MappingEnum.DEFAULT); args[1] = setParameter((MappedStatement) args[0], args[1], pageParam, dialect); Object result = invocation.proceed(); page.addAll((List) result); log.debug("分页查询结束 {}", pageParam); } return page; }
@Mock public String getConfig(Invocation inv) { String config = inv.proceed(); assertNull(config); return "test"; }
@Mock(minInvocations = 1, maxInvocations = 3) public int computeX(Invocation inv, int a, int b) { assertTrue(a + b >= 0); assertNotNull(inv.getInvokedInstance()); return a - b; }
@Mock(invocations = 1) public void $init(Invocation inv, String config) { assertNotNull(inv.getInvokedInstance()); assertEquals("config", config); }
@Mock public int computeX(Invocation inv, int a, int b) { values.add(a + b); return inv.proceed(); }
@Mock public Object invokeExplosively(Invocation invocation, Object target, Object... params) throws Throwable { FrameworkMethod it = invocation.getInvokedInstance(); return decorator.invokeExplosively(it, target, params); }