Skip to content

Commit c4b936b

Browse files
committed
Add missing javadoc
1 parent 159c983 commit c4b936b

25 files changed

Lines changed: 1549 additions & 39 deletions

src/main/java/org/codehaus/groovy/runtime/metaclass/ClosureMetaClass.java

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ public final class ClosureMetaClass extends MetaClassImpl {
8787
resetCachedMetaClasses();
8888
}
8989

90+
/**
91+
* Resets the cached metaclasses for Closure and Class. This method is used to reinitialize
92+
* the metaclass caches when the system configuration changes, ensuring that any cached
93+
* metaclass information is refreshed.
94+
*/
9095
public static void resetCachedMetaClasses() {
9196
MetaClassImpl temp = new MetaClassImpl(Closure.class);
9297
temp.initialize();
@@ -170,10 +175,24 @@ public Object chooseMethod(final Class<?>[] arguments, final boolean coerce) {
170175

171176
//--------------------------------------------------------------------------
172177

178+
/**
179+
* Constructs a new ClosureMetaClass for the given closure class.
180+
* This metaclass handles method invocation and property access for closure instances,
181+
* providing special handling for closure-specific behavior.
182+
*
183+
* @param registry the metaclass registry
184+
* @param theClass the closure class this metaclass represents
185+
*/
173186
public ClosureMetaClass(final MetaClassRegistry registry, final Class theClass) {
174187
super(registry, theClass);
175188
}
176189

190+
/**
191+
* Gets the meta property for the given name from the closure metaclass.
192+
*
193+
* @param name the property name
194+
* @return the meta property or null if not found
195+
*/
177196
@Override
178197
public MetaProperty getMetaProperty(final String name) {
179198
return CLOSURE_METACLASS.getMetaProperty(name);
@@ -395,6 +414,9 @@ private synchronized void initAttributes() {
395414
}
396415
}
397416

417+
/**
418+
* Initializes the closure metaclass by discovering and indexing all closure-related methods.
419+
*/
398420
@Override
399421
public synchronized void initialize() {
400422
if (!isInitialized()) {
@@ -488,6 +510,13 @@ private void assignMethodChooser() {
488510
}
489511
}
490512

513+
/**
514+
* Looks up the metaclass for the given object, handling special cases for
515+
* GroovyObject, Class instances, and regular objects.
516+
*
517+
* @param object the object to look up
518+
* @return the metaclass for the object
519+
*/
491520
private MetaClass lookupObjectMetaClass(final Object object) {
492521
MetaClass metaClass;
493522
if (object instanceof GroovyObject) {
@@ -500,23 +529,47 @@ private MetaClass lookupObjectMetaClass(final Object object) {
500529
return metaClass;
501530
}
502531

532+
/**
533+
* Returns a list of all methods available to closures, including both
534+
* closure-specific doCall methods and inherited Closure class methods.
535+
*
536+
* @return a list of all available methods
537+
*/
503538
@Override
504539
public List<MetaMethod> getMethods() {
505540
List<MetaMethod> answer = CLOSURE_METACLASS.getMetaMethods();
506541
answer.addAll(closureMethods.toList());
507542
return answer;
508543
}
509544

545+
/**
546+
* Returns a list of all metamethods in the Closure metaclass.
547+
*
548+
* @return a list of all metamethods
549+
*/
510550
@Override
511551
public List<MetaMethod> getMetaMethods() {
512552
return CLOSURE_METACLASS.getMetaMethods();
513553
}
514554

555+
/**
556+
* Returns a list of all properties available to closures.
557+
*
558+
* @return a list of all available properties
559+
*/
515560
@Override
516561
public List<MetaProperty> getProperties() {
517562
return CLOSURE_METACLASS.getProperties();
518563
}
519564

565+
/**
566+
* Picks the most appropriate method from the available closure methods that matches
567+
* the given method name and argument types.
568+
*
569+
* @param methodName the name of the method to pick (typically "call" or "doCall" for closures)
570+
* @param argumentTypes the types of arguments to match against method signatures
571+
* @return the matching method, or null if no match is found
572+
*/
520573
@Override
521574
public MetaMethod pickMethod(final String methodName, final Class[] argumentTypes) {
522575
if (CLOSURE_CALL_METHOD.equals(methodName) || CLOSURE_DO_CALL_METHOD.equals(methodName)) {
@@ -525,19 +578,54 @@ public MetaMethod pickMethod(final String methodName, final Class[] argumentType
525578
return CLOSURE_METACLASS.getMetaMethod(methodName, argumentTypes);
526579
}
527580

581+
/**
582+
* Retrieves a static method with the given name and argument types.
583+
* Closures do not support static methods, so this always returns null.
584+
*
585+
* @param methodName the name of the static method to retrieve
586+
* @param arguments the argument types
587+
* @return null, as closures do not have static methods
588+
*/
528589
public MetaMethod retrieveStaticMethod(final String methodName, final Class[] arguments) {
529590
return null;
530591
}
531592

593+
/**
594+
* Gets a static meta method by name and argument objects.
595+
* Delegates to the Closure metaclass.
596+
*
597+
* @param name the name of the static method
598+
* @param args the arguments
599+
* @return the matching static meta method, or null if not found
600+
*/
532601
@Override
533602
public MetaMethod getStaticMetaMethod(final String name, final Object[] args) {
534603
return CLOSURE_METACLASS.getStaticMetaMethod(name, args);
535604
}
536605

606+
/**
607+
* Gets a static meta method by name and argument types.
608+
* Delegates to the Closure metaclass.
609+
*
610+
* @param name the name of the static method
611+
* @param argTypes the argument types
612+
* @return the matching static meta method, or null if not found
613+
*/
537614
public MetaMethod getStaticMetaMethod(final String name, final Class[] argTypes) {
538615
return CLOSURE_METACLASS.getStaticMetaMethod(name, argTypes);
539616
}
540617

618+
/**
619+
* Gets a property on the closure or the object it represents.
620+
* For Class objects, delegates to the static metaclass; otherwise delegates to Closure metaclass.
621+
*
622+
* @param sender the class that is sending this request
623+
* @param object the object to get the property from
624+
* @param name the name of the property
625+
* @param useSuper whether to use super resolution
626+
* @param fromInsideClass whether the call is from inside the class
627+
* @return the property value
628+
*/
541629
@Override
542630
public Object getProperty(final Class sender, final Object object, final String name, final boolean useSuper, final boolean fromInsideClass) {
543631
if (object instanceof Class) {
@@ -547,6 +635,18 @@ public Object getProperty(final Class sender, final Object object, final String
547635
}
548636
}
549637

638+
/**
639+
* Gets an attribute from the closure or the object it represents.
640+
* For Class objects, delegates to the static metaclass. For closures, checks the
641+
* cached fields first before delegating to the Closure metaclass.
642+
*
643+
* @param sender the class that is sending this request
644+
* @param object the object to get the attribute from
645+
* @param attribute the name of the attribute
646+
* @param useSuper whether to use super resolution
647+
* @param fromInsideClass whether the call is from inside the class
648+
* @return the attribute value
649+
*/
550650
@Override
551651
public Object getAttribute(final Class sender, final Object object, final String attribute, final boolean useSuper, final boolean fromInsideClass) {
552652
if (object instanceof Class) {
@@ -562,6 +662,18 @@ public Object getAttribute(final Class sender, final Object object, final String
562662
}
563663
}
564664

665+
/**
666+
* Sets an attribute on the closure or the object it represents.
667+
* For Class objects, delegates to the static metaclass. For closures, checks the
668+
* cached fields first before delegating to the Closure metaclass.
669+
*
670+
* @param sender the class that is sending this request
671+
* @param object the object on which to set the attribute
672+
* @param attribute the name of the attribute
673+
* @param newValue the new value for the attribute
674+
* @param useSuper whether to use super resolution
675+
* @param fromInsideClass whether the call is from inside the class
676+
*/
565677
@Override
566678
public void setAttribute(final Class sender, final Object object, final String attribute, final Object newValue, final boolean useSuper, final boolean fromInsideClass) {
567679
if (object instanceof Class) {
@@ -577,11 +689,31 @@ public void setAttribute(final Class sender, final Object object, final String a
577689
}
578690
}
579691

692+
/**
693+
* Invokes a static method on the closure class itself.
694+
* Delegates to the static metaclass.
695+
*
696+
* @param object the object on which to invoke the static method
697+
* @param methodName the name of the static method
698+
* @param arguments the arguments to pass to the method
699+
* @return the result of the method invocation
700+
*/
580701
@Override
581702
public Object invokeStaticMethod(final Object object, final String methodName, final Object[] arguments) {
582703
return getStaticMetaClass().invokeMethod(Class.class, object, methodName, arguments, false, false);
583704
}
584705

706+
/**
707+
* Sets a property on the closure or the object it represents.
708+
* For Class objects, delegates to the static metaclass; otherwise delegates to Closure metaclass.
709+
*
710+
* @param sender the class that is sending this request
711+
* @param object the object on which to set the property
712+
* @param name the name of the property
713+
* @param newValue the new value for the property
714+
* @param useSuper whether to use super resolution
715+
* @param fromInsideClass whether the call is from inside the class
716+
*/
585717
@Override
586718
public void setProperty(final Class sender, final Object object, final String name, final Object newValue, final boolean useSuper, final boolean fromInsideClass) {
587719
if (object instanceof Class) {
@@ -591,61 +723,161 @@ public void setProperty(final Class sender, final Object object, final String na
591723
}
592724
}
593725

726+
/**
727+
* Gets a method without using the cache.
728+
* This operation is not supported for ClosureMetaClass.
729+
*
730+
* @param index the method index
731+
* @param sender the class that is sending this request
732+
* @param methodName the name of the method
733+
* @param arguments the argument types
734+
* @param isCallToSuper whether this is a call to a super method
735+
* @return never returns; always throws UnsupportedOperationException
736+
* @throws UnsupportedOperationException this method is not supported for closures
737+
*/
594738
public MetaMethod getMethodWithoutCaching(final int index, final Class sender, final String methodName, final Class[] arguments, final boolean isCallToSuper) {
595739
throw new UnsupportedOperationException();
596740
}
597741

742+
/**
743+
* Sets multiple properties on a bean.
744+
* This operation is not supported for ClosureMetaClass.
745+
*
746+
* @param bean the bean object
747+
* @param map the properties to set
748+
* @throws UnsupportedOperationException this method is not supported for closures
749+
*/
598750
@Override
599751
public void setProperties(final Object bean, final Map map) {
600752
throw new UnsupportedOperationException();
601753
}
602754

755+
/**
756+
* Adds a meta bean property to this metaclass.
757+
* This operation is not supported for ClosureMetaClass as it is immutable.
758+
*
759+
* @param mp the meta bean property to add
760+
* @throws UnsupportedOperationException this method is not supported for closures
761+
*/
603762
@Override
604763
public void addMetaBeanProperty(final MetaBeanProperty mp) {
605764
throw new UnsupportedOperationException();
606765
}
607766

767+
/**
768+
* Adds a meta method to this metaclass.
769+
* This operation is not supported for ClosureMetaClass as it is immutable.
770+
*
771+
* @param method the meta method to add
772+
* @throws UnsupportedOperationException this method is not supported for closures
773+
*/
608774
@Override
609775
public void addMetaMethod(final MetaMethod method) {
610776
throw new UnsupportedOperationException();
611777
}
612778

779+
/**
780+
* Adds a new instance method to this metaclass.
781+
* This operation is not supported for ClosureMetaClass as it is immutable.
782+
*
783+
* @param method the method to add
784+
* @throws UnsupportedOperationException this method is not supported for closures
785+
*/
613786
@Override
614787
public void addNewInstanceMethod(final Method method) {
615788
throw new UnsupportedOperationException();
616789
}
617790

791+
/**
792+
* Adds a new static method to this metaclass.
793+
* This operation is not supported for ClosureMetaClass as it is immutable.
794+
*
795+
* @param method the static method to add
796+
* @throws UnsupportedOperationException this method is not supported for closures
797+
*/
618798
@Override
619799
public void addNewStaticMethod(final Method method) {
620800
throw new UnsupportedOperationException();
621801
}
622802

803+
/**
804+
* Retrieves a constructor matching the given argument types.
805+
* This operation is not supported for ClosureMetaClass.
806+
*
807+
* @param arguments the argument types
808+
* @return never returns; always throws UnsupportedOperationException
809+
* @throws UnsupportedOperationException this method is not supported for closures
810+
*/
623811
@Override
624812
public Constructor retrieveConstructor(final Class[] arguments) {
625813
throw new UnsupportedOperationException();
626814
}
627815

816+
/**
817+
* Creates a POJO (Plain Old Java Object) call site for this metaclass.
818+
* This operation is not supported for ClosureMetaClass.
819+
*
820+
* @param site the call site
821+
* @param receiver the receiver object
822+
* @param args the arguments
823+
* @return never returns; always throws UnsupportedOperationException
824+
* @throws UnsupportedOperationException this method is not supported for closures
825+
*/
628826
@Override
629827
public CallSite createPojoCallSite(final CallSite site, final Object receiver, final Object[] args) {
630828
throw new UnsupportedOperationException();
631829
}
632830

831+
/**
832+
* Creates a POGO (Plain Old Groovy Object) call site for this metaclass.
833+
* Provides optimized method invocation for POGO objects.
834+
*
835+
* @param site the call site
836+
* @param args the arguments
837+
* @return a POGO-specific call site
838+
*/
633839
@Override
634840
public CallSite createPogoCallSite(final CallSite site, final Object[] args) {
635841
return new PogoMetaClassSite(site, this);
636842
}
637843

844+
/**
845+
* Creates a POGO call site for the current class context.
846+
* Provides optimized method invocation for POGO objects within a specific class context.
847+
*
848+
* @param site the call site
849+
* @param sender the class sending the call
850+
* @param args the arguments
851+
* @return a POGO-specific call site
852+
*/
638853
@Override
639854
public CallSite createPogoCallCurrentSite(final CallSite site, final Class sender, final Object[] args) {
640855
return new PogoMetaClassSite(site, this);
641856
}
642857

858+
/**
859+
* Checks if this metaclass responds to the given method name with the specified argument types.
860+
* Returns a list of methods that match the given name and signature.
861+
*
862+
* @param obj the object to check
863+
* @param name the method name
864+
* @param argTypes the argument types
865+
* @return a list of matching methods, or an empty list if none match
866+
*/
643867
@Override
644868
public List respondsTo(final Object obj, final String name, final Object[] argTypes) {
645869
loadMetaInfo();
646870
return super.respondsTo(obj, name, argTypes);
647871
}
648872

873+
/**
874+
* Checks if this metaclass responds to the given method name.
875+
* Returns a list of methods that match the given name.
876+
*
877+
* @param obj the object to check
878+
* @param name the method name
879+
* @return a list of matching methods, or an empty list if none match
880+
*/
649881
@Override
650882
public List respondsTo(final Object obj, final String name) {
651883
loadMetaInfo();

0 commit comments

Comments
 (0)