├── .github
└── dependabot.yml
├── .gitignore
├── .travis.yml
├── README.md
├── pom.xml
└── src
└── main
├── java
└── com
│ └── yourorganization
│ └── maven_sample
│ └── LogicPositivizer.java
└── resources
└── Blabla.java
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | version: 2
2 | updates:
3 | - package-ecosystem: maven
4 | directory: "/"
5 | schedule:
6 | interval: daily
7 | open-pull-requests-limit: 10
8 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | target
2 | .idea
3 | *.iml
4 | output
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | # This is for https://travis-ci.org/ - you can ignore it.
2 | language: java
3 | jdk:
4 | - openjdk8
5 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | JavaParser and Maven sample
2 | ---
3 |
4 | This fully working sample Maven project parses and generates code with [JavaParser](http://www.javaparser.org).
5 |
6 | This sample is targeted at people without [Maven](https://maven.apache.org/) experience.
7 |
8 | To build it, you will need to download and unpack the latest (or recent) version of Maven (https://maven.apache.org/download.cgi)
9 | and put the `mvn` command on your path.
10 |
11 | Then, you will need to install a Java 1.8 (or higher) JDK (not JRE!), and make sure you can run `java` from the command line.
12 |
13 | If required, install git. If you haven't already done so, clone this sample repository with `git clone https://github.com/javaparser/javaparser-maven-sample.git`.
14 |
15 | Change to the folder of where this sample project was installed (where the pom.xml file is located).
16 | Now run `mvn clean install` and Maven will compile your project,
17 | and put the results into two jar files in the `target` directory.
18 |
19 | You can now run the sample from the command line with
20 | `java -jar target/javaparser-maven-sample-1.0-SNAPSHOT-shaded.jar`.
21 | This runs the sample program, LogicPositivizer, which reads, parses, and modifies the code in resources/Blabla.java and then writes the modified file with the same name to the output folder.
22 |
23 | To better understand this sample, you can read [JavaParser: Visited.](https://leanpub.com/javaparservisited) In this book, key contributors to the JavaParser library teach you how you can use JavaParser to programmatically analyse, transform and generate your java code base.
24 |
25 | How you run this code is up to you, but usually you would start by using an IDE like [NetBeans](https://netbeans.org/), [Intellij IDEA](https://www.jetbrains.com/idea/), or [Eclipse](https://eclipse.org/ide/).
26 |
27 | The Maven dependencies may lag behind the official releases a bit.
28 |
29 | If you notice some problems with this setup, please open an issue.
30 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 |
8 | com.yourorganization
9 |
10 | javaparser-maven-sample
11 |
12 | 1.0-SNAPSHOT
13 |
14 |
15 |
16 | 1.8
17 | 1.8
18 |
19 | UTF-8
20 |
21 |
22 |
23 |
24 |
25 | com.github.javaparser
26 | javaparser-core
27 | 3.26.4
28 |
29 |
30 |
31 |
35 |
36 |
37 |
38 | org.apache.maven.plugins
39 | maven-shade-plugin
40 | 3.6.0
41 |
42 |
43 |
44 | shade
45 |
46 |
47 | true
48 |
49 |
51 | com.yourorganization.maven_sample.LogicPositivizer
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/src/main/java/com/yourorganization/maven_sample/LogicPositivizer.java:
--------------------------------------------------------------------------------
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() {
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 | }
66 |
--------------------------------------------------------------------------------
/src/main/resources/Blabla.java:
--------------------------------------------------------------------------------
1 | import com.github.javaparser.utils.CodeGenerationUtils;
2 | import com.github.javaparser.utils.SourceRoot;
3 |
4 | public class Blabla {
5 |
6 | private final void method1013(StreamBuffer buf, int opcode) {
7 | if (opcode != 1) {
8 | if (opcode != 2) {
9 | if (opcode != 4) {
10 | do {
11 | if (opcode != 5) {
12 | if (opcode == 6)
13 | ((Class94) this).anInt1477 = buf.readUnsignedShort();
14 | else {
15 | if (opcode != 7) {
16 | if (opcode != 8) {
17 | if (opcode == 11)
18 | ((Class94) this).anInt1456 = 1;
19 | else if (opcode != 12) {
20 | if (opcode != 16) {
21 | if (opcode == 23)
22 | ((Class94) this).anInt1424 = (buf.readUnsignedShort());
23 | else if (opcode != 24) {
24 | if (opcode == 25)
25 | ((Class94) this).anInt1487 = (buf.readUnsignedShort());
26 | else if (opcode == 26)
27 | anInt1435 = (buf.readUnsignedShort());
28 | else if (opcode < 30 || opcode >= 35) {
29 | if (opcode >= 35 && opcode < 40)
30 | ((Class94) this).aStringArray1475[-35 + opcode] = (buf.readString());
31 | else if (opcode == 40) {
32 | int i_48_ = (buf.readUnsignedByte());
33 | aShortArray1457 = (new short[i_48_]);
34 | aShortArray1492 = (new short[i_48_]);
35 | for (int i_49_ = 0; i_49_ < i_48_; i_49_++) {
36 | aShortArray1457[i_49_] = (short) buf.readUnsignedShort();
37 | aShortArray1492[i_49_] = (short) buf.readUnsignedShort();
38 | }
39 | } else if (opcode != 41) {
40 | if (opcode == 42) {
41 | int i_50_ = (buf.readUnsignedByte());
42 | aByteArray1501 = (new byte[i_50_]);
43 | for (int i_51_ = 0; (i_51_ < i_50_); i_51_++) aByteArray1501[i_51_] = (buf.readByte(false));
44 | } else if (opcode != 65) {
45 | if (opcode == 78)
46 | anInt1479 = (buf.readUnsignedShort());
47 | else if (opcode == 79)
48 | anInt1438 = (buf.readUnsignedShort());
49 | else if (opcode != 90) {
50 | if (opcode != 91) {
51 | if (opcode == 92)
52 | anInt1450 = buf.readUnsignedShort();
53 | else if (opcode != 93) {
54 | if (opcode != 95) {
55 | if (opcode != 96) {
56 | if (opcode == 97)
57 | ((Class94) this).anInt1474 = buf.readUnsignedShort();
58 | else if (opcode == 98)
59 | ((Class94) this).anInt1500 = buf.readUnsignedShort();
60 | else if (opcode < 100 || opcode >= 110) {
61 | if (opcode == 110)
62 | anInt1423 = buf.readUnsignedShort();
63 | else if (opcode != 111) {
64 | if (opcode == 112)
65 | anInt1480 = buf.readUnsignedShort();
66 | else if (opcode != 113) {
67 | if (opcode == 114)
68 | anInt1439 = buf.readByte(false) * 5;
69 | else if (opcode == 115)
70 | ((Class94) this).anInt1462 = buf.readUnsignedByte();
71 | else if (opcode != 121) {
72 | if (opcode != 122) {
73 | if (opcode == 125) {
74 | anInt1493 = buf.readByte(false) << 2;
75 | anInt1465 = buf.readByte(false) << 2;
76 | anInt1437 = buf.readByte(false) << 2;
77 | } else if (opcode == 126) {
78 | anInt1498 = buf.readByte(false) << 2;
79 | anInt1470 = buf.readByte(false) << 2;
80 | anInt1446 = buf.readByte(false) << 2;
81 | } else if (opcode == 127) {
82 | ((Class94) this).anInt1455 = buf.readUnsignedByte();
83 | ((Class94) this).anInt1426 = buf.readUnsignedShort();
84 | } else if (opcode != 128) {
85 | if (opcode == 129) {
86 | ((Class94) this).anInt1433 = buf.readUnsignedByte();
87 | ((Class94) this).anInt1468 = buf.readUnsignedShort();
88 | } else if (opcode == 130) {
89 | ((Class94) this).anInt1440 = buf.readUnsignedByte();
90 | ((Class94) this).anInt1483 = buf.readUnsignedShort();
91 | } else if (opcode != 132) {
92 | if (opcode == 249) {
93 | int i_52_ = buf.readUnsignedByte();
94 | if (((Class94) this).aClass194_1472 == null) {
95 | int i_53_ = Class307.calculateSize(i_52_);
96 | ((Class94) this).aClass194_1472 = new HashTable(i_53_);
97 | }
98 | for (int i_54_ = 0; i_54_ < i_52_; i_54_++) {
99 | boolean bool = buf.readUnsignedByte() == 1;
100 | int i_55_ = buf.method2507(125);
101 | Node class279;
102 | if (bool)
103 | class279 = new StringNode(buf.readString());
104 | else
105 | class279 = new IntegerNode(buf.readInt());
106 | ((Class94) this).aClass194_1472.add((long) i_55_, class279);
107 | }
108 | }
109 | } else {
110 | int i_56_ = buf.readUnsignedByte();
111 | ((Class94) this).anIntArray1441 = new int[i_56_];
112 | for (int i_57_ = 0; i_56_ > i_57_; i_57_++) ((Class94) this).anIntArray1441[i_57_] = buf.readUnsignedShort();
113 | }
114 | } else {
115 | ((Class94) this).anInt1442 = buf.readUnsignedByte();
116 | ((Class94) this).anInt1476 = buf.readUnsignedShort();
117 | }
118 | } else
119 | ((Class94) this).anInt1431 = buf.readUnsignedShort();
120 | } else
121 | ((Class94) this).anInt1429 = buf.readUnsignedShort();
122 | } else
123 | anInt1458 = buf.readByte(false);
124 | } else
125 | anInt1503 = buf.readUnsignedShort();
126 | } else {
127 | if (((Class94) this).anIntArray1460 == null) {
128 | ((Class94) this).anIntArray1460 = new int[10];
129 | ((Class94) this).anIntArray1445 = new int[10];
130 | }
131 | ((Class94) this).anIntArray1460[-100 + opcode] = buf.readUnsignedShort();
132 | ((Class94) this).anIntArray1445[opcode - 100] = buf.readUnsignedShort();
133 | }
134 | } else
135 | ((Class94) this).anInt1443 = buf.readUnsignedByte();
136 | } else
137 | ((Class94) this).anInt1494 = buf.readUnsignedShort();
138 | } else
139 | anInt1490 = buf.readUnsignedShort();
140 | } else
141 | anInt1466 = buf.readUnsignedShort();
142 | } else
143 | anInt1454 = (buf.readUnsignedShort());
144 | } else
145 | ((Class94) this).aBoolean1463 = true;
146 | } else {
147 | int i_58_ = (buf.readUnsignedByte());
148 | aShortArray1504 = (new short[i_58_]);
149 | aShortArray1488 = (new short[i_58_]);
150 | for (int i_59_ = 0; i_59_ < i_58_; i_59_++) {
151 | aShortArray1488[i_59_] = (short) buf.readUnsignedShort();
152 | aShortArray1504[i_59_] = (short) buf.readUnsignedShort();
153 | }
154 | }
155 | } else
156 | ((Class94) this).aStringArray1485[opcode + -30] = (buf.readString());
157 | } else
158 | anInt1449 = (buf.readUnsignedShort());
159 | } else
160 | ((Class94) this).aBoolean1502 = true;
161 | } else
162 | ((Class94) this).anInt1473 = (buf.readInt());
163 | } else {
164 | ((Class94) this).anInt1491 = buf.readUnsignedShort();
165 | if (((Class94) this).anInt1491 > 32767)
166 | ((Class94) this).anInt1491 -= 65536;
167 | }
168 | } else {
169 | ((Class94) this).anInt1425 = buf.readUnsignedShort();
170 | if (((Class94) this).anInt1425 <= 32767)
171 | break;
172 | ((Class94) this).anInt1425 -= 65536;
173 | }
174 | break;
175 | }
176 | break;
177 | }
178 | ((Class94) this).anInt1444 = buf.readUnsignedShort();
179 | } while (false);
180 | } else
181 | ((Class94) this).anInt1436 = buf.readUnsignedShort();
182 | } else
183 | ((Class94) this).aString1434 = buf.readString();
184 | } else
185 | anInt1481 = buf.readUnsignedShort();
186 | }
187 | }
188 |
--------------------------------------------------------------------------------