override java final methods via reflection or othe

2019-01-23 06:53发布

This question arise while trying to write test cases. Foo is a class within the framework library which I dont have source access to.

public class Foo{
  public final Object getX(){
  ...
  }
}

my applications will

public class Bar extends Foo{
  public int process(){
    Object value = getX();
    ...
  }
}

The unit test case is unable to initalize as I can't create a Foo object due to other dependencies. The BarTest throws a null pointer as value is null.

public class BarTest extends TestCase{
  public testProcess(){
    Bar bar = new Bar();        
    int result = bar.process();
    ...
  }
}

Is there a way i can use reflection api to set the getX() to non-final? or how should I go about testing?

6条回答
SAY GOODBYE
2楼-- · 2019-01-23 07:00

Seb is correct, and just to ensure that you get an answer to your question, short of doing something in native code (and I am pretty sure that would not work) or modifying the bytecode of the class at runtime, and creating the class that overrides the method at runtime, I cannot see a way to alter the "finalness" of a method. Reflection will not help you here.

查看更多
老娘就宠你
3楼-- · 2019-01-23 07:00

If your unit test case can't create Foo due to other dependencies, that might be a sign that you're not making your unit test right in the first place.

Unit tests are meant to test under the same circumstances a production code would run, so I'd suggest recreating the same production environment inside your tests. Otherwise, your tests wouldn't be complete.

查看更多
\"骚年 ilove
4楼-- · 2019-01-23 07:05

As this was one of the top results for "override final method java" in google. I thought I would leave my solution. This class shows a simple solution using the example "Bagel" class and a free to use javassist library:

//This class shows how you can override a final method of a super class using the Javassist's bytecode toolkit
//The library can be found here: http://jboss-javassist.github.io/javassist/
//
//The basic idea is that you get the super class and reset the modifiers so the modifiers of the method don't include final.
//Then you add in a new method to the sub class which overrides the now non final method of the super class.
//
//The only "catch" is you have to do the class manipulation before any calls to the class happen in your code. So put the
//manipulation as early in your code as you can otherwise you will get exceptions.

package packagename;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.CtNewMethod;
import javassist.Modifier;

public class TestCt {

    public static void main(String[] args) {
        try
        {
            // get the super class
            CtClass bagel = ClassPool.getDefault().get("packagename.TestCt$Bagel");

            // get the method you want to override
            CtMethod originalMethod = bagel.getDeclaredMethod("getDescription");

            // set the modifier. This will remove the 'final' modifier from the method.
            // If for whatever reason you needed more than one modifier just add them together
            originalMethod.setModifiers(Modifier.PUBLIC);

            // save the changes to the super class
            bagel.toClass();

            // get the subclass
            CtClass bagelsolver = ClassPool.getDefault().get("packagename.TestCt$BagelWithOptions");

            // create the method that will override the super class's method
            CtMethod overrideMethod = CtNewMethod.make("public String getDescription() { return super.getDescription() + \" with \" + getOptions(); }", bagelsolver);

            // add the new method to the sub class
            bagelsolver.addMethod(overrideMethod);

            // save the changes to the sub class
            bagelsolver.toClass();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        BagelWithOptions myBagel = new BagelWithOptions();

        myBagel.setOptions("cheese, bacon and eggs");

        System.out.println("My bagel is: " + myBagel.getDescription());

        //The output would be:
        //**My bagel is: a plain bagel with cheese, bacon and eggs**
    }

    public static class Bagel {
        public final String getDescription() {
            return "a plain bagel";
        }
    }

    public static class BagelWithOptions extends Bagel {
        String  options;

        public BagelWithOptions() {
            options = "nothing else";
        }

        public void setOptions(String options) {
            this.options = options;
        }

        public String getOptions() {
            return options;
        }
    }
}
查看更多
狗以群分
5楼-- · 2019-01-23 07:08

you could create another method which you could override in your test:

public class Bar extends Foo {
  protected Object doGetX() {
    return getX();
  }
  public int process(){
    Object value = doGetX();
    ...
  }
}

then, you could override doGetX in BarTest.

查看更多
叛逆
6楼-- · 2019-01-23 07:10

If the variable returned by getX() is not final you can use the technique explained in What’s the best way of unit testing private methods? for changing the value of the private variable through Reflection.

查看更多
啃猪蹄的小仙女
7楼-- · 2019-01-23 07:14
public class Bar extends Foo{
  public int process(){
    Object value = getX();
    return process2(value);
  }
  public int process2(Object value){
  ...
  }
}

public class BarTest extends TestCase{
  public testProcess(){
    Bar bar = new Bar();   
    Mockobj mo = new Mockobj();     
    int result = bar.process2(mo);
    ...
  }
}

what i did eventually was the above. it is a bit ugly... James solution is definitely much better than this...

查看更多
登录 后发表回答