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; }
@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; }