Search in sources :

Example 1 with ClassConfig

use of org.apache.servicecomb.common.javassist.ClassConfig in project incubator-servicecomb-java-chassis by apache.

the class ClassUtils method getOrCreateClass.

// 获取modelImpl对应的class
public static Class<?> getOrCreateClass(ClassLoader classLoader, String packageName, Swagger swagger, Map<String, Property> properties, String clsName) {
    Class<?> cls = getClassByName(classLoader, clsName);
    if (cls != null) {
        return cls;
    }
    ClassConfig classConfig = new ClassConfig();
    classConfig.setClassName(clsName);
    if (null != properties) {
        for (Entry<String, Property> entry : properties.entrySet()) {
            JavaType propertyJavaType = ConverterMgr.findJavaType(classLoader, packageName, swagger, entry.getValue());
            classConfig.addField(entry.getKey(), propertyJavaType);
        }
    }
    cls = JavassistUtils.createClass(classLoader, classConfig);
    return cls;
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) ClassConfig(org.apache.servicecomb.common.javassist.ClassConfig) Property(io.swagger.models.properties.Property)

Example 2 with ClassConfig

use of org.apache.servicecomb.common.javassist.ClassConfig in project incubator-servicecomb-java-chassis by apache.

the class ClassUtils method getOrCreateBodyClass.

// 将一系列body parameter包装成一个class
public static Class<?> getOrCreateBodyClass(OperationGenerator operationGenerator, List<BodyParameter> bodyParameters) {
    SwaggerGenerator swaggerGenerator = operationGenerator.getSwaggerGenerator();
    Method method = operationGenerator.getProviderMethod();
    String clsName = swaggerGenerator.ensureGetPackageName() + "." + method.getName() + "Body";
    Class<?> cls = getClassByName(swaggerGenerator.getClassLoader(), clsName);
    if (cls != null) {
        return cls;
    }
    ClassConfig classConfig = new ClassConfig();
    classConfig.setClassName(clsName);
    // 2.预备body与明确body混合
    for (BodyParameter bp : bodyParameters) {
        JavaType javaType = ConverterMgr.findJavaType(swaggerGenerator.getClassLoader(), swaggerGenerator.ensureGetPackageName(), swaggerGenerator.getSwagger(), bp);
        classConfig.addField(bp.getName(), javaType);
    }
    return JavassistUtils.createClass(swaggerGenerator.getClassLoader(), classConfig);
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) SwaggerGenerator(org.apache.servicecomb.swagger.generator.core.SwaggerGenerator) ClassConfig(org.apache.servicecomb.common.javassist.ClassConfig) Method(java.lang.reflect.Method) BodyParameter(io.swagger.models.parameters.BodyParameter)

Example 3 with ClassConfig

use of org.apache.servicecomb.common.javassist.ClassConfig in project incubator-servicecomb-java-chassis by apache.

the class ClassUtils method createInterface.

private static Class<?> createInterface(Swagger swagger, ClassLoader classLoader, String packageName, String intfName) {
    ClassConfig classConfig = new ClassConfig();
    classConfig.setClassName(intfName);
    classConfig.setIntf(true);
    for (Path path : swagger.getPaths().values()) {
        for (Operation operation : path.getOperations()) {
            // 参数可能重名,所以packageName必须跟operation相关才能隔离
            String opPackageName = packageName + "." + operation.getOperationId();
            Response result = operation.getResponses().get(SwaggerConst.SUCCESS_KEY);
            JavaType resultJavaType = ConverterMgr.findJavaType(classLoader, opPackageName, swagger, result.getSchema());
            MethodConfig methodConfig = new MethodConfig();
            methodConfig.setName(operation.getOperationId());
            methodConfig.setResult(resultJavaType);
            for (Parameter parameter : operation.getParameters()) {
                String paramName = parameter.getName();
                paramName = correctMethodParameterName(paramName);
                JavaType paramJavaType = ConverterMgr.findJavaType(classLoader, opPackageName, swagger, parameter);
                methodConfig.addParameter(paramName, paramJavaType);
            }
            classConfig.addMethod(methodConfig);
        }
    }
    return JavassistUtils.createClass(classLoader, classConfig);
}
Also used : Path(io.swagger.models.Path) Response(io.swagger.models.Response) MethodConfig(org.apache.servicecomb.common.javassist.MethodConfig) JavaType(com.fasterxml.jackson.databind.JavaType) ClassConfig(org.apache.servicecomb.common.javassist.ClassConfig) BodyParameter(io.swagger.models.parameters.BodyParameter) Parameter(io.swagger.models.parameters.Parameter) Operation(io.swagger.models.Operation)

Aggregations

JavaType (com.fasterxml.jackson.databind.JavaType)3 ClassConfig (org.apache.servicecomb.common.javassist.ClassConfig)3 BodyParameter (io.swagger.models.parameters.BodyParameter)2 Operation (io.swagger.models.Operation)1 Path (io.swagger.models.Path)1 Response (io.swagger.models.Response)1 Parameter (io.swagger.models.parameters.Parameter)1 Property (io.swagger.models.properties.Property)1 Method (java.lang.reflect.Method)1 MethodConfig (org.apache.servicecomb.common.javassist.MethodConfig)1 SwaggerGenerator (org.apache.servicecomb.swagger.generator.core.SwaggerGenerator)1