0

I created the following generic class (and a number of custom widgets) and want to do two things:

1) externalize to a JAR for portability

2) import and use that JAR in my GWT project

How might I go about doing so? Do other items (i.e. dependencies) need to be included when I right-click->export on the class(es) in Eclipse?

import java.io.Serializable;
import com.google.gwt.user.client.ui.HasValue;

public class MinMaxAvg<T> extends HasValueConcrete<MinMax<T>> implements HasValue<MinMax<T>>, Serializable {

    private static final long serialVersionUID = -54806010801403294L;
    private T min;
    private T max;
    private T avg;

    public MinMaxAvg() {
        super();
    }

    public MinMaxAvg(T min, T max, T avg) {
        super();
        this.min = min;
        this.max = max;
        this.avg = avg;
    }

    public MinMaxAvg(MinMaxAvg<T> rhs) {
        super();
        if (rhs != null) {
            this.min = rhs.min;
            this.max = rhs.max;
            this.avg = rhs.avg;
        }
    }

    public T getMin() {
        return min;
    }

    public void setMin(T min) {
        this.min = min;
    }

    public T getMax() {
        return max;
    }

    public void setMax(T max) {
        this.max = max;
    }

    public T getAvg() {
        return avg;
    }

    public void setAvg(T avg) {
        this.avg = avg;
    }

    public boolean hasMin() {
        return min != null;
    }

    public boolean hasMax() {
        return max != null;
    }

    public boolean hasAvg() {
        return avg != null;
    }

    @Override
    public MinMax<T> getValue() {
        return new MinMax<T>(min, max);
    }

    @Override
    public void setValue(MinMax<T> value) {
        min = value.getMin();
        max = value.getMax();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((avg == null) ? 0 : avg.hashCode());
        result = prime * result + ((max == null) ? 0 : max.hashCode());
        result = prime * result + ((min == null) ? 0 : min.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        @SuppressWarnings("unchecked")
        MinMaxAvg<T> other = (MinMaxAvg<T>) obj;
        if (avg == null) {
            if (other.avg != null)
                return false;
        } else if (!avg.equals(other.avg))
            return false;
        if (max == null) {
            if (other.max != null)
                return false;
        } else if (!max.equals(other.max))
            return false;
        if (min == null) {
            if (other.min != null)
                return false;
        } else if (!min.equals(other.min))
            return false;
        return true;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("MinMaxAvg [min=");
        builder.append(min);
        builder.append(", max=");
        builder.append(max);
        builder.append(", avg=");
        builder.append(avg);
        builder.append("]");
        return builder.toString();
    }
}

1 Answer 1

2

Importing and using jars in GWT is done by means of modules. Since GWT Compiler needs to see the source of your classes to produce javascript, if you intend to use your classes not only on server side but in a browser as well, you need to include the source files(.java) along with your .class files in the same package in your jar. You need also to have a moduleName.gwt.xml file at the root of your module. An example package would look like this :

org.test.mymodule

--> MyModule.gwt.xml

org.test.mymodule.client

--> MinMaxAvg.class

--> MinMaxAvg.java

Moreover to include this module in a GWT project you need to include the jar you created and you need to add <inherits name="org.test.mymodule.MyModule" /> to module descriptor of your parent project.

There is also a link I find useful about Creating Reusable Modules here.

Sign up to request clarification or add additional context in comments.

1 Comment

Big +1 for the clear response. I managed to get my classes exported to a JAR, but it didn't work on the client side. Thanks to your response, I've re-exported the class with source code included and it works well on the front- and backend. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.