Java interop: non-static generic inner class

84 views
Skip to first unread message

Dzmitry Lazerka

unread,
Oct 7, 2011, 4:33:34 AM10/7/11
to scala...@googlegroups.com
I'm trying to override a Java method:
  • with "override" keyword compiler says it overrides nothing.
  • without "override" keyword compiler says "name clash between defined and inherited member ... have same type after erasure"
My Scala code:
import org.apache.hadoop.mapreduce.Mapper
class MyMapper extends Mapper[String, String, String, String] {
  override def setup(context: Context){
  }
}

Library Java code:
public class Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT> {
  public class Context extends MapContext<KEYIN,VALUEIN,KEYOUT,VALUEOUT> {}
  protected void setup(Context context) throws IOException, InterruptedException {}
}

Error:  method setup overrides nothing

Without "override": name clash between defined and inherited member:
  method setup:(context: MyMapper.this.Context)Unit and
  method setup:(x$1: org.apache.hadoop.mapreduce.Mapper[String,String,String,String]#Context)Unit in class Mapper
  have same type after erasure: (context: org.apache.hadoop.mapreduce.Mapper#Context)Unit
    def setup(context: Context){

Any ideas?

Dzmitry Lazerka

unread,
Oct 7, 2011, 4:39:18 AM10/7/11
to scala...@googlegroups.com
I was unable to make work any StackOverflow answers on this question.

Jason Zaugg

unread,
Oct 7, 2011, 5:21:52 AM10/7/11
to scala...@googlegroups.com
If you're just trying to get things moving, you could write MyMapper in Java and delegate to an implementation in Scala.

-jason

Kevin Wright

unread,
Oct 7, 2011, 5:28:25 AM10/7/11
to Jason Zaugg, scala...@googlegroups.com
This looks familiar...

I think I had to specify @throws[classOf[IOException]] in my Scala override when faced with a similar problem before, so that the checked exception would also be matched in the signature.
--
Kevin Wright
mail: kevin....@scalatechnology.com
gtalk / msn : kev.lee...@gmail.com
vibe / skype: kev.lee.wright
steam: kev_lee_wright

"My point today is that, if we wish to count lines of code, we should not regard them as "lines produced" but as "lines spent": the current conventional wisdom is so foolish as to book that count on the wrong side of the ledger" ~ Dijkstra

Moritz Uhlig

unread,
Oct 7, 2011, 6:52:49 AM10/7/11
to scala...@googlegroups.com

On 07.10.2011, at 10:39, Dzmitry Lazerka wrote:

I was unable to make work any StackOverflow answers on this question.

Works for me. In your case it should be:

    import org.apache.hadoop.mapreduce._
   
    class MyMapper extends Mapper[String,String,String,String] {

      type Context = Mapper[String,String,String,String]#Context

      override def setup(context: Context) = {}

      override def map(key: String, value: String, context: Context) = sys.error("Not implemented")
    }


As Context is an inner class of Mapper you need to use a type projection here. Either override setup like this:

  override def setup(context: Mapper[String,String,String,String]#Context)

or use the type alias in the example above.



Moritz

Dzmitry Lazerka

unread,
Oct 8, 2011, 12:55:41 AM10/8/11
to scala...@googlegroups.com
Yes, it works, but actually I'm extending AppEngineMapper which in turn extends Mapper:
public abstract class AppEngineMapper<KEYIN,VALUEIN,KEYOUT,VALUEOUT>
    extends Mapper<KEYIN,VALUEIN,KEYOUT,VALUEOUT> {}
And :
class MyMapper extends AppEngineMapper[String, String, String, String] {
  override def setup(context: Mapper[String,String,String,String]#Context){}
}
shows "name clash ... have same type after erasure: (context: org.apache.hadoop.mapreduce.Mapper#Context)Unit"
While
class MyMapper extends AppEngineMapper[String, String, String, String] {
  override def setup(context: AppEngineMapper[String,String,String,String]#Context){}
}
shows "method setup overrides nothing".

Dzmitry Lazerka

unread,
Oct 8, 2011, 1:00:54 AM10/8/11
to scala...@googlegroups.com
Thank you, already done. Just want to know Scala in details.

Dzmitry Lazerka

unread,
Oct 8, 2011, 1:01:59 AM10/8/11
to scala...@googlegroups.com, Jason Zaugg
No, @throws(...) doesn't affect behavior in any case.

Dzmitry Lazerka

unread,
May 23, 2012, 4:10:36 PM5/23/12
to scala...@googlegroups.com
Bug: https://issues.scala-lang.org/browse/SI-1409
Looks like it gonna be fixed in Scala v3.
Reply all
Reply to author
Forward
0 new messages