Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9185137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:13:13+00:00 2026-06-17T19:13:13+00:00

I have some legacy Java code that defines a generic payload variable somewhere outside

  • 0

I have some legacy Java code that defines a generic payload variable somewhere outside of my control (i.e. I can not change its type):

// Java code
Wrapper<? extends SomeBaseType> payload = ...

I receive such a payload value as a method parameter in my code and want to pass it on to a Scala case class (to use as message with an actor system), but do not get the definitions right such that I do not get at least a compiler warning.

// still Java code
ScalaMessage msg = new ScalaMessage(payload);

This gives a compiler warning “Type safety: contructor… belongs to raw type…”

The Scala case class is defined as:

// Scala code
case class ScalaMessage[T <: SomeBaseType](payload: Wrapper[T]) 

How can I define the case class such that the code compiles cleanly? (sadly, changing the code of the Java Wrapper class or the type of the payload parameter is not an option)

Updated to clarify the origin of the payload parameter

Added For comparison, in Java I can define a parameter just in the same way as the payload variable is defined:

// Java code
void doSomethingWith(Wrapper<? extends SomeBaseType> payload) {}

and call it accordingly

// Java code
doSomethingWith(payload)

But I can’t instantiate e.g. a Wrapper object directly without getting a “raw type” warning. Here, I need to use a static helper method:

static <T> Wrapper<T> of(T value) {
   return new Wrapper<T>(value);
}

and use this static helper to instantiate a Wrapper object:

// Java code
MyDerivedType value = ... // constructed elsewhere, actual type is not known!
Wrapper<? extends SomeBaseType> payload = Wrapper.of(value);

Solution

I can add a similar helper method to a Scala companion object:

// Scala code
object ScalaMessageHelper {
    def apply[T <: SomeBaseType](payload: Wrapper[T]) = 
        new ScalaMessage(payload)
}
object ScalaMessageHelper2 {
    def apply[T <: SomeBaseType](payload: Wrapper[T]) = 
        ScalaMessage(payload) // uses implicit apply() method of case class
}

and use this from Java to instantiate the ScalaMessage class w/o problems:

// Java code
ScalaMessage msg = ScalaMessageHelper.apply(payload);

Unless someone comes up with a more elegant solution, I will extract this as an answer…

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:13:14+00:00Added an answer on June 17, 2026 at 7:13 pm

    I think the problem is that in Java if you do the following:

    ScalaMessage msg = new ScalaMessage(payload);
    

    Then you are instantiating ScalaMessage using its raw type. Or in other words, you use ScalaMessage as a non generic type (when Java introduced generics, they kept the ability to treat a generic class as a non-generic one, mostly for backward compatibility).

    You should simply specify the type parameters when instantiating ScalaMessage:

    // (here T = MyDerivedType, where MyDerivedType must extend SomeBaseType
    ScalaMessage<MyDerivedType> msg = new ScalaMessage<>(payload);
    

    UPDATE: After seeing your comment, I actually tried it in a dummy project, and I actually get an error:

    [error] C:\Code\sandbox\src\main\java\bla\Test.java:8: cannot find symbol
    [error] symbol  : constructor ScalaMessage(bla.Wrapper<capture#64 of ? extends bla.SomeBaseType>)
    [error] location: class test.ScalaMessage<bla.SomeBaseType>
    [error]     ScalaMessage<SomeBaseType> msg = new ScalaMessage<SomeBaseType>(payload);
    

    It seems like a mismatch between java generics (that we can emulate through exitsentials in scala ) and scala generics. You can fix this by just dropping the type parameter in ScalaMessage and using existentials instead:

    case class ScalaMessage(payload: Wrapper[_ <: SomeBaseType]) 
    

    and then instantiate it in java like this:

    new ScalaMessage(payload)
    

    This works. However, now ScalaMessage is not generic anymore, which might be a problem if you want use it with more refined paylods (say a Wrapper<? extends MyDerivedType>).

    To fix this, let’s do yet another small change to ScalaMessage:

    case class ScalaMessage[T<:SomeBaseType](payload: Wrapper[_ <: T]) 
    

    And then in java:

    ScalaMessage<SomeBaseType> msg = new ScalaMessage<SomeBaseType>(payload);
    

    Problem solved 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you have legacy java code which can not be compiled by an up-to-date
We have some legacy ASP.NET code that detects if a request is secure, and
I have some legacy code that was used to monitor my applications cpu,memory etc
I have some legacy C++ code that I am trying to understand a bit
I have some legacy Java code inside which I'd like to call a groovy
I have some 10 year old Java code for calling a legacy SOAP authentication
I have some legacy code that I am now trying to re-use under Spring.
I have some data (produced by a legacy application) that I know is invalid
I need to integrate some legacy 32-bit code - for which I don't have
I have encountered a weird situation while updating/upgrading some legacy code. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.