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

net.sourceforge.jweb.mybatis.generator.plugins.CriteriaBuilderPlugin Maven / Gradle / Ivy

There is a newer version: 1.0.13
Show newest version
package net.sourceforge.jweb.mybatis.generator.plugins;
/*
 * Copyright 2002-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;

import java.util.List;

/**
 * Adds "builder-style" to the generated XXXExample classes. Thus inner XXXExample.Criteria class will become an
 * additional "example()" method the returns the example instance itself. 
Example configuration:
*
 * <generatorConfiguration>
 *  <context ...>
 *
 *      <plugin type="net.sourceforge.jweb.mybatis.generator.plugins.CriteriaBuilderPlugin"/>
 *      ...
 *
 *  </context>
 * </generatorConfiguration>
 * 
* */ public class CriteriaBuilderPlugin extends PluginAdapter { /** * {@inheritDoc} */ public boolean validate(List warnings) { return true; } /** * {@inheritDoc} */ @Override public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { List innerClasses = topLevelClass.getInnerClasses(); for (InnerClass innerClass : innerClasses) { if ("Criteria".equals(innerClass.getType().getShortName())) { addFactoryMethodToCriteria(topLevelClass, innerClass, introspectedTable); } } List methods = topLevelClass.getMethods(); for (Method method : methods) { if (!"createCriteriaInternal".equals(method.getName())) continue; method.getBodyLines().set(0, "Criteria criteria = new Criteria(this);"); } return true; } private void addFactoryMethodToCriteria(TopLevelClass topLevelClass, InnerClass innerClass, IntrospectedTable introspectedTable) { Field f = new Field("example", topLevelClass.getType()); f.setVisibility(JavaVisibility.PRIVATE); innerClass.addField(f); // overwrite constructor List methods = innerClass.getMethods(); for (Method method : methods) { if (method.isConstructor()) { method.addParameter(new Parameter(topLevelClass.getType(), "example")); method.addBodyLine("this.example = example;"); } } // add factory method "example" Method method = new Method("example"); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(topLevelClass.getType()); method.addBodyLine("return this.example;"); context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable); innerClass.addMethod(method); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy