Search in sources :

Example 26 with TopLevelClass

use of org.mybatis.generator.api.dom.java.TopLevelClass in project generator by mybatis.

the class SupersGenerator method getSuperClass.

private TopLevelClass getSuperClass() {
    FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(BASE_PACKAGE + ".sub.SuperClass");
    TopLevelClass tlc = new TopLevelClass(fqjt);
    tlc.setVisibility(JavaVisibility.PUBLIC);
    return tlc;
}
Also used : FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) TopLevelClass(org.mybatis.generator.api.dom.java.TopLevelClass)

Example 27 with TopLevelClass

use of org.mybatis.generator.api.dom.java.TopLevelClass in project littlefisher-system by littlefishercoder.

the class SortImportsAndFieldsJavaFormatter method getClassFormattedContent.

/**
 * Description: 类format
 */
private String getClassFormattedContent(CompilationUnit compilationUnit) {
    StringBuilder sb = new StringBuilder();
    TopLevelClass topLevelClass = (TopLevelClass) compilationUnit;
    topLevelClass.getFileCommentLines().forEach(fileCommentLine -> {
        sb.append(fileCommentLine);
        newLine(sb);
    });
    formatPackage(sb, topLevelClass.getType().getPackageName());
    formatStaticImports(sb, topLevelClass.getStaticImports());
    if (topLevelClass.getStaticImports().size() > 0) {
        newLine(sb);
    }
    Set<String> importStrings = calculateImports(topLevelClass.getImportedTypes());
    sortImportTypes(sb, importStrings);
    if (importStrings.size() > 0) {
        newLine(sb);
    }
    sb.append(topLevelClass.getFormattedContent(0, compilationUnit));
    return sb.toString();
}
Also used : TopLevelClass(org.mybatis.generator.api.dom.java.TopLevelClass)

Example 28 with TopLevelClass

use of org.mybatis.generator.api.dom.java.TopLevelClass in project littlefisher-system by littlefishercoder.

the class SortImportsAndFieldsJavaFormatter method formatFields.

/**
 * 对model中的field顺序进行重构
 * 把serialVersionUID放在首位
 */
private void formatFields(CompilationUnit compilationUnit) {
    String serializableFieldName = "serialVersionUID";
    TopLevelClass topLevelClass = (TopLevelClass) compilationUnit;
    Ordering<Field> ordering = new Ordering<Field>() {

        @Override
        public int compare(Field left, Field right) {
            if (serializableFieldName.equals(left.getName())) {
                return -1;
            } else if (serializableFieldName.equals(right.getName())) {
                return 1;
            } else {
                return 0;
            }
        }
    };
    List<Field> fieldList = ordering.sortedCopy(topLevelClass.getFields());
    topLevelClass.getFields().clear();
    topLevelClass.getFields().addAll(fieldList);
}
Also used : Field(org.mybatis.generator.api.dom.java.Field) TopLevelClass(org.mybatis.generator.api.dom.java.TopLevelClass) Ordering(com.google.common.collect.Ordering)

Example 29 with TopLevelClass

use of org.mybatis.generator.api.dom.java.TopLevelClass in project generator by mybatis.

the class DAOGenerator method getTopLevelClassShell.

protected TopLevelClass getTopLevelClassShell() {
    FullyQualifiedJavaType interfaceType = new FullyQualifiedJavaType(introspectedTable.getDAOInterfaceType());
    FullyQualifiedJavaType implementationType = new FullyQualifiedJavaType(introspectedTable.getDAOImplementationType());
    CommentGenerator commentGenerator = context.getCommentGenerator();
    TopLevelClass answer = new TopLevelClass(implementationType);
    answer.setVisibility(JavaVisibility.PUBLIC);
    answer.setSuperClass(daoTemplate.getSuperClass());
    answer.addImportedType(daoTemplate.getSuperClass());
    answer.addSuperInterface(interfaceType);
    answer.addImportedType(interfaceType);
    for (FullyQualifiedJavaType fqjt : daoTemplate.getImplementationImports()) {
        answer.addImportedType(fqjt);
    }
    commentGenerator.addJavaFileComment(answer);
    // add constructor from the template
    answer.addMethod(daoTemplate.getConstructorClone(commentGenerator, implementationType, introspectedTable));
    // add any fields from the template
    for (Field field : daoTemplate.getFieldClones(commentGenerator, introspectedTable)) {
        answer.addField(field);
    }
    // add any methods from the template
    for (Method method : daoTemplate.getMethodClones(commentGenerator, introspectedTable)) {
        answer.addMethod(method);
    }
    return answer;
}
Also used : Field(org.mybatis.generator.api.dom.java.Field) CommentGenerator(org.mybatis.generator.api.CommentGenerator) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) TopLevelClass(org.mybatis.generator.api.dom.java.TopLevelClass) Method(org.mybatis.generator.api.dom.java.Method)

