#669 Making 'haxXYZ' the default presenceChecker

This commit is contained in:
sjaakd 2016-07-06 20:43:36 +02:00
parent b1f03689d9
commit e725b9a2c6
3 changed files with 11 additions and 56 deletions

View File

@ -47,6 +47,9 @@ public class DefaultAccessorNamingStrategy implements AccessorNamingStrategy {
else if ( isAdderMethod( method ) ) {
return MethodType.ADDER;
}
else if ( isPresenceCheckMethod( method ) ) {
return MethodType.PRESENCE_CHECKER;
}
else {
return MethodType.OTHER;
}
@ -123,4 +126,12 @@ public class DefaultAccessorNamingStrategy implements AccessorNamingStrategy {
return typeElement != null ? typeElement.getQualifiedName().toString() : null;
}
private boolean isPresenceCheckMethod(ExecutableElement method) {
String methodName = method.getSimpleName().toString();
return methodName.startsWith( "has" ) && methodName.length() > 3 &&
( method.getReturnType().getKind() == TypeKind.BOOLEAN ||
"java.lang.Boolean".equals( getQualifiedName( method.getReturnType() ) ) );
}
}

View File

@ -1,53 +0,0 @@
/**
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.spi;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.TypeKind;
/**
* The default JavaBeans-compliant implementation of the {@link AccessorNamingStrategy} service provider interface.
*
* @author Sjaak Derksen
*/
public class PresenceCheckAccessorNamingStrategy
extends DefaultAccessorNamingStrategy
implements AccessorNamingStrategy {
@Override
public MethodType getMethodType(ExecutableElement method) {
if ( isPresenceCheckMethod( method ) ) {
return MethodType.PRESENCE_CHECKER;
}
else {
return super.getMethodType( method );
}
}
private boolean isPresenceCheckMethod(ExecutableElement method) {
String methodName = method.getSimpleName().toString();
return methodName.startsWith( "has" ) && methodName.length() > 3 &&
( method.getReturnType().getKind() == TypeKind.BOOLEAN ||
"java.lang.Boolean".equals( getQualifiedName( method.getReturnType() ) ) );
}
}

View File

@ -23,10 +23,8 @@ import static org.fest.assertions.Assertions.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mapstruct.ap.spi.PresenceCheckAccessorNamingStrategy;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
import org.mapstruct.ap.testutil.WithServiceImplementation;
/**
* Test for correct handling of source presence checks.
@ -42,7 +40,6 @@ import org.mapstruct.ap.testutil.WithServiceImplementation;
GoalKeeper.class,
SoccerTeamTarget.class
})
@WithServiceImplementation( PresenceCheckAccessorNamingStrategy.class )
@RunWith(AnnotationProcessorTestRunner.class)
public class PresenceCheckTest {