Skip to content

Commit

Permalink
#3717 Fix ClassCastException when getting thrown types for a record a…
Browse files Browse the repository at this point in the history
…ccessor
  • Loading branch information
filiphr committed Sep 16, 2024
1 parent c74e62a commit a3b4139
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.itest.records.nested;

/**
* @author Filip Hrisafov
*/
public record Address(String street, String city) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.itest.records.nested;

/**
* @author Filip Hrisafov
*/
public record CareProvider(String externalId, Address address) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.itest.records.nested;

/**
* @author Filip Hrisafov
*/
public record CareProviderDto(String id, String street, String city) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.itest.records.nested;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;

/**
* @author Filip Hrisafov
*/
@Mapper(unmappedTargetPolicy = ReportingPolicy.ERROR)
public interface CareProviderMapper {

CareProviderMapper INSTANCE = Mappers.getMapper( CareProviderMapper.class );

@Mapping(target = "id", source = "externalId")
@Mapping(target = "street", source = "address.street")
@Mapping(target = "city", source = "address.city")
CareProviderDto map(CareProvider source);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.itest.records.nested;

import java.util.Arrays;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;

public class NestedRecordsTest {

@Test
public void shouldMapRecord() {
CareProvider source = new CareProvider( "kermit", new Address( "Sesame Street", "New York" ) );
CareProviderDto target = CareProviderMapper.INSTANCE.map( source );

assertThat( target ).isNotNull();
assertThat( target.id() ).isEqualTo( "kermit" );
assertThat( target.street() ).isEqualTo( "Sesame Street" );
assertThat( target.city() ).isEqualTo( "New York" );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,11 @@ public List<Type> getThrownTypes(Accessor accessor) {
if (accessor.getAccessorType().isFieldAssignment()) {
return new ArrayList<>();
}
return extractTypes( ( (ExecutableElement) accessor.getElement() ).getThrownTypes() );
Element element = accessor.getElement();
if ( element instanceof ExecutableElement ) {
return extractTypes( ( (ExecutableElement) element ).getThrownTypes() );
}
return new ArrayList<>();
}

private List<Type> extractTypes(List<? extends TypeMirror> typeMirrors) {
Expand Down

0 comments on commit a3b4139

Please sign in to comment.