Skip to content

Commit d534989

Browse files
committed
cleanup processor StringUtil
1 parent 09c627c commit d534989

File tree

3 files changed

+62
-64
lines changed

3 files changed

+62
-64
lines changed

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

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,90 +14,83 @@
1414
* @author Hardy Ferentschik
1515
*/
1616
public final class StringUtil {
17-
private static final String NAME_SEPARATOR = ".";
18-
private static final String PROPERTY_PREFIX_GET = "get";
19-
private static final String PROPERTY_PREFIX_IS = "is";
20-
private static final String PROPERTY_PREFIX_HAS = "has";
17+
private static final String GET = "get";
18+
private static final String IS = "is";
19+
private static final String HAS = "has";
2120

2221
private StringUtil() {
2322
}
2423

2524
public static String determineFullyQualifiedClassName(String defaultPackage, String name) {
26-
if ( isFullyQualified( name ) ) {
27-
return name;
28-
}
29-
else {
30-
return defaultPackage + NAME_SEPARATOR + name;
31-
}
25+
return isFullyQualified( name ) ? name : defaultPackage + "." + name;
3226
}
3327

3428
public static boolean isFullyQualified(String name) {
35-
return name.contains( NAME_SEPARATOR );
29+
return name.contains(".");
3630
}
3731

38-
public static String packageNameFromFqcn(String fqcn) {
39-
return fqcn.substring( 0, fqcn.lastIndexOf( NAME_SEPARATOR ) );
32+
public static String packageNameFromFullyQualifiedName(String fullyQualifiedName) {
33+
return fullyQualifiedName.substring( 0, fullyQualifiedName.lastIndexOf(".") );
4034
}
4135

42-
public static String classNameFromFqcn(String fqcn) {
43-
return fqcn.substring( fqcn.lastIndexOf( NAME_SEPARATOR ) + 1 );
36+
public static String classNameFromFullyQualifiedName(String fullyQualifiedName) {
37+
return fullyQualifiedName.substring( fullyQualifiedName.lastIndexOf(".") + 1 );
4438
}
4539

46-
public static boolean isProperty(String methodName, String returnTypeAsString) {
47-
if ( methodName == null || "void".equals( returnTypeAsString ) ) {
40+
public static boolean isProperty(String methodName, String returnType) {
41+
if ( methodName == null ) {
4842
return false;
4943
}
50-
51-
if ( isValidPropertyName( methodName, PROPERTY_PREFIX_GET ) ) {
52-
return true;
44+
else {
45+
return !isVoid( returnType )
46+
&& isValidPropertyName( methodName, GET )
47+
|| isBoolean( returnType )
48+
&& ( isValidPropertyName( methodName, IS )
49+
|| isValidPropertyName( methodName, HAS ) );
5350
}
5451

55-
if ( isValidPropertyName( methodName, PROPERTY_PREFIX_IS )
56-
|| isValidPropertyName( methodName, PROPERTY_PREFIX_HAS ) ) {
57-
return isBooleanGetter( returnTypeAsString );
58-
}
52+
}
5953

60-
return false;
54+
private static boolean isVoid(String returnType) {
55+
return "void".equals( returnType );
6156
}
6257

63-
private static boolean isBooleanGetter(String type) {
64-
return "Boolean".equals( type ) || "java.lang.Boolean".equals( type );
58+
private static boolean isBoolean(String type) {
59+
return "Boolean".equals( type ) || "java.lang.Boolean".equals( type ) || "boolean".equals( type );
6560
}
6661

6762
private static boolean isValidPropertyName(String name, String prefix) {
68-
if ( !name.startsWith( prefix ) ) {
69-
return false;
70-
}
71-
7263
// the name has to start with the prefix and have at least one more character
73-
return name.length() >= prefix.length() + 1;
64+
return name.startsWith( prefix ) && name.length() > prefix.length();
7465
}
7566

7667
public static String getPropertyName(String name) {
77-
String tmp = name;
78-
if ( name.startsWith( PROPERTY_PREFIX_GET ) ) {
79-
tmp = name.replaceFirst( PROPERTY_PREFIX_GET, "" );
68+
return decapitalize( trimPropertyPrefix( name ) );
69+
}
70+
71+
private static String trimPropertyPrefix(String name) {
72+
if ( name.startsWith( GET ) ) {
73+
return name.replaceFirst( GET, "" );
8074
}
81-
else if ( name.startsWith( PROPERTY_PREFIX_IS ) ) {
82-
tmp = name.replaceFirst( PROPERTY_PREFIX_IS, "" );
75+
else if ( name.startsWith( IS ) ) {
76+
return name.replaceFirst( IS, "" );
8377
}
84-
else if ( name.startsWith( PROPERTY_PREFIX_HAS ) ) {
85-
tmp = name.replaceFirst( PROPERTY_PREFIX_HAS, "" );
78+
else if ( name.startsWith( HAS ) ) {
79+
return name.replaceFirst( HAS, "" );
80+
}
81+
else {
82+
return name;
8683
}
87-
return decapitalize( tmp );
8884
}
8985

9086
public static String decapitalize(String string) {
91-
if ( string == null || string.isEmpty() || startsWithSeveralUpperCaseLetters( string ) ) {
92-
return string;
93-
}
94-
else {
95-
return string.substring( 0, 1 ).toLowerCase(Locale.ROOT) + string.substring( 1 );
96-
}
87+
return string == null || string.isEmpty() || startsWithSeveralUpperCaseLetters( string )
88+
? string
89+
: string.substring( 0, 1 ).toLowerCase(Locale.ROOT) + string.substring( 1 );
9790
}
9891

9992
public static String nameToFieldName(String name){
100-
return getUpperUnderscoreCaseFromLowerCamelCase(nameToMethodName(name));
93+
return getUpperUnderscoreCaseFromLowerCamelCase( nameToMethodName( name ) );
10194
}
10295

10396
public static String nameToMethodName(String name) {
@@ -119,8 +112,8 @@ public static String getUpperUnderscoreCaseFromLowerCamelCase(String lowerCamelC
119112
}
120113

121114
private static boolean startsWithSeveralUpperCaseLetters(String string) {
122-
return string.length() > 1 &&
123-
Character.isUpperCase( string.charAt( 0 ) ) &&
124-
Character.isUpperCase( string.charAt( 1 ) );
115+
return string.length() > 1
116+
&& isUpperCase( string.charAt( 0 ) )
117+
&& isUpperCase( string.charAt( 1 ) );
125118
}
126119
}

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@
3939
import org.hibernate.processor.Context;
4040
import org.hibernate.processor.util.AccessTypeInformation;
4141
import org.hibernate.processor.util.FileTimeStampChecker;
42-
import org.hibernate.processor.util.StringUtil;
4342
import org.hibernate.processor.util.TypeUtils;
4443
import org.hibernate.processor.util.xml.XmlParserHelper;
4544

4645
import jakarta.persistence.AccessType;
4746
import org.checkerframework.checker.nullness.qual.Nullable;
4847

48+
import static org.hibernate.processor.util.StringUtil.determineFullyQualifiedClassName;
49+
import static org.hibernate.processor.util.StringUtil.packageNameFromFullyQualifiedName;
50+
4951
/**
5052
* Parser for JPA XML descriptors (persistence.xml and referenced mapping files).
5153
*
@@ -259,7 +261,7 @@ private FileTimeStampChecker loadTimeStampCache() {
259261

260262
private void parseEntities(List<JaxbEntityImpl> entities, String defaultPackageName) {
261263
for ( JaxbEntityImpl entity : entities ) {
262-
String fqcn = StringUtil.determineFullyQualifiedClassName( defaultPackageName, entity.getClazz() );
264+
String fqcn = determineFullyQualifiedClassName( defaultPackageName, entity.getClazz() );
263265

264266
if ( !xmlMappedTypeExists( fqcn ) ) {
265267
context.logMessage(
@@ -286,9 +288,9 @@ private void parseEmbeddable(
286288
List<JaxbEmbeddableImpl> embeddables,
287289
String defaultPackageName) {
288290
for ( JaxbEmbeddableImpl embeddable : embeddables ) {
289-
String fqcn = StringUtil.determineFullyQualifiedClassName( defaultPackageName, embeddable.getClazz() );
291+
String fqcn = determineFullyQualifiedClassName( defaultPackageName, embeddable.getClazz() );
290292
// we have to extract the package name from the fqcn. Maybe the entity was setting a fqcn directly
291-
String pkg = StringUtil.packageNameFromFqcn( fqcn );
293+
String pkg = packageNameFromFullyQualifiedName( fqcn );
292294

293295
if ( !xmlMappedTypeExists( fqcn ) ) {
294296
context.logMessage(
@@ -313,11 +315,11 @@ private void parseMappedSuperClass(
313315
List<JaxbMappedSuperclassImpl> mappedSuperClasses,
314316
String defaultPackageName) {
315317
for ( JaxbMappedSuperclassImpl mappedSuperClass : mappedSuperClasses ) {
316-
String fqcn = StringUtil.determineFullyQualifiedClassName(
318+
String fqcn = determineFullyQualifiedClassName(
317319
defaultPackageName, mappedSuperClass.getClazz()
318320
);
319321
// we have to extract the package name from the fqcn. Maybe the entity was setting a fqcn directly
320-
String pkg = StringUtil.packageNameFromFqcn( fqcn );
322+
String pkg = packageNameFromFullyQualifiedName( fqcn );
321323

322324
if ( !xmlMappedTypeExists( fqcn ) ) {
323325
context.logMessage(
@@ -367,23 +369,23 @@ private void determineXmlAccessTypes() {
367369

368370
for ( JaxbEntityImpl entity : mappings.getEntities() ) {
369371
final String name = entity.getClazz();
370-
fqcn = StringUtil.determineFullyQualifiedClassName( packageName, name );
372+
fqcn = determineFullyQualifiedClassName( packageName, name );
371373
final AccessType explicitAccessType = entity.getAccess();
372374
final AccessTypeInformation accessInfo = new AccessTypeInformation( fqcn, explicitAccessType, defaultAccessType );
373375
context.addAccessTypeInformation( fqcn, accessInfo );
374376
}
375377

376378
for ( JaxbMappedSuperclassImpl mappedSuperClass : mappings.getMappedSuperclasses() ) {
377379
final String name = mappedSuperClass.getClazz();
378-
fqcn = StringUtil.determineFullyQualifiedClassName( packageName, name );
380+
fqcn = determineFullyQualifiedClassName( packageName, name );
379381
final AccessType explicitAccessType = mappedSuperClass.getAccess();
380382
final AccessTypeInformation accessInfo = new AccessTypeInformation( fqcn, explicitAccessType, defaultAccessType );
381383
context.addAccessTypeInformation( fqcn, accessInfo );
382384
}
383385

384386
for ( JaxbEmbeddableImpl embeddable : mappings.getEmbeddables() ) {
385387
final String name = embeddable.getClazz();
386-
fqcn = StringUtil.determineFullyQualifiedClassName( packageName, name );
388+
fqcn = determineFullyQualifiedClassName( packageName, name );
387389
final AccessType explicitAccessType = embeddable.getAccess();
388390
final AccessTypeInformation accessInfo = new AccessTypeInformation( fqcn, explicitAccessType, defaultAccessType );
389391
context.addAccessTypeInformation( fqcn, accessInfo );
@@ -398,7 +400,7 @@ private void determineAnnotationAccessTypes() {
398400

399401
for ( JaxbEntityImpl entity : mappings.getEntities() ) {
400402
String name = entity.getClazz();
401-
fqcn = StringUtil.determineFullyQualifiedClassName( packageName, name );
403+
fqcn = determineFullyQualifiedClassName( packageName, name );
402404
TypeElement element = context.getTypeElementForFullyQualifiedName( fqcn );
403405
if ( element != null ) {
404406
TypeUtils.determineAccessTypeForHierarchy( element, context );
@@ -407,7 +409,7 @@ private void determineAnnotationAccessTypes() {
407409

408410
for ( JaxbMappedSuperclassImpl mappedSuperClass : mappings.getMappedSuperclasses() ) {
409411
String name = mappedSuperClass.getClazz();
410-
fqcn = StringUtil.determineFullyQualifiedClassName( packageName, name );
412+
fqcn = determineFullyQualifiedClassName( packageName, name );
411413
TypeElement element = context.getTypeElementForFullyQualifiedName( fqcn );
412414
if ( element != null ) {
413415
TypeUtils.determineAccessTypeForHierarchy( element, context );

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@
4848

4949
import static jakarta.persistence.AccessType.FIELD;
5050
import static java.util.Collections.emptyList;
51+
import static org.hibernate.processor.util.StringUtil.classNameFromFullyQualifiedName;
5152
import static org.hibernate.processor.util.StringUtil.determineFullyQualifiedClassName;
53+
import static org.hibernate.processor.util.StringUtil.isFullyQualified;
54+
import static org.hibernate.processor.util.StringUtil.packageNameFromFullyQualifiedName;
5255
import static org.hibernate.processor.util.TypeUtils.extractClosestRealTypeAsString;
5356
import static org.hibernate.processor.util.TypeUtils.findMappedSuperClass;
5457
import static org.hibernate.processor.util.TypeUtils.getElementKindForAccessType;
@@ -121,11 +124,11 @@ private XmlMetaEntity(String clazz, String defaultPackageName, TypeElement eleme
121124
this.defaultPackageName = defaultPackageName;
122125
String className = clazz;
123126
String pkg = defaultPackageName;
124-
if ( StringUtil.isFullyQualified( className ) ) {
127+
if ( isFullyQualified( className ) ) {
125128
// if the class name is fully qualified we have to extract the package name from the fqcn.
126129
// default package name gets ignored
127-
pkg = StringUtil.packageNameFromFqcn( className );
128-
className = StringUtil.classNameFromFqcn( clazz );
130+
pkg = packageNameFromFullyQualifiedName( className );
131+
className = classNameFromFullyQualifiedName( clazz );
129132
}
130133
this.clazzName = className;
131134
this.packageName = pkg;

0 commit comments

Comments
 (0)