Skip to content
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

Backslashes in Patterns are doubled #9

Closed
albers opened this issue Jun 29, 2015 · 19 comments · Fixed by highsource/jaxb-tools#330
Closed

Backslashes in Patterns are doubled #9

albers opened this issue Jun 29, 2015 · 19 comments · Fixed by highsource/jaxb-tools#330
Assignees
Labels

Comments

@albers
Copy link

albers commented Jun 29, 2015

I'm trying to add a JSR-303 @Pattern constraint to a generated class. The pattern contains backslashes.
In the generated code, all backslashes are doubled:
"[0-9]+\\.[0-9]{1,2}\\.[0-9]{4}\\.[0-9]+" -> "[0-9]+\\\\.[0-9]{1,2}\\\\.[0-9]{4}\\\\.[0-9]+"

This happens when invoking from maven and from Eclipse using m2e connector for jaxb2
Resorting to single backslashes in the (xjb) source won't help as it produces parser errors.

XJB fragment:

<bindings version="2.1" 
    xmlns="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:annox="http://annox.dev.java.net"
    extensionBindingPrefixes="xjc annox">
    [...]
    <bindings node="xs:attribute[@name='build']">
        <annox:annotateProperty target="field">@javax.validation.constraints.Pattern(regexp = "[0-9]+\\.[0-9]{1,2}\\.[0-9]{4}\\.[0-9]+")</annox:annotateProperty>
    </bindings>
    [...]
</bindings>

Maven build section:

    <build>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.12.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <args>
                        <arg>-Xannotate</arg>
                    </args>
                    <plugins>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics-annotate</artifactId>
                            <version>1.0.1</version>
                        </plugin>
                    </plugins>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.glassfish.jaxb</groupId>
                        <artifactId>jaxb-runtime</artifactId>
                        <version>2.2.11</version>
                    </dependency>
                    <dependency>
                        <groupId>javax.validation</groupId>
                        <artifactId>validation-api</artifactId>
                        <version>1.0.0.GA</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

Is there any way to circumvent this behaviour?

@highsource
Copy link
Owner

If forget the necessary escaping, which regexp do you pursue? [0-9]+\.[0-9]{1,2}\.[0-9]{4}\.[0-9]+?

@albers
Copy link
Author

albers commented Jun 29, 2015

Thanks for responding so quickly!

The regular expression I need is [0-9]+\.[0-9]{1,2}\.[0-9]{4}\.[0-9]+ (a version string, e.g. 1.20.1003.20).
With the quoting required for Java syntax, it becomes [0-9]+\\.[0-9]{1,2}\\.[0-9]{4}\\.[0-9]+.

My goal is to generate an annotation that looks exactly like

@Pattern(regexp = "[0-9]+\\.[0-9]{1,2}\\.[0-9]{4}\\.[0-9]+")

with Pattern being javax.validation.constraints.Pattern.

@highsource
Copy link
Owner

Plase try with single backslashes first. Just to make sure.
Feels like a bug at the moment, I think your annotation seems to be correct.

On Mon, Jun 29, 2015 at 11:11 PM, Harald Albers notifications@github.com
wrote:

Thanks for responding so quickly!

The regular expression I need is [0-9]+.[0-9]{1,2}.[0-9]{4}.[0-9]+ (a
version string, e.g. 1.20.1003.20).
With the quoting required for Java syntax, it becomes
[0-9]+.[0-9]{1,2}.[0-9]{4}.[0-9]+.

My goal is to generate an annotation that looks exactly like

@pattern(regexp = "[0-9]+.[0-9]{1,2}.[0-9]{4}.[0-9]+")

with Pattern being javax.validation.constraints.Pattern.


Reply to this email directly or view it on GitHub
#9 (comment)
.

@albers
Copy link
Author

albers commented Jun 29, 2015

The single backslash syntax, i.e.

<annox:annotateProperty target="field">@javax.validation.constraints.Pattern(regexp = "[0-9]+\.[0-9]{1,2}\.[0-9]{4}\.[0-9]+")</annox:annotateProperty>

is rejected due to parser errors. Here's the corresponding Maven stackdump:

