org.codehaus.groovy.transform
Class ImmutableASTTransformation

java.lang.Object
  extended by org.codehaus.groovy.transform.ImmutableASTTransformation
All Implemented Interfaces:
ASTTransformation, org.objectweb.asm.Opcodes

public class ImmutableASTTransformation
extends java.lang.Object
implements ASTTransformation, org.objectweb.asm.Opcodes

Handles generation of code for the @Immutable annotation. This is experimental, use at your own risk.

Author:
Paul King

Field Summary
private static Token ASSIGN
           
private static ClassNode CLONEABLE_TYPE
           
private static ClassNode COLLECTION_TYPE
           
private static Token COMPARE_EQUAL
           
private static Token COMPARE_IDENTICAL
           
private static Token COMPARE_NOT_EQUAL
           
private static ClassNode DATE_TYPE
           
private static ClassNode DGM_TYPE
           
private static ClassNode HASHMAP_TYPE
           
private static ClassNode HASHUTIL_TYPE
           
private static java.lang.Class[] immutableList
           
private static ClassNode MAP_TYPE
           
private static java.lang.Class MY_CLASS
           
private static ClassNode MY_TYPE
           
private static java.lang.String MY_TYPE_NAME
           
private static ClassNode OBJECT_TYPE
           
private static ClassNode SELF_TYPE
           
private static ClassNode STRINGBUFFER_TYPE
           
 
Fields inherited from interface org.objectweb.asm.Opcodes
AALOAD, AASTORE, ACC_ABSTRACT, ACC_ANNOTATION, ACC_BRIDGE, ACC_DEPRECATED, ACC_ENUM, ACC_FINAL, ACC_INTERFACE, ACC_NATIVE, ACC_PRIVATE, ACC_PROTECTED, ACC_PUBLIC, ACC_STATIC, ACC_STRICT, ACC_SUPER, ACC_SYNCHRONIZED, ACC_SYNTHETIC, ACC_TRANSIENT, ACC_VARARGS, ACC_VOLATILE, ACONST_NULL, ALOAD, ANEWARRAY, ARETURN, ARRAYLENGTH, ASTORE, ATHROW, BALOAD, BASTORE, BIPUSH, CALOAD, CASTORE, CHECKCAST, D2F, D2I, D2L, DADD, DALOAD, DASTORE, DCMPG, DCMPL, DCONST_0, DCONST_1, DDIV, DLOAD, DMUL, DNEG, DREM, DRETURN, DSTORE, DSUB, DUP, DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2, F2D, F2I, F2L, FADD, FALOAD, FASTORE, FCMPG, FCMPL, FCONST_0, FCONST_1, FCONST_2, FDIV, FLOAD, FMUL, FNEG, FREM, FRETURN, FSTORE, FSUB, GETFIELD, GETSTATIC, GOTO, I2B, I2C, I2D, I2F, I2L, I2S, IADD, IALOAD, IAND, IASTORE, ICONST_0, ICONST_1, ICONST_2, ICONST_3, ICONST_4, ICONST_5, ICONST_M1, IDIV, IF_ACMPEQ, IF_ACMPNE, IF_ICMPEQ, IF_ICMPGE, IF_ICMPGT, IF_ICMPLE, IF_ICMPLT, IF_ICMPNE, IFEQ, IFGE, IFGT, IFLE, IFLT, IFNE, IFNONNULL, IFNULL, IINC, ILOAD, IMUL, INEG, INSTANCEOF, INVOKEINTERFACE, INVOKESPECIAL, INVOKESTATIC, INVOKEVIRTUAL, IOR, IREM, IRETURN, ISHL, ISHR, ISTORE, ISUB, IUSHR, IXOR, JSR, L2D, L2F, L2I, LADD, LALOAD, LAND, LASTORE, LCMP, LCONST_0, LCONST_1, LDC, LDIV, LLOAD, LMUL, LNEG, LOOKUPSWITCH, LOR, LREM, LRETURN, LSHL, LSHR, LSTORE, LSUB, LUSHR, LXOR, MONITORENTER, MONITOREXIT, MULTIANEWARRAY, NEW, NEWARRAY, NOP, POP, POP2, PUTFIELD, PUTSTATIC, RET, RETURN, SALOAD, SASTORE, SIPUSH, SWAP, T_BOOLEAN, T_BYTE, T_CHAR, T_DOUBLE, T_FLOAT, T_INT, T_LONG, T_SHORT, TABLESWITCH, V1_1, V1_2, V1_3, V1_4, V1_5, V1_6
 
