-
Notifications
You must be signed in to change notification settings - Fork 103
Plugin to remove java bean methods for when you want to use Lombok or similar #654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Asger-KB
wants to merge
1
commit into
highsource:master
Choose a base branch
from
Asger-KB:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...ugins-parent/jaxb-plugins/src/main/java/org/jvnet/jaxb/plugin/nobean/NoGettersPlugin.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package org.jvnet.jaxb.plugin.nobean; | ||
|
|
||
| import com.sun.codemodel.JMethod; | ||
| import com.sun.tools.xjc.Options; | ||
| import com.sun.tools.xjc.Plugin; | ||
| import com.sun.tools.xjc.outline.ClassOutline; | ||
| import com.sun.tools.xjc.outline.Outline; | ||
| import org.xml.sax.ErrorHandler; | ||
| import org.xml.sax.SAXException; | ||
|
|
||
| import java.util.Collection; | ||
|
|
||
|
|
||
| /** | ||
| * XJC plugin to remove getXX functions. | ||
| * The motivation is to allow for bean methods from annotations | ||
| * | ||
| * @author Asger Askov Blekinge "asga@kb.dk" | ||
| * | ||
| */ | ||
| public class NoGettersPlugin extends Plugin { | ||
|
|
||
| protected final String OPTION_NAME = "XnoGetters"; | ||
|
|
||
| @Override | ||
| public String getOptionName() { | ||
| return OPTION_NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public String getUsage() { | ||
| return "-" + OPTION_NAME + " : Do not generate getter methods for fields\n"; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean run(Outline model, Options opts, ErrorHandler errors) throws SAXException { | ||
| for (ClassOutline co : model.getClasses()) { | ||
| Collection<JMethod> methods = co.implClass.methods(); | ||
| methods.removeIf(next -> next.name().startsWith("get")); | ||
| methods.removeIf(next -> next.name().startsWith("is")); | ||
| } | ||
| return true; | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
...ugins-parent/jaxb-plugins/src/main/java/org/jvnet/jaxb/plugin/nobean/NoSettersPlugin.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package org.jvnet.jaxb.plugin.nobean; | ||
|
|
||
| import com.sun.codemodel.JMethod; | ||
| import com.sun.tools.xjc.Options; | ||
| import com.sun.tools.xjc.Plugin; | ||
| import com.sun.tools.xjc.outline.ClassOutline; | ||
| import com.sun.tools.xjc.outline.Outline; | ||
| import org.xml.sax.ErrorHandler; | ||
| import org.xml.sax.SAXException; | ||
|
|
||
| import java.util.Collection; | ||
|
|
||
|
|
||
| /** | ||
| * XJC plugin to remove setXX functions. | ||
| * The motivation is to allow for immutable objects or setters generated with Lombok annotations | ||
| * | ||
| * @author Asger Askov Blekinge "asga@kb.dk" | ||
| * | ||
| */ | ||
| public class NoSettersPlugin extends Plugin { | ||
|
|
||
| protected final String OPTION_NAME = "XnoSetters"; | ||
|
|
||
| @Override | ||
| public String getOptionName() { | ||
| return OPTION_NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public String getUsage() { | ||
| return "-" + OPTION_NAME + " : Do not generate setter methods for fields\n"; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean run(Outline model, Options opts, ErrorHandler errors) throws SAXException { | ||
| for (ClassOutline co : model.getClasses()) { | ||
| Collection<JMethod> methods = co.implClass.methods(); | ||
| methods.removeIf(next -> next.name().startsWith("set")); | ||
| } | ||
| return true; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...nt/jaxb-plugins/src/test/java/org/jvnet/jaxb/plugin/nobean/tests/NoGettersPluginTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package org.jvnet.jaxb.plugin.nobean.tests; | ||
|
|
||
| import com.sun.tools.xjc.Options; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.jvnet.jaxb.maven.AbstractXJCMojo; | ||
| import org.jvnet.jaxb.maven.test.RunXJCMojo; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| class NoGettersPluginTest extends RunXJCMojo { | ||
|
|
||
| @Override | ||
| public File getSchemaDirectory() { | ||
| return new File(getBaseDir(), "src/test/resources"); | ||
| } | ||
|
|
||
| @Override | ||
| protected void configureMojo(AbstractXJCMojo<Options> mojo) { | ||
| super.configureMojo(mojo); | ||
| mojo.setForceRegenerate(true); | ||
| } | ||
|
|
||
| @Override | ||
| public List<String> getArgs() { | ||
| final List<String> args = new ArrayList<>(super.getArgs()); | ||
| args.add("-XnoGetters"); | ||
| return args; | ||
| } | ||
|
|
||
| @Test | ||
| @Override | ||
| public void testExecute() throws Exception { | ||
| super.testExecute(); | ||
|
|
||
| List<Path> javaFiles = listGeneratedFiles(); | ||
|
|
||
| for (Path javaFile : javaFiles) { | ||
| List<String> lines = Files.readAllLines(javaFile); | ||
| for (int i = 0; i < lines.size(); i++) { | ||
| String line = lines.get(i); | ||
| if (line.matches("^ *public [a-zA-Z]+ (isSet|get)[A-Z]\\w+\\(\\) \\{")) { | ||
| String fileLinePath = getGeneratedDirectory() | ||
| .toPath() | ||
| .relativize(javaFile) | ||
| .toString() | ||
| .replaceFirst(".java$", "") | ||
| .replaceAll("/", ".") | ||
| + "(" + javaFile.getFileName().toString() + ":" + i + ")"; | ||
| Assertions.fail("One getter remains in file " + fileLinePath + " in the line '" + line + "'"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private List<Path> listGeneratedFiles() throws IOException { | ||
| try (Stream<Path> list = Files.list(getGeneratedDirectory().toPath() | ||
| .resolve(Path.of("org/jvnet/jaxb/tests/one")));) { | ||
| return list.collect(Collectors.toList()); | ||
| } | ||
| } | ||
| } | ||
68 changes: 68 additions & 0 deletions
68
...nt/jaxb-plugins/src/test/java/org/jvnet/jaxb/plugin/nobean/tests/NoSettersPluginTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package org.jvnet.jaxb.plugin.nobean.tests; | ||
|
|
||
| import com.sun.tools.xjc.Options; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.jvnet.jaxb.maven.AbstractXJCMojo; | ||
| import org.jvnet.jaxb.maven.test.RunXJCMojo; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| class NoSettersPluginTest extends RunXJCMojo { | ||
|
|
||
| @Override | ||
| public File getSchemaDirectory() { | ||
| return new File(getBaseDir(), "src/test/resources"); | ||
| } | ||
|
|
||
| @Override | ||
| protected void configureMojo(AbstractXJCMojo<Options> mojo) { | ||
| super.configureMojo(mojo); | ||
| mojo.setForceRegenerate(true); | ||
| } | ||
|
|
||
| @Override | ||
| public List<String> getArgs() { | ||
| final List<String> args = new ArrayList<>(super.getArgs()); | ||
| args.add("-XnoGetters"); | ||
| return args; | ||
| } | ||
| @Test | ||
| @Override | ||
| public void testExecute() throws Exception { | ||
| super.testExecute(); | ||
|
|
||
| List<Path> javaFiles = listGeneratedFiles(); | ||
|
|
||
| for (Path javaFile : javaFiles) { | ||
| List<String> lines = Files.readAllLines(javaFile); | ||
| for (int i = 0; i < lines.size(); i++) { | ||
| String line = lines.get(i); | ||
| if (line.matches("^ *public [a-zA-Z]+ (set)[A-Z]\\w+\\(\\) \\{")) { | ||
| String fileLinePath = getGeneratedDirectory() | ||
| .toPath() | ||
| .relativize(javaFile) | ||
| .toString() | ||
| .replaceFirst(".java$", "") | ||
| .replaceAll("/", ".") | ||
| + "(" + javaFile.getFileName().toString() + ":" + i + ")"; | ||
| Assertions.fail("One set remains in file " + fileLinePath + " in the line '" + line + "'"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private List<Path> listGeneratedFiles() throws IOException { | ||
| try (Stream<Path> list = Files.list(getGeneratedDirectory().toPath() | ||
| .resolve(Path.of("org/jvnet/jaxb/tests/one")));) { | ||
| return list.collect(Collectors.toList()); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't
isSetjust beis?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isSetis only to check for list fields.I guess
isshould be enough but if we only want to targetgettersandsetters, my best bet would be to find them according properties of the bean ?