public static MSimpleType tryForClass(Class<?> cls) { if(cls.isEnum()){ Field field = Reflection.findField(cls, "value"); if(null != field) { return tryForClass(field.getType()); }else{ return tryForClass(String.class); } } for(MSimpleType t : all()){ if(t.getJavaType().equals(cls)){ return t; } } for(MSimpleType t : all()){ for(Class<?> javaType : t.getJavaTypes()){ if(javaType.equals(cls)){ return t; } } } return null; }
@Override public Object apply(ActionParams params) { try { Map<String, Object> map = params.toMap(); engine.setContextVariable(PARAMS, Collections.unmodifiableMap(map)); Object value = JsConvert.convert(module.eval(map)); log.debug("Result : {}", value); if (returnTypeDefined && null != value && returnType.isSimpleType()) { value = Converts.convert(value, returnType.asSimpleType().getJavaType()); } return value; }catch (RuntimeException e) { throw e; }catch (Exception e) { throw new RuntimeException(e.getMessage(), e); }finally { engine.removeContextVariable(PARAMS); } }
Class<?> toJavaType(MType type) { if(type.isSimpleType()) { return type.asSimpleType().getJavaType(); } if(type.isCollectionType()) { return Array.newInstance(toJavaType(type.asCollectionType().getElementType()), 0).getClass(); } if(type.isComplexType() || type.isDictionaryType() || type.isTypeRef()) { return Map.class; } if(type.isObjectType()) { return Object.class; } throw new IllegalStateException("Unsupported data type '" + type + "'"); }
arg.setType(type.asSimpleType().getJavaType()); return; MType elementType = type.asCollectionType().getElementType(); if(elementType.isSimpleType()) { arg.setType(Array.newInstance(elementType.asSimpleType().getJavaType(),0).getClass()); }else if(elementType.isObjectType()){ arg.setType(Object[].class);
@Override public Object apply(ActionParams params) { Map<String,Object> map = params.toMap(); Object result; if(command.getMetadata().isSelect()) { //todo: page query, total count Query query = dao.createQuery(command).params(map); if(null != returnType) { if(returnType.isSimpleType()) { result = Converts.convert(query.scalarValueOrNull(), returnType.asSimpleType().getJavaType()); }else if(returnType.isCollectionType() && returnType.asCollectionType().getElementType().isSimpleType()) { result = query.scalars().list(returnType.asCollectionType().getElementType().asSimpleType().getJavaType()); }else { result = query.list(); } }else { result = query.list(); } }else{ //todo: the return type must be simple type result = dao.executeUpdate(command, map); if(null != returnType) { result = Converts.convert(result, returnType.asSimpleType().getJavaType()); } } return ApiResponse.of(result); }