All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.linkkou.databulid.extension.asinglefield.BulidAsingleField Maven / Gradle / Ivy

package com.github.linkkou.databulid.extension.asinglefield;

import com.github.linkkou.databulid.annotation.MapperConfig;
import com.github.linkkou.databulid.extension.asinglefield.annotation.MappersField;
import com.github.linkkou.databulid.extension.asinglefield.entity.ParametersEntity;
import com.github.linkkou.databulid.extension.asinglefield.parsing.ParsingParameters;
import com.github.linkkou.databulid.extension.asinglefield.parsing.ParsingReturn;
import com.github.linkkou.databulid.impl.DefaultCode;
import com.github.linkkou.databulid.spi.DataBuildSpi;
import com.github.linkkou.databulid.utils.AnnotationUtils;
import com.github.linkkou.databulid.utils.ClassUtils;
import com.github.linkkou.databulid.utils.MethodUtils;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author lk
 * @version 1.0
 * @date 2019/10/2 15:01
 */
@MapperConfig(MappersField.class)
public class BulidAsingleField implements DataBuildSpi {


    private StringBuilder stringBuilder = new StringBuilder();

    private ExecutableElement executableElement;

    private ProcessingEnvironment processingEnv;


    private static final String SETTER_FORMAT = "set(.*)";

    private static final Pattern SETTER_PATTERN = Pattern.compile(SETTER_FORMAT);

    @Override
    public String getCode(DefaultCode defaultCode, ExecutableElement executableElement, ProcessingEnvironment processingEnv) {
        this.executableElement = executableElement;
        this.processingEnv = processingEnv;
        final ParametersEntity parametersEntity = parsingReturn(defaultCode);
        final List parametersEntity1 = parsingParameters();
        //对比字段,生成代码
        parametersEntity.getVariableMethodParameterList().stream().forEach(x -> {
            parametersEntity1.forEach(x1 -> {
                x1.getVariableMethodParameterList().stream().forEach(x2 -> {
                    if (x.getMatchingMethodName().equals(x2.getMatchingMethodName())) {
                        stringBuilder.append(String.format("%s.%s(%s.%s());", parametersEntity.getName(), x.getOriginalMethodName(), x1.getName(), x2.getOriginalMethodName()));
                    }
                });
            });
        });
        stringBuilder.append(String.format("return %s", parametersEntity.getName()));
        return stringBuilder.toString();
    }


    /**
     * 获取方法返回的对象
     */
    private ParametersEntity parsingReturn(DefaultCode defaultCode) {
        ParsingReturn parsingReturn = new ParsingReturn(defaultCode, executableElement, processingEnv, stringBuilder);
        return parsingReturn.parsing();
    }

    /**
     * 获取到参数
     *
     * @return list
     */
    private List parsingParameters() {
        ParsingParameters parsingParameters = new ParsingParameters(executableElement, processingEnv);
        return parsingParameters.parsing();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy