Getting java bean property of unknown class

I would like to be able to call "getProgram" for objects that have this method, not knowing which class they belong to. I know that I have to use the interface here, but I work with different code and I can not reverse engineer the classes I work with. I thought BeanUtils.getProperty might help me, but it seems like it is returning strings. Is there something like Beanutils.getProperty that will return a castable object? Or another, smarter way to work with two similar classes that do not share an interface? thanks, -Morgan

+3
source share
7 answers

Presumably, you have a finite number of classes implementing this method, and you can directly link them. Therefore you do not need to speculate. Reflection is evil.

Say you have a set of classes using a method:

public class LibA { public Program getProgram() { return program; } ... };
public class LibB { public Program getProgram() { return program; } ... };
...

Then you just need instanceof / cast pairs. You can put this in a method, so you only need to do this once.

public static Program getProgram(Object obj) {
    if        (obj instanceof LibA) {
        return              ((LibA)obj).getProgram();
    } else if (obj instanceof LibB) {
        return              ((LibB)obj).getProgram();
    } else {
        throw new IllegalArgumentException(obj+" doesn't have a known getProgram");
            // Or an appropriate application exception.
    }
}

Alternatively, you can use an adapter:

public interface ProgramContainer { 
    Program getProgram();
    ...
}

public class LibAContainer implements ProgramContainer {
    private final LibA libA;
    public LibAContainer(LibA libA) {
        this.libA = libA;
    }
    public Program getProgram() {
        return libA.getProgram();
    }
    ...
}
0
source

Use PropertyUtils (from apache commons-beanutils) instead of BeanUtils.

It has a getProperty (Object bean, String name) method that returns an object instead of a string.

See the JavaDoc for more information .

+5
source

... , , .

    public static void main(String[] args) throws Exception {
    doSomething(new A());
    doSomething(new B());
}

private static void doSomething(Object object) throws Exception {
    Method m = object.getClass().getMethod("doSomething", (Class[])null);
    m.invoke(object, (Object[])null);
}

private static class A {
    public void doSomething() {
        System.out.println("I'm doing it already!");
    }
}

private static class B {
    public void doSomething() {
        System.out.println("I'm doing it too!");
    }
}
+2

API :

Class.getMethod() ( getMethods()), .

0

: , :

interface GetProgram
{
    String getProgram ();
}

class AWrapper implements GetProgram
{
    A a;
    public AWrapper (A a) { this.a = a;
    String getProgram () { return a.getProgram(); }
}

, .

: , - . , A , .

0

, Java 5 +

public static void main(String[] args) throws Exception {
    System.out.println(invoke("toString", new A());
    System.out.println(invoke("toString", new B());
}

private static <R> R invoke(Object object, String methodName) throws Exception {
    return (R) object.getClass().getMethod(methodName).invoke(object);
}
0

java.beans.Expression , .

public static void main(String[] args) throws Exception {
    new Expression(new A(), "doSomething", null).getValue();
    new Expression(new B(), "doSomething", null).getValue();
}

public static class A {
    public void doSomething() {
            System.out.println("I'm doing it already!");
    }
}

public static class B {
    public void doSomething() {
            System.out.println("I'm doing it too!");
    }
}
-1

Source: https://habr.com/ru/post/1702308/


All Articles