Skip to content

URL conversion uses deprecated constructor new URL(String) in Java 20+ #4041

@yvasyliev

Description

@yvasyliev

Expected behavior

In Java 20 and later, the URL(String) constructor has been deprecated in favor of using URI to handle the parsing logic:

if ( url != null ) {
    url1 = URI.create( url ).toURL();
}

Actual behavior

Currently, MapStruct generates code using the deprecated constructor when mapping a String to a java.net.URL:

if ( url != null ) {
    url1 = new URL( url );
}

Steps to reproduce the problem

  1. Use Java 20 or higher.
  2. Define a mapper that converts a String to a java.net.URL (or a record/bean containing a URL).
  3. Observe the generated implementation.

Example:

@Mapper
public interface ProductMapper {
    Product map(String url) throws MalformedURLException;

    record Product(URL url) {}
}

Generated implementation:

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2026-04-23T12:14:28+0300",
    comments = "version: 1.6.3, compiler: IncrementalProcessingEnvironment from gradle-language-java-9.4.1.jar, environment: Java 25.0.1 (Eclipse Adoptium)"
)
public class ProductMapperImpl implements ProductMapper {

    @Override
    public ProductMapper.Product map(String url) throws MalformedURLException {
        if ( url == null ) {
            return null;
        }

        URL url1 = null;

        if ( url != null ) {
            url1 = new URL( url );
        }

        ProductMapper.Product product = new ProductMapper.Product( url1 );

        return product;
    }
}

Compiling the mapper above will produce the following message in the console output:

Note: C:\Users\Yevhen\IdeaProjects\gradle-mapstruct-demo\build\generated\sources\annotationProcessor\java\main\io\github\yvasyliev\mapstruct\ProductMapperImpl.java uses or overrides a deprecated API.

MapStruct Version

1.6.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions