Top Description Inners Fields Constructors Methods
org.codehaus.groovy.transform.trait

public abstract Class Traits

extends Object
Class Inheritance
Imports
groovy.lang.GeneratedGroovyProxy, groovy.transform.SelfType, .Trait, org.codehaus.groovy.GroovyBugError, org.codehaus.groovy.ast.AnnotationNode, .ClassHelper, .ClassNode, .FieldNode, .InnerClassNode, .MethodNode, org.codehaus.groovy.ast.expr.ClassExpression, .Expression, .ListExpression, org.codehaus.groovy.ast.tools.GenericsUtils, org.codehaus.groovy.classgen.asm.BytecodeHelper, org.codehaus.groovy.runtime.DefaultGroovyMethods, org.objectweb.asm.Opcodes, java.lang.annotation.ElementType, .Retention, .RetentionPolicy, .Target, java.lang.reflect.Method, java.util.Arrays, .Iterator, .LinkedHashSet, .LinkedList, .List

A collection of utility methods used to deal with traits.
Since
2.3.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static @interface
Traits.Implemented

Internal annotation used to indicate which methods in a trait interface have a default implementation.

public static @interface
Traits.TraitBridge

Internal annotation used to indicate that a method is a bridge method to a trait default implementation.

Field Summary

Modifier and TypeField and Description
pack-priv static final String
pack-priv static final String
pack-priv static final String
pack-priv static final String
pack-priv static final int
pack-priv static final List<Integer>
public static final ClassNode
public static final ClassNode
pack-priv static final String
pack-priv static final String
pack-priv static final String
public static final ClassNode
pack-priv static final String
pack-priv static final String
pack-priv static final String
public static final String
pack-priv static final String
public static final String
public static final Class<Trait>
public static final ClassNode
pack-priv static final String
pack-priv static final String
public static final ClassNode

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static LinkedHashSet<ClassNode>
collectAllInterfacesReverseOrder(final ClassNode
a class node
cNode
,
final LinkedHashSet<ClassNode>
ordered set of interfaces
interfaces
)

Collects all interfaces of a class node, but reverses the order of the declaration of direct interfaces of this class node.

public static LinkedHashSet<ClassNode>

Returns:

the selfTypes collection
collectSelfTypes
(final ClassNode
a class node that may implement a trait
receiver
,
final LinkedHashSet<ClassNode>
a set where the self types will be put
selfTypes
)

Collects all the self types that a type should extend or implement, given the traits is implements.

public static LinkedHashSet<ClassNode>

Returns:

the selfTypes collection
collectSelfTypes
(final ClassNode
a class node that may implement a trait
receiver
,
final LinkedHashSet<ClassNode>
a set where the self types will be put
selfTypes
,
final boolean
should the interfaces that the node implements be collected too
checkInterfaces
,
final boolean
should we collect from the superclass too
checkSuperClass
)

Collects all the self types that a type should extend or implement, given the traits is implements.

public static String[]
decomposeSuperCallName(final String methodName)

Returns the trait and method names derived from super-trait name scheme or null if the method name doesn't correspond to a trait method.

pack-priv static String
public static ClassNode
public static ClassNode
findHelper(final ClassNode trait)

pack-priv static TraitHelpersTuple
findHelpers(final ClassNode trait)

public static ClassNode
public static List<ClassNode>

Returns:

the list of ordered trait classnodes
findTraits
(final ClassNode
the given classnode
cNode
)

Find all traits associated with the given type.

public static <T> T

Returns:

the object coerced to the target class, or the proxy instance if it is compatible with the target class.
getAsType
(Object
an object to be coerced to some class
self
,
Class<T>
the class to be coerced to
clazz
)

Converts a class implementing some trait into a target class.

public static Method

Returns:

null if it is not a method implemented in a trait. If it is, returns the method from the trait class.
getBridgeMethodTarget
(Method
a method node
someMethod
)

Reflection API to find the method corresponding to the default implementation of a trait, given a bridge method.

pack-priv static String
public static boolean

Returns:

true if the method has a default implementation in the trait
hasDefaultImplementation
(final MethodNode
a method node
method
)

Indicates whether a method in a trait interface has a default implementation.

public static boolean

Returns:

true if the method has a default implementation in the trait
hasDefaultImplementation
(final Method
a method node
method
)

Indicates whether a method in a trait interface has a default implementation.

pack-priv static String
helperClassName(final ClassNode traitNode)

pack-priv static String
pack-priv static String
public static boolean

Returns:

true if the specified class node is annotated with the Trait interface.
isAnnotatedWithTrait
(final ClassNode
a class node
cNode
)

Returns true if the specified class node is annotated with the Trait interface.

public static boolean

Returns:

null if it is not a method implemented in a trait. If it is, returns the method from the trait class.
isBridgeMethod
(Method
a method node
someMethod
)

Reflection API to indicate whether some method is a bridge method to the default implementation of a trait.

public static boolean

Returns:

true if the classnode represents a trait
isTrait
(final ClassNode
a class node to test
cNode
)

Returns true if the specified class node is a trait.

public static boolean

Returns:

true if the classnode represents a trait
isTrait
(final Class<?>
a class to test
clazz
)

Returns true if the specified class is a trait.

pack-priv static String
remappedFieldName(final ClassNode traitNode, final String name)

pack-priv static String
private static ClassNode
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DIRECT_GETTER_SUFFIXback to summary
pack-priv static final String DIRECT_GETTER_SUFFIX
DIRECT_SETTER_SUFFIXback to summary
pack-priv static final String DIRECT_SETTER_SUFFIX
FIELD_HELPERback to summary
pack-priv static final String FIELD_HELPER
FIELD_PREFIXback to summary
pack-priv static final String FIELD_PREFIX
FIELD_PREFIX_MASKback to summary
pack-priv static final int FIELD_PREFIX_MASK
FIELD_PREFIXESback to summary
pack-priv static final List<Integer> FIELD_PREFIXES
GENERATED_PROXY_CLASSNODEback to summary
public static final ClassNode GENERATED_PROXY_CLASSNODE
IMPLEMENTED_CLASSNODEback to summary
public static final ClassNode IMPLEMENTED_CLASSNODE
INIT_METHODback to summary
pack-priv static final String INIT_METHOD
PRIVATE_FIELD_PREFIXback to summary
pack-priv static final String PRIVATE_FIELD_PREFIX
PUBLIC_FIELD_PREFIXback to summary
pack-priv static final String PUBLIC_FIELD_PREFIX
SELFTYPE_CLASSNODEback to summary
public static final ClassNode SELFTYPE_CLASSNODE
STATIC_FIELD_HELPERback to summary
pack-priv static final String STATIC_FIELD_HELPER
STATIC_FIELD_PREFIXback to summary
pack-priv static final String STATIC_FIELD_PREFIX
STATIC_INIT_METHODback to summary
pack-priv static final String STATIC_INIT_METHOD
STATIC_THIS_OBJECTback to summary
public static final String STATIC_THIS_OBJECT
SUPER_TRAIT_METHOD_PREFIXback to summary
pack-priv static final String SUPER_TRAIT_METHOD_PREFIX
THIS_OBJECTback to summary
public static final String THIS_OBJECT
TRAIT_CLASSback to summary
public static final Class<Trait> TRAIT_CLASS
TRAIT_CLASSNODEback to summary
public static final ClassNode TRAIT_CLASSNODE
TRAIT_HELPERback to summary
pack-priv static final String TRAIT_HELPER
TRAIT_TYPE_NAMEback to summary
pack-priv static final String TRAIT_TYPE_NAME
TRAITBRIDGE_CLASSNODEback to summary
public static final ClassNode TRAITBRIDGE_CLASSNODE

Constructor Detail

Traitsback to summary
public Traits()

Method Detail

collectAllInterfacesReverseOrderback to summary
public static LinkedHashSet<ClassNode> collectAllInterfacesReverseOrder(final ClassNode cNode, final LinkedHashSet<ClassNode> interfaces)

Collects all interfaces of a class node, but reverses the order of the declaration of direct interfaces of this class node. This is used to make sure a trait implementing A,B where both A and B have the same method will take the method from B (latest), aligning the behavior with categories.

Parameters
cNode:ClassNode

a class node

interfaces:LinkedHashSet<ClassNode>

ordered set of interfaces

collectSelfTypesback to summary
public static LinkedHashSet<ClassNode> collectSelfTypes(final ClassNode receiver, final LinkedHashSet<ClassNode> selfTypes)

Collects all the self types that a type should extend or implement, given the traits is implements. Collects from interfaces and superclasses too.

Parameters
receiver:ClassNode

a class node that may implement a trait

selfTypes:LinkedHashSet<ClassNode>

a set where the self types will be put

Returns:LinkedHashSet<ClassNode>

the selfTypes collection

Since
2.4.0
collectSelfTypesback to summary
public static LinkedHashSet<ClassNode> collectSelfTypes(final ClassNode receiver, final LinkedHashSet<ClassNode> selfTypes, final boolean checkInterfaces, final boolean checkSuperClass)

Collects all the self types that a type should extend or implement, given the traits is implements.

Parameters
receiver:ClassNode

a class node that may implement a trait

selfTypes:LinkedHashSet<ClassNode>

a set where the self types will be put

checkInterfaces:boolean

should the interfaces that the node implements be collected too

checkSuperClass:boolean

should we collect from the superclass too

Returns:LinkedHashSet<ClassNode>

the selfTypes collection

Since
2.4.0
decomposeSuperCallNameback to summary
public static String[] decomposeSuperCallName(final String methodName)

