ma.glasnost.orika.impl.generator
Class SourceCodeContext

java.lang.Object
  extended by ma.glasnost.orika.impl.generator.SourceCodeContext

public class SourceCodeContext
extends Object

SourceCodeContext contains the state information necessary while generating source code for a given mapping object; it also houses various utility methods which can be used to aid in code generation.


Constructor Summary
SourceCodeContext(String baseClassName, Class<?> superClass, MappingContext mappingContext, StringBuilder logDetails)
          Constructs a new instance of SourceCodeContext
 
Method Summary
 void addField(String fieldSource)
          Adds a field definition to the class based on the provided source.
 void addMethod(String methodSource)
          Adds a method definition to the class based on the provided source.
 boolean aggregateSpecsApply(FieldMap fieldMap)
          Tests whether any aggregate specifications apply for the specified FieldMap, and if so, adds it to the list of FieldMaps for that spec, returning true.
static void append(StringBuilder out, String... statements)
          Appends all of the String values provided to the StringBuilder in order, as "statements"
 String assureInstanceExists(VariableRef propertyRef, VariableRef source)
          Append a statement which assures that the variable reference has an existing instance; if it does not, a new object is generated using MapperFacade.newObject
 String callMapper(Type<?> sourceType, Type<?> destinationType, String sourceExpression)
           
 String callMapper(Type<?> sourceType, Type<?> destinationType, String sourceExpression, String destExpression)
           
 String callMapper(VariableRef source, Type<?> destination)
           
 String callMapper(VariableRef source, VariableRef destination)
           
 String compareFields(FieldMap fieldMap, VariableRef sourceProperty, VariableRef destinationProperty, Type<?> destinationType, StringBuilder logDetails)
          Generates source code for an "equality" comparison of two variables, based on the FieldMap passed
protected  Class<?> compileClass()
          Compile and return the (generated) class; this will also cause the generated class to be detached from the class-pool, and any (optional) source and/or class files to be written.
 String currentElementComparator(Node source, Node dest, Node.NodeList srcNodes, Node.NodeList destNodes)
           
 void debug(String msg)
           
static VariableRef entrySetRef(VariableRef s)
          Creates a VariableRef representing a Set for the provided VariableRef (which should be a Map)
 Set<FieldMap> getAssociatedMappings(Collection<FieldMap> fieldMaps, FieldMap map)
          Finds all field maps out of the provided set which are associated with the map passed in ( including that map itself); by "associated", we mean any mappings which are connected to the original FieldMap by having a matching source or destination, including transitive associations.
 String getClassName()
           
 String getClassSimpleName()
           
<T extends GeneratedObjectBase>
T
getInstance()
           
 MappingContext getMappingContext()
           
 String getPackageName()
           
protected  StringBuilder getSourceBuilder()
           
 Class<?> getSuperClass()
           
 boolean isDebugEnabled()
           
static String join(List<?> list, String separator)
          Join the items in the list together in a String, separated by the provided separator
 String mapAggregateFields()
           
 String mapFields(FieldMap fieldMap, VariableRef sourceProperty, VariableRef destinationProperty, Type<?> destinationType, StringBuilder logDetails)
          Generate the code necessary to process the provided FieldMap.
 String newObject(VariableRef source, Type<?> destinationType)
           
 String newObjectFromMapper(Type<?> sourceType, Type<?> destinationType)
           
 String newObjectFromMapper(VariableRef source, Type<?> destinationType)
          Generates a code snippet to generate a new instance of the destination type from a mapper
 boolean shouldMapNulls()
           
static String statement(String str, Object... args)
          Appends the provided string as a source code statement, ending it with a statement terminator as appropriate.
 String toSourceFile()
           
 String usedConverter(Converter<?,?> converter)
           
 String usedMapperFacadeNewObjectCall(VariableRef source, VariableRef destination)
           
 String usedType(Type<?> type)
           
 String usedType(VariableRef r)
          Generate usedType array index code for the provided variable
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SourceCodeContext

public SourceCodeContext(String baseClassName,
                         Class<?> superClass,
                         MappingContext mappingContext,
                         StringBuilder logDetails)
Constructs a new instance of SourceCodeContext

Parameters:
baseClassName -
superClass -
compilerStrategy -
propertyResolver -
mapperFactory -
logDetails -
Method Detail

isDebugEnabled

public boolean isDebugEnabled()
Returns:
true if debug logging is enabled for this context

debug

public void debug(String msg)

getSourceBuilder

protected StringBuilder getSourceBuilder()
Returns:
the StringBuilder containing the current accumulated source.

getSuperClass

public Class<?> getSuperClass()

getClassSimpleName

public String getClassSimpleName()

getPackageName

public String getPackageName()

getClassName

public String getClassName()

shouldMapNulls

public boolean shouldMapNulls()

getMappingContext

public MappingContext getMappingContext()

addMethod

public void addMethod(String methodSource)
Adds a method definition to the class based on the provided source.

Parameters:
methodSource -

addField

public void addField(String fieldSource)
Adds a field definition to the class based on the provided source.

Parameters:
fieldSource - the source from which to compile the field

toSourceFile

public String toSourceFile()
Returns:
the completed generated java source for the class.

compileClass

protected Class<?> compileClass()
                         throws CompilerStrategy.SourceCodeGenerationException
Compile and return the (generated) class; this will also cause the generated class to be detached from the class-pool, and any (optional) source and/or class files to be written.

Returns:
the (generated) compiled class
Throws:
javassist.CannotCompileException
IOException
CompilerStrategy.SourceCodeGenerationException

getInstance

public <T extends GeneratedObjectBase> T getInstance()
                                          throws CompilerStrategy.SourceCodeGenerationException,
                                                 InstantiationException,
                                                 IllegalAccessException
Returns:
a new instance of the (generated) compiled class
Throws:
javassist.CannotCompileException
IOException
InstantiationException
IllegalAccessException
CompilerStrategy.SourceCodeGenerationException

usedConverter

public String usedConverter(Converter<?,?> converter)

usedType

public String usedType(Type<?> type)

callMapper

public String callMapper(Type<?> sourceType,
                         Type<?> destinationType,
                         String sourceExpression,
                         String destExpression)

callMapper

public String callMapper(Type<?> sourceType,
                         Type<?> destinationType,
                         String sourceExpression)

callMapper

public String callMapper(VariableRef source,
                         VariableRef destination)

callMapper

public String callMapper(VariableRef source,
                         Type<?> destination)

usedMapperFacadeNewObjectCall

public String usedMapperFacadeNewObjectCall(VariableRef source,
                                            VariableRef destination)

newObjectFromMapper

public String newObjectFromMapper(Type<?> sourceType,
                                  Type<?> destinationType)

newObjectFromMapper

public String newObjectFromMapper(VariableRef source,
                                  Type<?> destinationType)
Generates a code snippet to generate a new instance of the destination type from a mapper

Parameters:
source -
destinationType -
Returns:
a code snippet to generate a new instance of the destination type from a mapper

usedType

public String usedType(VariableRef r)
Generate usedType array index code for the provided variable

Parameters:
r -
Returns:
the code snippet for referencing a used type by it's array index

newObject

public String newObject(VariableRef source,
                        Type<?> destinationType)
Parameters:
source -
destinationType -
Returns:
the code snippet for generating a new instance, or assigning the default value in cases of primitive types

assureInstanceExists

public String assureInstanceExists(VariableRef propertyRef,
                                   VariableRef source)
Append a statement which assures that the variable reference has an existing instance; if it does not, a new object is generated using MapperFacade.newObject

Parameters:
propertyRef - the property or variable reference on which to check for an instance
source -
Returns:
a reference to this SourceCodeBuilder

statement

public static String statement(String str,
                               Object... args)
Appends the provided string as a source code statement, ending it with a statement terminator as appropriate.

Parameters:
str -
args -
Returns:
a reference to this SourceCodeBuilder

append

public static void append(StringBuilder out,
                          String... statements)
Appends all of the String values provided to the StringBuilder in order, as "statements"

Parameters:
out -
statements -

join

public static String join(List<?> list,
                          String separator)
Join the items in the list together in a String, separated by the provided separator

Parameters:
list -
separator -
Returns:
a String which joins the items of the list

entrySetRef

public static VariableRef entrySetRef(VariableRef s)
Creates a VariableRef representing a Set for the provided VariableRef (which should be a Map)

Parameters:
s - the Map type variable ref
Returns:
a new VariableRef corresponding to an EntrySet for the provided variable ref, which should be a Map type

currentElementComparator

public String currentElementComparator(Node source,
                                       Node dest,
                                       Node.NodeList srcNodes,
                                       Node.NodeList destNodes)
Parameters:
source -
dest -
srcNodes -
destNodes -
Returns:
a code snippet suitable to use as an equality comparison test for the provided source and destination nodes

getAssociatedMappings

public Set<FieldMap> getAssociatedMappings(Collection<FieldMap> fieldMaps,
                                           FieldMap map)
Finds all field maps out of the provided set which are associated with the map passed in ( including that map itself); by "associated", we mean any mappings which are connected to the original FieldMap by having a matching source or destination, including transitive associations.

Parameters:
fieldMaps - the set of all field maps
map - the field map from which to start searching for reference
Returns:
a Set of FieldMaps which are associated; they must be mapped in parallel

aggregateSpecsApply

public boolean aggregateSpecsApply(FieldMap fieldMap)
Tests whether any aggregate specifications apply for the specified FieldMap, and if so, adds it to the list of FieldMaps for that spec, returning true. Otherwise, false is returned.

Parameters:
fieldMap -
Returns:

mapAggregateFields

public String mapAggregateFields()
Returns:
the source code generated from applying all aggregated specs with accumulated FieldMaps to those FieldMap lists.

mapFields

public String mapFields(FieldMap fieldMap,
                        VariableRef sourceProperty,
                        VariableRef destinationProperty,
                        Type<?> destinationType,
                        StringBuilder logDetails)
Generate the code necessary to process the provided FieldMap.

Parameters:
fieldMap - the FieldMap describing fields to be mapped
sourceProperty - a variable reference to the source property
destinationProperty - a variable reference to the destination property
destinationType - the destination's type
logDetails - a StringBuilder to contain the debug output
Returns:
a reference to this CodeSourceBuilder

compareFields

public String compareFields(FieldMap fieldMap,
                            VariableRef sourceProperty,
                            VariableRef destinationProperty,
                            Type<?> destinationType,
                            StringBuilder logDetails)
Generates source code for an "equality" comparison of two variables, based on the FieldMap passed

Parameters:
fieldMap -
sourceProperty -
destinationProperty -
destinationType -
logDetails -
Returns:
the source code for equality test of the provided fields


Copyright © 2013 Glasnost. All Rights Reserved.