1

Possible duplicate of: Alternatives to java.lang.reflect.Proxy for creating proxies of abstract classes (rather than interfaces)

Suppose I have this class:

public class MyClass {
    public void doStuff() {
        //stuff
    }

    public int getSomeIntStuff() {
        //int stuff
        return intStuff;
    }

    private void helperStuff() {
        //helper Stuff
    }
}

Is there a way to get an interface class (dynamically generated?) that represents the public interface of this class?

The public interface of this class (if it would have been written) would look:

public interface MyClassInterface {
    void doStuff(); 
    int getSomeIntStuff();
}

but this has to be somehow dynamically generated and I would expect something like this:

Class<?> interfaceClass = MyClass.class.getEncompasingInterface();

What I try to achieve is to create a Java Proxy based on some existing classes not on existing interfaces, and the Proxy.newProxyInstance(ClassLoader, Class<?>[], InvocationHandler) method expects as a second argument strictly a list of Java interfaces not classes.

Community
  • 1
  • 1
Random42
  • 8,989
  • 6
  • 55
  • 86
  • 2
    See http://stackoverflow.com/questions/3291637/alternatives-to-java-lang-reflect-proxy-for-creating-proxies-of-abstract-classes – axtavt Apr 11 '13 at 14:12
  • @axtavt Actually I would need it on a normal class, not an abstract class. – Random42 Apr 11 '13 at 14:14
  • 1
    axtavt's link also works for non-abstract classes. – WilQu Apr 11 '13 at 15:19
  • @WilQu Yes, you are right! – Random42 Apr 11 '13 at 16:59
  • FYI I voted to close for duplicate based on @axtavt comment (I can only vote to do it as I don't have the rep to close it.) – Adam Gent Apr 11 '13 at 17:18
  • @AdamGent Actually the question is if you can get the interface of class if it is not defined although I have solved my problem with a work-around. – Random42 Apr 12 '13 at 07:51
  • @m3th0dman I thought you might have been just confused or mis-worded the Q. I cannot understand why you would want to dynamically generate an interface from a public class. Its always the other way around: given a class or interface you want to dynamically create one. Not: given a class let me make a more abstract version. To do what you want you will have to bytecode modify (and not generate in the case of proxy) `MyClass`. Only Javassist, ASM or true code generation will do what you want. If you do this what namespace would the interface be? I think your confused. – Adam Gent Apr 12 '13 at 15:10

0 Answers0