mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#6 Raising error in case of mappings from iterable to non-iterable types and vice versa
This commit is contained in:
parent
eec600c3bf
commit
50d916c370
@ -82,6 +82,11 @@
|
||||
<artifactId>fest-assert</artifactId>
|
||||
<version>1.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>14.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jolira</groupId>
|
||||
<artifactId>hickory</artifactId>
|
||||
|
@ -61,6 +61,11 @@
|
||||
<artifactId>fest-assert</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -34,6 +34,7 @@ import javax.lang.model.type.TypeMirror;
|
||||
import javax.lang.model.util.ElementKindVisitor6;
|
||||
import javax.lang.model.util.Elements;
|
||||
import javax.lang.model.util.Types;
|
||||
import javax.tools.Diagnostic.Kind;
|
||||
import javax.tools.JavaFileObject;
|
||||
|
||||
import org.mapstruct.ap.conversion.Conversion;
|
||||
@ -308,6 +309,26 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
||||
properties
|
||||
)
|
||||
);
|
||||
|
||||
if ( declaringMapper == null ) {
|
||||
if ( parameter.getType().isIterableType() && !returnType.isIterableType() ) {
|
||||
processingEnvironment.getMessager()
|
||||
.printMessage(
|
||||
Kind.ERROR,
|
||||
"Can't generate mapping method from iterable type to non-iterable ype.",
|
||||
method
|
||||
);
|
||||
}
|
||||
|
||||
if ( !parameter.getType().isIterableType() && returnType.isIterableType() ) {
|
||||
processingEnvironment.getMessager()
|
||||
.printMessage(
|
||||
Kind.ERROR,
|
||||
"Can't generate mapping method from non-iterable type to iterable ype.",
|
||||
method
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MapperPrism mapperPrism = MapperPrism.getInstanceOn( element );
|
||||
|
@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
*
|
||||
* 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.test.collection.erronuous;
|
||||
|
||||
import javax.tools.Diagnostic.Kind;
|
||||
|
||||
import org.mapstruct.ap.testutil.IssueKey;
|
||||
import org.mapstruct.ap.testutil.MapperTestBase;
|
||||
import org.mapstruct.ap.testutil.WithClasses;
|
||||
import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult;
|
||||
import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic;
|
||||
import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@WithClasses({ ErronuousMapper.class })
|
||||
public class ErronuousCollectionMappingTest extends MapperTestBase {
|
||||
|
||||
@Test
|
||||
@IssueKey("6")
|
||||
@ExpectedCompilationOutcome(
|
||||
value = CompilationResult.FAILED,
|
||||
diagnostics = {
|
||||
@Diagnostic(type = ErronuousMapper.class, kind = Kind.ERROR, line = 25),
|
||||
@Diagnostic(type = ErronuousMapper.class, kind = Kind.ERROR, line = 27)
|
||||
}
|
||||
)
|
||||
public void shouldFailToGenerateMappingFromListToString() {
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
*
|
||||
* 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.test.collection.erronuous;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface ErronuousMapper {
|
||||
|
||||
Integer stringSetToInteger(Set<String> strings);
|
||||
|
||||
Set<String> integerToStringSet(Integer integer);
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
/**
|
||||
* Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
*
|
||||
* 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.test.collection.iterabletononiterable;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.mapstruct.ap.testutil.IssueKey;
|
||||
import org.mapstruct.ap.testutil.MapperTestBase;
|
||||
import org.mapstruct.ap.testutil.WithClasses;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
|
||||
@WithClasses({ Source.class, Target.class, SourceTargetMapper.class, StringListMapper.class })
|
||||
public class IterableToNonIterableMappingTest extends MapperTestBase {
|
||||
|
||||
@Test
|
||||
@IssueKey("6")
|
||||
public void shouldMapStringListToStringUsingCustomMapper() {
|
||||
Source source = new Source();
|
||||
source.setNames( Arrays.asList( "Alice", "Bob", "Jim" ) );
|
||||
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
|
||||
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getNames() ).isEqualTo( "Alice-Bob-Jim" );
|
||||
}
|
||||
|
||||
@Test
|
||||
@IssueKey("6")
|
||||
public void shouldReverseMapStringListToStringUsingCustomMapper() {
|
||||
Target target = new Target();
|
||||
target.setNames( "Alice-Bob-Jim" );
|
||||
|
||||
Source source = SourceTargetMapper.INSTANCE.targetToSource( target );
|
||||
|
||||
assertThat( source ).isNotNull();
|
||||
assertThat( source.getNames() ).isEqualTo( Arrays.asList( "Alice", "Bob", "Jim" ) );
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
*
|
||||
* 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.test.collection.iterabletononiterable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Source {
|
||||
|
||||
private List<String> names;
|
||||
|
||||
public List<String> getNames() {
|
||||
return names;
|
||||
}
|
||||
|
||||
public void setNames(List<String> names) {
|
||||
this.names = names;
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
/**
|
||||
* Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
*
|
||||
* 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.test.collection.iterabletononiterable;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mappers;
|
||||
|
||||
@Mapper(uses = StringListMapper.class)
|
||||
public interface SourceTargetMapper {
|
||||
|
||||
public static SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
|
||||
|
||||
Target sourceToTarget(Source source);
|
||||
|
||||
Source targetToSource(Target target);
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
*
|
||||
* 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.test.collection.iterabletononiterable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
|
||||
public class StringListMapper {
|
||||
|
||||
public String stringListToString(List<String> strings) {
|
||||
return strings == null ? null : Joiner.on( "-" ).join( strings );
|
||||
}
|
||||
|
||||
public List<String> stringToStringList(String string) {
|
||||
return string == null ? null : Arrays.asList( string.split( "-" ) );
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
/**
|
||||
* Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
*
|
||||
* 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.test.collection.iterabletononiterable;
|
||||
|
||||
public class Target {
|
||||
|
||||
private String names;
|
||||
|
||||
public String getNames() {
|
||||
return names;
|
||||
}
|
||||
|
||||
public void setNames(String names) {
|
||||
this.names = names;
|
||||
}
|
||||
}
|
@ -59,7 +59,7 @@ public abstract class MapperTestBase {
|
||||
private DiagnosticCollector<JavaFileObject> diagnostics;
|
||||
|
||||
public MapperTestBase() {
|
||||
this.libraries = Arrays.asList( "mapstruct.jar" );
|
||||
this.libraries = Arrays.asList( "mapstruct.jar", "guava.jar" );
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
@ -107,7 +107,7 @@ public abstract class MapperTestBase {
|
||||
|
||||
if ( expectedResult.getCompilationResult() == CompilationResult.SUCCEEDED ) {
|
||||
assertThat( actualResult.getCompilationResult() )
|
||||
.describedAs( "Compilation failed. Diagnostics: " + actualResult.getDiagnostics() )
|
||||
.describedAs( "Compilation failed. Diagnostics: " + diagnostics.getDiagnostics() )
|
||||
.isEqualTo( CompilationResult.SUCCEEDED );
|
||||
}
|
||||
else {
|
||||
|
@ -19,6 +19,7 @@ import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import javax.tools.Diagnostic.Kind;
|
||||
import javax.tools.JavaFileObject;
|
||||
|
||||
import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult;
|
||||
@ -63,7 +64,10 @@ public class CompilationOutcomeDescriptor {
|
||||
|
||||
Set<DiagnosticDescriptor> diagnosticDescriptors = new HashSet<DiagnosticDescriptor>();
|
||||
for ( javax.tools.Diagnostic<? extends JavaFileObject> diagnostic : diagnostics ) {
|
||||
diagnosticDescriptors.add( DiagnosticDescriptor.forDiagnostic( sourceDir, diagnostic ) );
|
||||
//ignore notes created by the compiler
|
||||
if ( diagnostic.getKind() != Kind.NOTE ) {
|
||||
diagnosticDescriptors.add( DiagnosticDescriptor.forDiagnostic( sourceDir, diagnostic ) );
|
||||
}
|
||||
}
|
||||
|
||||
return new CompilationOutcomeDescriptor( compilationResult, diagnosticDescriptors );
|
||||
|
Loading…
x
Reference in New Issue
Block a user