Raising an error when trying to generate a mapping from primitive to reference type or vice versa

This commit is contained in:
Gunnar Morling 2013-05-20 12:55:15 +02:00
parent b826304d4c
commit 0c6c28f13c
6 changed files with 52 additions and 25 deletions

View File

@ -21,6 +21,7 @@ package org.mapstruct.ap;
import java.beans.Introspector;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
@ -69,7 +70,7 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
private final TypeUtil typeUtil;
private final Options options;
private boolean mappingErronuous = false;
private boolean mappingErroneous = false;
public MapperGenerationVisitor(ProcessingEnvironment processingEnvironment, Options options) {
this.processingEnvironment = processingEnvironment;
@ -83,7 +84,7 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
public Void visitTypeAsInterface(TypeElement element, Void p) {
Mapper model = retrieveModel( element );
if ( !mappingErronuous ) {
if ( !mappingErroneous ) {
String sourceFileName = element.getQualifiedName() + IMPLEMENTATION_SUFFIX;
writeModelToSourceFile( sourceFileName, model );
}
@ -254,7 +255,7 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
method.getExecutable()
);
mappingErronuous = true;
mappingErroneous = true;
if ( reverseMethod == null ||
reversePropertyMappingMethod != null ||
@ -349,7 +350,29 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
for ( ExecutableElement method : methodsIn( element.getEnclosedElements() ) ) {
Parameter parameter = retrieveParameter( method );
Type returnType = retrieveReturnType( method );
List<MappedProperty> properties = retrieveMappedProperties( method );
if ( declaringMapper == null ) {
if ( parameter.getType().isIterableType() && !returnType.isIterableType() ) {
reportError( "Can't generate mapping method from iterable type to non-iterable ype.", method );
}
if ( !parameter.getType().isIterableType() && returnType.isIterableType() ) {
reportError( "Can't generate mapping method from non-iterable type to iterable ype.", method );
}
if ( parameter.getType().isPrimitive() ) {
reportError( "Can't generate mapping method with primitive parameter type.", method );
}
if ( returnType.isPrimitive() ) {
reportError( "Can't generate mapping method with primitive return type.", method );
}
if ( mappingErroneous ) {
continue;
}
}
//retrieve property mappings if an implementation for the method needs to be generated
List<MappedProperty> properties = declaringMapper == null ? retrieveMappedProperties( method ) : Collections
.<MappedProperty>emptyList();
methods.add(
new Method(
@ -361,16 +384,6 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
properties
)
);
if ( declaringMapper == null ) {
if ( parameter.getType().isIterableType() && !returnType.isIterableType() ) {
reportError( "Can't generate mapping method from iterable type to non-iterable ype.", method );
}
if ( !parameter.getType().isIterableType() && returnType.isIterableType() ) {
reportError( "Can't generate mapping method from non-iterable type to iterable ype.", method );
}
}
}
MapperPrism mapperPrism = MapperPrism.getInstanceOn( element );
@ -481,6 +494,6 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
private void reportError(String message, Element element) {
processingEnvironment.getMessager().printMessage( Kind.ERROR, message, element );
mappingErronuous = true;
mappingErroneous = true;
}
}

View File

@ -35,4 +35,12 @@ public class Mapping {
public String getTargetName() {
return targetName;
}
@Override
public String toString() {
return "Mapping {" +
"\n sourceName='" + sourceName + "\'," +
"\n targetName='" + targetName + "\'," +
"\n}";
}
}

View File

@ -16,14 +16,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.test.erronuous;
package org.mapstruct.ap.test.erroneous;
import org.mapstruct.Mapper;
@Mapper
public interface ErronuousMapper {
public interface ErroneousMapper {
Target sourceToTarget(Source source);
Source targetToSource(Target target);
long sourceToLong(Source source);
Source longToSource(long id);
}

View File

@ -16,7 +16,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.test.erronuous;
package org.mapstruct.ap.test.erroneous;
import javax.tools.Diagnostic.Kind;
@ -28,18 +28,20 @@ import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic;
import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome;
import org.testng.annotations.Test;
@WithClasses({ ErronuousMapper.class, Source.class, Target.class })
public class MappedPropertiesWithDifferentTypesTest extends MapperTestBase {
@WithClasses({ ErroneousMapper.class, Source.class, Target.class })
public class ErroneousMappingsTest extends MapperTestBase {
@Test
@IssueKey("6")
@ExpectedCompilationOutcome(
value = CompilationResult.FAILED,
diagnostics = {
@Diagnostic(type = ErronuousMapper.class, kind = Kind.ERROR, line = 26),
@Diagnostic(type = ErronuousMapper.class, kind = Kind.ERROR, line = 28)
@Diagnostic(type = ErroneousMapper.class, kind = Kind.ERROR, line = 26),
@Diagnostic(type = ErroneousMapper.class, kind = Kind.ERROR, line = 28),
@Diagnostic(type = ErroneousMapper.class, kind = Kind.ERROR, line = 30),
@Diagnostic(type = ErroneousMapper.class, kind = Kind.ERROR, line = 32)
}
)
public void shouldFailToGenerateMappingFromListToString() {
public void shouldFailToGenerateMappings() {
}
}

View File

@ -16,7 +16,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.test.erronuous;
package org.mapstruct.ap.test.erroneous;
public class Source {

View File

@ -16,7 +16,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.test.erronuous;
package org.mapstruct.ap.test.erroneous;
public class Target {