more improvements to generated Javadoc in Hibernate Processor

This commit is contained in:
Gavin King 2024-10-02 19:53:25 +02:00
parent cf626df3db
commit f474cacf8a
10 changed files with 89 additions and 83 deletions

View File

@ -103,7 +103,8 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
final List<MetaAttribute> members = entity.getMembers();
for ( MetaAttribute metaMember : members ) {
if ( metaMember.hasStringAttribute() ) {
pw.println( '\t' + metaMember.getAttributeNameDeclarationString() );
metaMember.getAttributeNameDeclarationString().lines()
.forEach(line -> pw.println('\t' + line));
}
}

View File

@ -150,7 +150,7 @@ private String importTypes(String originalArgList) {
StringBuilder acc = new StringBuilder();
StringTokenizer args = new StringTokenizer( originalArgList, "," );
while ( args.hasMoreTokens() ) {
if ( acc.length() > 0 ) {
if ( !acc.isEmpty() ) {
acc.append( ',' );
}
acc.append( args.nextToken() );
@ -166,7 +166,7 @@ private String importTypes(String originalArgList) {
}
}
if ( nesting == 0 ) {
if ( argList.length() > 0 ) {
if ( !argList.isEmpty() ) {
argList.append(',');
}
argList.append( importType( acc.toString() ) );
@ -177,16 +177,10 @@ private String importTypes(String originalArgList) {
}
public String staticImport(String fqcn, String member) {
String local = fqcn + "." + member;
final String local = fqcn + "." + member;
imports.add( local );
staticImports.add( local );
if ( member.equals( "*" ) ) {
return "";
}
else {
return member;
}
return "*".equals(member) ? "" : member;
}
private boolean inDefaultPackage(String className) {
@ -206,8 +200,7 @@ private boolean inJavaLang(String className) {
}
public String generateImports() {
StringBuilder builder = new StringBuilder();
final StringBuilder builder = new StringBuilder();
for ( String next : imports ) {
// don't add automatically "imported" stuff
if ( !isAutoImported( next ) ) {
@ -219,15 +212,14 @@ public String generateImports() {
}
}
}
if ( builder.indexOf( "$" ) >= 0 ) {
return builder.toString();
}
return builder.toString();
}
private boolean isAutoImported(String next) {
return isPrimitive( next ) || inDefaultPackage( next ) || inJavaLang( next ) || inSamePackage( next );
return isPrimitive( next )
|| inDefaultPackage( next )
|| inJavaLang( next )
|| inSamePackage( next );
}
public static String unqualify(String qualifiedName) {

View File

@ -5,14 +5,11 @@
package org.hibernate.processor.annotation;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.hibernate.internal.util.StringHelper;
import javax.lang.model.element.ExecutableElement;
import java.util.List;
import java.util.Locale;
import static org.hibernate.processor.util.Constants.HIB_SESSION;
import static org.hibernate.processor.util.StringUtil.getUpperUnderscoreCaseFromLowerCamelCase;
/**
* @author Gavin King
@ -70,27 +67,7 @@ public String getTypeDeclaration() {
@Override
public String getAttributeNameDeclarationString() {
return new StringBuilder()
.append("public static final String ")
.append(constantName())
.append(" = \"!")
.append(annotationMetaEntity.getQualifiedName())
.append('.')
.append(methodName)
.append("(")
.append(parameterList())
.append(")")
.append("\";")
.toString();
}
String constantName() {
return getUpperUnderscoreCaseFromLowerCamelCase(methodName) + "_BY_"
+ paramNames.stream()
.map(StringHelper::unqualify)
.map(name -> name.toUpperCase(Locale.ROOT))
.reduce((x,y) -> x + "_AND_" + y)
.orElse("");
throw new UnsupportedOperationException("operation not supported");
}
void comment(StringBuilder declaration) {
@ -188,20 +165,6 @@ void tryReturn(StringBuilder declaration) {
.append(sessionName);
}
// private void returnType(StringBuilder declaration) {
// if ( isReactive() ) {
// declaration
// .append(annotationMetaEntity.importType(UNI))
// .append('<');
// }
// declaration
// .append(annotationMetaEntity.importType(entity));
// if ( isReactive() ) {
// declaration
// .append('>');
// }
// }
void modifiers(StringBuilder declaration) {
declaration
.append(belongsToDao ? "@Override\npublic " : "public static ");

View File

@ -105,10 +105,21 @@ String parameterList() {
.orElse("");
}
String strip(String type) {
int index = type.indexOf("<");
String stripped = index > 0 ? type.substring(0, index) : type;
return type.endsWith("...") ? stripped + "..." : stripped;
String strip(final String fullType) {
String type = fullType;
// strip off type annotations
while ( type.charAt(0) == '@' ) {
int startIndex = type.indexOf( ' ' );
if ( startIndex > 0 ) {
type = type.substring(startIndex+1);
}
}
// strip off type arguments
int endIndex = type.indexOf("<");
if ( endIndex > 0 ) {
type = type.substring(0, endIndex);
}
return fullType.endsWith("...") ? type + "..." : type;
}
void preamble(StringBuilder declaration, List<String> paramTypes) {
@ -175,7 +186,12 @@ void see(StringBuilder declaration) {
declaration
.append("\n * @see ")
.append(annotationMetaEntity.getQualifiedName())
.append("#")
.append("#");
signature(declaration);
}
void signature(StringBuilder declaration) {
declaration
.append(methodName)
.append("(")
.append(parameterList())

View File

@ -44,11 +44,11 @@ public boolean hasStringAttribute() {
@Override
public String getAttributeDeclarationString() {
return new StringBuilder()
.append("\n/**\n * @see ")
.append("\n/**\n * Static metamodel for attribute {@link ")
.append(parent.getQualifiedName())
.append('#')
.append(element.getSimpleName())
.append("\n **/\n")
.append("}\n **/\n")
.append("public static volatile ")
.append(parent.importType(getMetaType()))
.append('<')
@ -65,6 +65,10 @@ public String getAttributeDeclarationString() {
@Override
public String getAttributeNameDeclarationString(){
return new StringBuilder()
.append("\n/**\n * @see ")
.append("#")
.append(getPropertyName())
.append( "\n **/\n" )
.append("public static final ")
.append(parent.importType(String.class.getName()))
.append(' ')

View File

@ -24,11 +24,11 @@ public AnnotationMetaMap(AnnotationMetaEntity parent, Element element, String co
@Override
public String getAttributeDeclarationString() {
return new StringBuilder()
.append("\n/**\n * @see ")
.append("\n/**\n * Static metamodel for attribute {@link ")
.append( parent.getQualifiedName() )
.append("#")
.append( element.getSimpleName() )
.append("\n **/\n")
.append("}\n **/\n")
.append("public static volatile ")
.append( parent.importType( getMetaType() ) )
.append("<")

View File

@ -35,9 +35,9 @@ public boolean hasStringAttribute() {
@Override
public String getAttributeDeclarationString() {
return new StringBuilder()
.append("\n/**\n * @see ")
.append("\n/**\n * Static metamodel type for {@link ")
.append( annotationMetaEntity.getQualifiedName() )
.append( "\n **/\n" )
.append( "}\n **/\n" )
.append("public static volatile ")
.append(annotationMetaEntity.importType(getTypeDeclaration()))
.append("<")

View File

@ -56,12 +56,11 @@ public String getAttributeDeclarationString() {
? parent.importType("jakarta.data.metamodel.impl.TextAttributeRecord")
: parent.importType("jakarta.data.metamodel.impl.SortableAttributeRecord");
return new StringBuilder()
.append("\n/**\n * @see ")
.append("\n/**\n * Static metamodel for attribute {@link ")
.append(className)
.append( "#")
.append("#")
.append(memberName)
.append( "\n **/\n" )
// .append( "public static final " )
.append( "}\n **/\n" )
.append( parent.importType( getMetaType() ) )
.append( "<" )
.append( className )
@ -78,7 +77,10 @@ public String getAttributeDeclarationString() {
@Override
public String getAttributeNameDeclarationString(){
return new StringBuilder()
// .append("public static final ")
.append("\n/**\n * @see ")
.append("#")
.append( getPropertyName().replace('.','_') )
.append( "\n **/\n" )
.append(parent.importType(String.class.getName()))
.append(" ")
.append(fieldName())

View File

@ -268,7 +268,13 @@ private void modifiers(List<String> paramTypes, StringBuilder declaration) {
@Override
public String getAttributeNameDeclarationString() {
StringBuilder sb = new StringBuilder(queryString.length() + 100)
StringBuilder declaration = new StringBuilder( queryString.length() + 200 );
declaration
.append("\n/**\n * @see ")
.append("#");
signature( declaration );
declaration
.append( "\n **/\n" )
.append( "static final String " )
.append( getConstantName() )
.append( " = \"" );
@ -276,23 +282,23 @@ public String getAttributeNameDeclarationString() {
final char c = queryString.charAt( i );
switch ( c ) {
case '\r':
sb.append( "\\r" );
declaration.append( "\\r" );
break;
case '\n':
sb.append( "\\n" );
declaration.append( "\\n" );
break;
case '\\':
sb.append( "\\\\" );
declaration.append( "\\\\" );
break;
case '"':
sb.append( "\\\"" );
declaration.append( "\\\"" );
break;
default:
sb.append( c );
declaration.append( c );
break;
}
}
return sb.append("\";").toString();
return declaration.append("\";").toString();
}
private String getConstantName() {

View File

@ -39,9 +39,26 @@ public boolean hasTypedAttribute() {
return true;
}
private boolean isQuery() {
return "QUERY_".equals(prefix); //UGLY!
}
@Override
public String getAttributeNameDeclarationString() {
StringBuilder declaration = new StringBuilder();
declaration
.append("\n/**\n * @see ")
.append("#");
appendFieldName( declaration, isQuery() );
return declaration
.append( "\n **/\n" )
.append(super.getAttributeNameDeclarationString())
.toString();
}
@Override
public String getAttributeDeclarationString() {
final boolean isQuery = "QUERY_".equals(prefix); //UGLY!
final boolean isQuery = isQuery();
final Metamodel entity = getHostingEntity();
final StringBuilder declaration = new StringBuilder();
declaration
@ -67,13 +84,18 @@ public String getAttributeDeclarationString() {
.append('<')
.append(entity.importType(resultType))
.append('>')
.append(' ')
.append(' ');
appendFieldName( declaration, isQuery );
declaration.append(';');
return declaration.toString();
}
private void appendFieldName(StringBuilder declaration, boolean isQuery) {
declaration
.append('_')
.append(nameToMethodName(getPropertyName()));
if ( isQuery ) {
declaration.append('_');
}
declaration.append(';');
return declaration.toString();
}
}