Class Traits
java.lang.Object
org.codehaus.groovy.transform.trait.Traits
A collection of utility methods used to deal with traits.
- Since:
- 2.3.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic @interfaceInternal annotation used to indicate which methods in a trait interface have a default implementation.static @interfaceInternal annotation used to indicate that a method is a bridge method to a trait default implementation. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ClassNodeClass node forGeneratedGroovyProxy.static final ClassNodeClass node forTraits.Implemented.static final ClassNodeClass node forSelfType.static final StringSynthetic parameter name for the current static trait receiver.static final StringSynthetic parameter name for the current trait receiver.Runtime annotation type that marks Groovy traits.static final ClassNodeClass node forTrait.static final ClassNodeClass node forTraits.TraitBridge. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic LinkedHashSet<ClassNode>collectAllInterfacesReverseOrder(ClassNode cNode, LinkedHashSet<ClassNode> interfaces) Collects all interfaces of a class node, but reverses the order of the declaration of direct interfaces of this class node.static LinkedHashSet<ClassNode>collectSelfTypes(ClassNode receiver, LinkedHashSet<ClassNode> selfTypes) Collects all the self types that a type should extend or implement, given the traits is implements.static LinkedHashSet<ClassNode>collectSelfTypes(ClassNode receiver, LinkedHashSet<ClassNode> selfTypes, boolean checkInterfaces, boolean checkSuperClass) Collects all the self types that a type should extend or implement, given the traits is implements.static String[]decomposeSuperCallName(String methodName) Returns the trait and method names derived from super-trait name scheme ornullif the method name doesn't correspond to a trait method.static ClassNodefindFieldHelper(ClassNode trait) Returns the generated instance field helper class for a trait.static ClassNodefindHelper(ClassNode trait) Returns the generated helper class for a trait.static ClassNodefindStaticFieldHelper(ClassNode trait) Returns the generated static field helper class for a trait.findTraits(ClassNode cNode) Find all traits associated with the given type.static <T> TConverts a class implementing some trait into a target class.static MethodgetBridgeMethodTarget(Method someMethod) Reflection API to find the method corresponding to the default implementation of a trait, given a bridge method.static booleanhasDefaultImplementation(Method method) Indicates whether a method in a trait interface has a default implementation.static booleanhasDefaultImplementation(MethodNode method) Indicates whether a method in a trait interface has a default implementation.static booleanisAnnotatedWithTrait(ClassNode cNode) Returns true if the specified class node is annotated with theTraitinterface.static booleanisBridgeMethod(Method someMethod) Reflection API to indicate whether some method is a bridge method to the default implementation of a trait.static booleanReturns true if the specified class is a trait.static booleanReturns true if the specified class node is a trait.
-
Field Details
-
IMPLEMENTED_CLASSNODE
Class node forTraits.Implemented. -
TRAITBRIDGE_CLASSNODE
Class node forTraits.TraitBridge. -
TRAIT_CLASS
Runtime annotation type that marks Groovy traits. -
TRAIT_CLASSNODE
Class node forTrait. -
GENERATED_PROXY_CLASSNODE
Class node forGeneratedGroovyProxy. -
SELFTYPE_CLASSNODE
Class node forSelfType. -
THIS_OBJECT
Synthetic parameter name for the current trait receiver.- See Also:
-
STATIC_THIS_OBJECT
Synthetic parameter name for the current static trait receiver.- See Also:
-
-
Constructor Details
-
Traits
public Traits()
-
-
Method Details
-
findHelper
Returns the generated helper class for a trait.- Parameters:
trait- the trait class node- Returns:
- the helper class node
-
findFieldHelper
Returns the generated instance field helper class for a trait.- Parameters:
trait- the trait class node- Returns:
- the field helper class node, or
nullif none exists
-
findStaticFieldHelper
Returns the generated static field helper class for a trait.- Parameters:
trait- the trait class node- Returns:
- the static field helper class node, or
nullif none exists
-
isTrait
Returns true if the specified class node is a trait.- Parameters:
cNode- a class node to test- Returns:
- true if the classnode represents a trait
-
isTrait
Returns true if the specified class is a trait.- Parameters:
clazz- a class to test- Returns:
- true if the classnode represents a trait
-
isAnnotatedWithTrait
Returns true if the specified class node is annotated with theTraitinterface.- Parameters:
cNode- a class node- Returns:
- true if the specified class node is annotated with the
Traitinterface.
-
hasDefaultImplementation
Indicates whether a method in a trait interface has a default implementation.- Parameters:
method- a method node- Returns:
- true if the method has a default implementation in the trait
-
hasDefaultImplementation
Indicates whether a method in a trait interface has a default implementation.- Parameters:
method- a method node- Returns:
- true if the method has a default implementation in the trait
-
isBridgeMethod
Reflection API to indicate whether some method is a bridge method to the default implementation of a trait.- Parameters:
someMethod- a method- Returns:
trueif the method bridges to a trait default implementation
-
getBridgeMethodTarget
Reflection API to find the method corresponding to the default implementation of a trait, given a bridge method.- Parameters:
someMethod- a method- Returns:
- null if it is not a method implemented in a trait. If it is, returns the method from the trait class.
-
getAsType
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 toDefaultGroovyMethods.asType(java.lang.Object, Class)- Parameters:
self- an object to be coerced to some classclazz- the class to be coerced to- Returns:
- the object coerced to the target class, or the proxy instance if it is compatible with the target class.
-
decomposeSuperCallName
Returns the trait and method names derived from super-trait name scheme ornullif the method name doesn't correspond to a trait method. -
collectAllInterfacesReverseOrder
public static LinkedHashSet<ClassNode> collectAllInterfacesReverseOrder(ClassNode cNode, 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- a class nodeinterfaces- ordered set of interfaces
-
collectSelfTypes
public static LinkedHashSet<ClassNode> collectSelfTypes(ClassNode receiver, 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- a class node that may implement a traitselfTypes- a set where the self types will be put- Returns:
- the
selfTypescollection - Since:
- 2.4.0
-
collectSelfTypes
public static LinkedHashSet<ClassNode> collectSelfTypes(ClassNode receiver, LinkedHashSet<ClassNode> selfTypes, boolean checkInterfaces, boolean checkSuperClass) Collects all the self types that a type should extend or implement, given the traits is implements.- Parameters:
receiver- a class node that may implement a traitselfTypes- a set where the self types will be putcheckInterfaces- should the interfaces that the node implements be collected toocheckSuperClass- should we collect from the superclass too- Returns:
- the
selfTypescollection - Since:
- 2.4.0
-
findTraits
Find all traits associated with the given type.- Parameters:
cNode- the given classnode- Returns:
- the list of ordered trait classnodes
-