Example 30 with TopLevelClass

use of org.mybatis.generator.api.dom.java.TopLevelClass in project generator by mybatis.

the class PrimaryKeyGenerator method getCompilationUnits.

@Override
public List<CompilationUnit> getCompilationUnits() {
    FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
    progressCallback.startTask(getString("Progress.7", //$NON-NLS-1$
    table.toString()));
    Plugin plugins = context.getPlugins();
    CommentGenerator commentGenerator = context.getCommentGenerator();
    TopLevelClass topLevelClass = new TopLevelClass(introspectedTable.getPrimaryKeyType());
    topLevelClass.setVisibility(JavaVisibility.PUBLIC);
    commentGenerator.addJavaFileComment(topLevelClass);
    String rootClass = getRootClass();
    if (rootClass != null) {
        topLevelClass.setSuperClass(new FullyQualifiedJavaType(rootClass));
        topLevelClass.addImportedType(topLevelClass.getSuperClass());
    }
    for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
        if (RootClassInfo.getInstance(rootClass, warnings).containsProperty(introspectedColumn)) {
            continue;
        }
        Field field = getJavaBeansField(introspectedColumn, context, introspectedTable);
        if (plugins.modelFieldGenerated(field, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.PRIMARY_KEY)) {
            topLevelClass.addField(field);
            topLevelClass.addImportedType(field.getType());
        }
        Method method = getJavaBeansGetter(introspectedColumn, context, introspectedTable);
        if (plugins.modelGetterMethodGenerated(method, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.PRIMARY_KEY)) {
            topLevelClass.addMethod(method);
        }
        method = getJavaBeansSetter(introspectedColumn, context, introspectedTable);
        if (plugins.modelSetterMethodGenerated(method, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.PRIMARY_KEY)) {
            topLevelClass.addMethod(method);
        }
    }
    List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
    if (context.getPlugins().modelPrimaryKeyClassGenerated(topLevelClass, introspectedTable)) {
        answer.add(topLevelClass);
    }
    return answer;
}
Also used : CompilationUnit(org.mybatis.generator.api.dom.java.CompilationUnit) Field(org.mybatis.generator.api.dom.java.Field) JavaBeansUtil.getJavaBeansField(org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansField) IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) CommentGenerator(org.mybatis.generator.api.CommentGenerator) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) FullyQualifiedTable(org.mybatis.generator.api.FullyQualifiedTable) TopLevelClass(org.mybatis.generator.api.dom.java.TopLevelClass) ArrayList(java.util.ArrayList) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) Method(org.mybatis.generator.api.dom.java.Method) Plugin(org.mybatis.generator.api.Plugin)

Aggregations

TopLevelClass (org.mybatis.generator.api.dom.java.TopLevelClass)31 FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)22 Method (org.mybatis.generator.api.dom.java.Method)19 ArrayList (java.util.ArrayList)17 Field (org.mybatis.generator.api.dom.java.Field)17 CompilationUnit (org.mybatis.generator.api.dom.java.CompilationUnit)15 CommentGenerator (org.mybatis.generator.api.CommentGenerator)12 FullyQualifiedTable (org.mybatis.generator.api.FullyQualifiedTable)11 IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)9 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)9 Plugin (org.mybatis.generator.api.Plugin)8 JavaBeansUtil.getJavaBeansField (org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansField)7 Parameter (org.mybatis.generator.api.dom.java.Parameter)6 Interface (org.mybatis.generator.api.dom.java.Interface)3 DefaultJavaFormatter (org.mybatis.generator.api.dom.DefaultJavaFormatter)2 Ordering (com.google.common.collect.Ordering)1 GeneratedJavaFile (org.mybatis.generator.api.GeneratedJavaFile)1 InnerClass (org.mybatis.generator.api.dom.java.InnerClass)1 Rules (org.mybatis.generator.internal.rules.Rules)1