#120 Don't hold a reference to TypeFactory in built-in method types

This commit is contained in:
Gunnar Morling 2014-02-22 21:41:04 +01:00
parent 52ccedb0e1
commit 2398d94752
9 changed files with 45 additions and 62 deletions

View File

@ -35,6 +35,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
@ -201,15 +202,6 @@ public class TypeFactory {
);
}
public Parameter createParameter(String name, Class<?> type) {
return new Parameter(
name,
getType( type )
);
}
public List<Parameter> getParameters(ExecutableElement method) {
List<? extends VariableElement> parameters = method.getParameters();
List<Parameter> result = new ArrayList<Parameter>( parameters.size() );

View File

@ -21,7 +21,6 @@ package org.mapstruct.ap.model.source.builtin;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Set;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
@ -33,27 +32,28 @@ import org.mapstruct.ap.model.common.TypeFactory;
import static org.mapstruct.ap.util.Collections.asSet;
/**
*
* @author Sjaak Derksen
*/
public class CalendarToXmlGregorianCalendar extends BuiltInMethod {
private final Parameter parameter;
private final Type returnType;
private final Set<Type> importTypes;
private final TypeFactory typeFactory;
public CalendarToXmlGregorianCalendar( TypeFactory typeFactory ) {
this.typeFactory = typeFactory;
this.parameter = typeFactory.createParameter( "cal ", Calendar.class );
public CalendarToXmlGregorianCalendar(TypeFactory typeFactory) {
this.parameter = new Parameter( "cal ", typeFactory.getType( Calendar.class ) );
this.returnType = typeFactory.getType( XMLGregorianCalendar.class );
this.importTypes = asSet(
typeFactory.getType( DatatypeFactory.class ),
typeFactory.getType( GregorianCalendar.class ),
typeFactory.getType( DatatypeConfigurationException.class )
);
}
@Override
public Set<Type> getImportTypes() {
return asSet( new Type[]{ typeFactory.getType( DatatypeFactory.class ),
typeFactory.getType( GregorianCalendar.class ),
typeFactory.getType( DatatypeConfigurationException.class ) } );
return importTypes;
}
@Override

View File

@ -21,7 +21,6 @@ package org.mapstruct.ap.model.source.builtin;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Set;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
@ -33,27 +32,28 @@ import org.mapstruct.ap.model.common.TypeFactory;
import static org.mapstruct.ap.util.Collections.asSet;
/**
*
* @author Sjaak Derksen
*/
public class DateToXmlGregorianCalendar extends BuiltInMethod {
private final Parameter parameter;
private final Type returnType;
private final Set<Type> importTypes;
private final TypeFactory typeFactory;
public DateToXmlGregorianCalendar( TypeFactory typeFactory ) {
this.typeFactory = typeFactory;
this.parameter = typeFactory.createParameter( "date", Date.class );
public DateToXmlGregorianCalendar(TypeFactory typeFactory) {
this.parameter = new Parameter( "date", typeFactory.getType( Date.class ) );
this.returnType = typeFactory.getType( XMLGregorianCalendar.class );
this.importTypes = asSet(
typeFactory.getType( GregorianCalendar.class ),
typeFactory.getType( DatatypeFactory.class ),
typeFactory.getType( DatatypeConfigurationException.class )
);
}
@Override
public Set<Type> getImportTypes() {
return asSet( new Type[]{ typeFactory.getType( GregorianCalendar.class ),
typeFactory.getType( DatatypeFactory.class ),
typeFactory.getType( DatatypeConfigurationException.class ) });
return importTypes;
}
@Override

View File

@ -25,7 +25,6 @@ import org.mapstruct.ap.model.common.Type;
import org.mapstruct.ap.model.common.TypeFactory;
/**
*
* @author Sjaak Derksen
*/
public class JaxbElemToValue extends BuiltInMethod {
@ -33,15 +32,15 @@ public class JaxbElemToValue extends BuiltInMethod {
private final Parameter parameter;
private final Type returnType;
public JaxbElemToValue( TypeFactory typeFactory ) {
this.parameter = typeFactory.createParameter( "element", JAXBElement.class );
public JaxbElemToValue(TypeFactory typeFactory) {
this.parameter = new Parameter( "element", typeFactory.getType( JAXBElement.class ) );
this.returnType = typeFactory.getType( Object.class );
}
@Override
public boolean doTypeVarsMatch(Type sourceType, Type targetType) {
boolean match = false;
if (sourceType.getTypeParameters().size() == 1) {
if ( sourceType.getTypeParameters().size() == 1 ) {
match = sourceType.getTypeParameters().get( 0 ).equals( targetType );
}
return match;

View File

@ -19,7 +19,6 @@
package org.mapstruct.ap.model.source.builtin;
import java.util.List;
import javax.xml.bind.JAXBElement;
import org.mapstruct.ap.model.common.Parameter;
@ -27,7 +26,6 @@ import org.mapstruct.ap.model.common.Type;
import org.mapstruct.ap.model.common.TypeFactory;
/**
*
* @author Sjaak Derksen
*/
public class ListOfJaxbElemToListOfValue extends BuiltInMethod {
@ -36,8 +34,8 @@ public class ListOfJaxbElemToListOfValue extends BuiltInMethod {
private final Type returnType;
private final Type genericParam;
public ListOfJaxbElemToListOfValue( TypeFactory typeFactory ) {
this.parameter = typeFactory.createParameter( "elementList", List.class );
public ListOfJaxbElemToListOfValue(TypeFactory typeFactory) {
this.parameter = new Parameter( "elementList", typeFactory.getType( List.class ) );
this.returnType = typeFactory.getType( List.class );
this.genericParam = typeFactory.getType( JAXBElement.class ).erasure();
}
@ -47,7 +45,7 @@ public class ListOfJaxbElemToListOfValue extends BuiltInMethod {
boolean match = false;
if ( ( sourceType.getTypeParameters().size() == 1 ) && ( targetType.getTypeParameters().size() == 1 ) ) {
Type typeParam = sourceType.getTypeParameters().get( 0 );
if ( typeParam.erasure().equals( genericParam ) && ( typeParam.getTypeParameters().size() == 1 ) ) {
if ( typeParam.erasure().equals( genericParam ) && ( typeParam.getTypeParameters().size() == 1 ) ) {
match = typeParam.getTypeParameters().get( 0 ).equals( targetType.getTypeParameters().get( 0 ) );
}
}

View File

@ -23,7 +23,6 @@ import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Set;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
@ -36,31 +35,31 @@ import org.mapstruct.ap.model.common.TypeFactory;
import static org.mapstruct.ap.util.Collections.asSet;
/**
*
* @author Sjaak Derksen
*/
public class StringToXmlGregorianCalendar extends BuiltInMethod {
private final Parameter parameter;
private final Type returnType;
private final Set<Type> importTypes;
private final TypeFactory typeFactory;
public StringToXmlGregorianCalendar( TypeFactory typeFactory ) {
this.typeFactory = typeFactory;
this.parameter = typeFactory.createParameter( "date" , String.class );
public StringToXmlGregorianCalendar(TypeFactory typeFactory) {
this.parameter = new Parameter( "date", typeFactory.getType( String.class ) );
this.returnType = typeFactory.getType( XMLGregorianCalendar.class );
this.importTypes = asSet(
typeFactory.getType( GregorianCalendar.class ),
typeFactory.getType( SimpleDateFormat.class ),
typeFactory.getType( DateFormat.class ),
typeFactory.getType( ParseException.class ),
typeFactory.getType( DatatypeFactory.class ),
typeFactory.getType( DatatypeConfigurationException.class )
);
}
@Override
public Set<Type> getImportTypes() {
return asSet( new Type[]{ typeFactory.getType( GregorianCalendar.class ),
typeFactory.getType( SimpleDateFormat.class ),
typeFactory.getType( DateFormat.class ),
typeFactory.getType( ParseException.class ),
typeFactory.getType( DatatypeFactory.class ),
typeFactory.getType( DatatypeConfigurationException.class ) });
return importTypes;
}
@Override

View File

@ -19,7 +19,6 @@
package org.mapstruct.ap.model.source.builtin;
import java.util.Calendar;
import javax.xml.datatype.XMLGregorianCalendar;
import org.mapstruct.ap.model.common.Parameter;
@ -27,7 +26,6 @@ import org.mapstruct.ap.model.common.Type;
import org.mapstruct.ap.model.common.TypeFactory;
/**
*
* @author Sjaak Derksen
*/
public class XmlGregorianCalendarToCalendar extends BuiltInMethod {
@ -35,8 +33,8 @@ public class XmlGregorianCalendarToCalendar extends BuiltInMethod {
private final Parameter parameter;
private final Type returnType;
public XmlGregorianCalendarToCalendar( TypeFactory typeFactory ) {
this.parameter = typeFactory.createParameter( "xcal", XMLGregorianCalendar.class );
public XmlGregorianCalendarToCalendar(TypeFactory typeFactory) {
this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) );
this.returnType = typeFactory.getType( Calendar.class );
}

View File

@ -19,7 +19,6 @@
package org.mapstruct.ap.model.source.builtin;
import java.util.Date;
import javax.xml.datatype.XMLGregorianCalendar;
import org.mapstruct.ap.model.common.Parameter;
@ -27,7 +26,6 @@ import org.mapstruct.ap.model.common.Type;
import org.mapstruct.ap.model.common.TypeFactory;
/**
*
* @author Sjaak Derksen
*/
public class XmlGregorianCalendarToDate extends BuiltInMethod {
@ -35,8 +33,8 @@ public class XmlGregorianCalendarToDate extends BuiltInMethod {
private final Parameter parameter;
private final Type returnType;
public XmlGregorianCalendarToDate( TypeFactory typeFactory ) {
this.parameter = typeFactory.createParameter( "xcal", XMLGregorianCalendar.class );
public XmlGregorianCalendarToDate(TypeFactory typeFactory) {
this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) );
this.returnType = typeFactory.getType( Date.class );
}

View File

@ -26,7 +26,6 @@ import org.mapstruct.ap.model.common.Type;
import org.mapstruct.ap.model.common.TypeFactory;
/**
*
* @author Sjaak Derksen
*/
public class XmlGregorianCalendarToString extends BuiltInMethod {
@ -34,8 +33,8 @@ public class XmlGregorianCalendarToString extends BuiltInMethod {
private final Parameter parameter;
private final Type returnType;
public XmlGregorianCalendarToString( TypeFactory typeFactory ) {
this.parameter = typeFactory.createParameter( "xcal" , XMLGregorianCalendar.class );
public XmlGregorianCalendarToString(TypeFactory typeFactory) {
this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) );
this.returnType = typeFactory.getType( String.class );
}