Skip to content

Commit a15a67f

Browse files
bedlasjaakd
authored andcommitted
mapstruct#636 - fix - default methods (Java 8) do not work anymore
1 parent c43b495 commit a15a67f

File tree

16 files changed

+420
-34
lines changed

16 files changed

+420
-34
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.ap.test.bugs._636;
20+
21+
public class Bar {
22+
private final String id;
23+
24+
public Bar(String id) {
25+
this.id = id;
26+
}
27+
28+
public String getId() {
29+
return id;
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.ap.test.bugs._636;
20+
21+
public class Foo {
22+
private final long id;
23+
24+
public Foo(long id) {
25+
this.id = id;
26+
}
27+
28+
public long getId() {
29+
return id;
30+
}
31+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.ap.test.bugs._636;
20+
21+
import java.math.BigDecimal;
22+
import java.math.BigInteger;
23+
24+
import org.mapstruct.Mapper;
25+
import org.mapstruct.Mapping;
26+
import org.mapstruct.Mappings;
27+
import org.mapstruct.factory.Mappers;
28+
29+
@Mapper
30+
public class MyMapper {
31+
public BigDecimal mapBigIntToDecimal(BigInteger source) {
32+
return source == null ? null : new BigDecimal( source );
33+
}
34+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.ap.test.bugs._636;
20+
21+
import java.math.BigInteger;
22+
23+
public class Source {
24+
private final long idFoo;
25+
private final String idBar;
26+
private final BigInteger number;
27+
28+
public Source(long idFoo, String idBar, BigInteger number) {
29+
this.idFoo = idFoo;
30+
this.idBar = idBar;
31+
this.number = number;
32+
}
33+
34+
public long getIdFoo() {
35+
return idFoo;
36+
}
37+
38+
public String getIdBar() {
39+
return idBar;
40+
}
41+
42+
public BigInteger getNumber() {
43+
return number;
44+
}
45+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.ap.test.bugs._636;
20+
21+
import org.mapstruct.Mapper;
22+
import org.mapstruct.Mapping;
23+
import org.mapstruct.Mappings;
24+
import org.mapstruct.factory.Mappers;
25+
26+
@Mapper(uses = MyMapper.class)
27+
public interface SourceTargetMapper {
28+
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
29+
30+
@Mappings({
31+
@Mapping(source = "idFoo", target = "foo"),
32+
@Mapping(source = "idBar", target = "bar"),
33+
@Mapping(source = "number", target = "number")
34+
})
35+
Target mapSourceToTarget(Source source);
36+
37+
default Foo fooFromId(long id) {
38+
return new Foo(id);
39+
}
40+
41+
static Bar barFromId(String id) {
42+
return new Bar(id);
43+
}
44+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.ap.test.bugs._636;
20+
21+
import java.math.BigDecimal;
22+
23+
public class Target {
24+
private Foo foo;
25+
private Bar bar;
26+
private BigDecimal number;
27+
28+
public Foo getFoo() {
29+
return foo;
30+
}
31+
32+
public void setFoo(Foo foo) {
33+
this.foo = foo;
34+
}
35+
36+
public Bar getBar() {
37+
return bar;
38+
}
39+
40+
public void setBar(Bar bar) {
41+
this.bar = bar;
42+
}
43+
44+
public BigDecimal getNumber() {
45+
return number;
46+
}
47+
48+
public void setNumber(BigDecimal number) {
49+
this.number = number;
50+
}
51+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.ap.test.bugs._636;
20+
21+
import java.math.BigInteger;
22+
23+
import org.junit.Test;
24+
25+
import static org.fest.assertions.Assertions.assertThat;
26+
27+
public class Issue636Test {
28+
29+
@Test
30+
public void shouldMapDataFromJava8Interface() {
31+
32+
final long idFoo = 123;
33+
final String idBar = "Bar456";
34+
final BigInteger number = BigInteger.valueOf( 789L );
35+
36+
final Source source = new Source( idFoo, idBar, number );
37+
38+
final Target target = SourceTargetMapper.INSTANCE.mapSourceToTarget( source );
39+
40+
assertThat( target ).isNotNull();
41+
assertThat( target.getFoo() ).isNotNull();
42+
assertThat( target.getFoo().getId() ).isEqualTo( idFoo );
43+
assertThat( target.getBar() ).isNotNull();
44+
assertThat( target.getBar().getId() ).isEqualTo( idBar );
45+
assertThat( target.getNumber() ).isNotNull();
46+
assertThat( target.getNumber().toBigInteger() ).isEqualTo( number );
47+
}
48+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public abstract class MappingMethod extends ModelElement {
4747
private final Accessibility accessibility;
4848
private final List<Type> thrownTypes;
4949
private final boolean isStatic;
50+
private final Type staticMethodFromInterfaceType;
5051
private final String resultName;
5152
private final List<LifecycleCallbackMethodReference> beforeMappingReferencesWithMappingTarget;
5253
private final List<LifecycleCallbackMethodReference> beforeMappingReferencesWithoutMappingTarget;
@@ -69,6 +70,7 @@ protected MappingMethod(Method method, Collection<String> existingVariableNames,
6970
this.accessibility = method.getAccessibility();
7071
this.thrownTypes = method.getThrownTypes();
7172
this.isStatic = method.isStatic();
73+
this.staticMethodFromInterfaceType = method.getStaticMethodFromInterfaceType();
7274
this.resultName = initResultName( existingVariableNames );
7375
this.beforeMappingReferencesWithMappingTarget = filterMappingTarget( beforeMappingReferences, true );
7476
this.beforeMappingReferencesWithoutMappingTarget = filterMappingTarget( beforeMappingReferences, false );
@@ -144,6 +146,10 @@ public boolean isStatic() {
144146
return isStatic;
145147
}
146148

149+
public Type getStaticMethodFromInterfaceType() {
150+
return staticMethodFromInterfaceType;
151+
}
152+
147153
@Override
148154
public Set<Type> getImportTypes() {
149155
Set<Type> types = new HashSet<Type>();
@@ -154,6 +160,9 @@ public Set<Type> getImportTypes() {
154160

155161
types.add( getReturnType() );
156162
types.addAll( thrownTypes );
163+
if ( staticMethodFromInterfaceType != null ) {
164+
types.add( staticMethodFromInterfaceType );
165+
}
157166
return types;
158167
}
159168

processor/src/main/java/org/mapstruct/ap/internal/model/source/ForgedMethod.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,16 @@ public boolean isStatic() {
192192
return false;
193193
}
194194

195+
@Override
196+
public boolean isDefault() {
197+
return false;
198+
}
199+
200+
@Override
201+
public Type getStaticMethodFromInterfaceType() {
202+
return null;
203+
}
204+
195205
@Override
196206
public MapperConfiguration getMapperConfiguration() {
197207
return null;

processor/src/main/java/org/mapstruct/ap/internal/model/source/Method.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ public interface Method {
144144
*/
145145
boolean isStatic();
146146

147+
/**
148+
* Whether this method is Java 8 default method
149+
*
150+
* @return true when Java 8 default method
151+
*/
152+
boolean isDefault();
153+
154+
/**
155+
* Returns method's enclosing type if method is Java 8 static method
156+
*
157+
* @return type of static method from Java 8 interface
158+
*/
159+
Type getStaticMethodFromInterfaceType();
160+
147161
/**
148162
*
149163
* @return the mapper config when this method needs to be implemented

0 commit comments

Comments
 (0)