Support for extension methods in builtin types (e.g. Number, String)

Bug #595972 reported by Timo Westkämper on 2010-06-18
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Querydsl
Fix Released
Undecided
Timo Westkämper

Bug Description

Currently extension methods can only be declared for entity types

Changed in querydsl:
status: New → Confirmed

I just began implementing since. There is a problem with types from java.lang and java.math packages. These can't be used. The compiler has some problems with them

So we could remap them to com.mysema.query.types

java.lang.QString -> com.mysema.query.types.QString

Changed in querydsl:
status: Confirmed → In Progress
assignee: nobody → Timo Westkämper (timo-westkamper)

As requested, some examples for this would be:
* A date period to be applied on java.sql.Date and other on java.sql.Timestamp (dates consider whole days, while timestamp is exact)
* Comparing dates with a TimeInterval (which has a number and a field - hours, days, months...)
* Checking whether a boolean column is false (could be either null or false)
* Comparing numbers with FileSize (which has a number and a field - bytes, KB, MB), where the original number is always in bytes

I just completed the support for customizations to builtin types. Here are implementations to some of your examples :

    @QueryDelegate(Date.class)
    public static EBoolean inPeriod(PDate<Date> date, Pair<Date,Date> period){
        return date.goe(period.getFirst()).and(date.loe(period.getSecond()));
    }

    @QueryDelegate(Boolean.class)
    public static EBoolean isFalse(PBoolean expr){
        return expr.isNull().or(expr.eq(false));
    }

    public static class FileSize {
        public int bytes = 1000;
    }

    @QueryDelegate(Integer.class)
    public static EBoolean eq(PNumber<Integer> intValue, FileSize size){
        return intValue.eq(size.bytes);
    }

    @QueryEntity
    public static class Entity {

        Date dateVal;

        Boolean booleanVal;

        Integer intVal;

    }

This is an excerpt of com.mysema.query.domain.QueryExtensions8Test from the querydsl-apt module.

The usage guide for this feature is quite simple :

* write a static delegate method
* the first argument needs to compatible with the Q-type of the type to be extended and is given the this reference
* the return type should be a subtype of Expr
* add the QueryDelegate annotation with the reference to the type to be extended

Could you test it before we make the next release?

I have tested it already within the Querydsl APT module, but there could be some hidden surprises.

Changed in querydsl:
status: In Progress → Fix Committed

Released in 1.8.2

Changed in querydsl:
status: Fix Committed → Fix Released

Luis, now you can test this feature via a Querydsl release (including standalone zips)

Amazing!
No more maven to download new versions...
Thanks!

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers