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+");" ))); } }
/** 导入logger */ private void addLogger(TopLevelClass topLevelClass) { Field field = new Field(); field.setFinal(true); field.setInitializationString( "LoggerFactory.getLogger(" + topLevelClass.getType().getShortName() + ".class)"); // 设置值 field.setName("logger"); // 设置变量名 field.setStatic(true); field.setType(new FullyQualifiedJavaType("Logger")); // 类型 field.setVisibility(JavaVisibility.PRIVATE); topLevelClass.addField(field); }
protected void addDefaultNoArgConstructor( TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { Method defaultNoArgConstructor = new Method(); defaultNoArgConstructor.setVisibility(JavaVisibility.PUBLIC); defaultNoArgConstructor.setName(topLevelClass.getType().getShortName()); defaultNoArgConstructor.setConstructor(true); defaultNoArgConstructor.addBodyLine("//Needed For JAXB serialization."); context .getCommentGenerator() .addGeneralMethodComment(defaultNoArgConstructor, introspectedTable); topLevelClass.addMethod(defaultNoArgConstructor); }
private void addParameterizedConstructor(TopLevelClass topLevelClass) { Method method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setConstructor(true); method.setName(topLevelClass.getType().getShortName()); context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable); for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) { method.addParameter( new Parameter( introspectedColumn.getFullyQualifiedJavaType(), introspectedColumn.getJavaProperty())); } boolean comma = false; StringBuilder sb = new StringBuilder(); sb.append("super("); // $NON-NLS-1$ for (IntrospectedColumn introspectedColumn : introspectedTable.getNonBLOBColumns()) { if (comma) { sb.append(", "); // $NON-NLS-1$ } else { comma = true; } sb.append(introspectedColumn.getJavaProperty()); } sb.append(");"); // $NON-NLS-1$ method.addBodyLine(sb.toString()); for (IntrospectedColumn introspectedColumn : introspectedTable.getBLOBColumns()) { sb.setLength(0); sb.append("this."); // $NON-NLS-1$ sb.append(introspectedColumn.getJavaProperty()); sb.append(" = "); // $NON-NLS-1$ sb.append(introspectedColumn.getJavaProperty()); sb.append(';'); method.addBodyLine(sb.toString()); } topLevelClass.addMethod(method); }
protected void addDefaultAnnotationsToClass(TopLevelClass topLevelClass) { String thisClassName = topLevelClass.getType().getShortName(); // Start of code to change the first letter of the Java Class name to lower case in the Xml Root // Element StringBuilder sb = new StringBuilder(thisClassName); sb.setCharAt(0, Character.toLowerCase(sb.charAt(0))); sb.insert(0, "@XmlRootElement(name=\""); sb.append("\")"); // End of code to change the first letter of the Java Class name to lower case in the Xml Root // Element // Add default JAXB imports addImportsForAnnotations(topLevelClass, "XmlAccessorType", "XmlAccessType", "XmlRootElement"); // Add default JAXB annotations topLevelClass.addAnnotation( getXmlAccessTypeConstant()); // Returns either: @XmlAccessorType(XmlAccessType.FIELD) or // @XmlAccessorType(XmlAccessType.PROPERTY) topLevelClass.addAnnotation(sb.toString()); }