Question

I've a very little problem in java. I think i'm not still really awake.

I have two classes:

The first one use constants from a jar (example : Highgui.CV_LOAD_IMAGE_COLOR). In the second one the user select in a list which constant he want to use.

So, the second class call a method of the first one, with the "stringify" constant to use in param.

But the first one can't use the string in param has a real class constant.

I know, that's a really stupid problem, I'm pretty sure I'm missing something obvious.

Someone have an idea to deal with that?

Edit :

A piece of code for more clarity :

In Class2

Class1.finder("c:aaa.jpg","Highgui.CV_LOAD_IMAGE_COLOR");

In Class1

public void finder(String path1, String constant){
    Mat object = Highgui.imread(path1, Highgui.CV_LOAD_IMAGE_COLOR);
    //I want to use "constant" instead of Highgui.CV_LOAD_IMAGE_COLOR
}
Was it helpful?

Solution

This looks like a design problem. Passing strings around, containing the name of a Java constant, is not a good idea. Why don't you simply create a class, or an enum, containing the value of your color and its description:

public enum DescribedColor {
    CV_LOAD_IMAGE_COLOR("CV load image color", "red"),
    ...

    private String description;
    private String color;

    private DescribedColor(String description, String color) {
        this.description = description;
        this.color = color;
    }
}

Then in Class1 :

public void finder(String path1, DescribedColor describedColor) {
    ...
}

And in Class2 :

Class1.finder("c:aaa.jpg", DescribedColor.CV_LOAD_IMAGE_COLOR);

And in the list displayed to the user, you just need to store all the DescribedColor instances, and use their description as the value displayed to the user.

This way, you're OO, type-safe, self-documenting, and don't need nasty reflection tricks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top