Constructor Summary
ImmutableASTTransformation()
           
 
Method Summary
private  void addProperty(ClassNode cNode, PropertyNode pNode)
           
private  void adjustPropertyForImmutability(PropertyNode pNode, java.util.List<PropertyNode> newNodes)
           
private  void adjustPropertyNode(PropertyNode pNode, Statement getterBody)
           
private  ExpressionStatement append(Expression result, Expression expr)
           
private  Expression assignExpr(Expression fieldExpr, Expression value)
           
private  Statement assignStatement(Expression fieldExpr, Expression value)
           
private  Statement calculateHashStatements(Expression hash, java.util.List<PropertyNode> list)
           
static java.lang.Object checkImmutable(java.lang.String className, java.lang.String fieldName, java.lang.Object field)
           
private  Expression checkUnresolved(ClassNode cNode, FieldNode fNode, Expression value)
           
private  Expression cloneArrayOrCloneableExpr(Expression fieldExpr)
           
private  Expression cloneCollectionExpr(Expression fieldExpr)
           
private  Expression cloneDateExpr(Expression origDate)
           
private  void createConstructor(ClassNode cNode)
           
private  void createConstructorMap(ClassNode cNode, FieldExpression constructorStyle, java.util.List<PropertyNode> list)
           
private  void createConstructorMapCommon(ClassNode cNode, FieldExpression constructorStyle, BlockStatement body)
           
private  void createConstructorMapSpecial(ClassNode cNode, FieldExpression constructorStyle, java.util.List<PropertyNode> list)
           
private  void createConstructorOrdered(ClassNode cNode, FieldExpression constructorStyle, java.util.List<PropertyNode> list)
           
private  Statement createConstructorStatement(ClassNode cNode, PropertyNode pNode)
           
private  Statement createConstructorStatementArrayOrCloneable(FieldNode fNode)
           
private  Statement createConstructorStatementCollection(FieldNode fNode)
           
private  Statement createConstructorStatementDate(FieldNode fNode)
           
private  Statement createConstructorStatementDefault(FieldNode fNode)
           
private  Statement createConstructorStatementGuarded(ClassNode cNode, FieldNode fNode)
           
private  Statement createConstructorStatementMapSpecial(FieldNode fNode)
           
private  void createEquals(ClassNode cNode)
           
private static java.lang.String createErrorMessage(java.lang.String className, java.lang.String fieldName, java.lang.String typeName, java.lang.String mode)
           
private  Statement createGetterBody(FieldNode fNode)
           
private  Statement createGetterBodyArrayOrCloneable(FieldNode fNode)
           
private  Statement createGetterBodyDate(FieldNode fNode)
           
private  Statement createGetterBodyDefault(FieldNode fNode)
           
private  void createHashCode(ClassNode cNode)
           
private  void createToString(ClassNode cNode)
           
private  void ensureNotPublic(java.lang.String cNode, FieldNode fNode)
           
private  BooleanExpression equalsNullExpr(Expression argExpr)
           
private  Expression findArg(java.lang.String fName)
           
private  BooleanExpression identicalExpr(Expression self, Expression other)
           
private  boolean implementsInterface(ClassNode fieldType, ClassNode interfaceType)
           
private static boolean inImmutableList(java.lang.Class typeClass)
           
private  boolean isKnownImmutable(ClassNode fieldType)
           
private  BooleanExpression isOneExpr(Expression expr)
           
