热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Mybatis生成器插件扩展,生成OR操作

Mybatis生成器插件扩展,生成OR操作ManExampleexamplenewManExample();ManExample.Criteriaandexample.create

Mybatis生成器插件扩展,生成OR操作

ManExample example = new ManExample();
ManExample.Criteria and = example.createCriteria();
and.andNameIsEmpty().andNameFindInSet("a").andNameFindInSetIn(Arrays.asList("1", "2", "3"));
ManExample.OrCriteria andOr = and.openOrCriteria();
andOr.andIdEqualTo(1).andNameFindInSetIn(Arrays.asList("5", "6", "7"));
ManExample.AndCriteria andOrAnd = andOr.openAndCriteria();
andOrAnd.andIdEqualTo(2).andIdGreaterThan(3);

 


一、生成OR原理

(1)添加OrCriterion和AndCriterion,用来包装Criteria

public abstract static class OpCriterion extends Criterion {
protected E criteria;
public OpCriterion() {
super(null);
}
public void setCriteria(E criteria) {
this.criteria = criteria;
}
public E getCriteria() {
return criteria;
}
}
public static class OrCriterion extends OpCriterion {
}
public static class AndCriterion extends OpCriterion {
}

(2)GeneratedCriteria类改为泛型

public abstract static class GeneratedCriteria {
}

(3)添加OrCriteria和AndCriteria,用来记录操作链

public abstract static class AbstractCriteria extends GeneratedCriteria {
protected GeneratedCriteria root;
protected Criterion first;
public GeneratedCriteria getRoot() {
return this.root;
}
public Criterion getFirst() {
return this.first;
}
public AbstractCriteria(GeneratedCriteria root, Criterion criterion) {
this.root = root;
this.first = criterion;
}
protected void addRoot(int currentIndex) {
Criterion criterion = this.first;
if (currentIndex > 0) {
criterion = criteria.get(currentIndex - 1);
}
int index = root.criteria.indexOf(criterion);
root.criteria.add(index + 1, criteria.get(currentIndex));

if(root instanceof AbstractCriteria){
((AbstractCriteria)root).addRoot(index + 1);
}
}
@Override
protected void addCriterion(String condition) {
super.addCriterion(condition);
addRoot(criteria.size() - 1);
}
@Override
protected void addCriterion(String condition, Object value, String property) {
super.addCriterion(condition, value, property);
addRoot(criteria.size() - 1);
}
@Override
protected void addCriterion(String condition, Object value1, Object value2, String property) {
super.addCriterion(condition, value1, value2, property);
addRoot(criteria.size() - 1);
}
@Override
public boolean isValid() {
for (Criterion criterion : criteria) {
if(!(criterion instanceof OpCriterion)){
return true;
}
}
return false;
}
}
public static class OrCriteria extends AbstractCriteria {
public OrCriteria(GeneratedCriteria root, Criterion criterion) {
super(root, criterion);
}
public AndCriteria openAndCriteria() {
AndCriterion andCriterion = new AndCriterion();
this.criteria.add(andCriterion);
this.addRoot(this.criteria.size() - 1);
AndCriteria andCriteria = new AndCriteria(this, andCriterion);
andCriterion.setCriteria(andCriteria);
return andCriteria;
}
}
public static class AndCriteria extends AbstractCriteria {
public AndCriteria(GeneratedCriteria root, Criterion criterion) {
super(root, criterion);
}
public OrCriteria openOrCriteria() {
OrCriterion orCriterion = new OrCriterion();
this.criteria.add(orCriterion);
this.addRoot(this.criteria.size() - 1);
OrCriteria orCriteria = new OrCriteria(this, orCriterion);
orCriterion.setCriteria(orCriteria);
return orCriteria;
}
}

(4)Criteria加泛型和生成入口or方法

public static class Criteria extends GeneratedCriteria {
public OrCriteria openOrCriteria() {
OrCriterion orCriterion = new OrCriterion();
this.criteria.add(orCriterion);
OrCriteria orCriteria = new OrCriteria(this, orCriterion);
orCriterion.setCriteria(orCriteria);
return orCriteria;
}
}

(5)改Example.applyWhere方法

java.util.Stack stack = new java.util.Stack();
for (int j = 0; j <= criterions.size(); j++) {
if (j == criterions.size()) {
for (Criterion c : stack) {
sb.append(")");
}
stack.clear();
continue;
}
Criterion criterion = criterions.get(j);
if (criterion instanceof ManExample.OpCriterion) {
ManExample.OpCriterion opCriterion = (ManExample.OpCriterion) criterion;
if (opCriterion.getCriteria().isValid()) {
if(!stack.isEmpty()){
Criterion sCriterion = stack.peek();
ManExample.OpCriterion sOpCriterion = (ManExample.OpCriterion) sCriterion;
ManExample.AbstractCriteria abstractCriteria = sOpCriterion.getCriteria();
List criteriOnList= abstractCriteria.getAllCriteria();
int index = criterionList.indexOf(criterion);
if (index <0) {
stack.pop();
sb.append(")");
j--;
continue;
}
}
stack.add(criterion);
ManExample.GeneratedCriteria root = opCriterion.getCriteria().getRoot();
Criterion first = opCriterion.getCriteria().getFirst();
List rootCriterion = root.getAllCriteria();
int firstIndex = rootCriterion.indexOf(first);
if (firstIndex > 0) {
for (int z = firstIndex - 1; z >= 0; z--) {
Criterion prev = rootCriterion.get(z);
if (prev instanceof ManExample.OpCriterion) {
if (((ManExample.OpCriterion) prev).getCriteria().isValid()) {
break;
}
} else {
if(root instanceof ManExample.OrCriteria) {
sb.append(" or ");
}else {
sb.append(" and ");
}
break;
}
}
}
sb.append("(");
}
continue;
}

if (j == 0) {
//do nothing
} else if (stack.isEmpty()) {
sb.append(" and ");
} else if (stack.peek() instanceof ManExample.OpCriterion) {
ManExample.OpCriterion opCriterion = (ManExample.OpCriterion) stack.peek();
ManExample.AbstractCriteria abstractCriteria = opCriterion.getCriteria();
List criteriOnList= abstractCriteria.getAllCriteria();
int index = criterionList.indexOf(criterion);
if (index == 0) {
//do nothing
} else if (index > 0) {
if (opCriterion instanceof ManExample.AndCriterion) {
sb.append(" and ");
} else {
sb.append(" or ");
}
} else {
stack.pop();
sb.append(")");
j--;
continue;
}
}

(6) xml更改,加XmlCriteria类和转换方法,更改xml逻辑

public static class Criteria extends GeneratedCriteria {
public List getXmlCriterion() {
List list = new java.util.LinkedList<>();
List criteriOns= criteria;
java.util.Stack stack = new java.util.Stack();
for (int j = 0; j <= criterions.size(); j++) {
if (j == criterions.size()) {
for (Criterion c : stack) {
list.add(new XmlCriterion(c, false, false, false, true));
}
stack.clear();
continue;
}

Criterion criterion = criterions.get(j);
if (criterion instanceof ManExample.OpCriterion) {
ManExample.OpCriterion opCriterion = (ManExample.OpCriterion) criterion;
if (opCriterion.getCriteria().isValid()) {

if (!stack.isEmpty()) {
Criterion sCriterion = stack.peek();
ManExample.OpCriterion sOpCriterion = (ManExample.OpCriterion) sCriterion;
ManExample.AbstractCriteria abstractCriteria = sOpCriterion.getCriteria();
List criteriOnList= abstractCriteria.getAllCriteria();
int index = criterionList.indexOf(criterion);
if (index <0) {
stack.pop();
list.add(new XmlCriterion(criterion, false, false, false, true));
j--;
continue;
}
}

stack.add(criterion);
boolean isFirst = true;
boolean isAnd = false;
ManExample.GeneratedCriteria root = opCriterion.getCriteria().getRoot();
Criterion first = opCriterion.getCriteria().getFirst();
List rootCriterion = root.getAllCriteria();
int firstIndex = rootCriterion.indexOf(first);
if (firstIndex > 0) {
for (int z = firstIndex - 1; z >= 0; z--) {
Criterion prev = rootCriterion.get(z);
if (prev instanceof ManExample.OpCriterion) {
if (((ManExample.OpCriterion) prev).getCriteria().isValid()) {
break;
}
} else {
isFirst = false;
if (root instanceof ManExample.OrCriteria) {
isAnd = false;
} else {
isAnd = true;
}
break;
}
}
}
list.add(new XmlCriterion(criterion, !isAnd, isFirst, true, false));
}
continue;
}

boolean isFirst = false;
boolean isAnd = false;
if (j == 0) {
isFirst = true;
} else if (stack.isEmpty()) {
isAnd = true;
} else if (stack.peek() instanceof ManExample.OpCriterion) {
ManExample.OpCriterion opCriterion = (ManExample.OpCriterion) stack.peek();
ManExample.AbstractCriteria abstractCriteria = opCriterion.getCriteria();
List criteriOnList= abstractCriteria.getAllCriteria();
int index = criterionList.indexOf(criterion);
if (index == 0) {
isFirst = true;
} else if (index > 0) {
if (opCriterion instanceof ManExample.AndCriterion) {
isAnd = true;
} else {
isAnd = false;
}
} else {
stack.pop();
list.add(new XmlCriterion(criterion,false, false, false, true));
j--;
continue;
}
}
list.add(new XmlCriterion(criterion, !isAnd, isFirst, false, false));
}

return list;
}
}

public static class XmlCriterion {
protected Criterion criterion;
protected boolean or;
protected boolean first;
protected boolean start;
protected boolean end;
public XmlCriterion(Criterion criterion, boolean or, boolean first, boolean start, boolean end) {
this.criterion = criterion;
this.or = or;
this.first = first;
this.start = start;
this.end = end;
}
public Criterion getCriterion() {
return this.criterion;
}
public List getCriterions() {
List list = new ArrayList<>();
if (this.criterion != null) {
list.add(this.criterion);
}
return list;
}
public boolean isOr() {
return this.or;
}
public boolean isAnd() {
return !this.isOr();
}
public boolean isFirst() {
return this.first;
}
public boolean isStart() {
return this.start;
}
public boolean isEnd() {
return this.end;
}
}






or


and


(


)




or

1=1


1=1


 


二、插件代码

package com.mk.mybatisgenerator.plugins;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Element;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import java.util.*;
public class OrPlugin extends PluginAdapter {
public OrPlugin() {
super();
}
@Override
public boolean validate(List warnings) {
return true;
}
//
//
//
//
//
// or
//

//
// and
//

//

// (
//

//
// )
//

//
//
//
// or
//

// 1=1
//

//
// 1=1
//

//
//
//xml where条件生成
@Override
public boolean sqlMapExampleWhereClauseElementGenerated(XmlElement xmlElement, IntrospectedTable introspectedTable) {
List elementList = new LinkedList<>();
elementList.add(xmlElement);
while (!elementList.isEmpty()) {
Element parent = elementList.remove(0);
if (!(parent instanceof XmlElement)) {
continue;
}
XmlElement xmlParent = (XmlElement) parent;
List elements = xmlParent.getElements();
elementList.addAll(0, elements);
for (int i = 0, len = elements.size(); i Element ele = elements.get(i);
if (!(ele instanceof XmlElement)) {
continue;
}
XmlElement element = (XmlElement) ele;
if (!element.getName().equals("foreach") || element.getAttributes().size() != 2) {
continue;
}
Map attributeMap = new HashMap<>();
for (Attribute attribute : element.getAttributes()) {
attributeMap.put(attribute.getName(), attribute);
}
//找到代码
if (attributeMap.containsKey("collection") && Objects.equals(attributeMap.get("collection").getValue(), "criteria.criteria")
&& attributeMap.containsKey("item") && Objects.equals(attributeMap.get("item").getValue(), "criterion")) {
//开始括号
XmlElement startWhenElement = new XmlElement("when");
{
XmlElement ifElement = new XmlElement("if");
ifElement.addAttribute(new Attribute("test", "!xmlCriterion.isFirst()"));
XmlElement ifOrElement = new XmlElement("if");
ifOrElement.addAttribute(new Attribute("test", "xmlCriterion.isOr()"));
ifOrElement.addElement(new TextElement(" or "));
XmlElement ifAndElement = new XmlElement("if");
ifAndElement.addAttribute(new Attribute("test", "xmlCriterion.isAnd()"));
ifAndElement.addElement(new TextElement(" and "));
ifElement.addElement(ifOrElement);
ifElement.addElement(ifAndElement);
startWhenElement.addAttribute(new Attribute("test", "xmlCriterion.isStart()"));
startWhenElement.addElement(ifElement);
startWhenElement.addElement(new TextElement("("));
}
//结束括号
XmlElement endWhenElement = new XmlElement("when");
endWhenElement.addAttribute(new Attribute("test", "xmlCriterion.isEnd()"));
endWhenElement.addElement(new TextElement(")"));
//otherwise
XmlElement otherwiseElement = new XmlElement("otherwise");
{
XmlElement ifFirstElement = new XmlElement("if");
ifFirstElement.addAttribute(new Attribute("test", "!xmlCriterion.isFirst()"));
ifFirstElement.addElement(new TextElement(" or "));
XmlElement ifOrElement = new XmlElement("if");
ifOrElement.addAttribute(new Attribute("test", "xmlCriterion.isOr()"));
ifOrElement.addElement(ifFirstElement);
ifOrElement.addElement(new TextElement(" 1=1 "));
XmlElement ifAndElement = new XmlElement("if");
ifAndElement.addAttribute(new Attribute("test", "xmlCriterion.isAnd() and xmlCriterion.isFirst()"));
ifAndElement.addElement(new TextElement(" 1=1 "));
otherwiseElement.addElement(ifOrElement);
otherwiseElement.addElement(ifAndElement);
XmlElement foreachElement = new XmlElement("foreach");
foreachElement.addAttribute(new Attribute("collection", "xmlCriterion.criterions"));
foreachElement.addAttribute(new Attribute("item", "criterion"));
for (Element e : element.getElements()) {
foreachElement.addElement(e);
}
otherwiseElement.addElement(foreachElement);
element.getElements().clear();
}
XmlElement orChooseElement = new XmlElement("choose");
orChooseElement.addElement(startWhenElement);
orChooseElement.addElement(endWhenElement);
orChooseElement.addElement(otherwiseElement);
element.getAttributes().clear();
element.addAttribute(new Attribute("collection", "criteria.xmlCriterion"));
element.addAttribute(new Attribute("item", "xmlCriterion"));
element.addElement(orChooseElement);
return true;
}
}
}
throw new RuntimeException(" not exist");
}
//example类生成
@Override
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,
IntrospectedTable introspectedTable) {
InnerClass generatedCriteria = generatedCriteria(topLevelClass);
InnerClass criteria = criteria(topLevelClass);
InnerClass abstractCriteria = addAbstractCriteria(topLevelClass);
InnerClass orCriteria = addOrCriteria(topLevelClass, abstractCriteria);
InnerClass andCriteria = addAndCriteria(topLevelClass, abstractCriteria);
InnerClass opCriterion = addOpCriterion(topLevelClass);
InnerClass orCriterion = addOrCriterion(topLevelClass, opCriterion);
InnerClass andCriterion = addAndCriterion(topLevelClass, opCriterion);
addCriteriaOrMethod(topLevelClass);
//适配生成xml解析方法
String type = introspectedTable.getContext().getJavaClientGeneratorConfiguration().getConfigurationType();
if("XMLMAPPER".equalsIgnoreCase(type)) {
InnerClass xmlCriterion = addXmlCriterion(topLevelClass);
addXmlCriteriaMethod(topLevelClass, xmlCriterion);
}
return true;
}
//添加xml解析Criteria方法
private void addXmlCriteriaMethod(TopLevelClass topLevelClass, InnerClass xmlCriterion) {
InnerClass criteria = null;
// first, find the Criteria inner class
for (InnerClass innerClass : topLevelClass.getInnerClasses()) {
if ("Criteria".equals(innerClass.getType().getShortName())) { //$NON-NLS-1$
criteria = innerClass;
break;
}
}
if (criteria == null) {
throw new RuntimeException("Criteria not exist");
}
String topClassName = topLevelClass.getType().getShortName();
Method getXmlCriterion = new Method();
getXmlCriterion.setVisibility(JavaVisibility.PUBLIC);
getXmlCriterion.setName("getXmlCriterion");
getXmlCriterion.setReturnType(new FullyQualifiedJavaType("List"));
getXmlCriterion.addBodyLine("List list = new java.util.LinkedList<>();");
getXmlCriterion.addBodyLine("List criteriOns= criteria;");
getXmlCriterion.addBodyLine("java.util.Stack stack = new java.util.Stack();");
getXmlCriterion.addBodyLine("for (int j = 0; j <= criterions.size(); j++) {");
getXmlCriterion.addBodyLine("if (j == criterions.size()) {");
getXmlCriterion.addBodyLine("for (Criterion c : stack) {");
getXmlCriterion.addBodyLine("list.add(new XmlCriterion(c, false, false, false, true));");
getXmlCriterion.addBodyLine("}");
getXmlCriterion.addBodyLine("stack.clear();");
getXmlCriterion.addBodyLine("continue;");
getXmlCriterion.addBodyLine("}");
getXmlCriterion.addBodyLine("");
getXmlCriterion.addBodyLine("Criterion criterion = criterions.get(j);");
getXmlCriterion.addBodyLine("if (criterion instanceof " + topClassName + ".OpCriterion) {");
getXmlCriterion.addBodyLine(topClassName + ".OpCriterion opCriterion = (" + topClassName + ".OpCriterion) criterion;");
getXmlCriterion.addBodyLine("if (opCriterion.getCriteria().isValid()) {");
getXmlCriterion.addBodyLine("");
getXmlCriterion.addBodyLine("if (!stack.isEmpty()) {");
getXmlCriterion.addBodyLine("Criterion sCriterion = stack.peek();");
getXmlCriterion.addBodyLine(topClassName + ".OpCriterion sOpCriterion = (" + topClassName + ".OpCriterion) sCriterion;");
getXmlCriterion.addBodyLine(topClassName + ".AbstractCriteria abstractCriteria = sOpCriterion.getCriteria();");
getXmlCriterion.addBodyLine("List criteriOnList= abstractCriteria.getAllCriteria();");
getXmlCriterion.addBodyLine("int index = criterionList.indexOf(criterion);");
getXmlCriterion.addBodyLine("if (index <0) {");
getXmlCriterion.addBodyLine("stack.pop();");
getXmlCriterion.addBodyLine("list.add(new XmlCriterion(criterion, false, false, false, true));");
getXmlCriterion.addBodyLine("j--;");
getXmlCriterion.addBodyLine("continue;");
getXmlCriterion.addBodyLine("}");
getXmlCriterion.addBodyLine("}");
getXmlCriterion.addBodyLine("");
getXmlCriterion.addBodyLine("stack.add(criterion);");
getXmlCriterion.addBodyLine("boolean isFirst = true;");
getXmlCriterion.addBodyLine("boolean isAnd = false;");
getXmlCriterion.addBodyLine(topClassName + ".GeneratedCriteria root = opCriterion.getCriteria().getRoot();");
getXmlCriterion.addBodyLine("Criterion first = opCriterion.getCriteria().getFirst();");
getXmlCriterion.addBodyLine("List rootCriterion = root.getAllCriteria();");
getXmlCriterion.addBodyLine("int firstIndex = rootCriterion.indexOf(first);");
getXmlCriterion.addBodyLine("if (firstIndex > 0) {");
getXmlCriterion.addBodyLine("for (int z = firstIndex - 1; z >= 0; z--) {");
getXmlCriterion.addBodyLine("Criterion prev = rootCriterion.get(z);");
getXmlCriterion.addBodyLine("if (prev instanceof " + topClassName + ".OpCriterion) {");
getXmlCriterion.addBodyLine("if (((" + topClassName + ".OpCriterion) prev).getCriteria().isValid()) {");
getXmlCriterion.addBodyLine("break;");
getXmlCriterion.addBodyLine("}");
getXmlCriterion.addBodyLine("} else {");
getXmlCriterion.addBodyLine("isFirst = false;");
getXmlCriterion.addBodyLine("if (root instanceof " + topClassName + ".OrCriteria) {");
getXmlCriterion.addBodyLine("isAnd = false;");
getXmlCriterion.addBodyLine("} else {");
getXmlCriterion.addBodyLine("isAnd = true;");
getXmlCriterion.addBodyLine("}");
getXmlCriterion.addBodyLine("break;");
getXmlCriterion.addBodyLine("}");
getXmlCriterion.addBodyLine("}");
getXmlCriterion.addBodyLine("}");
getXmlCriterion.addBodyLine("list.add(new XmlCriterion(criterion, !isAnd, isFirst, true, false));");
getXmlCriterion.addBodyLine("}");
getXmlCriterion.addBodyLine("continue;");
getXmlCriterion.addBodyLine("}");
getXmlCriterion.addBodyLine("");
getXmlCriterion.addBodyLine("boolean isFirst = false;");
getXmlCriterion.addBodyLine("boolean isAnd = false;");
getXmlCriterion.addBodyLine("if (j == 0) {");
getXmlCriterion.addBodyLine("isFirst = true;");
getXmlCriterion.addBodyLine("} else if (stack.isEmpty()) {");
getXmlCriterion.addBodyLine("isAnd = true;");
getXmlCriterion.addBodyLine("} else if (stack.peek() instanceof " + topClassName + ".OpCriterion) {");
getXmlCriterion.addBodyLine(topClassName + ".OpCriterion opCriterion = (" + topClassName + ".OpCriterion) stack.peek();");
getXmlCriterion.addBodyLine(topClassName + ".AbstractCriteria abstractCriteria = opCriterion.getCriteria();");
getXmlCriterion.addBodyLine("List criteriOnList= abstractCriteria.getAllCriteria();");
getXmlCriterion.addBodyLine("int index = criterionList.indexOf(criterion);");
getXmlCriterion.addBodyLine("if (index == 0) {");
getXmlCriterion.addBodyLine("isFirst = true;");
getXmlCriterion.addBodyLine("} else if (index > 0) {");
getXmlCriterion.addBodyLine("if (opCriterion instanceof " + topClassName + ".AndCriterion) {");
getXmlCriterion.addBodyLine("isAnd = true;");
getXmlCriterion.addBodyLine("} else {");
getXmlCriterion.addBodyLine("isAnd = false;");
getXmlCriterion.addBodyLine("}");
getXmlCriterion.addBodyLine("} else {");
getXmlCriterion.addBodyLine("stack.pop();");
getXmlCriterion.addBodyLine("list.add(new XmlCriterion(criterion,false, false, false, true));");
getXmlCriterion.addBodyLine("j--;");
getXmlCriterion.addBodyLine("continue;");
getXmlCriterion.addBodyLine("}");
getXmlCriterion.addBodyLine("}");
getXmlCriterion.addBodyLine("list.add(new XmlCriterion(criterion, !isAnd, isFirst, false, false));");
getXmlCriterion.addBodyLine("}");
getXmlCriterion.addBodyLine("");
getXmlCriterion.addBodyLine("return list;");
criteria.addMethod(getXmlCriterion);
}
//添加XmlCriterion类
private InnerClass addXmlCriterion(TopLevelClass topLevelClass) {
InnerClass innerClass = new InnerClass("XmlCriterion");
innerClass.setStatic(true);
innerClass.setVisibility(JavaVisibility.PUBLIC);
Field criterion = new Field();
criterion.setVisibility(JavaVisibility.PROTECTED);
criterion.setType(new FullyQualifiedJavaType("Criterion"));
criterion.setName("criterion");
innerClass.addField(criterion);
Field or = new Field();
or.setVisibility(JavaVisibility.PROTECTED);
or.setType(new FullyQualifiedJavaType("boolean"));
or.setName("or");
innerClass.addField(or);
Field first = new Field();
first.setVisibility(JavaVisibility.PROTECTED);
first.setType(new FullyQualifiedJavaType("boolean"));
first.setName("first");
innerClass.addField(first);
Field start = new Field();
start.setVisibility(JavaVisibility.PROTECTED);
start.setType(new FullyQualifiedJavaType("boolean"));
start.setName("start");
innerClass.addField(start);
Field end = new Field();
end.setVisibility(JavaVisibility.PROTECTED);
end.setType(new FullyQualifiedJavaType("boolean"));
end.setName("end");
innerClass.addField(end);
Method cOnstructor= new Method();
constructor.setVisibility(JavaVisibility.PUBLIC);
constructor.setConstructor(true);
constructor.setName("XmlCriterion");
constructor.addParameter(new Parameter(new FullyQualifiedJavaType("Criterion"), "criterion"));
constructor.addParameter(new Parameter(new FullyQualifiedJavaType("boolean"), "or"));
constructor.addParameter(new Parameter(new FullyQualifiedJavaType("boolean"), "first"));
constructor.addParameter(new Parameter(new FullyQualifiedJavaType("boolean"), "start"));
constructor.addParameter(new Parameter(new FullyQualifiedJavaType("boolean"), "end"));
constructor.addBodyLine("this.criterion = criterion;");
constructor.addBodyLine("this.or = or;");
constructor.addBodyLine("this.first = first;");
constructor.addBodyLine("this.start = start;");
constructor.addBodyLine("this.end = end;");
innerClass.addMethod(constructor);
Method getCriterion = new Method();
getCriterion.setVisibility(JavaVisibility.PUBLIC);
getCriterion.setName("getCriterion");
getCriterion.setReturnType(new FullyQualifiedJavaType("Criterion"));
getCriterion.addBodyLine("return this.criterion;");
innerClass.addMethod(getCriterion);
Method getCriteriOns= new Method();
getCriterions.setVisibility(JavaVisibility.PUBLIC);
getCriterions.setName("getCriterions");
getCriterions.setReturnType(new FullyQualifiedJavaType("List"));
getCriterions.addBodyLine("List list = new ArrayList<>();");
getCriterions.addBodyLine("if (this.criterion != null) {");
getCriterions.addBodyLine("list.add(this.criterion);");
getCriterions.addBodyLine("}");
getCriterions.addBodyLine("return list;");
innerClass.addMethod(getCriterions);
Method isOr = new Method();
isOr.setVisibility(JavaVisibility.PUBLIC);
isOr.setName("isOr");
isOr.setReturnType(new FullyQualifiedJavaType("boolean"));
isOr.addBodyLine("return this.or;");
innerClass.addMethod(isOr);
Method isAnd = new Method();
isAnd.setVisibility(JavaVisibility.PUBLIC);
isAnd.setName("isAnd");
isAnd.setReturnType(new FullyQualifiedJavaType("boolean"));
isAnd.addBodyLine("return !this.isOr();");
innerClass.addMethod(isAnd);
Method isFirst = new Method();
isFirst.setVisibility(JavaVisibility.PUBLIC);
isFirst.setName("isFirst");
isFirst.setReturnType(new FullyQualifiedJavaType("boolean"));
isFirst.addBodyLine("return this.first;");
innerClass.addMethod(isFirst);
Method isStart = new Method();
isStart.setVisibility(JavaVisibility.PUBLIC);
isStart.setName("isStart");
isStart.setReturnType(new FullyQualifiedJavaType("boolean"));
isStart.addBodyLine("return this.start;");
innerClass.addMethod(isStart);
Method isEnd = new Method();
isEnd.setVisibility(JavaVisibility.PUBLIC);
isEnd.setName("isEnd");
isEnd.setReturnType(new FullyQualifiedJavaType("boolean"));
isEnd.addBodyLine("return this.end;");
innerClass.addMethod(isEnd);
topLevelClass.addInnerClass(innerClass);
return innerClass;
}
//更改GeneratedCriteria为泛型类
private InnerClass generatedCriteria(TopLevelClass topLevelClass) {
InnerClass innerClass1 = null;
// first, find the Criteria inner class
for (InnerClass innerClass : topLevelClass.getInnerClasses()) {
if ("GeneratedCriteria".equals(innerClass.getType().getShortName())) { //$NON-NLS-1$
innerClass1 = innerClass;
break;
}
}
if (innerClass1 == null) {
throw new RuntimeException("GeneratedCriteria not exist");
}
innerClass1.setVisibility(JavaVisibility.PUBLIC);
innerClass1.addTypeParameter(new TypeParameter("Criteria"));
return innerClass1;
}
//更改Criteria为继承GeneratedCriteria泛型类
private InnerClass criteria(TopLevelClass topLevelClass) {
InnerClass criteria = null;
// first, find the Criteria inner class
for (InnerClass innerClass : topLevelClass.getInnerClasses()) {
if ("Criteria".equals(innerClass.getType().getShortName())) { //$NON-NLS-1$
criteria = innerClass;
break;
}
}
if (criteria == null) {
throw new RuntimeException("GeneratedCriteria not exist");
}
criteria.setSuperClass("GeneratedCriteria");
return criteria;
}
//加OpCriterion抽象类
private InnerClass addOpCriterion(TopLevelClass topLevelClass) {
InnerClass innerClass = new InnerClass("OpCriterion");
innerClass.setSuperClass("Criterion");
innerClass.setStatic(true);
innerClass.setAbstract(true);
innerClass.setVisibility(JavaVisibility.PUBLIC);
innerClass.addTypeParameter(new TypeParameter("E extends AbstractCriteria"));
Field criteria = new Field();
criteria.setVisibility(JavaVisibility.PROTECTED);
criteria.setType(new FullyQualifiedJavaType("E"));
criteria.setName("criteria");
innerClass.addField(criteria);
Method cOnstructor= new Method();
constructor.setVisibility(JavaVisibility.PUBLIC);
constructor.setConstructor(true);
constructor.setName("OpCriterion");
constructor.addBodyLine("super(null);");
innerClass.addMethod(constructor);
Method setOrCriteria = new Method();
setOrCriteria.setVisibility(JavaVisibility.PUBLIC);
setOrCriteria.setName("setCriteria");
setOrCriteria.addParameter(new Parameter(new FullyQualifiedJavaType("E"), "criteria"));
setOrCriteria.addBodyLine("this.criteria = criteria;");
innerClass.addMethod(setOrCriteria);
Method getOrCriteria = new Method();
getOrCriteria.setVisibility(JavaVisibility.PUBLIC);
getOrCriteria.setName("getCriteria");
getOrCriteria.setReturnType(new FullyQualifiedJavaType("E"));
getOrCriteria.addBodyLine("return criteria;");
innerClass.addMethod(getOrCriteria);
topLevelClass.addInnerClass(innerClass);
return innerClass;
}
//加AndCriterion类,包装AndCriteria
private InnerClass addAndCriterion(TopLevelClass topLevelClass, InnerClass opCriterion) {
InnerClass innerClass = new InnerClass("AndCriterion");
innerClass.setSuperClass(opCriterion.getType().getShortName() + "");
innerClass.setStatic(true);
innerClass.setVisibility(JavaVisibility.PUBLIC);
topLevelClass.addInnerClass(innerClass);
return innerClass;
}
//加OrCriterion类,包装OrCriteria
private InnerClass addOrCriterion(TopLevelClass topLevelClass, InnerClass opCriterion) {
InnerClass innerClass = new InnerClass("OrCriterion");
innerClass.setSuperClass(opCriterion.getType().getShortName() + "");
innerClass.setStatic(true);
innerClass.setVisibility(JavaVisibility.PUBLIC);
topLevelClass.addInnerClass(innerClass);
return innerClass;
}
//加AndCriteria类,继承AbstractCriteria类
private InnerClass addAndCriteria(TopLevelClass topLevelClass, InnerClass abstractCriteria) {
FullyQualifiedJavaType criteria = new FullyQualifiedJavaType("GeneratedCriteria");
InnerClass innerClass = new InnerClass("AndCriteria");
innerClass.setSuperClass(abstractCriteria.getType().getShortName() + "");
innerClass.setStatic(true);
innerClass.setVisibility(JavaVisibility.PUBLIC);
Method cOnstructor= new Method();
constructor.setVisibility(JavaVisibility.PUBLIC);
constructor.setConstructor(true);
constructor.setName("AndCriteria");
constructor.addParameter(new Parameter(criteria, "root"));
constructor.addParameter(new Parameter(new FullyQualifiedJavaType("Criterion"), "criterion"));
constructor.addBodyLine("super(root, criterion);");
innerClass.addMethod(constructor);
Method or = new Method();
or.setVisibility(JavaVisibility.PUBLIC);
or.setName("openOrCriteria");
or.setReturnType(new FullyQualifiedJavaType("OrCriteria"));
or.addBodyLine("OrCriterion orCriterion = new OrCriterion();");
or.addBodyLine("this.criteria.add(orCriterion);");
or.addBodyLine("this.addRoot(this.criteria.size() - 1);");
or.addBodyLine("OrCriteria orCriteria = new OrCriteria(this, orCriterion);");
or.addBodyLine("orCriterion.setCriteria(orCriteria);");
or.addBodyLine("return orCriteria;");
innerClass.addMethod(or);
topLevelClass.addInnerClass(innerClass);
return innerClass;
}
//加OrCriteria类,继承AbstractCriteria类
private InnerClass addOrCriteria(TopLevelClass topLevelClass, InnerClass abstractCriteria) {
FullyQualifiedJavaType criteria = new FullyQualifiedJavaType("GeneratedCriteria");
InnerClass innerClass = new InnerClass("OrCriteria");
innerClass.setSuperClass(abstractCriteria.getType() + "");
innerClass.setStatic(true);
innerClass.setVisibility(JavaVisibility.PUBLIC);
Method cOnstructor= new Method();
constructor.setVisibility(JavaVisibility.PUBLIC);
constructor.setConstructor(true);
constructor.setName("OrCriteria");
constructor.addParameter(new Parameter(criteria, "root"));
constructor.addParameter(new Parameter(new FullyQualifiedJavaType("Criterion"), "criterion"));
constructor.addBodyLine("super(root, criterion);");
innerClass.addMethod(constructor);
Method and = new Method();
and.setVisibility(JavaVisibility.PUBLIC);
and.setName("openAndCriteria");
and.setReturnType(new FullyQualifiedJavaType("AndCriteria"));
and.addBodyLine("AndCriterion andCriterion = new AndCriterion();");
and.addBodyLine("this.criteria.add(andCriterion);");
and.addBodyLine("this.addRoot(this.criteria.size() - 1);");
and.addBodyLine("AndCriteria andCriteria = new AndCriteria(this, andCriterion);");
and.addBodyLine("andCriterion.setCriteria(andCriteria);");
and.addBodyLine("return andCriteria;");
innerClass.addMethod(and);
topLevelClass.addInnerClass(innerClass);
return innerClass;
}
//加AbstractCriteria类
private InnerClass addAbstractCriteria(TopLevelClass topLevelClass) {
FullyQualifiedJavaType criteria = new FullyQualifiedJavaType("GeneratedCriteria");
InnerClass innerClass = new InnerClass("AbstractCriteria");
innerClass.addTypeParameter(new TypeParameter("E"));
innerClass.setSuperClass("GeneratedCriteria");
innerClass.setAbstract(true);
innerClass.setStatic(true);
innerClass.setVisibility(JavaVisibility.PUBLIC);
Field root = new Field();
root.setVisibility(JavaVisibility.PROTECTED);
root.setType(criteria);
root.setName("root");
innerClass.addField(root);
Method getRoot = new Method();
getRoot.setVisibility(JavaVisibility.PUBLIC);
getRoot.setName("getRoot");
getRoot.setReturnType(criteria);
getRoot.addBodyLine("return this.root;");
innerClass.addMethod(getRoot);
Field first = new Field();
first.setVisibility(JavaVisibility.PROTECTED);
first.setType(new FullyQualifiedJavaType("Criterion"));
first.setName("first");
innerClass.addField(first);
Method getFirst = new Method();
getFirst.setVisibility(JavaVisibility.PUBLIC);
getFirst.setReturnType(new FullyQualifiedJavaType("Criterion"));
getFirst.setName("getFirst");
getFirst.addBodyLine("return this.first;");
innerClass.addMethod(getFirst);
Method cOnstructor= new Method();
constructor.setVisibility(JavaVisibility.PUBLIC);
constructor.setConstructor(true);
constructor.setName("AbstractCriteria");
constructor.addParameter(new Parameter(criteria, "root"));
constructor.addParameter(new Parameter(new FullyQualifiedJavaType("Criterion"), "criterion"));
constructor.addBodyLine("this.root = root;");
constructor.addBodyLine("this.first = criterion;");
innerClass.addMethod(constructor);
Method addRoot = new Method();
addRoot.setVisibility(JavaVisibility.PROTECTED);
addRoot.setName("addRoot");
addRoot.addParameter(new Parameter(FullyQualifiedJavaType.getIntInstance(), "currentIndex"));
addRoot.addBodyLine("Criterion criterion = this.first;");
addRoot.addBodyLine("if (currentIndex > 0) {");
addRoot.addBodyLine("criterion = criteria.get(currentIndex - 1);");
addRoot.addBodyLine("}");
addRoot.addBodyLine("int index = root.criteria.indexOf(criterion);");
addRoot.addBodyLine("root.criteria.add(index + 1, criteria.get(currentIndex));");
addRoot.addBodyLine("");
addRoot.addBodyLine("if(root instanceof AbstractCriteria){");
addRoot.addBodyLine("((AbstractCriteria)root).addRoot(index + 1);");
addRoot.addBodyLine("}");
innerClass.addMethod(addRoot);
Method addCriterion1 = new Method();
addCriterion1.setVisibility(JavaVisibility.PROTECTED);
addCriterion1.addAnnotation("@Override");
addCriterion1.setName("addCriterion");
addCriterion1.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition"));
addCriterion1.addBodyLine("super.addCriterion(condition);");
addCriterion1.addBodyLine("addRoot(criteria.size() - 1);");
innerClass.addMethod(addCriterion1);
Method addCriterion3 = new Method();
addCriterion3.setVisibility(JavaVisibility.PROTECTED);
addCriterion3.addAnnotation("@Override");
addCriterion3.setName("addCriterion");
addCriterion3.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition"));
addCriterion3.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), "value"));
addCriterion3.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "property"));
addCriterion3.addBodyLine("super.addCriterion(condition, value, property);");
addCriterion3.addBodyLine("addRoot(criteria.size() - 1);");
innerClass.addMethod(addCriterion3);
Method addCriterion4 = new Method();
addCriterion4.setVisibility(JavaVisibility.PROTECTED);
addCriterion4.addAnnotation("@Override");
addCriterion4.setName("addCriterion");
addCriterion4.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition"));
addCriterion4.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), "value1"));
addCriterion4.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), "value2"));
addCriterion4.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "property"));
addCriterion4.addBodyLine("super.addCriterion(condition, value1, value2, property);");
addCriterion4.addBodyLine("addRoot(criteria.size() - 1);");
innerClass.addMethod(addCriterion4);
Method isValid = new Method();
isValid.setVisibility(JavaVisibility.PUBLIC);
isValid.addAnnotation("@Override");
isValid.setName("isValid");
isValid.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
isValid.addBodyLine("for (Criterion criterion : criteria) {");
isValid.addBodyLine("if(!(criterion instanceof OpCriterion)){");
isValid.addBodyLine("return true;");
isValid.addBodyLine("}");
isValid.addBodyLine("}");
isValid.addBodyLine("return false;");
innerClass.addMethod(isValid);
topLevelClass.addInnerClass(innerClass);
return innerClass;
}
//给Criteria加or方法
private void addCriteriaOrMethod(TopLevelClass topLevelClass) {
InnerClass criteria = null;
// first, find the Criteria inner class
for (InnerClass innerClass : topLevelClass.getInnerClasses()) {
if ("Criteria".equals(innerClass.getType().getShortName())) { //$NON-NLS-1$
criteria = innerClass;
break;
}
}
if (criteria == null) {
throw new RuntimeException("GeneratedCriteria not exist");
}
Method or = new Method();
or.setVisibility(JavaVisibility.PUBLIC);
or.setName("openOrCriteria");
or.setReturnType(new FullyQualifiedJavaType("OrCriteria"));
or.addBodyLine("OrCriterion orCriterion = new OrCriterion();");
or.addBodyLine("this.criteria.add(orCriterion);");
or.addBodyLine("OrCriteria orCriteria = new OrCriteria(this, orCriterion);");
or.addBodyLine("orCriterion.setCriteria(orCriteria);");
or.addBodyLine("return orCriteria;");
criteria.addMethod(or);
}
// java.util.Stack stack = new java.util.Stack();
// for (int j = 0; j <= criterions.size(); j++) {
// if (j == criterions.size()) {
// for (Criterion c : stack) {
// sb.append(")");
// }
// stack.clear();
// continue;
// }
// Criterion criterion = criterions.get(j);
// if (criterion instanceof ManExample.OpCriterion) {
// ManExample.OpCriterion opCriterion = (ManExample.OpCriterion) criterion;
// if (opCriterion.getCriteria().isValid()) {
// if(!stack.isEmpty()){
// Criterion sCriterion = stack.peek();
// ManExample.OpCriterion sOpCriterion = (ManExample.OpCriterion) sCriterion;
// ManExample.AbstractCriteria abstractCriteria = sOpCriterion.getCriteria();
// List criteriOnList= abstractCriteria.getAllCriteria();
// int index = criterionList.indexOf(criterion);
// if (index <0) {
// stack.pop();
// sb.append(")");
// j--;
// continue;
// }
// }
// stack.add(criterion);
// ManExample.GeneratedCriteria root = opCriterion.getCriteria().getRoot();
// Criterion first = opCriterion.getCriteria().getFirst();
// List rootCriterion = root.getAllCriteria();
// int firstIndex = rootCriterion.indexOf(first);
// if (firstIndex > 0) {
// for (int z = firstIndex - 1; z >= 0; z--) {
// Criterion prev = rootCriterion.get(z);
// if (prev instanceof ManExample.OpCriterion) {
// if (((ManExample.OpCriterion) prev).getCriteria().isValid()) {
// break;
// }
// } else {
// if(root instanceof ManExample.OrCriteria) {
// sb.append(" or ");
// }else {
// sb.append(" and ");
// }
// break;
// }
// }
// }
// sb.append("(");
// }
// continue;
// }
//
// if (j == 0) {
// //do nothing
// } else if (stack.isEmpty()) {
// sb.append(" and ");
// } else if (stack.peek() instanceof ManExample.OpCriterion) {
// ManExample.OpCriterion opCriterion = (ManExample.OpCriterion) stack.peek();
// ManExample.AbstractCriteria abstractCriteria = opCriterion.getCriteria();
// List criteriOnList= abstractCriteria.getAllCriteria();
// int index = criterionList.indexOf(criterion);
// if (index == 0) {
// //do nothing
// } else if (index > 0) {
// if (opCriterion instanceof ManExample.AndCriterion) {
// sb.append(" and ");
// } else {
// sb.append(" or ");
// }
// } else {
// stack.pop();
// sb.append(")");
// j--;
// continue;
// }
// }
/**
* 重写ApplyWhere方法
*
* @param method
* @param topLevelClass
* @param introspectedTable
* @return
*/
@Override
public boolean providerApplyWhereMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
List lines = method.getBodyLines();
boolean hasChange = false;
for (int i = 0, len = lines.size(); i String line = lines.get(i).trim();
//找到boolean firstCriterion = true;行插入模式代码
if (line.matches("boolean\\s*firstCriterion\\s*=\\s*true;\\s*")) {
lines.set(i, "java.util.Stack stack = new java.util.Stack();");
hasChange = true;
break;
}
}
if (!hasChange) {
throw new RuntimeException("boolean firstCriterion = true; not exist");
}
final String topClassName = introspectedTable.getExampleType().substring(introspectedTable.getExampleType().lastIndexOf('.') + 1);
hasChange = false;
for (int i = 0, len = lines.size(); i String line = lines.get(i).trim();
//找到boolean firstCriterion = true;行插入模式代码
if (line.matches("for\\s*\\(\\s*int\\s*j\\s*=\\s*0\\s*;\\s*j\\s*<\\s*criterions\\.size\\(\\)\\s*;\\s*j\\s*\\+\\+\\s*\\)\\s*\\{")) {
lines.set(i, "for (int j = 0; j <= criterions.size(); j++) {");
if (lines.get(i + 1).trim().matches("Criterion\\s*criterion\\s*=\\s*criterions\\.get\\(\\s*j\\s*\\)\\s*;")
&& lines.get(i + 2).trim().matches("if\\s*\\(\\s*firstCriterion\\s*\\)\\s*\\{")
&& lines.get(i + 3).trim().matches("firstCriterion\\s*=\\s*false\\s*;")
&& lines.get(i + 4).trim().matches("}\\s*else\\s*\\{")
&& lines.get(i + 5).trim().matches("sb\\.append\\(\\s*\"\\s+and\\s+\"\\s*\\);")
&& lines.get(i + 6).trim().matches("}")) {
} else {
throw new RuntimeException("if (firstCriterion) { not exist");
}
List handles = new ArrayList<>();
handles.add("if (criterion instanceof " + topClassName + ".OpCriterion) {");
handles.add(topClassName + ".OpCriterion opCriterion = (" + topClassName + ".OpCriterion) criterion;");
handles.add("if (opCriterion.getCriteria().isValid()) {");
handles.add("if(!stack.isEmpty()){");
handles.add("Criterion sCriterion = stack.peek();");
handles.add(topClassName + ".OpCriterion sOpCriterion = (" + topClassName + ".OpCriterion) sCriterion;");
handles.add(topClassName + ".AbstractCriteria abstractCriteria = sOpCriterion.getCriteria();");
handles.add("List criteriOnList= abstractCriteria.getAllCriteria();");
handles.add("int index = criterionList.indexOf(criterion);");
handles.add("if (index <0) {");
handles.add("stack.pop();");
handles.add("sb.append(\")\");");
handles.add("j--;");
handles.add("continue;");
handles.add("}");
handles.add("}");
handles.add("stack.add(criterion);");
handles.add(topClassName + ".GeneratedCriteria root = opCriterion.getCriteria().getRoot();");
handles.add("Criterion first = opCriterion.getCriteria().getFirst();");
handles.add("List rootCriterion = root.getAllCriteria();");
handles.add("int firstIndex = rootCriterion.indexOf(first);");
handles.add("if (firstIndex > 0) {");
handles.add("for (int z = firstIndex - 1; z >= 0; z--) {");
handles.add("Criterion prev = rootCriterion.get(z);");
handles.add("if (prev instanceof " + topClassName + ".OpCriterion) {");
handles.add("if (((" + topClassName + ".OpCriterion) prev).getCriteria().isValid()) {");
handles.add("break;");
handles.add("}");
handles.add("} else {");
handles.add("if(root instanceof " + topClassName + ".OrCriteria) {");
handles.add("sb.append(\" or \");");
handles.add("}else {");
handles.add("sb.append(\" and \");");
handles.add("}");
handles.add("break;");
handles.add("}");
handles.add("}");
handles.add("}");
handles.add("sb.append(\"(\");");
handles.add("}");
handles.add("continue;");
handles.add("}");
handles.add("");
handles.add("if (j == 0) {");
handles.add("//do nothing");
handles.add("} else if (stack.isEmpty()) {");
handles.add("sb.append(\" and \");");
handles.add("} else if (stack.peek() instanceof " + topClassName + ".OpCriterion) {");
handles.add(topClassName + ".OpCriterion opCriterion = (" + topClassName + ".OpCriterion) stack.peek();");
handles.add(topClassName + ".AbstractCriteria abstractCriteria = opCriterion.getCriteria();");
handles.add("List criteriOnList= abstractCriteria.getAllCriteria();");
handles.add("int index = criterionList.indexOf(criterion);");
handles.add("if (index == 0) {");
handles.add("//do nothing");
handles.add("} else if (index > 0) {");
handles.add("if (opCriterion instanceof " + topClassName + ".AndCriterion) {");
handles.add("sb.append(\" and \");");
handles.add("} else {");
handles.add("sb.append(\" or \");");
handles.add("}");
handles.add("} else {");
handles.add("stack.pop();");
handles.add("sb.append(\")\");");
handles.add("j--;");
handles.add("continue;");
handles.add("}");
handles.add("}");
lines.remove(i + 6);
lines.remove(i + 5);
lines.remove(i + 4);
lines.remove(i + 3);
lines.remove(i + 2);
lines.addAll(i + 2, handles);
List posts = new ArrayList<>();
posts.add("if (j == criterions.size()) {");
posts.add("for (Criterion c : stack) {");
posts.add("sb.append(\")\");");
posts.add("}");
posts.add("stack.clear();");
posts.add("continue;");
posts.add("}");
lines.addAll(i + 1, posts);
hasChange = true;
break;
}
}
if (!hasChange) {
throw new RuntimeException("boolean firstCriterion = true; not exist");
}
return true;
}
}

三、配置插件




 



推荐阅读
  • 使用 ListView 浏览安卓系统中的回收站文件 ... [详细]
  • 深入剖析Java中SimpleDateFormat在多线程环境下的潜在风险与解决方案
    深入剖析Java中SimpleDateFormat在多线程环境下的潜在风险与解决方案 ... [详细]
  • 优化后的标题:深入探讨网关安全:将微服务升级为OAuth2资源服务器的最佳实践
    本文深入探讨了如何将微服务升级为OAuth2资源服务器,以订单服务为例,详细介绍了在POM文件中添加 `spring-cloud-starter-oauth2` 依赖,并配置Spring Security以实现对微服务的保护。通过这一过程,不仅增强了系统的安全性,还提高了资源访问的可控性和灵活性。文章还讨论了最佳实践,包括如何配置OAuth2客户端和资源服务器,以及如何处理常见的安全问题和错误。 ... [详细]
  • 在处理 XML 数据时,如果需要解析 `` 标签的内容,可以采用 Pull 解析方法。Pull 解析是一种高效的 XML 解析方式,适用于流式数据处理。具体实现中,可以通过 Java 的 `XmlPullParser` 或其他类似的库来逐步读取和解析 XML 文档中的 `` 元素。这样不仅能够提高解析效率,还能减少内存占用。本文将详细介绍如何使用 Pull 解析方法来提取 `` 标签的内容,并提供一个示例代码,帮助开发者快速解决问题。 ... [详细]
  • 本文探讨了如何利用Java代码获取当前本地操作系统中正在运行的进程列表及其详细信息。通过引入必要的包和类,开发者可以轻松地实现这一功能,为系统监控和管理提供有力支持。示例代码展示了具体实现方法,适用于需要了解系统进程状态的开发人员。 ... [详细]
  • 在对WordPress Duplicator插件0.4.4版本的安全评估中,发现其存在跨站脚本(XSS)攻击漏洞。此漏洞可能被利用进行恶意操作,建议用户及时更新至最新版本以确保系统安全。测试方法仅限于安全研究和教学目的,使用时需自行承担风险。漏洞编号:HTB23162。 ... [详细]
  • 本文介绍了一种自定义的Android圆形进度条视图,支持在进度条上显示数字,并在圆心位置展示文字内容。通过自定义绘图和组件组合的方式实现,详细展示了自定义View的开发流程和关键技术点。示例代码和效果展示将在文章末尾提供。 ... [详细]
  • 本文介绍了如何利用Struts1框架构建一个简易的四则运算计算器。通过采用DispatchAction来处理不同类型的计算请求,并使用动态Form来优化开发流程,确保代码的简洁性和可维护性。同时,系统提供了用户友好的错误提示,以增强用户体验。 ... [详细]
  • 在Java程序设计中,实现高效的分页功能是提升应用性能的关键之一。本文介绍了通过使用 `PageController` 类来处理大数据集的分页操作,该类能够从一个较大的集合中提取出指定大小的小集合。具体实现中,通过优化数据访问和减少内存消耗,确保了分页操作的高效性和稳定性。此外,文章还探讨了分页算法的优化策略,包括缓存机制和懒加载技术的应用,以进一步提高系统的响应速度和用户体验。 ... [详细]
  • Python 伦理黑客技术:深入探讨后门攻击(第三部分)
    在《Python 伦理黑客技术:深入探讨后门攻击(第三部分)》中,作者详细分析了后门攻击中的Socket问题。由于TCP协议基于流,难以确定消息批次的结束点,这给后门攻击的实现带来了挑战。为了解决这一问题,文章提出了一系列有效的技术方案,包括使用特定的分隔符和长度前缀,以确保数据包的准确传输和解析。这些方法不仅提高了攻击的隐蔽性和可靠性,还为安全研究人员提供了宝贵的参考。 ... [详细]
  • 本文详细介绍了 Java 中遍历 Map 对象的几种常见方法及其应用场景。首先,通过 `entrySet` 方法结合增强型 for 循环进行遍历是最常用的方式,适用于需要同时访问键和值的场景。此外,还探讨了使用 `keySet` 和 `values` 方法分别遍历键和值的技巧,以及使用迭代器(Iterator)进行更灵活的遍历操作。每种方法都附有示例代码和具体的应用实例,帮助开发者更好地理解和选择合适的遍历策略。 ... [详细]
  • 在使用 Qt 进行 YUV420 图像渲染时,由于 Qt 本身不支持直接绘制 YUV 数据,因此需要借助 QOpenGLWidget 和 OpenGL 技术来实现。通过继承 QOpenGLWidget 类并重写其绘图方法,可以利用 GPU 的高效渲染能力,实现高质量的 YUV420 图像显示。此外,这种方法还能显著提高图像处理的性能和流畅性。 ... [详细]
  • 深入理解Java中的多态性概念及其应用
    多态是面向对象编程中的三大核心特性之一,与封装和继承共同构成了面向对象的基础。多态使得代码更加灵活和可扩展,封装和继承则为其提供了必要的支持。本文将深入探讨多态的概念及其在Java中的具体应用,帮助读者全面理解和掌握这一关键知识点。 ... [详细]
  • 卓盟科技:动态资源加载技术的兼容性优化与升级 | Android 开发者案例分享
    随着游戏内容日益复杂,资源加载过程已不仅仅是简单的进度显示,而是连接玩家与开发者的桥梁。玩家对快速加载的需求越来越高,这意味着开发者需要不断优化和提升动态资源加载技术的兼容性和性能。卓盟科技通过一系列的技术创新,不仅提高了加载速度,还确保了不同设备和系统的兼容性,为用户提供更加流畅的游戏体验。 ... [详细]
  • 在Java基础中,私有静态内部类是一种常见的设计模式,主要用于防止外部类的直接调用或实例化。这种内部类仅服务于其所属的外部类,确保了代码的封装性和安全性。通过分析JDK源码,我们可以发现许多常用类中都包含了私有静态内部类,这些内部类虽然功能强大,但其复杂性往往让人感到困惑。本文将深入探讨私有静态内部类的作用、实现方式及其在实际开发中的应用,帮助读者更好地理解和使用这一重要的编程技巧。 ... [详细]
author-avatar
王小小小小弦
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有