public void refresh() { Iterable<Property> properties = Collections.emptyList(); if (beanHelper != null) { Image icon = beanHelper.getIcon(BeanInfo.ICON_COLOR_32x32); if (icon == null) { remove(top); } else { JXImageView logo = new JXImageView(); logo.setImage(icon); add(logo, BorderLayout.NORTH); } properties = filter( beanHelper.getProperties(), new Predicate<Property>() { @Override public boolean apply(Property input) { return !input.isHidden(); } }); } table.setModel(new MyTableModel(properties)); // should we expose minimum row/column sizes as a user property? table.setRowHeight(18); // essentially the minimum row height table.getColumnModel().getColumn(0).setMinWidth(1); table.getColumnModel().getColumn(0).setMaxWidth(1); table.getColumnModel().getColumn(1).setMinWidth(1); table.packAll(); revalidate(); }
public List<List<StageOutputJson>> getSnapshotBatches() { List<List<StageOutputJson>> result = new ArrayList<>(snapshotData.getSnapshotBatches().size()); for (List<StageOutput> snapshot : snapshotData.getSnapshotBatches()) { result.add(BeanHelper.wrapStageOutput(snapshot)); } return result; }
public SnapshotDataJson(List<List<StageOutputJson>> snapshotJson) { List<List<StageOutput>> result = new ArrayList<>(snapshotJson.size()); for (List<StageOutputJson> snapshot : snapshotJson) { result.add(BeanHelper.unwrapStageOutput(snapshot)); } this.snapshotData = new SnapshotData(result); }
static { // 获取所有的bean类与bean实例之间的映射关系(简称Bean Map) Map<Class<?>, Object> beanMap = BeanHelper.getBeanMap(); if (MapUtils.isNotEmpty(beanMap)) { // 遍历 bean map for (Map.Entry<Class<?>, Object> beanEntry : beanMap.entrySet()) { // 从bean map中获取bean类和bean实例 Class<?> beanClass = beanEntry.getKey(); Object beanInstance = beanEntry.getValue(); // 获取bean定义的所有成员变量 Field[] beanFields = beanClass.getDeclaredFields(); if (ArrayUtils.isNotEmpty(beanFields)) { // 遍历 bean field for (Field beanField : beanFields) { // 判断当前bean field是否带有Inject注解 if (beanField.isAnnotationPresent(Inject.class)) { // 在bean map中获取bean field对应的实例 Class<?> beanFieldClass = beanField.getType(); Object beanFieldInstance = beanMap.get(beanFieldClass); if (null != beanFieldInstance) { // 通过反射初始化bean field的值 ReflectionUtil.setField(beanInstance, beanField, beanFieldInstance); } } } } } } }
private Table createTable(Connection conn, ResultSet rs) throws SQLException { String realTableName = null; try { ResultSetMetaData rsMetaData = rs.getMetaData(); String schemaName = rs.getString("TABLE_SCHEM") == null ? "" : rs.getString("TABLE_SCHEM"); realTableName = rs.getString("TABLE_NAME"); String tableType = rs.getString("TABLE_TYPE"); String remarks = rs.getString("REMARKS"); if (remarks == null && dbHelper.isOracleDataBase()) { remarks = getOracleTableComments(realTableName); } Table table = new Table(); table.setSqlName(realTableName); table.setRemarks(remarks); if ("SYNONYM".equals(tableType) && dbHelper.isOracleDataBase()) { table.setOwnerSynonymName(getSynonymOwner(realTableName)); } retriveTableColumns(table); table.initExportedKeys(conn.getMetaData()); table.initImportedKeys(conn.getMetaData()); BeanHelper.copyProperties( table, TableOverrideValuesProvider.getTableOverrideValues(table.getSqlName())); return table; } catch (SQLException e) { throw new RuntimeException("create table object error,tableName:" + realTableName, e); } }
private MethodInfo chooseMethodWithMatchingParameters( Exchange exchange, String parameters, Collection<MethodInfo> operationList) throws AmbiguousMethodCallException { // we have hardcoded parameters so need to match that with the given operations Iterator<?> it = ObjectHelper.createIterator(parameters); int count = 0; while (it.hasNext()) { it.next(); count++; } List<MethodInfo> operations = new ArrayList<MethodInfo>(); for (MethodInfo info : operationList) { if (info.getParameters().size() == count) { operations.add(info); } } if (operations.isEmpty()) { return null; } else if (operations.size() == 1) { return operations.get(0); } // okay we still got multiple operations, so need to match the best one List<MethodInfo> candidates = new ArrayList<MethodInfo>(); for (MethodInfo info : operations) { it = ObjectHelper.createIterator(parameters); int index = 0; boolean matches = true; while (it.hasNext()) { String parameter = (String) it.next(); Class<?> parameterType = BeanHelper.getValidParameterType(parameter); Class<?> expectedType = info.getParameters().get(index).getType(); if (parameterType != null && expectedType != null) { if (!parameterType.isAssignableFrom(expectedType)) { matches = false; break; } } index++; } if (matches) { candidates.add(info); } } if (candidates.size() > 1) { MethodInfo answer = getSingleCovariantMethod(candidates); if (answer == null) { throw new AmbiguousMethodCallException(exchange, candidates); } return answer; } return candidates.size() == 1 ? candidates.get(0) : null; }
@Override public void propertyChange(PropertyChangeEvent evt) { if (evt.getSource() != beanHelper.getBean() || evt.getPropagationId() == JBeanEditor.class) { return; } refresh(); }
private List getTableColumns( Table table, List primaryKeys, List indices, Map uniqueIndices, Map uniqueColumns) throws SQLException { // get the columns List columns = new LinkedList(); ResultSet columnRs = getColumnsResultSet(table); while (columnRs.next()) { int sqlType = columnRs.getInt("DATA_TYPE"); String sqlTypeName = columnRs.getString("TYPE_NAME"); String columnName = columnRs.getString("COLUMN_NAME"); String columnDefaultValue = columnRs.getString("COLUMN_DEF"); String remarks = columnRs.getString("REMARKS"); if (remarks == null && dbHelper.isOracleDataBase()) { remarks = getOracleColumnComments(table.getSqlName(), columnName); } // if columnNoNulls or columnNullableUnknown assume "not nullable" boolean isNullable = (DatabaseMetaData.columnNullable == columnRs.getInt("NULLABLE")); int size = columnRs.getInt("COLUMN_SIZE"); int decimalDigits = columnRs.getInt("DECIMAL_DIGITS"); boolean isPk = primaryKeys.contains(columnName); boolean isIndexed = indices.contains(columnName); String uniqueIndex = (String) uniqueIndices.get(columnName); List columnsInUniqueIndex = null; if (uniqueIndex != null) { columnsInUniqueIndex = (List) uniqueColumns.get(uniqueIndex); } boolean isUnique = columnsInUniqueIndex != null && columnsInUniqueIndex.size() == 1; if (isUnique) { GLogger.trace("unique column:" + columnName); } Column column = new Column( table, sqlType, sqlTypeName, columnName, size, decimalDigits, isPk, isNullable, isIndexed, isUnique, columnDefaultValue, remarks); BeanHelper.copyProperties( column, TableOverrideValuesProvider.getColumnOverrideValues(table, column)); columns.add(column); } columnRs.close(); return columns; }
/** * Evaluate using parameter values where the values can be provided in the method name syntax. * * <p>This methods returns accordingly: * * <ul> * <li><tt>null</tt> - if not a parameter value * <li><tt>Void.TYPE</tt> - if an explicit null, forcing Camel to pass in <tt>null</tt> for * that given parameter * <li>a non <tt>null</tt> value - if the parameter was a parameter value, and to be used * </ul> * * @since 2.9 */ private Object evaluateParameterValue( Exchange exchange, int index, Object parameterValue, Class<?> parameterType) { Object answer = null; // convert the parameter value to a String String exp = exchange .getContext() .getTypeConverter() .convertTo(String.class, exchange, parameterValue); if (exp != null) { // check if its a valid parameter value boolean valid = BeanHelper.isValidParameterValue(exp); if (!valid) { // it may be a parameter type instead, and if so, then we should return null, // as this method is only for evaluating parameter values Boolean isClass = BeanHelper.isAssignableToExpectedType( exchange.getContext().getClassResolver(), exp, parameterType); // the method will return a non null value if exp is a class if (isClass != null) { return null; } } // use simple language to evaluate the expression, as it may use the simple language to // refer to message body, headers etc. Expression expression = null; try { expression = exchange.getContext().resolveLanguage("simple").createExpression(exp); parameterValue = expression.evaluate(exchange, Object.class); // use "null" to indicate the expression returned a null value which is a valid response // we need to honor if (parameterValue == null) { parameterValue = "null"; } } catch (Exception e) { throw new ExpressionEvaluationException( expression, "Cannot create/evaluate simple expression: " + exp + " to be bound to parameter at index: " + index + " on method: " + getMethod(), exchange, e); } // special for explicit null parameter values (as end users can explicit indicate they want // null as parameter) // see method javadoc for details if ("null".equals(parameterValue)) { return Void.TYPE; } // the parameter value may match the expected type, then we use it as-is if (parameterType.isAssignableFrom(parameterValue.getClass())) { valid = true; } else { // the parameter value was not already valid, but since the simple language have evaluated // the expression // which may change the parameterValue, so we have to check it again to see if its now // valid exp = exchange.getContext().getTypeConverter().tryConvertTo(String.class, parameterValue); // String values from the simple language is always valid if (!valid) { // re validate if the parameter was not valid the first time (String values should be // accepted) valid = parameterValue instanceof String || BeanHelper.isValidParameterValue(exp); } } if (valid) { // we need to unquote String parameters, as the enclosing quotes is there to denote a // parameter value if (parameterValue instanceof String) { parameterValue = StringHelper.removeLeadingAndEndingQuotes((String) parameterValue); } if (parameterValue != null) { try { // its a valid parameter value, so convert it to the expected type of the parameter answer = exchange .getContext() .getTypeConverter() .mandatoryConvertTo(parameterType, exchange, parameterValue); if (LOG.isTraceEnabled()) { LOG.trace( "Parameter #{} evaluated as: {} type: ", new Object[] {index, answer, ObjectHelper.type(answer)}); } } catch (Exception e) { if (LOG.isDebugEnabled()) { LOG.debug( "Cannot convert from type: {} to type: {} for parameter #{}", new Object[] {ObjectHelper.type(parameterValue), parameterType, index}); } throw new ParameterBindingException(e, method, index, parameterType, parameterValue); } } } } return answer; }
private MethodInfo chooseMethodWithMatchingParameters( Exchange exchange, String parameters, Collection<MethodInfo> operationList) throws AmbiguousMethodCallException { // we have hardcoded parameters so need to match that with the given operations Iterator<?> it = ObjectHelper.createIterator(parameters); int count = 0; while (it.hasNext()) { it.next(); count++; } List<MethodInfo> operations = new ArrayList<MethodInfo>(); for (MethodInfo info : operationList) { if (info.getParameters().size() == count) { operations.add(info); } } if (operations.isEmpty()) { return null; } else if (operations.size() == 1) { return operations.get(0); } // okay we still got multiple operations, so need to match the best one List<MethodInfo> candidates = new ArrayList<MethodInfo>(); MethodInfo fallbackCandidate = null; for (MethodInfo info : operations) { it = ObjectHelper.createIterator(parameters, ",", false); int index = 0; boolean matches = true; while (it.hasNext()) { String parameter = (String) it.next(); if (parameter != null) { // must trim parameter = parameter.trim(); } Class<?> parameterType = BeanHelper.getValidParameterType(parameter); Class<?> expectedType = info.getParameters().get(index).getType(); if (parameterType != null && expectedType != null) { // if its a simple language then we need to evaluate the expression // so we have the result and can find out what type the parameter actually is if (StringHelper.hasStartToken(parameter, "simple")) { LOG.trace( "Evaluating simple expression for parameter #{}: {} to determine the class type of the parameter", index, parameter); Object out = getCamelContext() .resolveLanguage("simple") .createExpression(parameter) .evaluate(exchange, Object.class); if (out != null) { parameterType = out.getClass(); } } // skip java.lang.Object type, when we have multiple possible methods we want to avoid it // if possible if (Object.class.equals(expectedType)) { fallbackCandidate = info; matches = false; break; } boolean matchingTypes = isParameterMatchingType(parameterType, expectedType); if (!matchingTypes) { matches = false; break; } } index++; } if (matches) { candidates.add(info); } } if (candidates.size() > 1) { MethodInfo answer = getSingleCovariantMethod(candidates); if (answer != null) { return answer; } } return candidates.size() == 1 ? candidates.get(0) : fallbackCandidate; }
private boolean matchMethod(Method method, String methodName) { if (methodName == null) { return true; } if (methodName.contains("(") && !methodName.endsWith(")")) { throw new IllegalArgumentException( "Name must have both starting and ending parenthesis, was: " + methodName); } // do not use qualifier for name matching String name = methodName; if (name.contains("(")) { name = ObjectHelper.before(name, "("); } // must match name if (name != null && !name.equals(method.getName())) { return false; } // is it a method with no parameters boolean noParameters = methodName.endsWith("()"); if (noParameters) { return method.getParameterTypes().length == 0; } // match qualifier types which is used to select among overloaded methods String types = ObjectHelper.between(methodName, "(", ")"); if (ObjectHelper.isNotEmpty(types)) { // we must qualify based on types to match method String[] parameters = StringQuoteHelper.splitSafeQuote(types, ','); Iterator<?> it = ObjectHelper.createIterator(parameters); for (int i = 0; i < method.getParameterTypes().length; i++) { if (it.hasNext()) { Class<?> parameterType = method.getParameterTypes()[i]; String qualifyType = (String) it.next(); if (ObjectHelper.isEmpty(qualifyType)) { continue; } // trim the type qualifyType = qualifyType.trim(); if ("*".equals(qualifyType)) { // * is a wildcard so we accept and match that parameter type continue; } if (BeanHelper.isValidParameterValue(qualifyType)) { // its a parameter value, so continue to next parameter // as we should only check for FQN/type parameters continue; } // if qualify type indeed is a class, then it must be assignable with the parameter type Boolean assignable = BeanHelper.isAssignableToExpectedType( getCamelContext().getClassResolver(), qualifyType, parameterType); // the method will return null if the qualifyType is not a class if (assignable != null && !assignable) { return false; } } else { // there method has more parameters than was specified in the method name qualifiers return false; } } // if the method has no more types then we can only regard it as matched // if there are no more qualifiers if (it.hasNext()) { return false; } } // the method matched return true; }
@Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String requestMethod = req.getMethod().toLowerCase(); String requestPath = req.getPathInfo(); Handler handler = ControllerHelper.getHandler(requestMethod, requestPath); if (handler != null) { Class<?> controllerClass = handler.getControllerClass(); Object controllerBean = BeanHelper.getBean(controllerClass); Map<String, Object> paramMap = new HashMap<String, Object>(); Enumeration<String> paramNames = req.getParameterNames(); while (paramNames.hasMoreElements()) { String paramName = paramNames.nextElement(); String paramValue = req.getParameter(paramName); paramMap.put(paramName, paramValue); } String body = CodecUtil.decodeURL(StreamUtil.getString(req.getInputStream())); if (StringUtil.isNotEmpty(body)) { String[] params = StringUtil.splitString(body, "&"); if (ArrayUtil.isNotEmpty(params)) { for (String param : params) { String[] array = StringUtil.splitString(param, "="); if (ArrayUtil.isNotEmpty(array) && array.length == 2) { String paramName = array[0]; String paramValue = array[1]; paramMap.put(paramName, paramValue); } } } } Param param = new Param(paramMap); Method actionMethod = handler.getActionMethod(); Object result = ReflectionUtil.invokeMethod(controllerBean, actionMethod, param); if (result instanceof View) { View view = (View) result; String path = view.getPath(); if (StringUtil.isNotEmpty(path)) { if (path.startsWith("/")) { resp.sendRedirect(req.getContextPath() + path); } else { Map<String, Object> model = view.getModel(); for (Map.Entry<String, Object> entry : model.entrySet()) { req.setAttribute(entry.getKey(), entry.getValue()); } req.getRequestDispatcher(ConfigHelper.getAppJspPath() + path).forward(req, resp); } } } else if (result instanceof Data) { Data data = (Data) result; Object model = data.getModel(); if (model != null) { resp.setContentType("application/json"); resp.setCharacterEncoding("UTF-8"); PrintWriter writer = resp.getWriter(); String json = JsonUtil.toJSON(model); writer.write(json); writer.flush(); writer.close(); } } } }