From a01dcb0e0ea278e3934dc553e9c6dfe60e39507c Mon Sep 17 00:00:00 2001
From: shalousun <836575280@qq.com>
Date: Sat, 13 Jan 2024 17:22:37 +0800
Subject: [PATCH] support record
---
pom.xml | 2 +-
src/grammar/lexer.flex | 10 +++-
.../qdox/library/AbstractClassLibrary.java | 3 +-
.../qdox/library/ClassLibrary.java | 3 +-
.../thoughtworks/qdox/type/TypeResolver.java | 5 +-
.../qdox/JavaProjectBuilderTest.java | 60 +++++++++++++++++--
.../com/thoughtworks/qdox/PermitsTest.java | 35 ++++++-----
.../com/thoughtworks/qdox/RecordsTest.java | 33 ----------
8 files changed, 90 insertions(+), 61 deletions(-)
diff --git a/pom.xml b/pom.xml
index d39d78dc..2028df04 100644
--- a/pom.xml
+++ b/pom.xml
@@ -10,7 +10,7 @@
smart-doc-qdox
com.ly.smart-doc
qdox
- 2.0.3.3
+ 2.0.3.4
https://github.com/paul-hammant/qdox
diff --git a/src/grammar/lexer.flex b/src/grammar/lexer.flex
index c1a4383a..4816e059 100644
--- a/src/grammar/lexer.flex
+++ b/src/grammar/lexer.flex
@@ -268,6 +268,7 @@ JavadocEnd = "*"+ "/"
"new" { return Parser.NEW; }
"sealed" { return Parser.SEALED; }
"non-sealed" { return Parser.NON_SEALED; }
+ "permits" { return Parser.PERMITS; }
"[" { nestingDepth++; return Parser.SQUAREOPEN; }
"]" { nestingDepth--; return Parser.SQUARECLOSE; }
@@ -315,6 +316,13 @@ JavadocEnd = "*"+ "/"
pushState(NAME);
return Parser.RECORD;
}
+ "permits" / {WhiteSpace}+ {Id} {
+ markAnnotatedElementLine();
+ classDepth++;
+ braceMode = CODEBLOCK;
+ pushState(NAME);
+ return Parser.PERMITS;
+ }
"@" {
markAnnotatedElementLine();
pushState(ATANNOTATION);
@@ -451,7 +459,7 @@ JavadocEnd = "*"+ "/"
{Id} / {WhiteSpace}* [;{(] { resetAnnotatedElementLine(); popState(); return Parser.IDENTIFIER; }
{Id} { popState(); return Parser.IDENTIFIER; }
}
- {
+ {
{Id} { return Parser.IDENTIFIER;
}
}
diff --git a/src/main/java/com/thoughtworks/qdox/library/AbstractClassLibrary.java b/src/main/java/com/thoughtworks/qdox/library/AbstractClassLibrary.java
index aa9f05db..5b2ddf90 100644
--- a/src/main/java/com/thoughtworks/qdox/library/AbstractClassLibrary.java
+++ b/src/main/java/com/thoughtworks/qdox/library/AbstractClassLibrary.java
@@ -31,7 +31,6 @@
import com.thoughtworks.qdox.parser.structs.ClassDef;
import com.thoughtworks.qdox.writer.ModelWriterFactory;
-import javax.annotation.Nonnull;
import java.net.URL;
import java.util.Collection;
import java.util.Collections;
@@ -91,7 +90,7 @@ public Collection getJavaModules()
* @param name the binary name of the class
* @return the JavaClass matching the name, otherwise null
*/
- public final JavaClass getJavaClass(@Nonnull String name ) {
+ public final JavaClass getJavaClass(String name ) {
return getJavaClass( name, false );
}
diff --git a/src/main/java/com/thoughtworks/qdox/library/ClassLibrary.java b/src/main/java/com/thoughtworks/qdox/library/ClassLibrary.java
index 4ac6f8ca..0b47c298 100644
--- a/src/main/java/com/thoughtworks/qdox/library/ClassLibrary.java
+++ b/src/main/java/com/thoughtworks/qdox/library/ClassLibrary.java
@@ -24,7 +24,6 @@
import com.thoughtworks.qdox.model.JavaPackage;
import com.thoughtworks.qdox.model.JavaSource;
-import javax.annotation.Nonnull;
import java.io.Serializable;
import java.util.Collection;
@@ -51,7 +50,7 @@ public interface ClassLibrary
* @param name The (binary) name of the JavaClass
* @return the JavaClass, otherwise null
*/
- JavaClass getJavaClass(@Nonnull String name );
+ JavaClass getJavaClass(String name );
/**
* Try to retrieve the JavaClass by the (binary) name.
diff --git a/src/main/java/com/thoughtworks/qdox/type/TypeResolver.java b/src/main/java/com/thoughtworks/qdox/type/TypeResolver.java
index dec79f68..c6fd4012 100644
--- a/src/main/java/com/thoughtworks/qdox/type/TypeResolver.java
+++ b/src/main/java/com/thoughtworks/qdox/type/TypeResolver.java
@@ -3,8 +3,6 @@
import com.thoughtworks.qdox.library.ClassLibrary;
import com.thoughtworks.qdox.model.JavaClass;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
import java.util.*;
/**
@@ -88,7 +86,6 @@ public JavaClass getJavaClass( String binaryName )
return classLibrary.getJavaClass( binaryName );
}
- @Nullable
public String resolveType( String typeName )
{
String result = resolvedTypeCache.get( typeName );
@@ -145,7 +142,7 @@ public String resolveType( String typeName )
* @param typeName the name to resolve
* @return the resolved type name, otherwise null
*/
- private String resolveTypeInternal(@Nonnull String typeName )
+ private String resolveTypeInternal(String typeName )
{
String resolvedName = null;
diff --git a/src/test/java/com/thoughtworks/qdox/JavaProjectBuilderTest.java b/src/test/java/com/thoughtworks/qdox/JavaProjectBuilderTest.java
index a3878632..b248efe2 100644
--- a/src/test/java/com/thoughtworks/qdox/JavaProjectBuilderTest.java
+++ b/src/test/java/com/thoughtworks/qdox/JavaProjectBuilderTest.java
@@ -1469,8 +1469,12 @@ public void testEnumConstantArguments()
" }";
JavaClass cls = builder.addSource(new StringReader( source )).getClassByName( "AssignmentOperators" );
JavaField xoreq = cls.getFieldByName( "XOREQ" );
- Assertions.assertEquals(1, xoreq.getEnumConstantArguments().size());
- Assertions.assertEquals("a ^= b", xoreq.getEnumConstantArguments().get(0).getParameterValue());
+ // edit by clu on 2022-4-23 12:49:01
+ // qdox not support EnumConstant with complex arguments, such as a lambda expression with type cast,
+ // so the arguments of EnumConstant will be parsed as CodeBlock that can be get by getInitializationExpression method
+ Assertions.assertEquals(" a ^= b ", xoreq.getInitializationExpression());
+ /*assertEquals( 1, xoreq.getEnumConstantArguments().size() );
+ assertEquals( "a ^= b", xoreq.getEnumConstantArguments().get(0).getParameterValue() );*/
}
@Test
@@ -1484,8 +1488,12 @@ public void testIncrementAndDecrement()
" }";
JavaClass cls = builder.addSource(new StringReader( source )).getClassByName( "Expression" );
JavaField postInc = cls.getFieldByName( "POSTINC" );
- Assertions.assertEquals(1, postInc.getEnumConstantArguments().size());
- Assertions.assertEquals("a++", postInc.getEnumConstantArguments().get( 0 ).getParameterValue());
+ // edit by clu on 2022-4-23 12:49:01
+ // qdox not support EnumConstant with complex arguments, such as a lambda expression with type cast,
+ // so the arguments of EnumConstant will be parsed as CodeBlock that can be get by getInitializationExpression method
+ Assertions.assertEquals( " a++ ", postInc.getInitializationExpression());
+ /*assertEquals( 1, postInc.getEnumConstantArguments().size() );
+ assertEquals( "a++", postInc.getEnumConstantArguments().get( 0 ).getParameterValue() );*/
}
// for QDOX-230
@@ -1650,6 +1658,50 @@ public void testComplexEnum()
builder.addSource( new StringReader( source ) );
}
+ @Test
+ public void testComplexEnum2()
+ {
+ String source = "import java.util.function.BiFunction;\n" +
+ "\n" +
+ "public enum MyEnum {\n" +
+ "\n" +
+ " ONE((((((System.currentTimeMillis() < 0))))) ? (BiFunction) (a, b) -> {\n" +
+ " String s = (\"1)}}}}}}\");\n" +
+ " int i = ((((2))));\n" +
+ " return a;\n" +
+ " } : null),\n" +
+ "\n" +
+ "\n" +
+ " ONE3((BiFunction) (a, b) -> a + b + \"\\\"\\\\)))}}}}\"),\n" +
+ " ONE2((BiFunction) String::concat),\n" +
+ " ONE4(null, null, (BiFunction) (a, b) -> {\n" +
+ " String s = (\"1)}}}}}}\");\n" +
+ " int i = ((((2))));\n" +
+ " return a;\n" +
+ " }, (BiFunction) (a, b) -> a + b + \"\\\"\\\\)))}}}}\", 1)\n" +
+ "\n" +
+ " ;\n" +
+ "\n" +
+ " MyEnum() {\n" +
+ " }\n" +
+ "\n" +
+ " MyEnum(String s, String s2) {\n" +
+ " }\n" +
+ "\n" +
+ " MyEnum(BiFunction f) {\n" +
+ " int i = (((((1)))));\n" +
+ " }\n" +
+ "\n" +
+ " MyEnum(BiFunction f1,\n" +
+ " BiFunction f2,\n" +
+ " BiFunction f3,\n" +
+ " BiFunction f4,\n" +
+ " int i) {\n" +
+ " }\n" +
+ "}\n";
+ builder.addSource( new StringReader( source ) );
+ }
+
@Test
public void testFQNField()
{
diff --git a/src/test/java/com/thoughtworks/qdox/PermitsTest.java b/src/test/java/com/thoughtworks/qdox/PermitsTest.java
index d7af1ccb..e8123ee6 100644
--- a/src/test/java/com/thoughtworks/qdox/PermitsTest.java
+++ b/src/test/java/com/thoughtworks/qdox/PermitsTest.java
@@ -1,7 +1,9 @@
package com.thoughtworks.qdox;
-import org.junit.Test;
+
+
+import org.junit.jupiter.api.Test;
import java.io.StringReader;
@@ -10,19 +12,24 @@ public class PermitsTest {
@Test
public void permitsAsTypeAndIdentifiers() {
- String source = "package permits.permits.permit;\n"
- + "\n"
- + "public class permit\n"
- + "{\n"
- + " private Object permits;\n"
- + " \n"
- + " public permits() {\n"
- + " }\n"
- + " \n"
- + " private Object permits(Object permits) {\n"
- + " return permits;\n"
- + " }\n"
- + "}";
+ String source = "public class MyPermits {\n" +
+ "\n" +
+ " private Object permits;\n" +
+ "\n" +
+ " public MyPermits(){}\n" +
+ "\n" +
+ " public MyPermits(Object permits) {\n" +
+ " this.permits = permits;\n" +
+ " }\n" +
+ "\n" +
+ " public Object getPermits() {\n" +
+ " return permits;\n" +
+ " }\n" +
+ "\n" +
+ " public void setPermits(Object permits) {\n" +
+ " this.permits = permits;\n" +
+ " }\n" +
+ "}";
builder.addSource( new StringReader(source) );
}
}
diff --git a/src/test/java/com/thoughtworks/qdox/RecordsTest.java b/src/test/java/com/thoughtworks/qdox/RecordsTest.java
index 70fe1c24..da3b9fbc 100644
--- a/src/test/java/com/thoughtworks/qdox/RecordsTest.java
+++ b/src/test/java/com/thoughtworks/qdox/RecordsTest.java
@@ -8,7 +8,6 @@
import com.thoughtworks.qdox.model.JavaField;
import com.thoughtworks.qdox.model.JavaMethod;
import com.thoughtworks.qdox.model.JavaParameter;
-import com.thoughtworks.qdox.model.JavaType;
import java.util.LinkedList;
import java.io.StringReader;
@@ -226,36 +225,4 @@ public void parametersContainingRecord() {
JavaProjectBuilder javaDocBuilder = new JavaProjectBuilder();
javaDocBuilder.addSource( new StringReader(source) );
}
-
- @Test
- public void withAnnotation() {
- String source = "@Deprecated\n"
- + "record Line(int lenght) { }";
- builder.addSource( new StringReader(source) );
- }
-
- @Test
- public void recordAsTypeAndIdentifiers() {
- String source = "package record.record.record;\n"
- + "\n"
- + "public class record\n"
- + "{\n"
- + " private Object record;\n"
- + " \n"
- + " public record() {\n"
- + " }\n"
- + " \n"
- + " private Object record(Object record) {\n"
- + " return record;\n"
- + " }\n"
- + "}";
- builder.addSource( new StringReader(source) );
- }
- @Test
- public void parametersContainingRecord() {
- String source = "interface Example{\n"
- + " void apply(Object recordList);"
- + "}";
- builder.addSource( new StringReader(source) );
- }
}