private  BooleanExpression isTrueExpr(Expression argExpr)
           
private  BooleanExpression isZeroExpr(Expression expr)
           
private  BooleanExpression notEqualsExpr(PropertyNode pNode, Expression other)
           
private static java.lang.String prettyTypeName(java.lang.String name)
           
private  IfStatement returnFalseIfNull(Expression other)
           
private  Statement returnFalseIfPropertyNotEqual(PropertyNode pNode, Expression other)
           
private  Statement returnFalseIfWrongType(ClassNode cNode, Expression other)
           
private  IfStatement returnTrueIfIdentical(Expression self, Expression other)
           
private  Statement safeExpression(Expression fieldExpr, Expression expression)
           
private  Statement toStringPropertyName(Expression result, java.lang.String fName)
           
 void visit(ASTNode[] nodes, SourceUnit source)
          The call made when the compiler encounters an AST Transformation Annotation
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

immutableList

private static java.lang.Class[] immutableList

MY_CLASS

private static final java.lang.Class MY_CLASS

MY_TYPE

private static final ClassNode MY_TYPE

MY_TYPE_NAME

private static final java.lang.String MY_TYPE_NAME

OBJECT_TYPE

private static final ClassNode OBJECT_TYPE

HASHMAP_TYPE

private static final ClassNode HASHMAP_TYPE

MAP_TYPE

private static final ClassNode MAP_TYPE

DATE_TYPE

private static final ClassNode DATE_TYPE

CLONEABLE_TYPE

private static final ClassNode CLONEABLE_TYPE

COLLECTION_TYPE

private static final ClassNode COLLECTION_TYPE

HASHUTIL_TYPE

private static final ClassNode HASHUTIL_TYPE

STRINGBUFFER_TYPE

private static final ClassNode STRINGBUFFER_TYPE

DGM_TYPE

private static final ClassNode DGM_TYPE

SELF_TYPE

private static final ClassNode SELF_TYPE

COMPARE_EQUAL

private static final Token COMPARE_EQUAL

COMPARE_NOT_EQUAL

private static final Token COMPARE_NOT_EQUAL

COMPARE_IDENTICAL

private static final Token COMPARE_IDENTICAL

ASSIGN

private static final Token ASSIGN
Constructor Detail

ImmutableASTTransformation

public ImmutableASTTransformation()
Method Detail

visit

public void visit(ASTNode[] nodes,
                  SourceUnit source)
Description copied from interface: ASTTransformation
The call made when the compiler encounters an AST Transformation Annotation

Specified by:
visit in interface ASTTransformation
Parameters:
nodes - The ASTnodes when the call was triggered
source - The source unit being compiled

ensureNotPublic

private void ensureNotPublic(java.lang.String cNode,
                             FieldNode fNode)

createHashCode

private void createHashCode(ClassNode cNode)

createToString

private void createToString(ClassNode cNode)

toStringPropertyName

private Statement toStringPropertyName(Expression result,
                                       java.lang.String fName)

append

private ExpressionStatement append(Expression result,
                                   Expression expr)

calculateHashStatements

private Statement calculateHashStatements(Expression hash,
                                          java.util.List<PropertyNode> list)

createEquals

private void createEquals(ClassNode cNode)

returnFalseIfWrongType

private Statement returnFalseIfWrongType(ClassNode cNode,
                                         Expression other)

returnFalseIfNull

private IfStatement returnFalseIfNull(Expression other)

returnTrueIfIdentical

private IfStatement returnTrueIfIdentical(Expression self,
                                          Expression other)

returnFalseIfPropertyNotEqual

private Statement returnFalseIfPropertyNotEqual(PropertyNode pNode,
                                                Expression other)

addProperty

private void addProperty(ClassNode cNode,
                         PropertyNode pNode)

createConstructor

private void createConstructor(ClassNode cNode)

createConstructorMapSpecial

private void createConstructorMapSpecial(ClassNode cNode,
                                         FieldExpression constructorStyle,
                                         java.util.List<PropertyNode> list)

createConstructorMap

private void createConstructorMap(ClassNode cNode,
                                  FieldExpression constructorStyle,
                                  java.util.List<PropertyNode> list)

createConstructorMapCommon

private void createConstructorMapCommon(ClassNode cNode,
                                        FieldExpression constructorStyle,
                                        BlockStatement body)

createConstructorOrdered

private void createConstructorOrdered(ClassNode cNode,
                                      FieldExpression constructorStyle,
                                      java.util.List<PropertyNode> list)

createConstructorStatement

private Statement createConstructorStatement(ClassNode cNode,
                                             PropertyNode pNode)

implementsInterface

private boolean implementsInterface(ClassNode fieldType,
                                    ClassNode interfaceType)

createConstructorStatementGuarded

private Statement createConstructorStatementGuarded(ClassNode cNode,
                                                    FieldNode fNode)

checkUnresolved

private Expression checkUnresolved(ClassNode cNode,
                                   FieldNode fNode,
                                   Expression value)

createConstructorStatementCollection

private Statement createConstructorStatementCollection(FieldNode fNode)

createConstructorStatementMapSpecial

private Statement createConstructorStatementMapSpecial(FieldNode fNode)

isKnownImmutable

private boolean isKnownImmutable(ClassNode fieldType)

inImmutableList

private static boolean inImmutableList(java.lang.Class typeClass)

createConstructorStatementDefault

private Statement createConstructorStatementDefault(FieldNode fNode)

createConstructorStatementArrayOrCloneable

private Statement createConstructorStatementArrayOrCloneable(FieldNode fNode)

createConstructorStatementDate

private Statement createConstructorStatementDate(FieldNode fNode)

cloneDateExpr

private Expression cloneDateExpr(Expression origDate)

assignStatement

private Statement assignStatement(Expression fieldExpr,
                                  Expression value)

assignExpr

private Expression assignExpr(Expression fieldExpr,
                              Expression value)

equalsNullExpr

private BooleanExpression equalsNullExpr(Expression argExpr)

isTrueExpr

private BooleanExpression isTrueExpr(Expression argExpr)

isZeroExpr

private BooleanExpression isZeroExpr(Expression expr)

isOneExpr

private BooleanExpression isOneExpr(Expression expr)

notEqualsExpr

private BooleanExpression notEqualsExpr(PropertyNode pNode,
                                        Expression other)

identicalExpr

private BooleanExpression identicalExpr(Expression self,
                                        Expression other)

findArg

private Expression findArg(java.lang.String fName)

adjustPropertyForImmutability

private void adjustPropertyForImmutability(PropertyNode pNode,
                                           java.util.List<PropertyNode> newNodes)

adjustPropertyNode

private void adjustPropertyNode(PropertyNode pNode,
                                Statement getterBody)

createGetterBody

private Statement createGetterBody(FieldNode fNode)

createGetterBodyDefault

private Statement createGetterBodyDefault(FieldNode fNode)

createErrorMessage

private static java.lang.String createErrorMessage(java.lang.String className,
                                                   java.lang.String fieldName,
                                                   java.lang.String typeName,
                                                   java.lang.String mode)

prettyTypeName

private static java.lang.String prettyTypeName(java.lang.String name)

createGetterBodyArrayOrCloneable

private Statement createGetterBodyArrayOrCloneable(FieldNode fNode)

cloneArrayOrCloneableExpr

private Expression cloneArrayOrCloneableExpr(Expression fieldExpr)

cloneCollectionExpr

private Expression cloneCollectionExpr(Expression fieldExpr)

createGetterBodyDate

private Statement createGetterBodyDate(FieldNode fNode)

safeExpression

private Statement safeExpression(Expression fieldExpr,
                                 Expression expression)

checkImmutable

public static java.lang.Object checkImmutable(java.lang.String className,
                                              java.lang.String fieldName,
                                              java.lang.Object field)


Copyright © ${year} The Codehaus. All Rights Reserved.