ma.glasnost.orika.impl.generator
Class VariableRef

java.lang.Object
  extended by ma.glasnost.orika.impl.generator.VariableRef
Direct Known Subclasses:
MapEntryRef, MultiOccurrenceVariableRef

public class VariableRef
extends Object

VariableRef represents a reference to a given variable or property; it contains various helper methods to properly set it's value and interrogate it's underlying property or type. It also returns a properly type-safe cast of it as the toString() method, so it can safely be used directly as a replacement parameter for source code statements.

Author:
matt.deboer@gmail.com

Field Summary
protected  String name
           
 
Constructor Summary
VariableRef(Property property, String name)
           
VariableRef(Property property, VariableRef anchor)
           
VariableRef(Type<?> type, String name)
           
 
Method Summary
 String assertType()
           
 String assign(String value, Object... replacements)
          Generates code to perform assignment to this VariableRef.
 String assign(VariableRef value)
          Returns java code which assigns the value of the provided PropertyRef to this PropertyRef
 String assignIfPossible(String value, Object... replacements)
          Generates code to perform assignment to this VariableRef, if it is assignable.
 String assignIfPossible(VariableRef value)
          Generates code to perform assignment to this VariableRef, if it is assignable.
 String asWrapper()
           
 String cast(String value)
          Returns Java code which provides a cast of the specified value to the type of this property ref
protected static String cast(String value, Type<?> type)
          Returns Java code which provides a cast of the specified value to the type of this property ref
 String cast(VariableRef ref)
           
protected static String cast(VariableRef value, Type<?> type)
          Returns Java code which provides a cast of the specified value to the type of this property ref
 String declare()
          Returns Java code which declares this variable, initialized with it's default value.
 String declare(String value, Object... args)
          Returns Java code which declares this variable, initialized with the provided value.
 String declare(VariableRef ref)
          Returns Java code which declares this variable, initialized with the provided value.
 VariableRef elementRef(String name)
           
 Type<?> elementType()
           
 String elementTypeName()
           
 Type<?> elementValueType()
           
 boolean equals(Object obj)
           
 Converter<?,?> getConverter()
           
static String getDefaultValue(Class<?> clazz)
          Returns the Java code which represents the default value for the specified type
protected static String getGetter(Property property, String variableExpression)
           
 VariableRef getOwner()
           
 List<VariableRef> getPath()
           
protected static String getSetter(Property property, String variableExpression)
          Returns a fully type-cast setter for the property which has no reliance on java generics.
protected  String getter()
           
 int hashCode()
           
 String ifNotNull()
           
 String ifNull()
           
 String ifPathNotNull()
          Return Java code which avoids a NullPointerException when accessing this variable reference; if it is not backed by a nested property, this method returns the empty string.
 boolean isArray()
           
 boolean isAssignable()
           
 boolean isCollection()
           
 boolean isDeclared()
           
 String isInstanceOf(Type<?> type)
           
 boolean isList()
           
 boolean isMap()
           
 boolean isMapEntry()
           
 boolean isNestedProperty()
           
 String isNull()
           
 boolean isPrimitive()
           
protected static boolean isPrimitiveLiteral(String expr, Type<?> type)
           
 boolean isReadable()
           
 boolean isSet()
           
 boolean isWrapper()
           
 Type<?> mapKeyType()
           
 Type<?> mapValueType()
           
 String name()
           
 String notNull()
           
 String owner()
           
 String primitiveType()
           
 String primitiveType(Class<?> clazz)
           
 Property property()
           
 Class<?> rawType()
           
 void setConverter(Converter<?,?> converter)
           
 void setOwner(VariableRef owner)
           
protected  String setter()
           
 String size()
          Generates java code for a reference to the "size" of this VariableRef
 String toString()
           
 Type<?> type()
           
 String typeName()
           
 String wrapperTypeName()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

name

protected String name
Constructor Detail

VariableRef

public VariableRef(Property property,
                   String name)

VariableRef

public VariableRef(Property property,
                   VariableRef anchor)

VariableRef

public VariableRef(Type<?> type,
                   String name)
Method Detail

setConverter

public void setConverter(Converter<?,?> converter)

getConverter

public Converter<?,?> getConverter()

setOwner

public void setOwner(VariableRef owner)

getOwner

public VariableRef getOwner()

getter

protected String getter()

setter

protected String setter()

isReadable

public boolean isReadable()

isAssignable

