From: Kent J. <ke...@td...> - 2005-03-10 18:57:19
|
Aaron Freeman wrote: > I know you can effectively rename java classes by using 'import as', but > is there also a way to rename all the stuff in the class? We're working > with a proprietary library, so I don't have access to the java source. You could subclass the Java class in Jython and define aliases pretty easily, e.g. if you have a Java class public class Foo { public String bar() { return "bar"; } } In Jython you could have import Foo class MyFoo(Foo): myBar = Foo.bar Here is a real example to make sure it works: >>> from java.util import ArrayList >>> class MyList(ArrayList): ... myContains = ArrayList.contains ... >>> ml=MyList() >>> ml.myContains(1) 0 >>> ml.add(1) 1 >>> ml.myContains(1) 1 You can even make it data-driven: >>> class MyList(ArrayList): ... pass ... >>> for name in [ 'contains', 'clear' ]: ... setattr(MyList, 'my_' + name, getattr(ArrayList, name)) ... >>> ml=MyList() >>> ml.add(3) 1 >>> ml.my_contains(3) 1 >>> ml.my_clear() >>> len(ml) 0 Though why you would want to do this escapes me... Kent |