Skip to content

Commit 2974f09

Browse files
committed
Plugin to remove java bean methods for when you want to use Lombok or similar
Signed-off-by: Asger Askov Blekinge <asga@kb.dk>
1 parent d970f53 commit 2974f09

File tree

6 files changed

+228
-0
lines changed

6 files changed

+228
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ JAXB Basics can only be used with JAXB/XJC 4.x.
147147
* [Xml ElementWrapper Plugin](https://github.com/highsource/jaxb-tools/wiki/JAXB-XML-ElementWrapper-Plugin) - generates `jakarta.xml.bind.annotation.XmlElementWrapper` annotation to simplify generated structure.
148148
* [Parent Pointer Plugin](https://github.com/highsource/jaxb-tools/wiki/JAXB-Parent-Pointer-Plugin) - generates getter in child elements to get the parent object (depends on `jaxb-plugins-runtime`)
149149
* [Property Listener Injector Plugin](https://github.com/highsource/jaxb-tools/wiki/JAXB-Property-Listener-Injector-Plugin) - adds methods in order to configure the generation of events on each `setXXX` method
150+
* [NoSetter Plugin](https://github.com/highsource/jaxb-tools/wiki/JAXB-NoBean-Plugin) - Removes setter methods (`setXXX`) from generated classes
151+
* [NoGetter Plugin](https://github.com/highsource/jaxb-tools/wiki/JAXB-NoBean-Plugin) - Removes getter methods (`getXXX` and `isXXX`) from generated classes
150152

151153
## Credits
152154

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.jvnet.jaxb.plugin.nobean;
2+
3+
import com.sun.codemodel.JMethod;
4+
import com.sun.tools.xjc.Options;
5+
import com.sun.tools.xjc.Plugin;
6+
import com.sun.tools.xjc.outline.ClassOutline;
7+
import com.sun.tools.xjc.outline.Outline;
8+
import org.xml.sax.ErrorHandler;
9+
import org.xml.sax.SAXException;
10+
11+
import java.util.Collection;
12+
13+
14+
/**
15+
* XJC plugin to remove getXX functions.
16+
* The motivation is to allow for bean methods from annotations
17+
*
18+
* @author Asger Askov Blekinge "asga@kb.dk"
19+
*
20+
*/
21+
public class NoGettersPlugin extends Plugin {
22+
23+
protected final String OPTION_NAME = "XnoGetters";
24+
25+
@Override
26+
public String getOptionName() {
27+
return OPTION_NAME;
28+
}
29+
30+
@Override
31+
public String getUsage() {
32+
return "-" + OPTION_NAME + " : Do not generate getter methods for fields\n";
33+
}
34+
35+
@Override
36+
public boolean run(Outline model, Options opts, ErrorHandler errors) throws SAXException {
37+
for (ClassOutline co : model.getClasses()) {
38+
Collection<JMethod> methods = co.implClass.methods();
39+
methods.removeIf(next -> next.name().startsWith("get"));
40+
methods.removeIf(next -> next.name().startsWith("is"));
41+
}
42+
return true;
43+
}
44+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.jvnet.jaxb.plugin.nobean;
2+
3+
import com.sun.codemodel.JMethod;
4+
import com.sun.tools.xjc.Options;
5+
import com.sun.tools.xjc.Plugin;
6+
import com.sun.tools.xjc.outline.ClassOutline;
7+
import com.sun.tools.xjc.outline.Outline;
8+
import org.xml.sax.ErrorHandler;
9+
import org.xml.sax.SAXException;
10+
11+
import java.util.Collection;
12+
13+
14+
/**
15+
* XJC plugin to remove setXX functions.
16+
* The motivation is to allow for immutable objects or setters generated with Lombok annotations
17+
*
18+
* @author Asger Askov Blekinge "asga@kb.dk"
19+
*
20+
*/
21+
public class NoSettersPlugin extends Plugin {
22+
23+
protected final String OPTION_NAME = "XnoSetters";
24+
25+
@Override
26+
public String getOptionName() {
27+
return OPTION_NAME;
28+
}
29+
30+
@Override
31+
public String getUsage() {
32+
return "-" + OPTION_NAME + " : Do not generate setter methods for fields\n";
33+
}
34+
35+
@Override
36+
public boolean run(Outline model, Options opts, ErrorHandler errors) throws SAXException {
37+
for (ClassOutline co : model.getClasses()) {
38+
Collection<JMethod> methods = co.implClass.methods();
39+
methods.removeIf(next -> next.name().startsWith("set"));
40+
}
41+
return true;
42+
}
43+
}

jaxb-plugins-parent/jaxb-plugins/src/main/resources/META-INF/services/com.sun.tools.xjc.Plugin

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ org.jvnet.jaxb.plugin.elementwrapper.XmlElementWrapperPlugin
2525
org.jvnet.jaxb.plugin.parentpointer.ParentPointerPlugin
2626
org.jvnet.jaxb.plugin.propertylistenerinjector.PropertyListenerInjectorPlugin
2727
org.jvnet.jaxb.plugin.map_init.MapInitPlugin
28+
org.jvnet.jaxb.plugin.nobean.NoSettersPlugin
29+
org.jvnet.jaxb.plugin.nobean.NoGettersPlugin
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.jvnet.jaxb.plugin.nobean.tests;
2+
3+
import com.sun.tools.xjc.Options;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
import org.jvnet.jaxb.maven.AbstractXJCMojo;
7+
import org.jvnet.jaxb.maven.test.RunXJCMojo;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
import java.util.stream.Collectors;
16+
import java.util.stream.Stream;
17+
18+
class NoGettersPluginTest extends RunXJCMojo {
19+
20+
@Override
21+
public File getSchemaDirectory() {
22+
return new File(getBaseDir(), "src/test/resources");
23+
}
24+
25+
@Override
26+
protected void configureMojo(AbstractXJCMojo<Options> mojo) {
27+
super.configureMojo(mojo);
28+
mojo.setForceRegenerate(true);
29+
}
30+
31+
@Override
32+
public List<String> getArgs() {
33+
final List<String> args = new ArrayList<>(super.getArgs());
34+
args.add("-XnoGetters");
35+
return args;
36+
}
37+
38+
@Test
39+
@Override
40+
public void testExecute() throws Exception {
41+
super.testExecute();
42+
43+
List<Path> javaFiles = listGeneratedFiles();
44+
45+
for (Path javaFile : javaFiles) {
46+
List<String> lines = Files.readAllLines(javaFile);
47+
for (int i = 0; i < lines.size(); i++) {
48+
String line = lines.get(i);
49+
if (line.matches("^ *public [a-zA-Z]+ (isSet|get)[A-Z]\\w+\\(\\) \\{")) {
50+
String fileLinePath = getGeneratedDirectory()
51+
.toPath()
52+
.relativize(javaFile)
53+
.toString()
54+
.replaceFirst(".java$", "")
55+
.replaceAll("/", ".")
56+
+ "(" + javaFile.getFileName().toString() + ":" + i + ")";
57+
Assertions.fail("One getter remains in file " + fileLinePath + " in the line '" + line + "'");
58+
}
59+
}
60+
}
61+
}
62+
63+
private List<Path> listGeneratedFiles() throws IOException {
64+
try (Stream<Path> list = Files.list(getGeneratedDirectory().toPath()
65+
.resolve(Path.of("org/jvnet/jaxb/tests/one")));) {
66+
return list.collect(Collectors.toList());
67+
}
68+
}
69+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.jvnet.jaxb.plugin.nobean.tests;
2+
3+
import com.sun.tools.xjc.Options;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
import org.jvnet.jaxb.maven.AbstractXJCMojo;
7+
import org.jvnet.jaxb.maven.test.RunXJCMojo;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
import java.util.stream.Collectors;
16+
import java.util.stream.Stream;
17+
18+
class NoSettersPluginTest extends RunXJCMojo {
19+
20+
@Override
21+
public File getSchemaDirectory() {
22+
return new File(getBaseDir(), "src/test/resources");
23+
}
24+
25+
@Override
26+
protected void configureMojo(AbstractXJCMojo<Options> mojo) {
27+
super.configureMojo(mojo);
28+
mojo.setForceRegenerate(true);
29+
}
30+
31+
@Override
32+
public List<String> getArgs() {
33+
final List<String> args = new ArrayList<>(super.getArgs());
34+
args.add("-XnoGetters");
35+
return args;
36+
}
37+
@Test
38+
@Override
39+
public void testExecute() throws Exception {
40+
super.testExecute();
41+
42+
List<Path> javaFiles = listGeneratedFiles();
43+
44+
for (Path javaFile : javaFiles) {
45+
List<String> lines = Files.readAllLines(javaFile);
46+
for (int i = 0; i < lines.size(); i++) {
47+
String line = lines.get(i);
48+
if (line.matches("^ *public [a-zA-Z]+ (set)[A-Z]\\w+\\(\\) \\{")) {
49+
String fileLinePath = getGeneratedDirectory()
50+
.toPath()
51+
.relativize(javaFile)
52+
.toString()
53+
.replaceFirst(".java$", "")
54+
.replaceAll("/", ".")
55+
+ "(" + javaFile.getFileName().toString() + ":" + i + ")";
56+
Assertions.fail("One set remains in file " + fileLinePath + " in the line '" + line + "'");
57+
}
58+
}
59+
}
60+
}
61+
62+
private List<Path> listGeneratedFiles() throws IOException {
63+
try (Stream<Path> list = Files.list(getGeneratedDirectory().toPath()
64+
.resolve(Path.of("org/jvnet/jaxb/tests/one")));) {
65+
return list.collect(Collectors.toList());
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)