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

  • SEARCH
  • Home
  • 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 6657013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:42:52+00:00 2026-05-26T01:42:52+00:00

Is there a way in java or scala, that allows to override methods of

  • 0

Is there a way in java or scala, that allows to override methods of the super class in an automated fashion?

Disclaimer: i don’t pretend to be an experienced programmer, please don’t kill me for any coding-atrocities below.

I have a wrapper B of class A that does some stuff and delegates to two different instances of A. Problem is, class A has a very big number of methods. Here is my original Java implementation. It’s a monster with enormous code duplication. It works but probably contains a lots of errors, some of which maybe hard to track, some of them just will produce wrong results unnoted.

Here is my first try of a reimplementation using scala structural typing.

def get(key: Array[Byte]): Array[Byte] = {
 val f: (Transaction) => Response[Array[Byte]] = _.get(key)
 wrap[Array[Byte]](f, redundancySwitch, currentDB, key).get()
 }

the rest is left to the common wrap function. This is already nice! But I’d still need to implement each single method individually and specifically.

My next thought was to try to have a single (reflective) generalized implementation to copy into each overriding method body, such that in definition of the function object f, there would be something to fill in the name of currently calling method.

def get(key: Array[Byte]): Array[Byte] = {
 val f: (Transaction) => Response[Array[Byte]] = _.someReflectionMagic.nameOfCurrentlyCallingMethod(args)

Getting the method object is ok, but not how to pass it to f, and there doesn’t seem to be a way to get and copy the arguments of the currently called method. Here’s my aching attempt:

def zadd(key: String, score: Int, value: String) = {

// get the current Method
val thisMethod = new Object().getClass.getEnclosingMethod
val paramTypes = thisMethod.getParameterTypes
val returnType = thisMethod.getReturnType;

 //  this doesn't exist. how to reflectively forward parameter values (not parameter Types) into an Array for later reflective method invocation?
val params = thisMethod.getParameters  

// I want to avoid to get it manually:
val params = new Array[Object]
params(0) = key
params(1) = score
params(2) = value

// this doesn't work. how to indicate the returnType here?
def f(t: Transaction): Response[returnType.class] =   
 t.getClass.getMethod(thisMethod.getName, paramTypes).
            invoke(t,params).
            asInstanceOf(Response[String]) 

 wrap[String](f, redundancySwitch, currentDB, key).get()
}

private def getMethodName: String = {
 val ste: Array[StackTraceElement] = Thread.currentThread.getStackTrace
 return ste(2).getMethodName
}

this looks horrible and probably really is horrible. You experts, is there anything that could make it work? I hope at least the intention is clear?

This approach would still require having those >200 method bodies in my wrapper class. I wonder if there is any way to completely go without that:
A class B wrapping class A, that looks to the outside as if B had all methods of A, without even explicitely implementing them. When B is invoked by an A’s method x(args:T):U, auto-override / auto-delegate to a single generalized method B.y(x: T=>U).
y() would be something like the wrap() method in the second link. However, many of the above problems remain… i.e. how to capture the arguments and apply the method name on a structural typed value…

does this make sense?
This doesn’t seem to be possible with Java / java’s reflection. Is there anything in scala that could help me? Maybe some experimental scala reflection?

thanks a lot for any hints

  • 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-05-26T01:42:52+00:00Added an answer on May 26, 2026 at 1:42 am

    One way would be to use java.lang.reflect.Proxy. This would require you to have an interface for all of your methods though, but you can get this reasonably easily using Refactor->Extract Interface in Eclipse.

    This will give you the interface to implement in your Proxy, and you call the correct instance of A in your InvocationHandler, using reflection. The reflection may be a potential performance problem.

    From the javadoc for Proxy:

    A dynamic proxy class (simply referred to as a proxy class below) is a
    class that implements a list of interfaces specified at runtime when
    the class is created, with behavior as described below. A proxy
    interface is such an interface that is implemented by a proxy class. A
    proxy instance is an instance of a proxy class. Each proxy instance
    has an associated invocation handler object, which implements the
    interface InvocationHandler. A method invocation on a proxy instance
    through one of its proxy interfaces will be dispatched to the invoke
    method of the instance’s invocation handler, passing the proxy
    instance, a java.lang.reflect.Method object identifying the method
    that was invoked, and an array of type Object containing the
    arguments. The invocation handler processes the encoded method
    invocation as appropriate and the result that it returns will be
    returned as the result of the method invocation on the proxy instance.

    EDIT: If I understand your problem correctly, you have a class B which delegates to A (or multiple As). You want to take the methods defined on class B and have them all redirect to a single method. This is exactly what a Proxy does. It works with interface, but you can get one easily enough with Extract Interface in Eclipse. The Proxy will implement that interface and redirect all calls to all methods to InvocationHandler.invoke() with the method that was called along with the instance of the object and parameters that were used.

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

Sidebar

Related Questions

Is there a way in java to have a ListModel that only accepts a
Is there a way in java to declare a method argument, that is some
While working with a Java class in Scala, I noticed that Scala can't multiply
Is there any way (in Java Servlet) to determine whether a HTTP POST or
Is there a way in Java to have a map where the type parameter
Is there a way in Java to declare an enumeration whose values can be
Is there a way in Java to get a method to lock (mutex) the
Is there a way in Java's for-each loop for(String s : stringArray) { doSomethingWith(s);
Is there a way using Java to over-ride the browser authentication dialog box when
Is there a way in Java to ask the system to get control over

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.