From 763ffbec4c64373af59c6ecb161be55ce7f52f1e Mon Sep 17 00:00:00 2001 From: shalousun <836575280@qq.com> Date: Mon, 25 Nov 2024 20:59:29 +0800 Subject: [PATCH] feat: Support textblock in Annotations --- pom.xml | 339 +++++++----------- src/grammar/lexer.flex | 2 + src/grammar/parser.y | 10 +- .../thoughtworks/qdox/AnnotationsTest.java | 32 ++ 4 files changed, 180 insertions(+), 203 deletions(-) diff --git a/pom.xml b/pom.xml index 31fd6e60..c20c242e 100644 --- a/pom.xml +++ b/pom.xml @@ -134,17 +134,17 @@ org.apache.maven.plugins maven-clean-plugin - 2.4.1 + 3.4.0 org.apache.maven.plugins maven-compiler-plugin - 3.11.0 + 3.13.0 org.apache.maven.plugins maven-surefire-plugin - 3.2.3 + 3.5.2 false @@ -152,29 +152,22 @@ org.apache.maven.plugins maven-jar-plugin - 3.3.0 - - - - com.thoughtworks.qdox - - - + 3.4.2 org.apache.maven.plugins maven-install-plugin - 3.1.1 + 3.1.3 org.apache.maven.plugins maven-deploy-plugin - 3.1.1 + 3.1.3 org.apache.maven.plugins maven-site-plugin - 3.8.2 + 3.21.0 ${basedir}/src/site/templates/site.vm @@ -182,7 +175,7 @@ org.apache.maven.plugins maven-release-plugin - 3.0.1 + 3.1.1 @@ -190,7 +183,7 @@ org.apache.maven.plugins maven-enforcer-plugin - 1.3.1 + 3.5.0 enforce-maven @@ -221,10 +214,37 @@ + + org.apache.maven.plugins + maven-compiler-plugin + + + default-compile + + 8 + + module-info.java + + + + + module-descriptor + + compile + + + 11 + + module-info.java + + + + + de.jflex jflex-maven-plugin - 1.8.2 + 1.9.1 @@ -243,7 +263,7 @@ org.codehaus.mojo exec-maven-plugin - 3.1.1 + 3.5.0 javacommentparser @@ -288,31 +308,10 @@ - - org.codehaus.mojo - animal-sniffer-maven-plugin - 1.7 - - - check-java15 - process-classes - - check - - - - org.codehaus.mojo.signature - java18 - 1.0 - - - - - org.apache.maven.plugins maven-failsafe-plugin - 2.22.1 + 3.5.2 @@ -325,7 +324,7 @@ org.apache.maven.plugins maven-invoker-plugin - 3.6.0 + 3.8.1 ${project.build.directory}/it verify @@ -354,7 +353,7 @@ org.apache.maven.plugins maven-assembly-plugin - 3.6.0 + 3.7.1 project @@ -371,14 +370,11 @@ - - org.apache.maven.plugins - maven-compiler-plugin - - 8 - 8 - - + + org.apache.maven.plugins + maven-gpg-plugin + 3.1.0 + @@ -386,13 +382,13 @@ org.junit.jupiter junit-jupiter - 5.10.0 + 5.11.3 test org.assertj assertj-core - 3.11.1 + 3.26.3 test @@ -403,152 +399,93 @@ - - - release - - - - org.apache.maven.plugins - maven-gpg-plugin - 3.0.1 - - - sign-artifacts - verify - - sign - - - - - - - org.apache.maven.plugins - maven-source-plugin - 3.2.1 - - - package - - jar-no-fork - - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 3.4.0 - - - package - - jar - - - - - - - - - oss01 - https://s01.oss.sonatype.org/content/repositories/snapshots/ - - - oss01 - https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ - - - - - linux - - - Linux - i386 - - - - ${basedir}/bootstrap/yacc.linux - - - - linux64 - - - Linux - amd64 - - - - ${basedir}/bootstrap/yacc.linux.x86_64 - - - - solaris - - - SunOS - - - - ${basedir}/bootstrap/yacc.solaris - - - - macosx-i386 - - - Mac - i386 - - - - ${basedir}/bootstrap/yacc.macosx - - - - macosx-powerpc - - - Mac - powerpc - - - - ${basedir}/bootstrap/yacc.macosx - - - - macosx-aarch64 - - - Mac - aarch64 - - - - ${basedir}/bootstrap/yacc.macosx.aarch64 - - - - windows - - - Windows - - - - ${basedir}/bootstrap/yacc.exe - - - - + + + linux + + + Linux + i386 + + + + ${basedir}/bootstrap/yacc.linux + + + + linux64 + + + Linux + amd64 + + + + ${basedir}/bootstrap/yacc.linux.x86_64 + + + + solaris + + + SunOS + + + + ${basedir}/bootstrap/yacc.solaris + + + + macosx-i386 + + + Mac + i386 + + + + ${basedir}/bootstrap/yacc.macosx + + + + macosx-powerpc + + + Mac + powerpc + + + + ${basedir}/bootstrap/yacc.macosx + + + + macosx-aarch64 + + + Mac + aarch64 + + + + ${basedir}/bootstrap/yacc.macosx.aarch64 + + + + windows + + + Windows + + + + ${basedir}/bootstrap/yacc.exe + + + + @@ -556,7 +493,7 @@ org.apache.maven.plugins maven-project-info-reports-plugin - 3.5.0 + 3.8.0 @@ -574,7 +511,7 @@ org.apache.maven.plugins maven-jxr-plugin - 2.3 + 3.6.0 @@ -591,7 +528,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 3.6.3 + 3.11.1 com.thoughtworks.qdox.* diff --git a/src/grammar/lexer.flex b/src/grammar/lexer.flex index 904bbd22..7f33b79c 100644 --- a/src/grammar/lexer.flex +++ b/src/grammar/lexer.flex @@ -546,6 +546,7 @@ JavadocEnd = "*"+ "/" "++" { return Parser.PLUSPLUS; } "--" { return Parser.MINUSMINUS; } + "\"\"\"" { appendingToCodeBody=true; codeBody.append("\"\"\""); pushState(ANNOSTRING); } "\"" { appendingToCodeBody=true; codeBody.append("\""); pushState(ANNOSTRING); } "\'" { appendingToCodeBody=true; codeBody.append("\'"); pushState(ANNOCHAR); } @@ -600,6 +601,7 @@ JavadocEnd = "*"+ "/" } { + "\"\"\"" { codeBody.append("\"\"\""); popState(); appendingToCodeBody=false; return Parser.TEXTBLOCK; } "\"" { codeBody.append("\""); popState(); appendingToCodeBody=false; return Parser.STRING_LITERAL; } "\\\"" { codeBody.append("\\\""); } "\\\\" { codeBody.append("\\\\"); } diff --git a/src/grammar/parser.y b/src/grammar/parser.y index afc2189c..d57aba77 100644 --- a/src/grammar/parser.y +++ b/src/grammar/parser.y @@ -49,7 +49,7 @@ import java.util.Stack; %token INTEGER_LITERAL %token FLOAT_LITERAL %token CHAR_LITERAL -%token STRING_LITERAL +%token STRING_LITERAL, TEXTBLOCK %token VERTLINE2 AMPERSAND2 VERTLINE CIRCUMFLEX AMPERSAND EQUALS2 NOTEQUALS %token LESSTHAN GREATERTHAN LESSEQUALS GREATEREQUALS LESSTHAN2 GREATERTHAN2 GREATERTHAN3 %token PLUS MINUS STAR SLASH PERCENT TILDE EXCLAMATION @@ -783,6 +783,7 @@ RecordComponent: Annotations_opt /* ={RecordComponentModifier} */ Type /* =Unann param.setName($3); param.setDimensions(0); param.setVarArgs(false); + param.setLineNumber(lexer.getLine()); builder.addParameter(param); recordHeaderStack.getFirst().addField(param); param = new FieldDef(); @@ -798,6 +799,7 @@ VariableArityRecordComponent: Annotations_opt /* ={RecordComponentModifier} */ T param.setName($4); param.setDimensions(0); param.setVarArgs(true); + param.setLineNumber(lexer.getLine()); builder.addParameter(param); recordHeaderStack.getFirst().addField(param); param = new FieldDef(); @@ -2030,7 +2032,11 @@ Literal: INTEGER_LITERAL { String s = lexer.getCodeBody(); $$ = new ConstantDef(s, Character.class); - } + } + | TEXTBLOCK { + String s = lexer.getCodeBody(); + $$ = new ConstantDef(s, String.class); + } | STRING_LITERAL { String s = lexer.getCodeBody(); diff --git a/src/test/java/com/thoughtworks/qdox/AnnotationsTest.java b/src/test/java/com/thoughtworks/qdox/AnnotationsTest.java index 257dd8f3..b7cc87e8 100644 --- a/src/test/java/com/thoughtworks/qdox/AnnotationsTest.java +++ b/src/test/java/com/thoughtworks/qdox/AnnotationsTest.java @@ -253,4 +253,36 @@ public void testDoubleEscapedString() String source = "public class Foo {\n" + "@SuppressWarnings({\"abc\\\\d\"})\n" + "private void bar() { } }"; builder.addSource( new StringReader( source ) ); } + + @Test + public void testTextBlock() { + JavaProjectBuilder builder = new JavaProjectBuilder(); + String source = "@SuppressWarnings(\"\"\"\n" + + " Lorem ipsum dolor sit amet\n" + + " \"\"\")\n" + + "public class Thingy {\n" + + "}"; + builder.addSource( new StringReader( source ) ); + Assertions.assertEquals("\"\"\"\n" + + " Lorem ipsum dolor sit amet\n" + + " \"\"\"", builder.getClassByName( "Thingy" ).getAnnotations().get( 0 ).getProperty( "value" ).getParameterValue()); + + } + + @Test + public void testValueArray() { + JavaProjectBuilder builder = new JavaProjectBuilder(); + String source = "@Fields({\n" + + " @Field(norms = Norms.NO, analyzer = @Analyzer(definition = \"whitespace_analyzer\")),\n" + + " })"; + builder.addSource( new StringReader( source ) ); + } + + @Test + public void testFQNAnnotation() { + JavaProjectBuilder builder = new JavaProjectBuilder(); + String source = "@Type(type = \"com.example.Type\", parameters = @com.example.Parameter(name = \"class\", value = \"com.example.Object\"))"; + builder.addSource( new StringReader( source ) ); + } + }