Skip to content

Commit 9996fc6

Browse files
author
sjaakd
committed
mapstruct#878 disable AcessorNamingStrategy#getCollectionGetterName from SPI
1 parent e57f4c9 commit 9996fc6

6 files changed

Lines changed: 23 additions & 63 deletions

File tree

integrationtest/src/test/resources/namingStrategyTest/strategy/src/main/java/org/mapstruct/itest/naming/CustomAccessorNamingStrategy.java

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,29 @@
2424
import javax.lang.model.type.TypeKind;
2525

2626
import org.mapstruct.ap.spi.AccessorNamingStrategy;
27-
import org.mapstruct.ap.spi.MethodType;
27+
import org.mapstruct.ap.spi.DefaultAccessorNamingStrategy;
2828

2929
/**
3030
* A custom {@link AccessorNamingStrategy} recognizing getters in the form of {@code property()} and setters in the
3131
* form of {@code withProperty(value)}.
3232
*
3333
* @author Gunnar Morling
3434
*/
35-
public class CustomAccessorNamingStrategy implements AccessorNamingStrategy {
35+
public class CustomAccessorNamingStrategy extends DefaultAccessorNamingStrategy implements AccessorNamingStrategy {
3636

3737
@Override
38-
public MethodType getMethodType(ExecutableElement method) {
39-
if ( isGetterMethod( method ) ) {
40-
return MethodType.GETTER;
41-
}
42-
else if ( isSetterMethod( method ) ) {
43-
return MethodType.SETTER;
44-
}
45-
else if ( isAdderMethod( method ) ) {
46-
return MethodType.ADDER;
47-
}
48-
else {
49-
return MethodType.OTHER;
50-
}
51-
}
52-
53-
private boolean isGetterMethod(ExecutableElement method) {
38+
public boolean isGetterMethod(ExecutableElement method) {
5439
return method.getReturnType().getKind() != TypeKind.VOID;
5540
}
5641

42+
@Override
5743
public boolean isSetterMethod(ExecutableElement method) {
5844
String methodName = method.getSimpleName().toString();
5945

6046
return methodName.startsWith( "with" ) && methodName.length() > 4;
6147
}
6248

49+
@Override
6350
public boolean isAdderMethod(ExecutableElement method) {
6451
String methodName = method.getSimpleName().toString();
6552
return methodName.startsWith( "add" ) && methodName.length() > 3;
@@ -77,8 +64,4 @@ public String getElementName(ExecutableElement adderMethod) {
7764
return Introspector.decapitalize( methodName.substring( 3 ) );
7865
}
7966

80-
@Override
81-
public String getCollectionGetterName(String property) {
82-
return property.substring( 0, 1 ).toUpperCase() + property.substring( 1 );
83-
}
8467
}

processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ private Assignment assignToCollection(Type targetType,
462462
// target accessor is setter, so wrap the setter in setter map/ collection handling
463463
result = new SetterWrapperForCollectionsAndMaps(
464464
result,
465-
Executables.getCollectionGetterName( targetWriteAccessor ),
465+
targetReadAccessor.getSimpleName().toString(),
466466
newCollectionOrMap,
467467
targetType,
468468
existingVariableNames

processor/src/main/java/org/mapstruct/ap/internal/util/Executables.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,6 @@ public static String getElementNameForAdder(ExecutableElement adderMethod) {
125125
return ACCESSOR_NAMING_STRATEGY.getElementName( adderMethod );
126126
}
127127

128-
public static String getCollectionGetterName(ExecutableElement targetSetter) {
129-
String propertyName = ACCESSOR_NAMING_STRATEGY.getPropertyName( targetSetter );
130-
return ACCESSOR_NAMING_STRATEGY.getCollectionGetterName( propertyName );
131-
}
132-
133128
/**
134129
* @param mirror the type mirror
135130
*

processor/src/main/java/org/mapstruct/ap/spi/AccessorNamingStrategy.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public interface AccessorNamingStrategy {
6060
*/
6161
String getElementName(ExecutableElement adderMethod);
6262

63+
6364
/**
6465
* Returns the getter name of the given collection property.
6566
* <p>
@@ -68,6 +69,11 @@ public interface AccessorNamingStrategy {
6869
* @param property to be getterOrSetterMethod.
6970
*
7071
* @return getter name for collection properties
72+
*
73+
* @deprecated MapStuct will not call this method anymore. Use {@link #getMethodType(ExecutableElement)} to
74+
* determine the {@link MethodType}. When collections somehow need to be treated special, it should be done in
75+
* {@link #getMethodType(ExecutableElement) } as well. In the future, this method will be removed.
7176
*/
77+
@Deprecated
7278
String getCollectionGetterName(String property);
7379
}

processor/src/main/java/org/mapstruct/ap/spi/DefaultAccessorNamingStrategy.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -163,19 +163,6 @@ public String getElementName(ExecutableElement adderMethod) {
163163
return Introspector.decapitalize( methodName.substring( 3 ) );
164164
}
165165

166-
/**
167-
* Returns the getter name of a collection given the property name. This will start with 'get' and the
168-
* first character of the remainder will be placed in upper case.
169-
*
170-
* @param property the property
171-
*
172-
* @return getter name for collections.
173-
*/
174-
@Override
175-
public String getCollectionGetterName(String property) {
176-
return "get" + property.substring( 0, 1 ).toUpperCase() + property.substring( 1 );
177-
}
178-
179166
/**
180167
* Helper method, to obtain the fully qualified name of a type.
181168
*
@@ -211,4 +198,10 @@ public TypeElement visitType(TypeElement e, Void p) {
211198
return typeElement != null ? typeElement.getQualifiedName().toString() : null;
212199
}
213200

201+
@Override
202+
public String getCollectionGetterName(String property) {
203+
throw new IllegalStateException( "This method is not intended to be called anymore and will be removed in "
204+
+ "future versions." );
205+
}
206+
214207
}

processor/src/test/java/org/mapstruct/ap/test/naming/spi/CustomAccessorNamingStrategy.java

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,29 @@
2424
import javax.lang.model.type.TypeKind;
2525

2626
import org.mapstruct.ap.spi.AccessorNamingStrategy;
27-
import org.mapstruct.ap.spi.MethodType;
27+
import org.mapstruct.ap.spi.DefaultAccessorNamingStrategy;
2828

2929
/**
3030
* A custom {@link AccessorNamingStrategy} recognizing getters in the form of {@code property()} and setters in the
3131
* form of {@code withProperty(value)}.
3232
*
3333
* @author Gunnar Morling
3434
*/
35-
public class CustomAccessorNamingStrategy implements AccessorNamingStrategy {
35+
public class CustomAccessorNamingStrategy extends DefaultAccessorNamingStrategy implements AccessorNamingStrategy {
3636

3737
@Override
38-
public MethodType getMethodType(ExecutableElement method) {
39-
if ( isGetterMethod( method ) ) {
40-
return MethodType.GETTER;
41-
}
42-
else if ( isSetterMethod( method ) ) {
43-
return MethodType.SETTER;
44-
}
45-
else if ( isAdderMethod( method ) ) {
46-
return MethodType.ADDER;
47-
}
48-
else {
49-
return MethodType.OTHER;
50-
}
51-
}
52-
53-
private boolean isGetterMethod(ExecutableElement method) {
38+
public boolean isGetterMethod(ExecutableElement method) {
5439
return method.getReturnType().getKind() != TypeKind.VOID;
5540
}
5641

42+
@Override
5743
public boolean isSetterMethod(ExecutableElement method) {
5844
String methodName = method.getSimpleName().toString();
5945

6046
return methodName.startsWith( "with" ) && methodName.length() > 4;
6147
}
6248

49+
@Override
6350
public boolean isAdderMethod(ExecutableElement method) {
6451
String methodName = method.getSimpleName().toString();
6552
return methodName.startsWith( "add" ) && methodName.length() > 3;
@@ -77,8 +64,4 @@ public String getElementName(ExecutableElement adderMethod) {
7764
return Introspector.decapitalize( methodName.substring( 3 ) );
7865
}
7966

80-
@Override
81-
public String getCollectionGetterName(String property) {
82-
return property.substring( 0, 1 ).toUpperCase() + property.substring( 1 );
83-
}
8467
}

0 commit comments

Comments
 (0)