简体   繁体   中英

Dreaded no-arg constructor issue using java2ws on a type that extends JodaTime's MutableInterval

To start with, I've already looked here:

for some possible help with respect to handling JodaTime classes with JAXB .

I've inherited a codebase with a class the impl (and XML adapter) for which is below.

When trying to generate a WSDL and client using the Apache CXF v2.7.2 Maven plugin for java2ws , I get the following:

Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
org.joda.time.base.BaseInterval does not have a no-arg default constructor.
    this problem is related to the following location:
            at org.joda.time.base.BaseInterval
            at org.joda.time.MutableInterval
            at com.etp.commons.persistence.model.time.Period

Well, Period (below) extends MutableInterval extends BaseInterval extends AbstractInterval which has a protected no-arg constructor in JodaTime v2.1 . Period has a public no-arg constructor that calls a constructor variant of MutableInterval passing in some default values.

Any hints as to how to proceed?

package com.etp.commons.persistence.model.time;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.MutableInterval;
import org.joda.time.ReadableInstant;
import org.joda.time.ReadableInterval;

import com.etp.commons.persistence.model.jaxb.XMLPeriodAdapter;
import com.etp.commons.time.services.core.TimeDispatcher;

@XmlType
@XmlJavaTypeAdapter(value=XMLPeriodAdapter.class)
public class Period extends MutableInterval implements Comparable<Period>{

private static final long serialVersionUID = 1L;

public static boolean endpointInclusive = true;
public static DateTime defaultEffectiveInstant = new DateTime(1900,1,1,0,0,0,0, DateTimeZone.forID("GMT"));
public static DateTime defaultTerminationInstant = new DateTime(9999,12,31,0,0,0,0, DateTimeZone.forID("GMT"));
public static Period defaultEffectiveInterval = new Period();       

@Override
public boolean contains(ReadableInstant time){
    if (endpointInclusive){
        return this.getStartMillis() < time.getMillis() && this.getEndMillis() >= time.getMillis();
    }else{
        return super.contains(time);
    }
}

@Override
public boolean contains(long time){
    if (endpointInclusive){
        return this.getStartMillis() < time && this.getEndMillis() >= time;
    }else{
        return super.contains(time);
    }
}

@Override
public boolean containsNow(){
    long time = TimeDispatcher.defaultTimeDispatcher.getCurrentTimeMillis();
    if (endpointInclusive){         
        return this.getStartMillis() < time && this.getEndMillis() >= time;
    }else{
        return super.contains(time);
    }
}

public DateTime getInclusiveTerminalInstant(){
    if (endpointInclusive){
        return this.getEnd();
    }else{
        return this.getStart();
    }
}

public Period getOverlap(Period period){
    if (!this.overlaps(period)){
        return null;
    }else if (this.contains(period)){
        return new Period(period);
    }else if (period.contains(this)){
        return new Period(this);
    }else{
        if (this.getStartMillis() < period.getStartMillis()){
            return new Period(period.getStart(),this.getEnd());
        }else{
            return new Period(this.getStart(),period.getEnd());
        }
    }
}

public Period offsetByJodaPeriod(org.joda.time.Period period, boolean reverse){
    Period p = null; 
    if (reverse){
        p = new Period(getStart().minus(period),getEnd().minus(period));
    }else{
        p = new Period(getStart().plus(period),getEnd().plus(period));
    }   
    return p;
}

public static List<Period> merge(List<Period> periods){
    List<Period> result = new ArrayList<Period>();
    periods = getDistinctPeriods(periods);
    Collections.sort(periods);

    DateTime start = null;
    DateTime end = null;
    for (Period period : periods){
        if (start == null){
            start = period.getStart();
            end = period.getEnd();
        }else{
            if (period.getStart().isEqual(end)){
                end = period.getEnd();
            }else{
                result.add(new Period(start,end));
                start = period.getStart();
                end = period.getEnd();
            }
        }
    }
    if (start != null){
        result.add(new Period(start,end));
    }
    return result;
}

public static Period getExtentPeriod(Collection<Period> periods){
    DateTime start = null;
    DateTime end = null;
    for (Period period : periods){
        if (start == null || start.getMillis() > period.getStartMillis()){
            start = period.getStart();
        }
        if (end == null || end.getMillis() < period.getEndMillis()){
            end = period.getEnd();
        }
    }
    return new Period(start,end);
}

public static Period getEnclosingPeriod(DateTime instant, FrequencyUnitType unit){

    DateTime start = unit.truncate(instant);
    DateTime end = start.plus(unit.getJodaPeriod());

    return new Period(start,end);

}

public Period subtractOverlap(Period period){
    if (this.overlaps(period)){
        if (period.contains(this)){
            return null;
        }else if (this.contains(period)){
            throw new IllegalArgumentException("Period contains target period. More than one period would result from subtract");
        }else if (this.getStartMillis() < period.getStartMillis()){
            return new Period(this.getStart(),period.getStart());
        }else if (this.getEndMillis() > period.getStartMillis()){
            return new Period(period.getEnd(),this.getEnd());
        }       
    }
    return this;
}

public Period subtractOverhang(Period period){
    if (this.overlaps(period)){
        if (period.contains(this)){
            return this;
        }else if (this.contains(period)){
            return new Period(period.getStart(),period.getEnd());
        }else if (this.getStartMillis() < period.getStartMillis()){
            return new Period(period.getStart(),this.getEnd());
        }else if (this.getEndMillis() > period.getStartMillis()){
            return new Period(this.getStart(),period.getEnd());
        }
    }
    return null;
}

/**
 * Returns the smallest period that is common to all of the passed periods and contains the passed time.
 *  
 * @param periods
 * @param time
 * @return
 */
public static Period getSmallestCommonIntersection(Collection<Period> periods, DateTime time){
    Period smallest = new Period();
    for (Period period : periods){
        if (!period.contains(time)){
            return null;
        }
        if (smallest.getStartMillis() < period.getStartMillis()){
            smallest.setStart(period.getStart());
        }
        if (smallest.getEndMillis() > period.getEndMillis()){
            smallest.setEnd(period.getEnd());
        }
    }
    return smallest;
}

public static List<? extends PeriodDatedObject> getCollectionWithinPeriod(Collection<? extends PeriodDatedObject> collection, Period effectivePeriod){
    List<PeriodDatedObject> effectiveObs = new ArrayList<PeriodDatedObject>();
    for (PeriodDatedObject ob : collection){
        if (ob.getPeriod().overlaps(effectivePeriod)){
            effectiveObs.add(ob);
        }
    }
    return effectiveObs;
}    

public static List<? extends PeriodDatedObject> getCollectionAtInstant(Collection<? extends PeriodDatedObject> collection, DateTime effectiveInstant){
    List<PeriodDatedObject> effectiveObs = new ArrayList<PeriodDatedObject>();
    for (PeriodDatedObject ob : collection){
        if (ob.getPeriod().contains(effectiveInstant)){
            effectiveObs.add(ob);
        }
    }
    return effectiveObs;
}

public static PeriodDatedObject getMemberAtInstant(Collection<? extends PeriodDatedObject> collection, DateTime effectiveInstant){
    for (PeriodDatedObject ob : collection){
        if (ob.getPeriod().contains(effectiveInstant)){
            return ob;
        }
    }
    return null;
}

public List<Period> getGaps(List<Period> periods){
    Collections.sort(periods);      
    List<Period> gaps = new ArrayList<Period>();

    DateTime contiguousTo = this.getStart();

    for (Period period : periods){
        if (period.getStart().getMillis() <= this.getStart().getMillis()){
            contiguousTo = period.getEnd();
        }else if (period.getStart().isEqual(contiguousTo)){
            contiguousTo = period.getEnd();
        }else if (period.getStartMillis() > contiguousTo.getMillis()){
            Period gap = new Period(contiguousTo, period.getStart());
            contiguousTo = period.getEnd();
            gaps.add(gap);
        }
    }

    if (contiguousTo.getMillis() < this.getEnd().getMillis()){
        Period gap = new Period(contiguousTo, this.getEnd());
        gaps.add(gap);
    }

    return gaps;
}

/**
 * Returns a list of distinct periods that results from overlaying the list 
 * of input periods. Non-contiguous periods will not be returned
 * 
 * @param periods
 * @return
 */
public static List<Period> getDistinctPeriods(Collection<Period> periods){

    Set<Long> instants = new HashSet<Long>();

    for (Period period : periods){
        instants.add(period.getStart().getMillis());
        instants.add(period.getEnd().getMillis());
    }

    List<Long> sorted = new ArrayList<Long>(instants);      
    Collections.sort(sorted);

    List<Period> distinctPeriods = new ArrayList<Period>();
    Period period = null;
    for (int i=0;i<sorted.size();i++){
        if (period != null){
            period.setEnd(new DateTime(sorted.get(i)));
            distinctPeriods.add(period);
        }
        period = new Period();
        period.setStart(new DateTime(sorted.get(i)));           
    }

    List<Period> emptyPeriods = new ArrayList<Period>();
    for (Period distinctPeriod : distinctPeriods){
        boolean empty = true;
        for (Period actualPeriod : periods){
            if (actualPeriod.contains(distinctPeriod)){
                empty = false;
                break;
            }
        }
        if (empty){
            emptyPeriods.add(distinctPeriod);
        }
    }
    distinctPeriods.removeAll(emptyPeriods);

    return distinctPeriods;
}

public boolean abuts(Period period){
    return this.getStartMillis() == period.getEndMillis() || this.getEndMillis() == period.getStartMillis();
}

public DateTime getStartDate(){
    return this.getStart();
}

public void setStartDate(DateTime date){
    this.setStart(date);
}

public DateTime getEndDate(){
    return this.getEnd();
}

public void setEndDate(DateTime date){
    this.setEnd(date);
}

public Period(){
    super(defaultEffectiveInstant,defaultTerminationInstant);
}   

public Period(DateTime start, DateTime end){
    super(start,end);
}

public Period(long start, long end) {
    super(start,end);
}

public Period(ReadableInterval period) {
    super(period.getStart(),period.getEnd());
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = prime;
    result = prime * result + new Long(this.getStartMillis()).hashCode();
    result = prime * result + new Long(this.getEndMillis()).hashCode();
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (!Period.class.isAssignableFrom(obj.getClass()))
        return false;
    Period other = (Period) obj;
    if (this.getEnd() == null) {
        if (other.getEnd() != null)
            return false;
    } else if (!this.getEnd().isEqual(other.getEnd()))
        return false;
    if (this.getStart() == null) {
        if (other.getStart() != null)
            return false;
    } else if (!this.getStart().isEqual(other.getStart()))
        return false;
    return true;
}

public int compareByEnd(Period o) {
    return o.getEnd().compareTo(this.getEnd());
}

@Override
public int compareTo(Period o) {
    int compare = this.getStart().compareTo(o.getStart());      
    return compare;
}    

public String toString(){
    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZZ");
    return fmt.format(this.getStart().toDate()) + "-" + fmt.format(this.getEnd().toDate());
}

public String toString(String format){
    SimpleDateFormat fmt = new SimpleDateFormat(format);
    return fmt.format(this.getStart().toDate()) + "-" + fmt.format(this.getEnd().toDate());
}

public boolean containsIncludingEndpoints(DateTime next) {
    return contains(next) || getStart().isEqual(next) || getEnd().isEqual(next);
}
}

And here's the adapter...

package com.etp.commons.persistence.model.jaxb;

import javax.xml.bind.annotation.adapters.XmlAdapter;

import com.etp.commons.persistence.model.time.Period;

public class XMLPeriodAdapter extends XmlAdapter<String, Period> {

/*
@Override
public Period unmarshal(JAXBAdaptedPeriod v) throws Exception {
    return new Period(new DateTime(v.getStart().getTime()), new DateTime(v.getEnd().getTime()));
}

@Override
public JAXBAdaptedPeriod marshal(Period v) throws Exception {
    JAXBAdaptedPeriod  period = new JAXBAdaptedPeriod();

    if(v!=null){
        period.setStart(v.getStart().toDate());
        period.setEnd(v.getEnd().toDate());
    }

    return period;
}
 */

@Override
public Period unmarshal(final String v) throws Exception {
    final int dashIndex = v.indexOf('-');
    final long start = Long.valueOf(v.substring(0, dashIndex));
    final long end = Long.valueOf(v.substring(dashIndex + 1));
    return new Period(start, end);
}

@Override
public String marshal(final Period v) throws Exception {
    return v.getStartMillis() + "-" + v.getEndMillis();
}
}

UPDATE WITH MORE DETAIL

Let's say I have a service like so...

@WebService(targetNamespace="http://www.foo.com/DerivationModelServices")
public interface DerivationModelService extends InternalDerivationModelService{


public Determinant getDeterminant(DeterminantQueryParameters params) throws PersistenceServiceException;



public DeterminantDefinition getDeterminantDefinition(DeterminantDefinitionQueryParameters params) throws PersistenceServiceException;



public Group getGroup(GroupQueryParameters params) throws Exception;    

public AttributeDefinition getAttributeDefinition(AttributeDefinitionQueryParameters params) throws PersistenceServiceException;

public void saveGroup(Group group) throws PersistenceServiceException;

public void saveDeterminant(Determinant determinant) throws PersistenceServiceException;

public DerivationModel getDerivationModel(DerivationModelQueryParameters model) throws PersistenceServiceException; 

public void saveReportDefinition(ReportDefinition def);

public PagedQueryResults<ReportDefinition> getReportDefinitions(ReportDefinitionsQueryParameters params);

public ReportDefinition getReportDefinition(ReportDefinitionQueryParameters params);

    }

Why would I get the no-arg constructor on Period ? Furthermore If I marked all of the super interface's methods with @WebMethod(excluded=true) so as not expose them, why would I get this exception? I ask because though the first answer I received looks like the way to go, I haven't been able to get past this. (Perhaps I've not looked everywhere in my codebase for the cases described?)

Annotation @XmlJavaTypeAdapter(value=XMLPeriodAdapter.class) should be used in youe POJOs.

@XmlType
@XmlJavaTypeAdapter(value=XMLPeriodAdapter.class)
class Response  
{    
   private Period period;  

   // another fields  
   // setters/getters
}  

if you don't want use separate POJO, you can use next

   @WebMethod  
   public void wsMethod(@WebParam   
                        @XmlJavaTypeAdapter(XMLPeriodAdapter.class)   Period period){}

   @WebMethod  
   @WebResult
   @XmlJavaTypeAdapter(XMLPeriodAdapter.class)
   public Period wsMethod(){}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM