properly exclude @QueryExclude annotated classes #30

Closed
subes opened this Issue Oct 17, 2011 · 4 comments

Comments

Projects
None yet
2 participants
@subes

subes commented Oct 17, 2011

Hi,

when generating a query class for a class that extends a @QueryExclude class, the super statement is still generated for the queryclass which fails to compile because there is no query class for the super class.

Expected behaviour would be that the @QueryExclude works in that case aswell and the super field does not get generated.

See attachment for the sample.

@subes

This comment has been minimized.

Show comment
Hide comment
@subes

subes Oct 17, 2011

Don't know how to upload a file, so heres the code:

package de.invesdwin.gemeinsam.beans.internal;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;

import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;

import com.mysema.query.annotations.QueryExclude;

@ThreadSafe
@QueryExclude
public abstract class APropertyChangeSupported implements Comparable<Object>, Cloneable, Serializable {

    private static final long serialVersionUID = 1L;

    private transient PropertyChangeSupport propertyChangeSupport;

    private synchronized PropertyChangeSupport lazyGetPropertyChangeSupport(final boolean initializeIfNull) {
        if (initializeIfNull && propertyChangeSupport == null) {
            propertyChangeSupport = new PropertyChangeSupport(this);
        }
        return propertyChangeSupport;
    }

    public final void addPropertyChangeListener(final PropertyChangeListener listener) {
        if (listener != null) {
            lazyGetPropertyChangeSupport(true).addPropertyChangeListener(listener);
        }
    }

    public final void addPropertyChangeListener(final String property, final PropertyChangeListener listener) {
        if (listener != null) {
            lazyGetPropertyChangeSupport(true).addPropertyChangeListener(property, listener);
        }
    }

    public final void removePropertyChangeListener(final PropertyChangeListener listener) {
        final PropertyChangeSupport ref = lazyGetPropertyChangeSupport(false);
        if (ref != null) {
            ref.removePropertyChangeListener(listener);
        }
    }

    public final void removePropertyChangeListener(final String property, final PropertyChangeListener listener) {
        final PropertyChangeSupport ref = lazyGetPropertyChangeSupport(false);
        if (ref != null) {
            ref.removePropertyChangeListener(property, listener);
        }
    }

    public final boolean hasListeners(final String propertyName) {
        final PropertyChangeSupport ref = lazyGetPropertyChangeSupport(false);
        return ref != null && ref.hasListeners(propertyName);
    }

    protected final void fireIndexedPropertyChange(final String propertyName, final int index, final Object oldValue,
            final Object newValue) {
        final PropertyChangeSupport ref = lazyGetPropertyChangeSupport(false);
        if (ref != null) {
            ref.fireIndexedPropertyChange(propertyName, index, oldValue, newValue);
        }
    }

    protected final void firePropertyChange(final String propertyName, final Object oldValue, final Object newValue) {
        final PropertyChangeSupport ref = lazyGetPropertyChangeSupport(false);
        if (ref != null) {
            ref.firePropertyChange(propertyName, oldValue, newValue);
        }
    }
}

subes commented Oct 17, 2011

Don't know how to upload a file, so heres the code:

package de.invesdwin.gemeinsam.beans.internal;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;

import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;

import com.mysema.query.annotations.QueryExclude;

@ThreadSafe
@QueryExclude
public abstract class APropertyChangeSupported implements Comparable<Object>, Cloneable, Serializable {

    private static final long serialVersionUID = 1L;

    private transient PropertyChangeSupport propertyChangeSupport;

    private synchronized PropertyChangeSupport lazyGetPropertyChangeSupport(final boolean initializeIfNull) {
        if (initializeIfNull && propertyChangeSupport == null) {
            propertyChangeSupport = new PropertyChangeSupport(this);
        }
        return propertyChangeSupport;
    }

    public final void addPropertyChangeListener(final PropertyChangeListener listener) {
        if (listener != null) {
            lazyGetPropertyChangeSupport(true).addPropertyChangeListener(listener);
        }
    }

    public final void addPropertyChangeListener(final String property, final PropertyChangeListener listener) {
        if (listener != null) {
            lazyGetPropertyChangeSupport(true).addPropertyChangeListener(property, listener);
        }
    }

    public final void removePropertyChangeListener(final PropertyChangeListener listener) {
        final PropertyChangeSupport ref = lazyGetPropertyChangeSupport(false);
        if (ref != null) {
            ref.removePropertyChangeListener(listener);
        }
    }

    public final void removePropertyChangeListener(final String property, final PropertyChangeListener listener) {
        final PropertyChangeSupport ref = lazyGetPropertyChangeSupport(false);
        if (ref != null) {
            ref.removePropertyChangeListener(property, listener);
        }
    }

    public final boolean hasListeners(final String propertyName) {
        final PropertyChangeSupport ref = lazyGetPropertyChangeSupport(false);
        return ref != null && ref.hasListeners(propertyName);
    }

    protected final void fireIndexedPropertyChange(final String propertyName, final int index, final Object oldValue,
            final Object newValue) {
        final PropertyChangeSupport ref = lazyGetPropertyChangeSupport(false);
        if (ref != null) {
            ref.fireIndexedPropertyChange(propertyName, index, oldValue, newValue);
        }
    }

    protected final void firePropertyChange(final String propertyName, final Object oldValue, final Object newValue) {
        final PropertyChangeSupport ref = lazyGetPropertyChangeSupport(false);
        if (ref != null) {
            ref.firePropertyChange(propertyName, oldValue, newValue);
        }
    }
}

@subes subes closed this Oct 17, 2011

@subes

This comment has been minimized.

Show comment
Hide comment
@subes

subes Oct 17, 2011

Second file which extends the first one that should not get generated from:

package de.invesdwin.gemeinsam.beans;

import java.beans.PropertyDescriptor;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import javax.annotation.concurrent.ThreadSafe;
import javax.persistence.Transient;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.lang3.builder.CompareToBuilder;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import com.mysema.query.annotations.QuerySupertype;

import de.invesdwin.gemeinsam.beans.internal.APropertyChangeSupported;
import de.invesdwin.gemeinsam.lang.Strings;
import de.invesdwin.gemeinsam.log.Err;

@ThreadSafe
@QuerySupertype
public abstract class AValueObject extends APropertyChangeSupported implements Comparable<Object>, Cloneable,
        Serializable {

    private static final long serialVersionUID = 1L;

    private static final BeanUtilsBean BEAN_UTILS = new BeanUtilsBean();

    static {
        //Defaults für BeanUtils setzen.
        BEAN_UTILS.getConvertUtils().register(false, true, 0);
    }

    @Override
    public String toString() {
        return Strings.asStringReflective(this);
    }

    public String prettyToString() {
        return Strings.asStringReflectivePretty(this);
    }

    @Transient
    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public boolean equals(final Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }

    @Override
    public int compareTo(final Object o) {
        return CompareToBuilder.reflectionCompare(this, o);
    }

    public void mergeFrom(final Object o) {
        mergeFrom(o, true);
    }

    public void mergeFrom(final Object o, final boolean ueberschreiben) {
        for (final PropertyDescriptor dortDesc : BEAN_UTILS.getPropertyUtils().getPropertyDescriptors(o)) {
            try {
                final Object valueDort = dortDesc.getReadMethod().invoke(o);
                if (valueDort != null) {
                    final PropertyDescriptor thisDesc = BEAN_UTILS.getPropertyUtils().getPropertyDescriptor(this,
                            dortDesc.getName());
                    if (thisDesc == null) {
                        continue;
                    }

                    boolean copy = true;
                    final Object valueHier = thisDesc.getReadMethod().invoke(this);
                    if (valueHier != null) {
                        if (valueHier instanceof AValueObject) {
                            ((AValueObject) valueHier).mergeFrom(valueDort, ueberschreiben);
                            copy = false;
                        } else {
                            copy = ueberschreiben;
                        }
                    }

                    if (copy) {
                        BEAN_UTILS.copyProperty(this, dortDesc.getName(), valueDort);
                    }
                }
            } catch (final Throwable e) { //SUPPRESS CHECKSTYLE empty statement
                //ignore (kein setter für property class etc, oder getter ohne setter)
            }
        }
    }

    @Override
    public AValueObject clone() { //SUPPRESS CHECKSTYLE super.clone()
        ObjectOutputStream out = null;
        ObjectInputStream in = null;
        try {
            final ByteArrayOutputStream outStore = new ByteArrayOutputStream();
            out = new ObjectOutputStream(outStore);
            out.writeObject(this);
            final ByteArrayInputStream inStore = new ByteArrayInputStream(outStore.toByteArray());
            in = new ObjectInputStream(inStore);
            return (AValueObject) in.readObject();
        } catch (final IOException e) {
            throw Err.process(e);
        } catch (final ClassNotFoundException e) {
            throw Err.process(e);
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (final IOException e) {
                throw Err.process(e);
            }
        }
    }

}

subes commented Oct 17, 2011

Second file which extends the first one that should not get generated from:

package de.invesdwin.gemeinsam.beans;

import java.beans.PropertyDescriptor;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import javax.annotation.concurrent.ThreadSafe;
import javax.persistence.Transient;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.lang3.builder.CompareToBuilder;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import com.mysema.query.annotations.QuerySupertype;

import de.invesdwin.gemeinsam.beans.internal.APropertyChangeSupported;
import de.invesdwin.gemeinsam.lang.Strings;
import de.invesdwin.gemeinsam.log.Err;

@ThreadSafe
@QuerySupertype
public abstract class AValueObject extends APropertyChangeSupported implements Comparable<Object>, Cloneable,
        Serializable {

    private static final long serialVersionUID = 1L;

    private static final BeanUtilsBean BEAN_UTILS = new BeanUtilsBean();

    static {
        //Defaults für BeanUtils setzen.
        BEAN_UTILS.getConvertUtils().register(false, true, 0);
    }

    @Override
    public String toString() {
        return Strings.asStringReflective(this);
    }

    public String prettyToString() {
        return Strings.asStringReflectivePretty(this);
    }

    @Transient
    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public boolean equals(final Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }

    @Override
    public int compareTo(final Object o) {
        return CompareToBuilder.reflectionCompare(this, o);
    }

    public void mergeFrom(final Object o) {
        mergeFrom(o, true);
    }

    public void mergeFrom(final Object o, final boolean ueberschreiben) {
        for (final PropertyDescriptor dortDesc : BEAN_UTILS.getPropertyUtils().getPropertyDescriptors(o)) {
            try {
                final Object valueDort = dortDesc.getReadMethod().invoke(o);
                if (valueDort != null) {
                    final PropertyDescriptor thisDesc = BEAN_UTILS.getPropertyUtils().getPropertyDescriptor(this,
                            dortDesc.getName());
                    if (thisDesc == null) {
                        continue;
                    }

                    boolean copy = true;
                    final Object valueHier = thisDesc.getReadMethod().invoke(this);
                    if (valueHier != null) {
                        if (valueHier instanceof AValueObject) {
                            ((AValueObject) valueHier).mergeFrom(valueDort, ueberschreiben);
                            copy = false;
                        } else {
                            copy = ueberschreiben;
                        }
                    }

                    if (copy) {
                        BEAN_UTILS.copyProperty(this, dortDesc.getName(), valueDort);
                    }
                }
            } catch (final Throwable e) { //SUPPRESS CHECKSTYLE empty statement
                //ignore (kein setter für property class etc, oder getter ohne setter)
            }
        }
    }

    @Override
    public AValueObject clone() { //SUPPRESS CHECKSTYLE super.clone()
        ObjectOutputStream out = null;
        ObjectInputStream in = null;
        try {
            final ByteArrayOutputStream outStore = new ByteArrayOutputStream();
            out = new ObjectOutputStream(outStore);
            out.writeObject(this);
            final ByteArrayInputStream inStore = new ByteArrayInputStream(outStore.toByteArray());
            in = new ObjectInputStream(inStore);
            return (AValueObject) in.readObject();
        } catch (final IOException e) {
            throw Err.process(e);
        } catch (final ClassNotFoundException e) {
            throw Err.process(e);
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (final IOException e) {
                throw Err.process(e);
            }
        }
    }

}

@subes subes reopened this Oct 17, 2011

@subes

This comment has been minimized.

Show comment
Hide comment
@subes

subes Oct 17, 2011

And here the generated class:

package de.invesdwin.gemeinsam.beans;


import com.mysema.query.types.*;
import com.mysema.query.types.path.*;

import javax.annotation.Generated;


/**
 * QAValueObject is a Querydsl query type for AValueObject
 */
@Generated("com.mysema.query.codegen.SupertypeSerializer")
public class QAValueObject extends ComparablePath<AValueObject> {

    private static final long serialVersionUID = 331184104;

    public final de.invesdwin.gemeinsam.beans.internal.QAPropertyChangeSupported _super = new de.invesdwin.gemeinsam.beans.internal.QAPropertyChangeSupported(this);

    public QAValueObject(BeanPath<? extends AValueObject> entity) {
        super(entity.getType(), entity.getMetadata());
    }

    public QAValueObject(PathMetadata<?> metadata) {
        super(AValueObject.class, metadata);
    }

}

And this is the line that I did not expect to be generated and which causes problems on compilation because the references class does not exist:

    public final de.invesdwin.gemeinsam.beans.internal.QAPropertyChangeSupported _super = new de.invesdwin.gemeinsam.beans.internal.QAPropertyChangeSupported(this);

subes commented Oct 17, 2011

And here the generated class:

package de.invesdwin.gemeinsam.beans;


import com.mysema.query.types.*;
import com.mysema.query.types.path.*;

import javax.annotation.Generated;


/**
 * QAValueObject is a Querydsl query type for AValueObject
 */
@Generated("com.mysema.query.codegen.SupertypeSerializer")
public class QAValueObject extends ComparablePath<AValueObject> {

    private static final long serialVersionUID = 331184104;

    public final de.invesdwin.gemeinsam.beans.internal.QAPropertyChangeSupported _super = new de.invesdwin.gemeinsam.beans.internal.QAPropertyChangeSupported(this);

    public QAValueObject(BeanPath<? extends AValueObject> entity) {
        super(entity.getType(), entity.getMetadata());
    }

    public QAValueObject(PathMetadata<?> metadata) {
        super(AValueObject.class, metadata);
    }

}

And this is the line that I did not expect to be generated and which causes problems on compilation because the references class does not exist:

    public final de.invesdwin.gemeinsam.beans.internal.QAPropertyChangeSupported _super = new de.invesdwin.gemeinsam.beans.internal.QAPropertyChangeSupported(this);
@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Oct 23, 2011

Member

Released in 2.2.4

Member

timowest commented Oct 23, 2011

Released in 2.2.4

@timowest timowest closed this Oct 23, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment