|
| 1 | +package com.yourorganization.maven_sample; |
| 2 | + |
| 3 | +import com.github.javaparser.ast.CompilationUnit; |
| 4 | +import com.github.javaparser.ast.expr.BinaryExpr; |
| 5 | +import com.github.javaparser.ast.stmt.IfStmt; |
| 6 | +import com.github.javaparser.ast.stmt.Statement; |
| 7 | +import com.github.javaparser.ast.visitor.ModifierVisitor; |
| 8 | +import com.github.javaparser.ast.visitor.Visitable; |
| 9 | +import com.github.javaparser.utils.CodeGenerationUtils; |
| 10 | +import com.github.javaparser.utils.Log; |
| 11 | +import com.github.javaparser.utils.SourceRoot; |
| 12 | + |
| 13 | +import java.nio.file.Paths; |
| 14 | + |
| 15 | +/** |
| 16 | + * Some code that uses JavaParser. |
| 17 | + */ |
| 18 | +public class LogicPositivizer { |
| 19 | + public static void main(String[] args) { |
| 20 | + // JavaParser has a minimal logging class that normally logs nothing. |
| 21 | + // Let's ask it to write to standard out: |
| 22 | + Log.setAdapter(new Log.StandardOutStandardErrorAdapter()); |
| 23 | + |
| 24 | + // SourceRoot is a tool that read and writes Java files from packages on a certain root directory. |
| 25 | + // In this case the root directory is found by taking the root from the current Maven module, |
| 26 | + // with src/main/resources appended. |
| 27 | + SourceRoot sourceRoot = new SourceRoot(CodeGenerationUtils.mavenModuleRoot(LogicPositivizer.class).resolve("src/main/resources")); |
| 28 | + |
| 29 | + // Our sample is in the root of this directory, so no package name. |
| 30 | + CompilationUnit cu = sourceRoot.parse("", "Blabla.java"); |
| 31 | + |
| 32 | + Log.info("Positivizing!"); |
| 33 | + |
| 34 | + cu.accept(new ModifierVisitor<Void>() { |
| 35 | + /** |
| 36 | + * For every if-statement, see if it has a comparison using "!=". |
| 37 | + * Change it to "==" and switch the "then" and "else" statements around. |
| 38 | + */ |
| 39 | + @Override |
| 40 | + public Visitable visit(IfStmt n, Void arg) { |
| 41 | + // Figure out what to get and what to cast simply by looking at the AST in a debugger! |
| 42 | + n.getCondition().ifBinaryExpr(binaryExpr -> { |
| 43 | + if (binaryExpr.getOperator() == BinaryExpr.Operator.NOT_EQUALS && n.getElseStmt().isPresent()) { |
| 44 | + /* It's a good idea to clone nodes that you move around. |
| 45 | + JavaParser (or you) might get confused about who their parent is! |
| 46 | + */ |
| 47 | + Statement thenStmt = n.getThenStmt().clone(); |
| 48 | + Statement elseStmt = n.getElseStmt().get().clone(); |
| 49 | + n.setThenStmt(elseStmt); |
| 50 | + n.setElseStmt(thenStmt); |
| 51 | + binaryExpr.setOperator(BinaryExpr.Operator.EQUALS); |
| 52 | + } |
| 53 | + }); |
| 54 | + return super.visit(n, arg); |
| 55 | + } |
| 56 | + }, null); |
| 57 | + |
| 58 | + // This saves all the files we just read to an output directory. |
| 59 | + sourceRoot.saveAll( |
| 60 | + // The path of the Maven module/project which contains the LogicPositivizer class. |
| 61 | + CodeGenerationUtils.mavenModuleRoot(LogicPositivizer.class) |
| 62 | + // appended with a path to "output" |
| 63 | + .resolve(Paths.get("output"))); |
| 64 | + } |
| 65 | +} |
0 commit comments