Search in sources :

Example 1 with ClassConfig

use of io.servicecomb.common.javassist.ClassConfig in project java-chassis by ServiceComb.

the class ClassUtils method getOrCreateClass.

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

Example 2 with ClassConfig

use of io.servicecomb.common.javassist.ClassConfig in project java-chassis by ServiceComb.

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);
    StringBuilder sbMethod = new StringBuilder();
    StringBuilder sbMethodGenericSignature = new StringBuilder();
    for (Path path : swagger.getPaths().values()) {
        for (Operation operation : path.getOperations()) {
            boolean hasGenericSignature = false;
            sbMethod.setLength(0);
            sbMethodGenericSignature.setLength(0);
            Response result = operation.getResponses().get(SwaggerConst.SUCCESS_KEY);
            JavaType resultJavaType = ConverterMgr.findJavaType(classLoader, packageName, swagger, result.getSchema());
            hasGenericSignature = hasGenericSignature || resultJavaType.hasGenericTypes();
            sbMethod.append(JavassistUtils.getNameForGenerateCode(resultJavaType)).append(" ").append(operation.getOperationId()).append("(");
            sbMethodGenericSignature.append("(");
            for (Parameter parameter : operation.getParameters()) {
                String paramName = parameter.getName();
                paramName = correctMethodParameterName(paramName);
                JavaType paramJavaType = ConverterMgr.findJavaType(classLoader, packageName, swagger, parameter);
                hasGenericSignature = hasGenericSignature || paramJavaType.hasGenericTypes();
                String code = String.format("%s %s,", paramJavaType.getRawClass().getName(), paramName);
                sbMethod.append(code);
                sbMethodGenericSignature.append(paramJavaType.getGenericSignature());
            }
            if (!operation.getParameters().isEmpty()) {
                sbMethod.setLength(sbMethod.length() - 1);
            }
            sbMethod.append(");");
            sbMethodGenericSignature.append(")");
            sbMethodGenericSignature.append(resultJavaType.getGenericSignature());
            if (hasGenericSignature) {
                classConfig.addMethod(sbMethod.toString(), sbMethodGenericSignature.toString());
            } else {
                classConfig.addMethod(sbMethod.toString(), null);
            }
        }
    }
    return JavassistUtils.createClass(classLoader, classConfig);
}
Also used : Path(io.swagger.models.Path) Response(io.swagger.models.Response) JavaType(com.fasterxml.jackson.databind.JavaType) ClassConfig(io.servicecomb.common.javassist.ClassConfig) BodyParameter(io.swagger.models.parameters.BodyParameter) Parameter(io.swagger.models.parameters.Parameter) Operation(io.swagger.models.Operation)

Example 3 with ClassConfig

use of io.servicecomb.common.javassist.ClassConfig in project java-chassis by ServiceComb.

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(classConfig);
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) SwaggerGenerator(io.servicecomb.swagger.generator.core.SwaggerGenerator) ClassConfig(io.servicecomb.common.javassist.ClassConfig) Method(java.lang.reflect.Method) BodyParameter(io.swagger.models.parameters.BodyParameter)

Aggregations

JavaType (com.fasterxml.jackson.databind.JavaType)3 ClassConfig (io.servicecomb.common.javassist.ClassConfig)3 BodyParameter (io.swagger.models.parameters.BodyParameter)2 SwaggerGenerator (io.servicecomb.swagger.generator.core.SwaggerGenerator)1 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