Returns the trait and method names derived from super-trait name scheme or null if the method name doesn't correspond to a trait method.

fieldHelperClassNameback to summary
pack-priv static String fieldHelperClassName(final ClassNode traitNode)
findFieldHelperback to summary
public static ClassNode findFieldHelper(final ClassNode trait)
findHelperback to summary
public static ClassNode findHelper(final ClassNode trait)
findHelpersback to summary
pack-priv static TraitHelpersTuple findHelpers(final ClassNode trait)
findStaticFieldHelperback to summary
public static ClassNode findStaticFieldHelper(final ClassNode trait)
findTraitsback to summary
public static List<ClassNode> findTraits(final ClassNode cNode)

Find all traits associated with the given type.

Parameters
cNode:ClassNode

the given classnode

Returns:List<ClassNode>

the list of ordered trait classnodes

getAsTypeback to summary
public static <T> T getAsType(Object self, Class<T> clazz)

Converts a class implementing some trait into a target class. If the trait is a dynamic proxy and that the target class is assignable to the target object of the proxy, then the target object is returned. Otherwise, falls back to org.codehaus.groovy.runtime.DefaultGroovyMethods#asType(java.lang.Object, Class)

Parameters
self:Object

an object to be coerced to some class

clazz:Class<T>

the class to be coerced to

Returns:T

the object coerced to the target class, or the proxy instance if it is compatible with the target class.

Annotations
@SuppressWarnings:unchecked
getBridgeMethodTargetback to summary
public static Method getBridgeMethodTarget(Method someMethod)

Reflection API to find the method corresponding to the default implementation of a trait, given a bridge method.

Parameters
someMethod:Method

a method node

Returns:Method

null if it is not a method implemented in a trait. If it is, returns the method from the trait class.

getSuperTraitMethodNameback to summary
pack-priv static String getSuperTraitMethodName(ClassNode trait, String method)
hasDefaultImplementationback to summary
public static boolean hasDefaultImplementation(final MethodNode method)

Indicates whether a method in a trait interface has a default implementation.

Parameters
method:MethodNode

a method node

Returns:boolean

true if the method has a default implementation in the trait

hasDefaultImplementationback to summary
public static boolean hasDefaultImplementation(final Method method)

Indicates whether a method in a trait interface has a default implementation.

Parameters
method:Method

a method node

Returns:boolean

true if the method has a default implementation in the trait

helperClassNameback to summary
pack-priv static String helperClassName(final ClassNode traitNode)
helperGetterNameback to summary
pack-priv static String helperGetterName(final FieldNode field)
helperSetterNameback to summary
pack-priv static String helperSetterName(final FieldNode field)
isAnnotatedWithTraitback to summary
public static boolean isAnnotatedWithTrait(final ClassNode cNode)

Returns true if the specified class node is annotated with the Trait interface.

Parameters
cNode:ClassNode

a class node

Returns:boolean

true if the specified class node is annotated with the Trait interface.

isBridgeMethodback to summary
public static boolean isBridgeMethod(Method someMethod)

Reflection API to indicate whether some method is a bridge method to the default implementation of a trait.

Parameters
someMethod:Method

a method node

Returns:boolean

null if it is not a method implemented in a trait. If it is, returns the method from the trait class.

isTraitback to summary
public static boolean isTrait(final ClassNode cNode)

Returns true if the specified class node is a trait.

Parameters
cNode:ClassNode

a class node to test

Returns:boolean

true if the classnode represents a trait

isTraitback to summary
public static boolean isTrait(final Class<?> clazz)

Returns true if the specified class is a trait.

Parameters
clazz:Class<?>

a class to test

Returns:boolean

true if the classnode represents a trait

remappedFieldNameback to summary
pack-priv static String remappedFieldName(final ClassNode traitNode, final String name)
staticFieldHelperClassNameback to summary
pack-priv static String staticFieldHelperClassName(final ClassNode traitNode)
unwrapOwnerback to summary
private static ClassNode unwrapOwner(ClassNode owner)
org.codehaus.groovy.transform.trait back to summary

public @Interface Traits.Implemented

extends Annotation
Annotations
@Retention:RUNTIME
@Target:METHOD

Internal annotation used to indicate which methods in a trait interface have a default implementation.

Method Summary

Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString
org.codehaus.groovy.transform.trait back to summary

public @Interface Traits.TraitBridge

extends Annotation
Annotations
@Retention:RUNTIME
@Target:METHOD

Internal annotation used to indicate that a method is a bridge method to a trait default implementation.

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

The method descriptor of the method from the trait
desc
()

public Class<?>

Returns:

the trait class
traitClass
()

Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString

Method Detail

descback to summary
public String desc()
Returns:String

The method descriptor of the method from the trait

traitClassback to summary
public Class<?> traitClass()
Returns:Class<?>

the trait class