public boolean isAssignable()

rawType

public Class<?> rawType()

property

public Property property()
Returns:
the Property (if any) associated with this VariableRef

type

public Type<?> type()

isPrimitive

public boolean isPrimitive()

isArray

public boolean isArray()

isCollection

public boolean isCollection()

isList

public boolean isList()

isSet

public boolean isSet()

isMap

public boolean isMap()

isMapEntry

public boolean isMapEntry()

isWrapper

public boolean isWrapper()

wrapperTypeName

public String wrapperTypeName()

elementRef

public VariableRef elementRef(String name)

elementTypeName

public String elementTypeName()

elementValueType

public Type<?> elementValueType()

elementType

public Type<?> elementType()

mapKeyType

public Type<?> mapKeyType()

mapValueType

public Type<?> mapValueType()

typeName

public String typeName()

asWrapper

public String asWrapper()

assign

public String assign(String value,
                     Object... replacements)
Generates code to perform assignment to this VariableRef.

Parameters:
value -
replacements -
Returns:

assignIfPossible

public String assignIfPossible(String value,
                               Object... replacements)
Generates code to perform assignment to this VariableRef, if it is assignable.

Parameters:
value -
replacements -
Returns:

assignIfPossible

public String assignIfPossible(VariableRef value)
Generates code to perform assignment to this VariableRef, if it is assignable.

Parameters:
value -
replacements -
Returns:

assign

public String assign(VariableRef value)
Returns java code which assigns the value of the provided PropertyRef to this PropertyRef

Parameters:
value -
Returns:

cast

public String cast(VariableRef ref)

cast

public String cast(String value)
Returns Java code which provides a cast of the specified value to the type of this property ref

Parameters:
value -
Returns:

cast

protected static String cast(String value,
                             Type<?> type)
Returns Java code which provides a cast of the specified value to the type of this property ref

Parameters:
value -
Returns:

cast

protected static String cast(VariableRef value,
                             Type<?> type)
Returns Java code which provides a cast of the specified value to the type of this property ref

Parameters:
value -
Returns:

declare

public String declare()
Returns Java code which declares this variable, initialized with it's default value.

Returns:
the code which declares this variable, and explicitly assigns it's default value.

declare

public String declare(String value,
                      Object... args)
Returns Java code which declares this variable, initialized with the provided value.

Parameters:
value - the value to assign
args - any replacement arguments to applied to value via String.format()
Returns:
the code which declares this variable, and explicitly assigns the provided value.

declare

public String declare(VariableRef ref)
Returns Java code which declares this variable, initialized with the provided value.

Parameters:
value - the value to assign
args - any replacement arguments to applied to value via String.format()
Returns:
the code which declares this variable, and explicitly assigns the provided value.

isDeclared

public boolean isDeclared()

getDefaultValue

public static String getDefaultValue(Class<?> clazz)
Returns the Java code which represents the default value for the specified type

Parameters:
clazz -
Returns:

primitiveType

public String primitiveType()

primitiveType

public String primitiveType(Class<?> clazz)

owner

public String owner()

name

public String name()

isNull

public String isNull()

notNull

public String notNull()
Returns:
a nested-property safe null check for this property

ifNotNull

public String ifNotNull()

ifNull

public String ifNull()

toString

public String toString()
Overrides:
toString in class Object

isNestedProperty

public boolean isNestedProperty()
Returns:
whether or not this VariableRef represents a nested property

getPath

public List<VariableRef> getPath()

assertType

public String assertType()

size

public String size()
Generates java code for a reference to the "size" of this VariableRef

Returns:

getGetter

protected static String getGetter(Property property,
                                  String variableExpression)

isInstanceOf

public String isInstanceOf(Type<?> type)

getSetter

protected static String getSetter(Property property,
                                  String variableExpression)
Returns a fully type-cast setter for the property which has no reliance on java generics.

Parameters:
property - the Property for which to return the getter
variableExpression - the String value to use for the variable on which the getter is called
Returns:

ifPathNotNull

public String ifPathNotNull()
Return Java code which avoids a NullPointerException when accessing this variable reference; if it is not backed by a nested property, this method returns the empty string.

Returns:

isPrimitiveLiteral

protected static boolean isPrimitiveLiteral(String expr,
                                            Type<?> type)

hashCode

public int hashCode()
Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
Overrides:
equals in class Object


Copyright © 2013 Glasnost. All Rights Reserved.