Exception in thread "main" japa.parser.TokenMgrError: Lexical error at line 1, column 56. Encountered: "." (46), after : ""[0-9]+"
at japa.parser.ASTParserTokenManager.getNextToken(ASTParserTokenManager.java:2301)
at japa.parser.ASTParser.jj_scan_token(ASTParser.java:7847)
at japa.parser.ASTParser.jj_3R_179(ASTParser.java:7118)
at japa.parser.ASTParser.jj_3R_149(ASTParser.java:7215)
at japa.parser.ASTParser.jj_3R_97(ASTParser.java:7238)
at japa.parser.ASTParser.jj_3R_146(ASTParser.java:6795)
at japa.parser.ASTParser.jj_3R_95(ASTParser.java:6823)
at japa.parser.ASTParser.jj_3R_265(ASTParser.java:6882)
at japa.parser.ASTParser.jj_3R_255(ASTParser.java:6927)
at japa.parser.ASTParser.jj_3R_237(ASTParser.java:6816)
at japa.parser.ASTParser.jj_3R_179(ASTParser.java:7123)
at japa.parser.ASTParser.jj_3R_149(ASTParser.java:7215)
at japa.parser.ASTParser.jj_3R_97(ASTParser.java:7238)
at japa.parser.ASTParser.jj_3R_96(ASTParser.java:6093)
at japa.parser.ASTParser.jj_3R_50(ASTParser.java:6111)
at japa.parser.ASTParser.jj_3_1(ASTParser.java:6135)
at japa.parser.ASTParser.jj_2_1(ASTParser.java:4348)
at japa.parser.ASTParser.CompilationUnit(ASTParser.java:124)
at japa.parser.JavaParser.parse(JavaParser.java:166)
at org.jvnet.annox.japa.parser.AnnotationExprParser.parse(AnnotationExprParser.java:22)
at org.jvnet.annox.parser.XAnnotationParser.parseAnnotationExprs(XAnnotationParser.java:209)
at org.jvnet.annox.parser.XAnnotationParser.parseAnnotationExpr(XAnnotationParser.java:184)
at org.jvnet.annox.parser.XAnnotationParser.parse(XAnnotationParser.java:163)
at org.jvnet.jaxb2_commons.plugin.annotate.AnnotatePlugin.annotate(AnnotatePlugin.java:425)
at org.jvnet.jaxb2_commons.plugin.annotate.AnnotatePlugin.annotate(AnnotatePlugin.java:390)
at org.jvnet.jaxb2_commons.plugin.annotate.AnnotatePlugin.processFieldOutline(AnnotatePlugin.java:172)
at org.jvnet.jaxb2_commons.plugin.annotate.AnnotatePlugin.processClassOutline(AnnotatePlugin.java:160)
at org.jvnet.jaxb2_commons.plugin.annotate.AnnotatePlugin.run(AnnotatePlugin.java:118)
at com.sun.tools.xjc.model.Model.generateCode(Model.java:292)
at org.jvnet.mjiip.v_2_2.XJC22Mojo.generateCode(XJC22Mojo.java:66)
at org.jvnet.mjiip.v_2_2.XJC22Mojo.doExecute(XJC22Mojo.java:41)
at org.jvnet.mjiip.v_2_2.XJC22Mojo.doExecute(XJC22Mojo.java:28)
at org.jvnet.jaxb2.maven2.RawXJC2Mojo.doExecute(RawXJC2Mojo.java:488)
at org.jvnet.jaxb2.maven2.RawXJC2Mojo.execute(RawXJC2Mojo.java:311)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:133)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:108)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:76)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:116)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:361)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:213)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:157)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)

@albers
Copy link
Author

albers commented Jun 29, 2015

Same error when used in Eclipse with m2e connector for jaxb2:

An internal error occurred during: "Building workspace".
Lexical error at line 1, column 56. Encountered: "." (46), after : ""[0-9]+"

@highsource highsource added the bug label Aug 28, 2015
@highsource highsource added this to the 1.0.2 milestone Aug 28, 2015
@highsource highsource self-assigned this Aug 28, 2015
@highsource highsource modified the milestones: 1.0.3, 1.0.2 Aug 29, 2015
@albers
Copy link
Author

albers commented Feb 18, 2016

@highsource Just a ping. Will you be able to provide a fix for this soon?
I'm still interested in a solution.
I would like to add JSR-303 annotations to my generated classes, but most usages of @Pattern will not work due to the extensive use of backslashes in regular expressions. Any backslash would break the generation.

