cleanup processor StringUtil

This commit is contained in:
Gavin King 2024-09-21 11:10:10 +02:00
parent 09c627c0b2
commit d53498910a
3 changed files with 63 additions and 65 deletions

View File

@ -14,90 +14,83 @@ import static java.lang.Character.toUpperCase;
* @author Hardy Ferentschik
*/
public final class StringUtil {
private static final String NAME_SEPARATOR = ".";
private static final String PROPERTY_PREFIX_GET = "get";
private static final String PROPERTY_PREFIX_IS = "is";
private static final String PROPERTY_PREFIX_HAS = "has";
private static final String GET = "get";
private static final String IS = "is";
private static final String HAS = "has";
private StringUtil() {
}
public static String determineFullyQualifiedClassName(String defaultPackage, String name) {
if ( isFullyQualified( name ) ) {
return name;
}
else {
return defaultPackage + NAME_SEPARATOR + name;
}
return isFullyQualified( name ) ? name : defaultPackage + "." + name;
}
public static boolean isFullyQualified(String name) {
return name.contains( NAME_SEPARATOR );
return name.contains(".");
}
public static String packageNameFromFqcn(String fqcn) {
return fqcn.substring( 0, fqcn.lastIndexOf( NAME_SEPARATOR ) );
public static String packageNameFromFullyQualifiedName(String fullyQualifiedName) {
return fullyQualifiedName.substring( 0, fullyQualifiedName.lastIndexOf(".") );
}
public static String classNameFromFqcn(String fqcn) {
return fqcn.substring( fqcn.lastIndexOf( NAME_SEPARATOR ) + 1 );
public static String classNameFromFullyQualifiedName(String fullyQualifiedName) {
return fullyQualifiedName.substring( fullyQualifiedName.lastIndexOf(".") + 1 );
}
public static boolean isProperty(String methodName, String returnTypeAsString) {
if ( methodName == null || "void".equals( returnTypeAsString ) ) {
public static boolean isProperty(String methodName, String returnType) {
if ( methodName == null ) {
return false;
}
if ( isValidPropertyName( methodName, PROPERTY_PREFIX_GET ) ) {
return true;
else {
return !isVoid( returnType )
&& isValidPropertyName( methodName, GET )
|| isBoolean( returnType )
&& ( isValidPropertyName( methodName, IS )
|| isValidPropertyName( methodName, HAS ) );
}
if ( isValidPropertyName( methodName, PROPERTY_PREFIX_IS )
|| isValidPropertyName( methodName, PROPERTY_PREFIX_HAS ) ) {
return isBooleanGetter( returnTypeAsString );
}
return false;
}
private static boolean isBooleanGetter(String type) {
return "Boolean".equals( type ) || "java.lang.Boolean".equals( type );
private static boolean isVoid(String returnType) {
return "void".equals( returnType );
}
private static boolean isBoolean(String type) {
return "Boolean".equals( type ) || "java.lang.Boolean".equals( type ) || "boolean".equals( type );
}
private static boolean isValidPropertyName(String name, String prefix) {
if ( !name.startsWith( prefix ) ) {
return false;
}
// the name has to start with the prefix and have at least one more character
return name.length() >= prefix.length() + 1;
return name.startsWith( prefix ) && name.length() > prefix.length();
}
public static String getPropertyName(String name) {
String tmp = name;
if ( name.startsWith( PROPERTY_PREFIX_GET ) ) {
tmp = name.replaceFirst( PROPERTY_PREFIX_GET, "" );
return decapitalize( trimPropertyPrefix( name ) );
}
private static String trimPropertyPrefix(String name) {
if ( name.startsWith( GET ) ) {
return name.replaceFirst( GET, "" );
}
else if ( name.startsWith( PROPERTY_PREFIX_IS ) ) {
tmp = name.replaceFirst( PROPERTY_PREFIX_IS, "" );
else if ( name.startsWith( IS ) ) {
return name.replaceFirst( IS, "" );
}
else if ( name.startsWith( PROPERTY_PREFIX_HAS ) ) {
tmp = name.replaceFirst( PROPERTY_PREFIX_HAS, "" );
else if ( name.startsWith( HAS ) ) {
return name.replaceFirst( HAS, "" );
}
else {
return name;
}
return decapitalize( tmp );
}
public static String decapitalize(String string) {
if ( string == null || string.isEmpty() || startsWithSeveralUpperCaseLetters( string ) ) {
return string;
}
else {
return string.substring( 0, 1 ).toLowerCase(Locale.ROOT) + string.substring( 1 );
}
return string == null || string.isEmpty() || startsWithSeveralUpperCaseLetters( string )
? string
: string.substring( 0, 1 ).toLowerCase(Locale.ROOT) + string.substring( 1 );
}
public static String nameToFieldName(String name){
return getUpperUnderscoreCaseFromLowerCamelCase(nameToMethodName(name));
return getUpperUnderscoreCaseFromLowerCamelCase( nameToMethodName( name ) );
}
public static String nameToMethodName(String name) {
@ -119,8 +112,8 @@ public final class StringUtil {
}
private static boolean startsWithSeveralUpperCaseLetters(String string) {
return string.length() > 1 &&
Character.isUpperCase( string.charAt( 0 ) ) &&
Character.isUpperCase( string.charAt( 1 ) );
return string.length() > 1
&& isUpperCase( string.charAt( 0 ) )
&& isUpperCase( string.charAt( 1 ) );
}
}

View File

@ -39,13 +39,15 @@ import org.hibernate.boot.jaxb.spi.JaxbBindableMappingDescriptor;
import org.hibernate.processor.Context;
import org.hibernate.processor.util.AccessTypeInformation;
import org.hibernate.processor.util.FileTimeStampChecker;
import org.hibernate.processor.util.StringUtil;
import org.hibernate.processor.util.TypeUtils;
import org.hibernate.processor.util.xml.XmlParserHelper;
import jakarta.persistence.AccessType;
import org.checkerframework.checker.nullness.qual.Nullable;
import static org.hibernate.processor.util.StringUtil.determineFullyQualifiedClassName;
import static org.hibernate.processor.util.StringUtil.packageNameFromFullyQualifiedName;
/**
* Parser for JPA XML descriptors (persistence.xml and referenced mapping files).
*
@ -259,7 +261,7 @@ public class JpaDescriptorParser {
private void parseEntities(List<JaxbEntityImpl> entities, String defaultPackageName) {
for ( JaxbEntityImpl entity : entities ) {
String fqcn = StringUtil.determineFullyQualifiedClassName( defaultPackageName, entity.getClazz() );
String fqcn = determineFullyQualifiedClassName( defaultPackageName, entity.getClazz() );
if ( !xmlMappedTypeExists( fqcn ) ) {
context.logMessage(
@ -286,9 +288,9 @@ public class JpaDescriptorParser {
List<JaxbEmbeddableImpl> embeddables,
String defaultPackageName) {
for ( JaxbEmbeddableImpl embeddable : embeddables ) {
String fqcn = StringUtil.determineFullyQualifiedClassName( defaultPackageName, embeddable.getClazz() );
String fqcn = determineFullyQualifiedClassName( defaultPackageName, embeddable.getClazz() );
// we have to extract the package name from the fqcn. Maybe the entity was setting a fqcn directly
String pkg = StringUtil.packageNameFromFqcn( fqcn );
String pkg = packageNameFromFullyQualifiedName( fqcn );
if ( !xmlMappedTypeExists( fqcn ) ) {
context.logMessage(
@ -313,11 +315,11 @@ public class JpaDescriptorParser {
List<JaxbMappedSuperclassImpl> mappedSuperClasses,
String defaultPackageName) {
for ( JaxbMappedSuperclassImpl mappedSuperClass : mappedSuperClasses ) {
String fqcn = StringUtil.determineFullyQualifiedClassName(
String fqcn = determineFullyQualifiedClassName(
defaultPackageName, mappedSuperClass.getClazz()
);
// we have to extract the package name from the fqcn. Maybe the entity was setting a fqcn directly
String pkg = StringUtil.packageNameFromFqcn( fqcn );
String pkg = packageNameFromFullyQualifiedName( fqcn );
if ( !xmlMappedTypeExists( fqcn ) ) {
context.logMessage(
@ -367,7 +369,7 @@ public class JpaDescriptorParser {
for ( JaxbEntityImpl entity : mappings.getEntities() ) {
final String name = entity.getClazz();
fqcn = StringUtil.determineFullyQualifiedClassName( packageName, name );
fqcn = determineFullyQualifiedClassName( packageName, name );
final AccessType explicitAccessType = entity.getAccess();
final AccessTypeInformation accessInfo = new AccessTypeInformation( fqcn, explicitAccessType, defaultAccessType );
context.addAccessTypeInformation( fqcn, accessInfo );
@ -375,7 +377,7 @@ public class JpaDescriptorParser {
for ( JaxbMappedSuperclassImpl mappedSuperClass : mappings.getMappedSuperclasses() ) {
final String name = mappedSuperClass.getClazz();
fqcn = StringUtil.determineFullyQualifiedClassName( packageName, name );
fqcn = determineFullyQualifiedClassName( packageName, name );
final AccessType explicitAccessType = mappedSuperClass.getAccess();
final AccessTypeInformation accessInfo = new AccessTypeInformation( fqcn, explicitAccessType, defaultAccessType );
context.addAccessTypeInformation( fqcn, accessInfo );
@ -383,7 +385,7 @@ public class JpaDescriptorParser {
for ( JaxbEmbeddableImpl embeddable : mappings.getEmbeddables() ) {
final String name = embeddable.getClazz();
fqcn = StringUtil.determineFullyQualifiedClassName( packageName, name );
fqcn = determineFullyQualifiedClassName( packageName, name );
final AccessType explicitAccessType = embeddable.getAccess();
final AccessTypeInformation accessInfo = new AccessTypeInformation( fqcn, explicitAccessType, defaultAccessType );
context.addAccessTypeInformation( fqcn, accessInfo );
@ -398,7 +400,7 @@ public class JpaDescriptorParser {
for ( JaxbEntityImpl entity : mappings.getEntities() ) {
String name = entity.getClazz();
fqcn = StringUtil.determineFullyQualifiedClassName( packageName, name );
fqcn = determineFullyQualifiedClassName( packageName, name );
TypeElement element = context.getTypeElementForFullyQualifiedName( fqcn );
if ( element != null ) {
TypeUtils.determineAccessTypeForHierarchy( element, context );
@ -407,7 +409,7 @@ public class JpaDescriptorParser {
for ( JaxbMappedSuperclassImpl mappedSuperClass : mappings.getMappedSuperclasses() ) {
String name = mappedSuperClass.getClazz();
fqcn = StringUtil.determineFullyQualifiedClassName( packageName, name );
fqcn = determineFullyQualifiedClassName( packageName, name );
TypeElement element = context.getTypeElementForFullyQualifiedName( fqcn );
if ( element != null ) {
TypeUtils.determineAccessTypeForHierarchy( element, context );

View File

@ -48,7 +48,10 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import static jakarta.persistence.AccessType.FIELD;
import static java.util.Collections.emptyList;
import static org.hibernate.processor.util.StringUtil.classNameFromFullyQualifiedName;
import static org.hibernate.processor.util.StringUtil.determineFullyQualifiedClassName;
import static org.hibernate.processor.util.StringUtil.isFullyQualified;
import static org.hibernate.processor.util.StringUtil.packageNameFromFullyQualifiedName;
import static org.hibernate.processor.util.TypeUtils.extractClosestRealTypeAsString;
import static org.hibernate.processor.util.TypeUtils.findMappedSuperClass;
import static org.hibernate.processor.util.TypeUtils.getElementKindForAccessType;
@ -121,11 +124,11 @@ public class XmlMetaEntity implements Metamodel {
this.defaultPackageName = defaultPackageName;
String className = clazz;
String pkg = defaultPackageName;
if ( StringUtil.isFullyQualified( className ) ) {
if ( isFullyQualified( className ) ) {
// if the class name is fully qualified we have to extract the package name from the fqcn.
// default package name gets ignored
pkg = StringUtil.packageNameFromFqcn( className );
className = StringUtil.classNameFromFqcn( clazz );
pkg = packageNameFromFullyQualifiedName( className );
className = classNameFromFullyQualifiedName( clazz );
}
this.clazzName = className;
this.packageName = pkg;