private void addUserDefinedMethods(TopLevelClass exampleClass, Interface mapperClass, IntrospectedTable introspectedTable, MyBatisClasses cls) {
        for (Method action : mapperClass.getMethods()) {
            if (!userDefinedMethods.matcher(action.getName()).matches()) continue;
            StringBuilder args = new StringBuilder();
            List<Parameter> params = new ArrayList<Parameter>();
            boolean example = false;
            if (action.getParameters() != null)
                for (Parameter param : action.getParameters()) {
                    String name;
                    if (Objects.equals(param.getType(), exampleClass.getType())) {
                        example = true;
                        name = "this";
                    } else {
                        name = param.getName();
                        params.add(new Parameter(param.getType(), name));
                    }
                    if (args.length() > 0)
                        args.append(", ");
                    args.append(name);
                }
            if (!example) {
                //System.err.println("Invalid user-defined mapper method: "+action.getName());
                continue;
            }

            exampleClass.addMethod(method(
                PUBLIC, INT, action.getName(), _(sqlSession, "sql"), params.toArray(new Parameter[params.size()]), __(
                    "return sql.getMapper(" + cls.names.mapper + ".class)."+action.getName()+"("+args+");"
            )));
            exampleClass.addMethod(method(
                PUBLIC, INT, action.getName(), _(cls.types.mapper, "mapper"), params.toArray(new Parameter[params.size()]), __(
                    "return mapper."+action.getName()+"("+args+");"
            )));
        }
    }
    private void addCriteriaMethods(TopLevelClass topLevelClass, int newMethodsStart) {
        if (!generateCriteriaMethods) return;
        InnerClass criteria = null;
        for (InnerClass c : topLevelClass.getInnerClasses()) {
            if (c.getType().getShortName().equals("Criteria")) criteria = c;
        }
        if (criteria == null) return;
        boolean owner = false;
        for (Field f : criteria.getFields()) if (ExampleMethodsChainPlugin.OWNER.equals(f.getName())) owner = true;
        if (!owner) return;

        for (ListIterator<Method> methods = topLevelClass.getMethods().listIterator(newMethodsStart); methods.hasNext(); ) {
            Method base = methods.next();
            if (base.getVisibility() != PUBLIC || base.isStatic() || base.isConstructor()) continue;
            Method m = method(PUBLIC, base.getReturnType(), base.getName());
            StringBuilder sb = new StringBuilder();
            sb.append("return ").append(ExampleMethodsChainPlugin.OWNER).append(".").append(base.getName()).append("(");
            for (ListIterator<Parameter> params = base.getParameters().listIterator(); params.hasNext(); ) {
                if (params.hasPrevious()) sb.append(", ");
                Parameter p = params.next();
                m.addParameter(new Parameter(p.getType(), p.getName()));
                sb.append(p.getName());
            }
            sb.append(");");
            m.addBodyLine(sb.toString());
            criteria.addMethod(m);
        }
    }
    @Override
    public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        MyBatisClasses cls = MyBatisClasses.calculate(topLevelClass, introspectedTable);
        topLevelClass.addImportedType(sqlSession);
        if (cls.imports.mapper != null)
            topLevelClass.addImportedType(cls.imports.mapper);

        int newMethodsStart = topLevelClass.getMethods().size();

        addListMethods(topLevelClass, introspectedTable, cls);
        addGetRecordMethods(topLevelClass, introspectedTable, cls, firstMethod, firstWithBLOBsMethod, "list == null || list.isEmpty() ? null : list.get(0)");
        addGetRecordMethods(topLevelClass, introspectedTable, cls, singleMethod, singleWithBLOBsMethod, "list == null || list.size() != 1 ? null : list.get(0)");
        addGetRecordMethods(topLevelClass, introspectedTable, cls, optionalMethod, optionalWithBLOBsMethod, "list == null || list.isEmpty() ? new @result() : list.get(0)");
        addUpdateMethods(topLevelClass, introspectedTable, cls, updateMethod, updateWithBLOBsMethod, false);
        addUpdateMethods(topLevelClass, introspectedTable, cls, updateSelectiveMethod, updateSelectiveWithBLOBsMethod, true);
        addDeleteMethods(topLevelClass, introspectedTable, cls);
        addCountMethods(topLevelClass, introspectedTable, cls);
        if (mapperClass != null) {
            addUserDefinedMethods(topLevelClass, mapperClass, introspectedTable, cls);
            mapperClass = null;
            exampleClass = null;
        } else {
            exampleClass = topLevelClass;
        }

        addCriteriaMethods(topLevelClass, newMethodsStart);

        return true;
    }
 private void addCountMethods(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, MyBatisClasses cls) {
     if (!countMethod.startsWith(SKIP) && introspectedTable.getRules().generateCountByExample()) {
         topLevelClass.addMethod(method(
             PUBLIC, INT, countMethod, _(sqlSession, "sql"), __(
                 "return sql.getMapper(" + cls.names.mapper + ".class).countByExample(this);"
         )));
         topLevelClass.addMethod(method(
             PUBLIC, INT, countMethod, _(cls.types.mapper, "mapper"), __(
                 "return mapper.countByExample(this);"
         )));
     }
 }
 private void addUpdateMethods(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, MyBatisClasses cls, String base, String withBLOBs, boolean selective) {
     String record = cls.names.base;
     topLevelClass.addImportedType(cls.imports.base);
     String mapperMethod = selective ? "updateByExampleSelective" : "updateByExample";
     Rules r = introspectedTable.getRules();
     if (!base.startsWith(SKIP) && ( selective ? r.generateUpdateByExampleSelective() : r.generateUpdateByExampleWithoutBLOBs() )) {
         if (selective && r.generateRecordWithBLOBsClass()) {
             record = cls.names.blob;
             topLevelClass.addImportedType(cls.imports.blob);
         }
         topLevelClass.addMethod(method(
             PUBLIC, INT, base, _(sqlSession, "sql"), _(new FullyQualifiedJavaType(record), "record"), __(
                 "return sql.getMapper(" + cls.names.mapper + ".class)."+mapperMethod+"(record, this);"
         )));
         topLevelClass.addMethod(method(
             PUBLIC, INT, base, _(cls.types.mapper, "mapper"), _(new FullyQualifiedJavaType(record), "record"), __(
                 "return mapper."+mapperMethod+"(record, this);"
         )));
     }
     if (introspectedTable.hasBLOBColumns() && !withBLOBs.startsWith(SKIP) && !selective && r.generateUpdateByExampleWithBLOBs()) {
         if (r.generateRecordWithBLOBsClass()) {
             record = cls.names.blob;
             topLevelClass.addImportedType(cls.imports.blob);
         }
         mapperMethod = selective ? "updateByExampleSelectiveWithBLOBs" /* not supported */ : "updateByExampleWithBLOBs";
         topLevelClass.addMethod(method(
             PUBLIC, INT, withBLOBs, _(sqlSession, "sql"), _(new FullyQualifiedJavaType(record), "record"), __(
                 "return sql.getMapper(" + cls.names.mapper + ".class)."+mapperMethod+"(record, this);"
         )));
         topLevelClass.addMethod(method(
             PUBLIC, INT, withBLOBs, _(cls.types.mapper, "mapper"), _(new FullyQualifiedJavaType(record), "record"), __(
                 "return mapper."+mapperMethod+"(record, this);"
         )));
     }
 }
 private void addGetRecordMethods(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, MyBatisClasses cls, String base, String withBLOBs, String expression) {
     String returnType = cls.names.base;
     topLevelClass.addImportedType(cls.imports.base);
     String listType = "java.util.List<"+returnType+">";
     if (!base.startsWith(SKIP) && introspectedTable.getRules().generateSelectByExampleWithoutBLOBs()) {
         topLevelClass.addMethod(method(
             PUBLIC, new FullyQualifiedJavaType(returnType), base, _(sqlSession, "sql"), __(
                 listType + " list = sql.getMapper(" + cls.names.mapper + ".class).selectByExample(this);",
                 "return "+expression.replace("@result", returnType)+";"
         )));
         topLevelClass.addMethod(method(
             PUBLIC, new FullyQualifiedJavaType(returnType), base, _(cls.types.mapper, "mapper"), __(
                 listType + " list = mapper.selectByExample(this);",
                 "return "+expression.replace("@result", returnType)+";"
         )));
     }
     if (introspectedTable.hasBLOBColumns() && !withBLOBs.startsWith(SKIP) && introspectedTable.getRules().generateSelectByExampleWithBLOBs()) {
         if (cls.exists.blob) {
             returnType = cls.names.blob;
             topLevelClass.addImportedType(cls.imports.blob);
         }
         listType = "java.util.List<"+returnType+">";
         topLevelClass.addMethod(method(
             PUBLIC, new FullyQualifiedJavaType(returnType), withBLOBs, _(sqlSession, "sql"), __(
                 listType + " list = sql.getMapper(" + cls.names.mapper + ".class).selectByExampleWithBLOBs(this);",
                 "return "+expression.replace("@result", returnType)+";"
         )));
         topLevelClass.addMethod(method(
             PUBLIC, new FullyQualifiedJavaType(returnType), withBLOBs, _(cls.types.mapper, "mapper"), __(
                 listType + " list = mapper.selectByExampleWithBLOBs(this);",
                 "return "+expression.replace("@result", returnType)+";"
         )));
     }
 }
 private void addListMethods(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, MyBatisClasses cls) {
     String returnType = "java.util.List<"+cls.names.base+">";
     topLevelClass.addImportedType(cls.imports.base);
     if (!listMethod.startsWith(SKIP) && introspectedTable.getRules().generateSelectByExampleWithoutBLOBs()) {
         topLevelClass.addMethod(method(
             PUBLIC, new FullyQualifiedJavaType(returnType), listMethod, _(sqlSession, "sql"), __(
                 "return sql.getMapper(" + cls.names.mapper + ".class).selectByExample(this);"
         )));
         topLevelClass.addMethod(method(
             PUBLIC, new FullyQualifiedJavaType(returnType), listMethod, _(cls.types.mapper, "mapper"), __(
                 "return mapper.selectByExample(this);"
         )));
     }
     if (introspectedTable.hasBLOBColumns() && !listWithBLOBsMethod.startsWith(SKIP) && introspectedTable.getRules().generateSelectByExampleWithBLOBs()) {
         if (cls.exists.blob) {
             returnType = "java.util.List<" + cls.names.blob + ">";
             topLevelClass.addImportedType(cls.imports.blob);
         }
         topLevelClass.addMethod(method(
             PUBLIC, new FullyQualifiedJavaType(returnType), listWithBLOBsMethod, _(sqlSession, "sql"), __(
                 "return sql.getMapper("+cls.names.mapper+".class).selectByExampleWithBLOBs(this);"
         )));
         topLevelClass.addMethod(method(
             PUBLIC, new FullyQualifiedJavaType(returnType), listWithBLOBsMethod, _(cls.types.mapper, "mapper"), __(
                 "return mapper.selectByExampleWithBLOBs(this);"
         )));
     }
 }
 @Override
 public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
     if (topLevelClass != null)
         return false;
     if (exampleClass != null) {
         MyBatisClasses cls = MyBatisClasses.calculate(exampleClass, introspectedTable);
         int newMethodsStart = exampleClass.getMethods().size();
         addUserDefinedMethods(exampleClass, interfaze, introspectedTable, cls);
         addCriteriaMethods(exampleClass, newMethodsStart);
         exampleClass = null;
         mapperClass = null;
     } else {
         mapperClass = interfaze;
     }
     return true;
 }