Skip to content

Commit f474cac

Browse files
committed
more improvements to generated Javadoc in Hibernate Processor
1 parent cf626df commit f474cac

File tree

10 files changed

+89
-83
lines changed

10 files changed

+89
-83
lines changed

tooling/metamodel-generator/src/main/java/org/hibernate/processor/ClassWriter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
103103
final List<MetaAttribute> members = entity.getMembers();
104104
for ( MetaAttribute metaMember : members ) {
105105
if ( metaMember.hasStringAttribute() ) {
106-
pw.println( '\t' + metaMember.getAttributeNameDeclarationString() );
106+
metaMember.getAttributeNameDeclarationString().lines()
107+
.forEach(line -> pw.println('\t' + line));
107108
}
108109
}
109110

tooling/metamodel-generator/src/main/java/org/hibernate/processor/ImportContextImpl.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private String importTypes(String originalArgList) {
150150
StringBuilder acc = new StringBuilder();
151151
StringTokenizer args = new StringTokenizer( originalArgList, "," );
152152
while ( args.hasMoreTokens() ) {
153-
if ( acc.length() > 0 ) {
153+
if ( !acc.isEmpty() ) {
154154
acc.append( ',' );
155155
}
156156
acc.append( args.nextToken() );
@@ -166,7 +166,7 @@ private String importTypes(String originalArgList) {
166166
}
167167
}
168168
if ( nesting == 0 ) {
169-
if ( argList.length() > 0 ) {
169+
if ( !argList.isEmpty() ) {
170170
argList.append(',');
171171
}
172172
argList.append( importType( acc.toString() ) );
@@ -177,16 +177,10 @@ private String importTypes(String originalArgList) {
177177
}
178178

179179
public String staticImport(String fqcn, String member) {
180-
String local = fqcn + "." + member;
180+
final String local = fqcn + "." + member;
181181
imports.add( local );
182182
staticImports.add( local );
183-
184-
if ( member.equals( "*" ) ) {
185-
return "";
186-
}
187-
else {
188-
return member;
189-
}
183+
return "*".equals(member) ? "" : member;
190184
}
191185

192186
private boolean inDefaultPackage(String className) {
@@ -206,8 +200,7 @@ private boolean inJavaLang(String className) {
206200
}
207201

208202
public String generateImports() {
209-
StringBuilder builder = new StringBuilder();
210-
203+
final StringBuilder builder = new StringBuilder();
211204
for ( String next : imports ) {
212205
// don't add automatically "imported" stuff
213206
if ( !isAutoImported( next ) ) {
@@ -219,15 +212,14 @@ public String generateImports() {
219212
}
220213
}
221214
}
222-
223-
if ( builder.indexOf( "$" ) >= 0 ) {
224-
return builder.toString();
225-
}
226215
return builder.toString();
227216
}
228217

229218
private boolean isAutoImported(String next) {
230-
return isPrimitive( next ) || inDefaultPackage( next ) || inJavaLang( next ) || inSamePackage( next );
219+
return isPrimitive( next )
220+
|| inDefaultPackage( next )
221+
|| inJavaLang( next )
222+
|| inSamePackage( next );
231223
}
232224

233225
public static String unqualify(String qualifiedName) {

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractFinderMethod.java

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@
55
package org.hibernate.processor.annotation;
66

77
import org.checkerframework.checker.nullness.qual.Nullable;
8-
import org.hibernate.internal.util.StringHelper;
98

109
import javax.lang.model.element.ExecutableElement;
1110
import java.util.List;
12-
import java.util.Locale;
1311

1412
import static org.hibernate.processor.util.Constants.HIB_SESSION;
15-
import static org.hibernate.processor.util.StringUtil.getUpperUnderscoreCaseFromLowerCamelCase;
1613

1714
/**
1815
* @author Gavin King
@@ -70,27 +67,7 @@ public String getTypeDeclaration() {
7067

7168
@Override
7269
public String getAttributeNameDeclarationString() {
73-
return new StringBuilder()
74-
.append("public static final String ")
75-
.append(constantName())
76-
.append(" = \"!")
77-
.append(annotationMetaEntity.getQualifiedName())
78-
.append('.')
79-
.append(methodName)
80-
.append("(")
81-
.append(parameterList())
82-
.append(")")
83-
.append("\";")
84-
.toString();
85-
}
86-
87-
String constantName() {
88-
return getUpperUnderscoreCaseFromLowerCamelCase(methodName) + "_BY_"
89-
+ paramNames.stream()
90-
.map(StringHelper::unqualify)
91-
.map(name -> name.toUpperCase(Locale.ROOT))
92-
.reduce((x,y) -> x + "_AND_" + y)
93-
.orElse("");
70+
throw new UnsupportedOperationException("operation not supported");
9471
}
9572

9673
void comment(StringBuilder declaration) {
@@ -188,20 +165,6 @@ void tryReturn(StringBuilder declaration) {
188165
.append(sessionName);
189166
}
190167

191-
// private void returnType(StringBuilder declaration) {
192-
// if ( isReactive() ) {
193-
// declaration
194-
// .append(annotationMetaEntity.importType(UNI))
195-
// .append('<');
196-
// }
197-
// declaration
198-
// .append(annotationMetaEntity.importType(entity));
199-
// if ( isReactive() ) {
200-
// declaration
201-
// .append('>');
202-
// }
203-
// }
204-
205168
void modifiers(StringBuilder declaration) {
206169
declaration
207170
.append(belongsToDao ? "@Override\npublic " : "public static ");

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractQueryMethod.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,21 @@ String parameterList() {
105105
.orElse("");
106106
}
107107

108-
String strip(String type) {
109-
int index = type.indexOf("<");
110-
String stripped = index > 0 ? type.substring(0, index) : type;
111-
return type.endsWith("...") ? stripped + "..." : stripped;
108+
String strip(final String fullType) {
109+
String type = fullType;
110+
// strip off type annotations
111+
while ( type.charAt(0) == '@' ) {
112+
int startIndex = type.indexOf( ' ' );
113+
if ( startIndex > 0 ) {
114+
type = type.substring(startIndex+1);
115+
}
116+
}
117+
// strip off type arguments
118+
int endIndex = type.indexOf("<");
119+
if ( endIndex > 0 ) {
120+
type = type.substring(0, endIndex);
121+
}
122+
return fullType.endsWith("...") ? type + "..." : type;
112123
}
113124

114125
void preamble(StringBuilder declaration, List<String> paramTypes) {
@@ -175,7 +186,12 @@ void see(StringBuilder declaration) {
175186
declaration
176187
.append("\n * @see ")
177188
.append(annotationMetaEntity.getQualifiedName())
178-
.append("#")
189+
.append("#");
190+
signature(declaration);
191+
}
192+
193+
void signature(StringBuilder declaration) {
194+
declaration
179195
.append(methodName)
180196
.append("(")
181197
.append(parameterList())

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaAttribute.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ public boolean hasStringAttribute() {
4444
@Override
4545
public String getAttributeDeclarationString() {
4646
return new StringBuilder()
47-
.append("\n/**\n * @see ")
47+
.append("\n/**\n * Static metamodel for attribute {@link ")
4848
.append(parent.getQualifiedName())
4949
.append('#')
5050
.append(element.getSimpleName())
51-
.append("\n **/\n")
51+
.append("}\n **/\n")
5252
.append("public static volatile ")
5353
.append(parent.importType(getMetaType()))
5454
.append('<')
@@ -65,6 +65,10 @@ public String getAttributeDeclarationString() {
6565
@Override
6666
public String getAttributeNameDeclarationString(){
6767
return new StringBuilder()
68+
.append("\n/**\n * @see ")
69+
.append("#")
70+
.append(getPropertyName())
71+
.append( "\n **/\n" )
6872
.append("public static final ")
6973
.append(parent.importType(String.class.getName()))
7074
.append(' ')

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaMap.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ public AnnotationMetaMap(AnnotationMetaEntity parent, Element element, String co
2424
@Override
2525
public String getAttributeDeclarationString() {
2626
return new StringBuilder()
27-
.append("\n/**\n * @see ")
27+
.append("\n/**\n * Static metamodel for attribute {@link ")
2828
.append( parent.getQualifiedName() )
2929
.append("#")
3030
.append( element.getSimpleName() )
31-
.append("\n **/\n")
31+
.append("}\n **/\n")
3232
.append("public static volatile ")
3333
.append( parent.importType( getMetaType() ) )
3434
.append("<")

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public boolean hasStringAttribute() {
3535
@Override
3636
public String getAttributeDeclarationString() {
3737
return new StringBuilder()
38-
.append("\n/**\n * @see ")
38+
.append("\n/**\n * Static metamodel type for {@link ")
3939
.append( annotationMetaEntity.getQualifiedName() )
40-
.append( "\n **/\n" )
40+
.append( "}\n **/\n" )
4141
.append("public static volatile ")
4242
.append(annotationMetaEntity.importType(getTypeDeclaration()))
4343
.append("<")

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/DataAnnotationMetaAttribute.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,11 @@ public String getAttributeDeclarationString() {
5656
? parent.importType("jakarta.data.metamodel.impl.TextAttributeRecord")
5757
: parent.importType("jakarta.data.metamodel.impl.SortableAttributeRecord");
5858
return new StringBuilder()
59-
.append("\n/**\n * @see ")
59+
.append("\n/**\n * Static metamodel for attribute {@link ")
6060
.append(className)
61-
.append( "#")
61+
.append("#")
6262
.append(memberName)
63-
.append( "\n **/\n" )
64-
// .append( "public static final " )
63+
.append( "}\n **/\n" )
6564
.append( parent.importType( getMetaType() ) )
6665
.append( "<" )
6766
.append( className )
@@ -78,7 +77,10 @@ public String getAttributeDeclarationString() {
7877
@Override
7978
public String getAttributeNameDeclarationString(){
8079
return new StringBuilder()
81-
// .append("public static final ")
80+
.append("\n/**\n * @see ")
81+
.append("#")
82+
.append( getPropertyName().replace('.','_') )
83+
.append( "\n **/\n" )
8284
.append(parent.importType(String.class.getName()))
8385
.append(" ")
8486
.append(fieldName())

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/QueryMethod.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -268,31 +268,37 @@ private void modifiers(List<String> paramTypes, StringBuilder declaration) {
268268

269269
@Override
270270
public String getAttributeNameDeclarationString() {
271-
StringBuilder sb = new StringBuilder(queryString.length() + 100)
271+
StringBuilder declaration = new StringBuilder( queryString.length() + 200 );
272+
declaration
273+
.append("\n/**\n * @see ")
274+
.append("#");
275+
signature( declaration );
276+
declaration
277+
.append( "\n **/\n" )
272278
.append( "static final String " )
273279
.append( getConstantName() )
274280
.append( " = \"" );
275281
for ( int i = 0; i < queryString.length(); i++ ) {
276282
final char c = queryString.charAt( i );
277283
switch ( c ) {
278284
case '\r':
279-
sb.append( "\\r" );
285+
declaration.append( "\\r" );
280286
break;
281287
case '\n':
282-
sb.append( "\\n" );
288+
declaration.append( "\\n" );
283289
break;
284290
case '\\':
285-
sb.append( "\\\\" );
291+
declaration.append( "\\\\" );
286292
break;
287293
case '"':
288-
sb.append( "\\\"" );
294+
declaration.append( "\\\"" );
289295
break;
290296
default:
291-
sb.append( c );
297+
declaration.append( c );
292298
break;
293299
}
294300
}
295-
return sb.append("\";").toString();
301+
return declaration.append("\";").toString();
296302
}
297303

298304
private String getConstantName() {

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/TypedMetaAttribute.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,26 @@ public boolean hasTypedAttribute() {
3939
return true;
4040
}
4141

42+
private boolean isQuery() {
43+
return "QUERY_".equals(prefix); //UGLY!
44+
}
45+
46+
@Override
47+
public String getAttributeNameDeclarationString() {
48+
StringBuilder declaration = new StringBuilder();
49+
declaration
50+
.append("\n/**\n * @see ")
51+
.append("#");
52+
appendFieldName( declaration, isQuery() );
53+
return declaration
54+
.append( "\n **/\n" )
55+
.append(super.getAttributeNameDeclarationString())
56+
.toString();
57+
}
58+
4259
@Override
4360
public String getAttributeDeclarationString() {
44-
final boolean isQuery = "QUERY_".equals(prefix); //UGLY!
61+
final boolean isQuery = isQuery();
4562
final Metamodel entity = getHostingEntity();
4663
final StringBuilder declaration = new StringBuilder();
4764
declaration
@@ -67,13 +84,18 @@ public String getAttributeDeclarationString() {
6784
.append('<')
6885
.append(entity.importType(resultType))
6986
.append('>')
70-
.append(' ')
87+
.append(' ');
88+
appendFieldName( declaration, isQuery );
89+
declaration.append(';');
90+
return declaration.toString();
91+
}
92+
93+
private void appendFieldName(StringBuilder declaration, boolean isQuery) {
94+
declaration
7195
.append('_')
7296
.append(nameToMethodName(getPropertyName()));
7397
if ( isQuery ) {
7498
declaration.append('_');
7599
}
76-
declaration.append(';');
77-
return declaration.toString();
78100
}
79101
}

0 commit comments

Comments
 (0)