Java – Creating an array of methods

Whenever you think of pointer-to-function, you translate to Java by using the Adapter pattern (or a variation). It would be something like this: public class Node { … public void goNorth() { … } public void goSouth() { … } public void goEast() { … } public void goWest() { … } interface MoveAction { … Read more

Generate List of methods of a class with method types

Sure – use Type.GetMethods(). You’ll want to specify different binding flags to get non-public methods etc. This is a pretty crude but workable starting point: using System; using System.Linq; class Test { static void Main() { ShowMethods(typeof(DateTime)); } static void ShowMethods(Type type) { foreach (var method in type.GetMethods()) { var parameters = method.GetParameters(); var parameterDescriptions … Read more

How to automatically log the entry/exit of methods in Java?

I suggest the use of Aspect Oriented Programming. For example, using the AspectJ compiler (which can be integrated to Eclipse, Emacs and others IDEs), you can create a piece of code like this one: aspect AspectExample { before() : execution(* Point.*(..)) { logger.entering(thisJoinPointStaticPart.getSignature().getName(), thisJoinPointStaticPart.getSignature().getDeclaringType() ); } after() : execution(* Point.*(..)) { logger.exiting(thisJoinPointStaticPart.getSignature().getName() , thisJoinPointStaticPart.getSignature().getDeclaringType() ); … Read more

How would I cross-reference a function generated by autodoc in Sphinx?

You don’t need to add labels. In order to refer to a Python class, method, or other documented object, use the markup provided by the Python domain. For example, the following defines a cross-reference to the mymethod method: :py:meth:`mymodule.MyClass.mymethod` Or even simpler (since the Python domain is the default): :meth:`mymodule.MyClass.mymethod` The documentation of TextWrapper.wrap that … Read more

If a synchronized method calls another non-synchronized method, is there a lock on the non-synchronized method

If a synchronized method calls another non-synchronized method, is there a lock on the non-synchronized method The answer depends on the context. If you are in a synchronized method for an object, then calls by other threads to other methods of the same object instance that are also synchronized are locked. However calls by other … Read more

How to bind an unbound method without calling it?

All functions are also descriptors, so you can bind them by calling their __get__ method: bound_handler = handler.__get__(self, MyWidget) Here’s R. Hettinger’s excellent guide to descriptors. As a self-contained example pulled from Keith’s comment: def bind(instance, func, as_name=None): “”” Bind the function *func* to *instance*, with either provided name *as_name* or the existing name of … Read more