@highsource
Copy link
Owner

Sorry, I'm pretty under water at the moment. Would accept a PR though.

@albers
Copy link
Author

albers commented Feb 19, 2016

No problem, thanks for the reply. Can you please give me a hint where I might look for potential problems?
I only know jaxb from the user perspective and have no idea how the components interact.

@highsource
Copy link
Owner

Hi,

the problem is that a string which is used as an annotation parameter is
changed.
The annotation happens here:

https://github.com/highsource/jaxb2-annotate-plugin/blob/master/plugin/src/main/java/org/jvnet/jaxb2_commons/plugin/annotate/AnnotatingSingleValueVisitor.java#L128

So I'd first try to reproduce this in the level of the JCodeModel. Create a
JCodeModel, create a class, add a field and try to annotate this field with
a regex - and see if this will be changed.

Best wishes,
Alexey

On Fri, Feb 19, 2016 at 1:28 PM, Harald Albers notifications@github.com
wrote:

No problem, thanks for the reply. Can you please give me a hint where I
might look for potential problems?
I only know jaxb from the user perspective and have no idea how the
components interact.


Reply to this email directly or view it on GitHub
#9 (comment)
.

@digitalfog
Copy link
Contributor

Hi, I've had a look at this issue. Yeah, the problem is that JAnnotationUse internally escapes value passed into its JAnnotationUse.param(String name, Stringvalue) method.

So, on one hand we have a method, that escapes String internally and it seems that we have to pass there unescaped value.
On the other hand, we have a japa.parser.JavaParser, which parses correct String - i.e. escaped. (Disclaimer: this assumption I've made only because JavaParser has thrown an exception on unescaped String above. But maybe I'm wrong)

As a possible solution, we could forcely unescape parsed String before passing it to JAnnotationUse. Maybe just in this mentioned method https://github.com/highsource/jaxb2-annotate-plugin/blob/master/plugin/src/main/java/org/jvnet/jaxb2_commons/plugin/annotate/AnnotatingSingleValueVisitor.java#L128

@highsource highsource removed this from the 1.0.3 milestone May 24, 2018
@rosko01
Copy link

rosko01 commented Nov 17, 2020

@highsource Any chance that this issue could be resolved in the future?

@laurentschoelens
Copy link
Collaborator

laurentschoelens commented Aug 25, 2023

@highsource Any chance that this issue could be resolved in the future?

Hi everyone 😄 (@rosko01 / @albers / @digitalfog)
Just provided a PR to new annotate plugin location in jaxb-tools (former maven-jaxb2-plugin repository).
This could be fixed soon.
Tested OK

@albers
Copy link
Author

albers commented Sep 23, 2023

Should this be fixed in org.jvnet.jaxb:jaxb-maven-plugin:2.0.9?
In that version, I still face the problem of doubled backslashes.

@laurentschoelens
Copy link
Collaborator

Should this be fixed in org.jvnet.jaxb:jaxb-maven-plugin:2.0.9?
In that version, I still face the problem of doubled backslashes.

Yes this should be fixed in with annotate v2.0.9
Could you share your pom config ?

@laurentschoelens
Copy link
Collaborator

laurentschoelens commented Sep 23, 2023

@albers :
You can follow this migration guide for helping move to v2

@albers
Copy link
Author

albers commented Sep 23, 2023

@laurentschoelens
Thanks for the hint, I found the issue.

I only updated the maven plugin coordinates and was still using org.jvnet.jaxb2_commons:jaxb2-basics-annotate instead of org.jvnet.jaxb:jaxb-basics-annotate.
With that corrected, generation of @Pattern(<some pattern with backslashes>) works perfectly.

@laurentschoelens
Copy link
Collaborator

Cool 👍
We added unit tests for that to check the fix but we still need to publish relocation coordinates in older pom.
Thanks for the confirmation

@laurentschoelens
Copy link
Collaborator

@albers did you manage to get this fixed ?

@albers
Copy link
Author

albers commented Oct 7, 2023

Yes, using the correct dependency, I had no more problems.
Thank you.

With that corrected, generation of @Pattern(<some pattern with backslashes>) works perfectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants