From 2398d947523f12b450076cbdfef9685450eba5f2 Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Sat, 22 Feb 2014 21:41:04 +0100 Subject: [PATCH] #120 Don't hold a reference to TypeFactory in built-in method types --- .../ap/model/common/TypeFactory.java | 10 +------- .../CalendarToXmlGregorianCalendar.java | 20 +++++++-------- .../builtin/DateToXmlGregorianCalendar.java | 20 +++++++-------- .../model/source/builtin/JaxbElemToValue.java | 7 +++--- .../builtin/ListOfJaxbElemToListOfValue.java | 8 +++--- .../builtin/StringToXmlGregorianCalendar.java | 25 +++++++++---------- .../XmlGregorianCalendarToCalendar.java | 6 ++--- .../builtin/XmlGregorianCalendarToDate.java | 6 ++--- .../builtin/XmlGregorianCalendarToString.java | 5 ++-- 9 files changed, 45 insertions(+), 62 deletions(-) diff --git a/processor/src/main/java/org/mapstruct/ap/model/common/TypeFactory.java b/processor/src/main/java/org/mapstruct/ap/model/common/TypeFactory.java index 4f4231076..19da1fb4f 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/common/TypeFactory.java +++ b/processor/src/main/java/org/mapstruct/ap/model/common/TypeFactory.java @@ -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 getParameters(ExecutableElement method) { List parameters = method.getParameters(); List result = new ArrayList( parameters.size() ); diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/CalendarToXmlGregorianCalendar.java b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/CalendarToXmlGregorianCalendar.java index 0e09217cb..b959ff5c2 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/CalendarToXmlGregorianCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/CalendarToXmlGregorianCalendar.java @@ -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 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 getImportTypes() { - return asSet( new Type[]{ typeFactory.getType( DatatypeFactory.class ), - typeFactory.getType( GregorianCalendar.class ), - typeFactory.getType( DatatypeConfigurationException.class ) } ); + return importTypes; } @Override diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/DateToXmlGregorianCalendar.java b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/DateToXmlGregorianCalendar.java index 1ed43e332..0839013bd 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/DateToXmlGregorianCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/DateToXmlGregorianCalendar.java @@ -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 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 getImportTypes() { - return asSet( new Type[]{ typeFactory.getType( GregorianCalendar.class ), - typeFactory.getType( DatatypeFactory.class ), - typeFactory.getType( DatatypeConfigurationException.class ) }); + return importTypes; } @Override diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/JaxbElemToValue.java b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/JaxbElemToValue.java index 128757743..c4e69abbe 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/JaxbElemToValue.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/JaxbElemToValue.java @@ -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; diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/ListOfJaxbElemToListOfValue.java b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/ListOfJaxbElemToListOfValue.java index 65fa7f6be..c98a4eee8 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/ListOfJaxbElemToListOfValue.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/ListOfJaxbElemToListOfValue.java @@ -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 ) ); } } diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/StringToXmlGregorianCalendar.java b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/StringToXmlGregorianCalendar.java index cd568b55a..3a800793b 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/StringToXmlGregorianCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/StringToXmlGregorianCalendar.java @@ -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 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 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 diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToCalendar.java b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToCalendar.java index c0088e902..a9895cf8c 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToCalendar.java @@ -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 ); } diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToDate.java b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToDate.java index 0d39463fa..dfdc77c70 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToDate.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToDate.java @@ -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 ); } diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToString.java b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToString.java index e31ebf58f..136c94c56 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToString.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToString.java @@ -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 ); }