mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
Raising an error when trying to generate a mapping from primitive to reference type or vice versa
This commit is contained in:
parent
b826304d4c
commit
0c6c28f13c
@ -21,6 +21,7 @@ package org.mapstruct.ap;
|
|||||||
import java.beans.Introspector;
|
import java.beans.Introspector;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
@ -69,7 +70,7 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
private final TypeUtil typeUtil;
|
private final TypeUtil typeUtil;
|
||||||
private final Options options;
|
private final Options options;
|
||||||
|
|
||||||
private boolean mappingErronuous = false;
|
private boolean mappingErroneous = false;
|
||||||
|
|
||||||
public MapperGenerationVisitor(ProcessingEnvironment processingEnvironment, Options options) {
|
public MapperGenerationVisitor(ProcessingEnvironment processingEnvironment, Options options) {
|
||||||
this.processingEnvironment = processingEnvironment;
|
this.processingEnvironment = processingEnvironment;
|
||||||
@ -83,7 +84,7 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
public Void visitTypeAsInterface(TypeElement element, Void p) {
|
public Void visitTypeAsInterface(TypeElement element, Void p) {
|
||||||
Mapper model = retrieveModel( element );
|
Mapper model = retrieveModel( element );
|
||||||
|
|
||||||
if ( !mappingErronuous ) {
|
if ( !mappingErroneous ) {
|
||||||
String sourceFileName = element.getQualifiedName() + IMPLEMENTATION_SUFFIX;
|
String sourceFileName = element.getQualifiedName() + IMPLEMENTATION_SUFFIX;
|
||||||
writeModelToSourceFile( sourceFileName, model );
|
writeModelToSourceFile( sourceFileName, model );
|
||||||
}
|
}
|
||||||
@ -254,7 +255,7 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
method.getExecutable()
|
method.getExecutable()
|
||||||
);
|
);
|
||||||
|
|
||||||
mappingErronuous = true;
|
mappingErroneous = true;
|
||||||
|
|
||||||
if ( reverseMethod == null ||
|
if ( reverseMethod == null ||
|
||||||
reversePropertyMappingMethod != null ||
|
reversePropertyMappingMethod != null ||
|
||||||
@ -349,7 +350,29 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
for ( ExecutableElement method : methodsIn( element.getEnclosedElements() ) ) {
|
for ( ExecutableElement method : methodsIn( element.getEnclosedElements() ) ) {
|
||||||
Parameter parameter = retrieveParameter( method );
|
Parameter parameter = retrieveParameter( method );
|
||||||
Type returnType = retrieveReturnType( 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(
|
methods.add(
|
||||||
new Method(
|
new Method(
|
||||||
@ -361,16 +384,6 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
properties
|
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 );
|
MapperPrism mapperPrism = MapperPrism.getInstanceOn( element );
|
||||||
@ -481,6 +494,6 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
|
|
||||||
private void reportError(String message, Element element) {
|
private void reportError(String message, Element element) {
|
||||||
processingEnvironment.getMessager().printMessage( Kind.ERROR, message, element );
|
processingEnvironment.getMessager().printMessage( Kind.ERROR, message, element );
|
||||||
mappingErronuous = true;
|
mappingErroneous = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,4 +35,12 @@ public class Mapping {
|
|||||||
public String getTargetName() {
|
public String getTargetName() {
|
||||||
return targetName;
|
return targetName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Mapping {" +
|
||||||
|
"\n sourceName='" + sourceName + "\'," +
|
||||||
|
"\n targetName='" + targetName + "\'," +
|
||||||
|
"\n}";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,14 +16,18 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.test.erronuous;
|
package org.mapstruct.ap.test.erroneous;
|
||||||
|
|
||||||
import org.mapstruct.Mapper;
|
import org.mapstruct.Mapper;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface ErronuousMapper {
|
public interface ErroneousMapper {
|
||||||
|
|
||||||
Target sourceToTarget(Source source);
|
Target sourceToTarget(Source source);
|
||||||
|
|
||||||
Source targetToSource(Target target);
|
Source targetToSource(Target target);
|
||||||
|
|
||||||
|
long sourceToLong(Source source);
|
||||||
|
|
||||||
|
Source longToSource(long id);
|
||||||
}
|
}
|
@ -16,7 +16,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.test.erronuous;
|
package org.mapstruct.ap.test.erroneous;
|
||||||
|
|
||||||
import javax.tools.Diagnostic.Kind;
|
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.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
@WithClasses({ ErronuousMapper.class, Source.class, Target.class })
|
@WithClasses({ ErroneousMapper.class, Source.class, Target.class })
|
||||||
public class MappedPropertiesWithDifferentTypesTest extends MapperTestBase {
|
public class ErroneousMappingsTest extends MapperTestBase {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@IssueKey("6")
|
@IssueKey("6")
|
||||||
@ExpectedCompilationOutcome(
|
@ExpectedCompilationOutcome(
|
||||||
value = CompilationResult.FAILED,
|
value = CompilationResult.FAILED,
|
||||||
diagnostics = {
|
diagnostics = {
|
||||||
@Diagnostic(type = ErronuousMapper.class, kind = Kind.ERROR, line = 26),
|
@Diagnostic(type = ErroneousMapper.class, kind = Kind.ERROR, line = 26),
|
||||||
@Diagnostic(type = ErronuousMapper.class, kind = Kind.ERROR, line = 28)
|
@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() {
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -16,7 +16,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.test.erronuous;
|
package org.mapstruct.ap.test.erroneous;
|
||||||
|
|
||||||
public class Source {
|
public class Source {
|
||||||
|
|
@ -16,7 +16,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.test.erronuous;
|
package org.mapstruct.ap.test.erroneous;
|
||||||
|
|
||||||
public class Target {
|
public class Target {
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user