mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-16 08:05:05 +00:00
cleanup processor StringUtil
This commit is contained in:
parent
09c627c0b2
commit
d53498910a
@ -14,90 +14,83 @@
|
|||||||
* @author Hardy Ferentschik
|
* @author Hardy Ferentschik
|
||||||
*/
|
*/
|
||||||
public final class StringUtil {
|
public final class StringUtil {
|
||||||
private static final String NAME_SEPARATOR = ".";
|
private static final String GET = "get";
|
||||||
private static final String PROPERTY_PREFIX_GET = "get";
|
private static final String IS = "is";
|
||||||
private static final String PROPERTY_PREFIX_IS = "is";
|
private static final String HAS = "has";
|
||||||
private static final String PROPERTY_PREFIX_HAS = "has";
|
|
||||||
|
|
||||||
private StringUtil() {
|
private StringUtil() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String determineFullyQualifiedClassName(String defaultPackage, String name) {
|
public static String determineFullyQualifiedClassName(String defaultPackage, String name) {
|
||||||
if ( isFullyQualified( name ) ) {
|
return isFullyQualified( name ) ? name : defaultPackage + "." + name;
|
||||||
return name;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return defaultPackage + NAME_SEPARATOR + name;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isFullyQualified(String name) {
|
public static boolean isFullyQualified(String name) {
|
||||||
return name.contains( NAME_SEPARATOR );
|
return name.contains(".");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String packageNameFromFqcn(String fqcn) {
|
public static String packageNameFromFullyQualifiedName(String fullyQualifiedName) {
|
||||||
return fqcn.substring( 0, fqcn.lastIndexOf( NAME_SEPARATOR ) );
|
return fullyQualifiedName.substring( 0, fullyQualifiedName.lastIndexOf(".") );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String classNameFromFqcn(String fqcn) {
|
public static String classNameFromFullyQualifiedName(String fullyQualifiedName) {
|
||||||
return fqcn.substring( fqcn.lastIndexOf( NAME_SEPARATOR ) + 1 );
|
return fullyQualifiedName.substring( fullyQualifiedName.lastIndexOf(".") + 1 );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isProperty(String methodName, String returnTypeAsString) {
|
public static boolean isProperty(String methodName, String returnType) {
|
||||||
if ( methodName == null || "void".equals( returnTypeAsString ) ) {
|
if ( methodName == null ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
if ( isValidPropertyName( methodName, PROPERTY_PREFIX_GET ) ) {
|
return !isVoid( returnType )
|
||||||
return true;
|
&& 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) {
|
private static boolean isVoid(String returnType) {
|
||||||
return "Boolean".equals( type ) || "java.lang.Boolean".equals( type );
|
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) {
|
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
|
// 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) {
|
public static String getPropertyName(String name) {
|
||||||
String tmp = name;
|
return decapitalize( trimPropertyPrefix( name ) );
|
||||||
if ( name.startsWith( PROPERTY_PREFIX_GET ) ) {
|
}
|
||||||
tmp = name.replaceFirst( PROPERTY_PREFIX_GET, "" );
|
|
||||||
|
private static String trimPropertyPrefix(String name) {
|
||||||
|
if ( name.startsWith( GET ) ) {
|
||||||
|
return name.replaceFirst( GET, "" );
|
||||||
}
|
}
|
||||||
else if ( name.startsWith( PROPERTY_PREFIX_IS ) ) {
|
else if ( name.startsWith( IS ) ) {
|
||||||
tmp = name.replaceFirst( PROPERTY_PREFIX_IS, "" );
|
return name.replaceFirst( IS, "" );
|
||||||
}
|
}
|
||||||
else if ( name.startsWith( PROPERTY_PREFIX_HAS ) ) {
|
else if ( name.startsWith( HAS ) ) {
|
||||||
tmp = name.replaceFirst( PROPERTY_PREFIX_HAS, "" );
|
return name.replaceFirst( HAS, "" );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return name;
|
||||||
}
|
}
|
||||||
return decapitalize( tmp );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String decapitalize(String string) {
|
public static String decapitalize(String string) {
|
||||||
if ( string == null || string.isEmpty() || startsWithSeveralUpperCaseLetters( string ) ) {
|
return string == null || string.isEmpty() || startsWithSeveralUpperCaseLetters( string )
|
||||||
return string;
|
? string
|
||||||
}
|
: string.substring( 0, 1 ).toLowerCase(Locale.ROOT) + string.substring( 1 );
|
||||||
else {
|
|
||||||
return string.substring( 0, 1 ).toLowerCase(Locale.ROOT) + string.substring( 1 );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String nameToFieldName(String name){
|
public static String nameToFieldName(String name){
|
||||||
return getUpperUnderscoreCaseFromLowerCamelCase(nameToMethodName(name));
|
return getUpperUnderscoreCaseFromLowerCamelCase( nameToMethodName( name ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String nameToMethodName(String name) {
|
public static String nameToMethodName(String name) {
|
||||||
@ -119,8 +112,8 @@ public static String getUpperUnderscoreCaseFromLowerCamelCase(String lowerCamelC
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static boolean startsWithSeveralUpperCaseLetters(String string) {
|
private static boolean startsWithSeveralUpperCaseLetters(String string) {
|
||||||
return string.length() > 1 &&
|
return string.length() > 1
|
||||||
Character.isUpperCase( string.charAt( 0 ) ) &&
|
&& isUpperCase( string.charAt( 0 ) )
|
||||||
Character.isUpperCase( string.charAt( 1 ) );
|
&& isUpperCase( string.charAt( 1 ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,13 +39,15 @@
|
|||||||
import org.hibernate.processor.Context;
|
import org.hibernate.processor.Context;
|
||||||
import org.hibernate.processor.util.AccessTypeInformation;
|
import org.hibernate.processor.util.AccessTypeInformation;
|
||||||
import org.hibernate.processor.util.FileTimeStampChecker;
|
import org.hibernate.processor.util.FileTimeStampChecker;
|
||||||
import org.hibernate.processor.util.StringUtil;
|
|
||||||
import org.hibernate.processor.util.TypeUtils;
|
import org.hibernate.processor.util.TypeUtils;
|
||||||
import org.hibernate.processor.util.xml.XmlParserHelper;
|
import org.hibernate.processor.util.xml.XmlParserHelper;
|
||||||
|
|
||||||
import jakarta.persistence.AccessType;
|
import jakarta.persistence.AccessType;
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
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).
|
* Parser for JPA XML descriptors (persistence.xml and referenced mapping files).
|
||||||
*
|
*
|
||||||
@ -259,7 +261,7 @@ private FileTimeStampChecker loadTimeStampCache() {
|
|||||||
|
|
||||||
private void parseEntities(List<JaxbEntityImpl> entities, String defaultPackageName) {
|
private void parseEntities(List<JaxbEntityImpl> entities, String defaultPackageName) {
|
||||||
for ( JaxbEntityImpl entity : entities ) {
|
for ( JaxbEntityImpl entity : entities ) {
|
||||||
String fqcn = StringUtil.determineFullyQualifiedClassName( defaultPackageName, entity.getClazz() );
|
String fqcn = determineFullyQualifiedClassName( defaultPackageName, entity.getClazz() );
|
||||||
|
|
||||||
if ( !xmlMappedTypeExists( fqcn ) ) {
|
if ( !xmlMappedTypeExists( fqcn ) ) {
|
||||||
context.logMessage(
|
context.logMessage(
|
||||||
@ -286,9 +288,9 @@ private void parseEmbeddable(
|
|||||||
List<JaxbEmbeddableImpl> embeddables,
|
List<JaxbEmbeddableImpl> embeddables,
|
||||||
String defaultPackageName) {
|
String defaultPackageName) {
|
||||||
for ( JaxbEmbeddableImpl embeddable : embeddables ) {
|
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
|
// 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 ) ) {
|
if ( !xmlMappedTypeExists( fqcn ) ) {
|
||||||
context.logMessage(
|
context.logMessage(
|
||||||
@ -313,11 +315,11 @@ private void parseMappedSuperClass(
|
|||||||
List<JaxbMappedSuperclassImpl> mappedSuperClasses,
|
List<JaxbMappedSuperclassImpl> mappedSuperClasses,
|
||||||
String defaultPackageName) {
|
String defaultPackageName) {
|
||||||
for ( JaxbMappedSuperclassImpl mappedSuperClass : mappedSuperClasses ) {
|
for ( JaxbMappedSuperclassImpl mappedSuperClass : mappedSuperClasses ) {
|
||||||
String fqcn = StringUtil.determineFullyQualifiedClassName(
|
String fqcn = determineFullyQualifiedClassName(
|
||||||
defaultPackageName, mappedSuperClass.getClazz()
|
defaultPackageName, mappedSuperClass.getClazz()
|
||||||
);
|
);
|
||||||
// we have to extract the package name from the fqcn. Maybe the entity was setting a fqcn directly
|
// 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 ) ) {
|
if ( !xmlMappedTypeExists( fqcn ) ) {
|
||||||
context.logMessage(
|
context.logMessage(
|
||||||
@ -367,7 +369,7 @@ private void determineXmlAccessTypes() {
|
|||||||
|
|
||||||
for ( JaxbEntityImpl entity : mappings.getEntities() ) {
|
for ( JaxbEntityImpl entity : mappings.getEntities() ) {
|
||||||
final String name = entity.getClazz();
|
final String name = entity.getClazz();
|
||||||
fqcn = StringUtil.determineFullyQualifiedClassName( packageName, name );
|
fqcn = determineFullyQualifiedClassName( packageName, name );
|
||||||
final AccessType explicitAccessType = entity.getAccess();
|
final AccessType explicitAccessType = entity.getAccess();
|
||||||
final AccessTypeInformation accessInfo = new AccessTypeInformation( fqcn, explicitAccessType, defaultAccessType );
|
final AccessTypeInformation accessInfo = new AccessTypeInformation( fqcn, explicitAccessType, defaultAccessType );
|
||||||
context.addAccessTypeInformation( fqcn, accessInfo );
|
context.addAccessTypeInformation( fqcn, accessInfo );
|
||||||
@ -375,7 +377,7 @@ private void determineXmlAccessTypes() {
|
|||||||
|
|
||||||
for ( JaxbMappedSuperclassImpl mappedSuperClass : mappings.getMappedSuperclasses() ) {
|
for ( JaxbMappedSuperclassImpl mappedSuperClass : mappings.getMappedSuperclasses() ) {
|
||||||
final String name = mappedSuperClass.getClazz();
|
final String name = mappedSuperClass.getClazz();
|
||||||
fqcn = StringUtil.determineFullyQualifiedClassName( packageName, name );
|
fqcn = determineFullyQualifiedClassName( packageName, name );
|
||||||
final AccessType explicitAccessType = mappedSuperClass.getAccess();
|
final AccessType explicitAccessType = mappedSuperClass.getAccess();
|
||||||
final AccessTypeInformation accessInfo = new AccessTypeInformation( fqcn, explicitAccessType, defaultAccessType );
|
final AccessTypeInformation accessInfo = new AccessTypeInformation( fqcn, explicitAccessType, defaultAccessType );
|
||||||
context.addAccessTypeInformation( fqcn, accessInfo );
|
context.addAccessTypeInformation( fqcn, accessInfo );
|
||||||
@ -383,7 +385,7 @@ private void determineXmlAccessTypes() {
|
|||||||
|
|
||||||
for ( JaxbEmbeddableImpl embeddable : mappings.getEmbeddables() ) {
|
for ( JaxbEmbeddableImpl embeddable : mappings.getEmbeddables() ) {
|
||||||
final String name = embeddable.getClazz();
|
final String name = embeddable.getClazz();
|
||||||
fqcn = StringUtil.determineFullyQualifiedClassName( packageName, name );
|
fqcn = determineFullyQualifiedClassName( packageName, name );
|
||||||
final AccessType explicitAccessType = embeddable.getAccess();
|
final AccessType explicitAccessType = embeddable.getAccess();
|
||||||
final AccessTypeInformation accessInfo = new AccessTypeInformation( fqcn, explicitAccessType, defaultAccessType );
|
final AccessTypeInformation accessInfo = new AccessTypeInformation( fqcn, explicitAccessType, defaultAccessType );
|
||||||
context.addAccessTypeInformation( fqcn, accessInfo );
|
context.addAccessTypeInformation( fqcn, accessInfo );
|
||||||
@ -398,7 +400,7 @@ private void determineAnnotationAccessTypes() {
|
|||||||
|
|
||||||
for ( JaxbEntityImpl entity : mappings.getEntities() ) {
|
for ( JaxbEntityImpl entity : mappings.getEntities() ) {
|
||||||
String name = entity.getClazz();
|
String name = entity.getClazz();
|
||||||
fqcn = StringUtil.determineFullyQualifiedClassName( packageName, name );
|
fqcn = determineFullyQualifiedClassName( packageName, name );
|
||||||
TypeElement element = context.getTypeElementForFullyQualifiedName( fqcn );
|
TypeElement element = context.getTypeElementForFullyQualifiedName( fqcn );
|
||||||
if ( element != null ) {
|
if ( element != null ) {
|
||||||
TypeUtils.determineAccessTypeForHierarchy( element, context );
|
TypeUtils.determineAccessTypeForHierarchy( element, context );
|
||||||
@ -407,7 +409,7 @@ private void determineAnnotationAccessTypes() {
|
|||||||
|
|
||||||
for ( JaxbMappedSuperclassImpl mappedSuperClass : mappings.getMappedSuperclasses() ) {
|
for ( JaxbMappedSuperclassImpl mappedSuperClass : mappings.getMappedSuperclasses() ) {
|
||||||
String name = mappedSuperClass.getClazz();
|
String name = mappedSuperClass.getClazz();
|
||||||
fqcn = StringUtil.determineFullyQualifiedClassName( packageName, name );
|
fqcn = determineFullyQualifiedClassName( packageName, name );
|
||||||
TypeElement element = context.getTypeElementForFullyQualifiedName( fqcn );
|
TypeElement element = context.getTypeElementForFullyQualifiedName( fqcn );
|
||||||
if ( element != null ) {
|
if ( element != null ) {
|
||||||
TypeUtils.determineAccessTypeForHierarchy( element, context );
|
TypeUtils.determineAccessTypeForHierarchy( element, context );
|
||||||
|
@ -48,7 +48,10 @@
|
|||||||
|
|
||||||
import static jakarta.persistence.AccessType.FIELD;
|
import static jakarta.persistence.AccessType.FIELD;
|
||||||
import static java.util.Collections.emptyList;
|
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.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.extractClosestRealTypeAsString;
|
||||||
import static org.hibernate.processor.util.TypeUtils.findMappedSuperClass;
|
import static org.hibernate.processor.util.TypeUtils.findMappedSuperClass;
|
||||||
import static org.hibernate.processor.util.TypeUtils.getElementKindForAccessType;
|
import static org.hibernate.processor.util.TypeUtils.getElementKindForAccessType;
|
||||||
@ -121,11 +124,11 @@ private XmlMetaEntity(String clazz, String defaultPackageName, TypeElement eleme
|
|||||||
this.defaultPackageName = defaultPackageName;
|
this.defaultPackageName = defaultPackageName;
|
||||||
String className = clazz;
|
String className = clazz;
|
||||||
String pkg = defaultPackageName;
|
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.
|
// if the class name is fully qualified we have to extract the package name from the fqcn.
|
||||||
// default package name gets ignored
|
// default package name gets ignored
|
||||||
pkg = StringUtil.packageNameFromFqcn( className );
|
pkg = packageNameFromFullyQualifiedName( className );
|
||||||
className = StringUtil.classNameFromFqcn( clazz );
|
className = classNameFromFullyQualifiedName( clazz );
|
||||||
}
|
}
|
||||||
this.clazzName = className;
|
this.clazzName = className;
|
||||||
this.packageName = pkg;
|
this.packageName = pkg;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user