mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#365 inject default implementation, add documentation for default behavior
This commit is contained in:
parent
130d58060f
commit
e62b612851
@ -29,6 +29,10 @@ import org.mapstruct.spi.AccessorNamingStrategy;
|
||||
*/
|
||||
class DefaultAccessorNamingStrategy implements AccessorNamingStrategy {
|
||||
|
||||
@Override
|
||||
public void setDefaultAccessorNamingStrategy(AccessorNamingStrategy defaultAccessorNamingStrategy) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNonBooleanGetterName(String methodName) {
|
||||
return methodName.startsWith( "get" ) && methodName.length() > 3;
|
||||
|
@ -48,10 +48,12 @@ public class Services {
|
||||
}
|
||||
|
||||
private static AccessorNamingStrategy findAccessorNamingStrategy() {
|
||||
AccessorNamingStrategy defaultImpl = new DefaultAccessorNamingStrategy();
|
||||
AccessorNamingStrategy impl = find( AccessorNamingStrategy.class );
|
||||
if ( impl == null ) {
|
||||
impl = new DefaultAccessorNamingStrategy();
|
||||
impl = defaultImpl;
|
||||
}
|
||||
impl.setDefaultAccessorNamingStrategy( defaultImpl );
|
||||
return impl;
|
||||
}
|
||||
|
||||
|
@ -25,8 +25,20 @@ package org.mapstruct.spi;
|
||||
*/
|
||||
public interface AccessorNamingStrategy {
|
||||
|
||||
/**
|
||||
* Set the default {@link AccessorNamingStrategy} implementation. Custom implementations may use it to
|
||||
* keep parts of the default behavior. This method is invoked before any other method of the interface.
|
||||
* <p>
|
||||
* Default implementation: Does nothing.
|
||||
*
|
||||
* @param defaultAccessorNamingStrategy The default implementation.
|
||||
*/
|
||||
void setDefaultAccessorNamingStrategy(AccessorNamingStrategy defaultAccessorNamingStrategy);
|
||||
|
||||
/**
|
||||
* Determine if a method name defines a getter with a non-boolean return type.
|
||||
* <p>
|
||||
* Default implementation: Method name starts with "get" and is longer than 3 characters.
|
||||
*
|
||||
* @param methodName The method name.
|
||||
* @return {@code true} if the method name can be a non-boolean getter, {@code false} otherwise.
|
||||
@ -35,6 +47,8 @@ public interface AccessorNamingStrategy {
|
||||
|
||||
/**
|
||||
* Determine if a method name defines a getter with a boolean return type.
|
||||
* <p>
|
||||
* Default implementation: Method name starts with "is" and is longer than 2 characters.
|
||||
*
|
||||
* @param methodName The method name.
|
||||
* @return {@code true} if the method name can be a boolean getter, {@code false} otherwise.
|
||||
@ -43,6 +57,8 @@ public interface AccessorNamingStrategy {
|
||||
|
||||
/**
|
||||
* Determine if a method name defines a setter.
|
||||
* <p>
|
||||
* Default implementation: Method name starts with "set" and is longer than 3 characters.
|
||||
*
|
||||
* @param methodName The method name.
|
||||
* @return {@code true} if the method name can be a setter, {@code false} otherwise.
|
||||
@ -51,6 +67,8 @@ public interface AccessorNamingStrategy {
|
||||
|
||||
/**
|
||||
* Determine if a method name defines an adder.
|
||||
* <p>
|
||||
* Default implementation: Method name starts with "add" and is longer than 3 characters.
|
||||
*
|
||||
* @param methodName The method name.
|
||||
* @return {@code true} if the method name can be an adder, {@code false} otherwise.
|
||||
@ -60,6 +78,9 @@ public interface AccessorNamingStrategy {
|
||||
/**
|
||||
* Extract the property name from a method name for which {@link #isNonBooleanGetterName(String)} returned
|
||||
* {@code true}.
|
||||
* <p>
|
||||
* Default implementation: Remove first 3 characters ("get") of the method name, and
|
||||
* {@link java.beans.Introspector#decapitalize(String) decapitalize} the result.
|
||||
*
|
||||
* @param methodName The method name, guaranteed to be a non-boolean getter name.
|
||||
* @return The property name corresponding to the method name.
|
||||
@ -69,6 +90,9 @@ public interface AccessorNamingStrategy {
|
||||
/**
|
||||
* Extract the property name from a method name for which {@link #isBooleanGetterName(String)} returned
|
||||
* {@code true}.
|
||||
* <p>
|
||||
* Default implementation: Remove the first 2 characters ("is") of the method name, and
|
||||
* {@link java.beans.Introspector#decapitalize(String) decapitalize} the result.
|
||||
*
|
||||
* @param methodName The method name, guaranteed to be a boolean getter name.
|
||||
* @return The property name corresponding to the method name.
|
||||
@ -77,6 +101,9 @@ public interface AccessorNamingStrategy {
|
||||
|
||||
/**
|
||||
* Extract the property name from a method name for which {@link #isSetterName(String)} returned {@code true}.
|
||||
* <p>
|
||||
* Default implementation: Remove the first 3 characters ("set") of the method name, and
|
||||
* {@link java.beans.Introspector#decapitalize(String) decapitalize} the result.
|
||||
*
|
||||
* @param methodName The method name, guaranteed to be a setter name.
|
||||
* @return The property name corresponding to the method name.
|
||||
@ -86,6 +113,9 @@ public interface AccessorNamingStrategy {
|
||||
/**
|
||||
* Extract the element name (singular form of the collection's property name) from a method name for which
|
||||
* {@link #isAdderName(String)} returned {@code true}.
|
||||
* <p>
|
||||
* Default implementation: Remove the first 3 characters ("add") of the method name, and
|
||||
* {@link java.beans.Introspector#decapitalize(String) decapitalize} the result.
|
||||
*
|
||||
* @param methodName The method name, guaranteed to be an adder name.
|
||||
* @return The element name corresponding to the method name.
|
||||
@ -94,6 +124,8 @@ public interface AccessorNamingStrategy {
|
||||
|
||||
/**
|
||||
* Extract the non-boolean getter method name from the setter method name of the same property.
|
||||
* <p>
|
||||
* Default implementation: Replace the first 3 characters ("get") of the method name with "set".
|
||||
*
|
||||
* @param methodName The method name, guaranteed to be a setter name.
|
||||
* @return The corresponding non-boolean getter method name.
|
||||
|
Loading…
x
Reference in New Issue
Block a user